option('approval')); if ($approval !== '' && (mb_strlen($approval) < 15 || mb_strlen($approval) > 3000)) { $this->error('A autorização deve conter entre 15 e 3000 caracteres.'); return self::FAILURE; } $data = json_decode(File::get(database_path('data/investment-portfolio.json')), true, 512, JSON_THROW_ON_ERROR); $published = 0; $preserved = 0; DB::transaction(function () use ($data, $approval, &$published, &$preserved) { app(InvestmentPortfolioSeeder::class)->run(); foreach (['projects' => ['project_id', 'projects'], 'clusters' => ['cluster_id', 'economic_clusters'], 'corridors' => ['corridor_id', 'development_corridors']] as $kind => [$foreign, $table]) { foreach ($data[$kind] as $item) { $entity = DB::table($table)->where('code', $item['code'])->where('source_system', 'upgrade-v4')->whereNull('deleted_at')->value('id'); $dossier = InvestmentDossier::where($foreign, $entity)->lockForUpdate()->first(); // Approval of this document never approves subsequent human edits or restores archived records. if (! $dossier || $dossier->title !== $item['name'] || $dossier->content != [...$item, 'notice' => $data['notice']] || $dossier->editor_id) { $preserved++; continue; } if ($approval === '' || $dossier->publication_status === 'published') { continue; } $dossier->update(['version_no' => $dossier->version_no + 1, 'publication_status' => 'published', 'published_at' => now(), 'approval_basis' => 'document_owner', 'approval_evidence' => $approval]); DB::table('audit_events')->insert(['source_system' => 'portfolio-import', 'entity_type' => 'investment_dossiers', 'entity_id' => $dossier->id, 'action' => 'owner_approved', 'after_data' => json_encode([ 'code' => $item['code'], 'approval_basis' => 'document_owner', 'approval_sha256' => hash('sha256', $approval), 'dataset_sha256' => hash_file('sha256', database_path('data/investment-portfolio.json')), ], JSON_THROW_ON_ERROR)]); $published++; } } }); $this->info("Carteira importada: 15 projectos, 8 clusters, 6 corredores. Publicados: {$published}. Fichas alteradas/arquivadas preservadas: {$preserved}."); return self::SUCCESS; } }