Skip to content

Commit

Permalink
Merge pull request #175 from bcgov/test
Browse files Browse the repository at this point in the history
Created new script for creating new institution caps
  • Loading branch information
m7amad-github authored Jan 24, 2025
2 parents 3b76e1e + 2f76940 commit 571ae95
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2,148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class AttestationStoreRequest extends BaseFormRequest
{
Expand Down Expand Up @@ -85,6 +86,12 @@ protected function prepareForValidation()
$cap = $progCap;
}

if ($cap === null) {
throw ValidationException::withMessages([
'cap' => ["The system can't find an active cap for this institution."]
]);
}

//get the next fed_guid
$fedGuid = $this->getFedGuid($cap);

Expand Down
68 changes: 68 additions & 0 deletions app/Console/Commands/ImportNewInstitutionCaps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Console\Commands;

use App\Models\Cap;
use Illuminate\Console\Command;
use League\Csv\Reader;

class ImportNewInstitutionCaps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:new-institutions-caps {file}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Import caps data from CSV file';

/**
* Execute the console command.
*/
public function handle()
{
$file = $this->argument('file');

if (!file_exists($file)) {
$this->error("File does not exist: $file");
return 1;
}

$csv = Reader::createFromPath($file, 'r');
$csv->setHeaderOffset(0);

$records = $csv->getRecords();

foreach ($records as $record) {
try {
// Remove any hyphens that could be exist in the csv file provided
$guid = str_replace('-', '', $record['guid']);

Cap::create([
'guid' => $guid,
'fed_cap_guid' => $record['fed_cap_guid'],
'institution_guid' => $record['institution_guid'],
'start_date' => $record['start_date'],
'end_date' => $record['end_date'],
'total_attestations' => $record['total_attestations'],
'active_status' => TRUE,
'confirmed' => TRUE,
'total_reserved_graduate_attestations' => $record['total_reserved_graduate_attestations'],
]);

$this->info("Created cap for: {$record['guid']}");
} catch (\Exception $e) {
$this->error("Error processing record for {$record['guid']}: " . $e->getMessage());
}
}

$this->info('Data import completed.');
return 0;
}
}
Loading

0 comments on commit 571ae95

Please sign in to comment.