@@ -17,7 +17,7 @@ class ImportInvestmentZones extends Command
|
||||
public function handle(): int
|
||||
{
|
||||
$path = database_path('data/investment-zones-initial.json');
|
||||
$document = json_decode(file_get_contents($path), true, flags: JSON_THROW_ON_ERROR);
|
||||
$document = json_decode($this->readSource($path), true, flags: JSON_THROW_ON_ERROR);
|
||||
$rows = [];
|
||||
foreach ($document['records'] as $record) {
|
||||
$existing = DB::table('investment_zones')->where('code', $record['code'])->first(['id', 'name', 'category', 'publication_status']);
|
||||
@@ -59,12 +59,12 @@ class ImportInvestmentZones extends Command
|
||||
|
||||
continue;
|
||||
}
|
||||
$record = collect($document['records'])->firstWhere('code', $row['code']);
|
||||
$record = collect(is_array($document['records'] ?? null) ? $document['records'] : [])->firstWhere('code', $row['code']);
|
||||
$requiresLegalSource = in_array($record['category'], ['zee', 'zfi'], true);
|
||||
$source = $requiresLegalSource
|
||||
? DB::table('investment_legal_sources')->where('url', $record['source']['url'])->where('diploma', $record['source']['diploma'])->first()
|
||||
: null;
|
||||
$sourceId = $requiresLegalSource ? ($source?->id ?? (string) Str::uuid()) : null;
|
||||
$sourceId = $requiresLegalSource ? ($source->id ?? (string) Str::uuid()) : null;
|
||||
if ($requiresLegalSource && ! $source) {
|
||||
DB::table('investment_legal_sources')->insert([
|
||||
'id' => $sourceId, ...$record['source'], 'validation_status' => 'pending', 'owner_id' => $actor,
|
||||
@@ -120,4 +120,14 @@ class ImportInvestmentZones extends Command
|
||||
{
|
||||
return Str::upper(Str::ascii(trim($value)));
|
||||
}
|
||||
|
||||
private function readSource(string $path): string
|
||||
{
|
||||
$bytes = file_get_contents($path);
|
||||
if ($bytes === false) {
|
||||
throw new RuntimeException('Source file is unavailable: '.$path);
|
||||
}
|
||||
|
||||
return $bytes;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user