fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -97,15 +97,16 @@ class InvestmentZones
|
||||
return DB::transaction(function () use ($id, $data, $actor) {
|
||||
$zone = $id ? InvestmentZone::whereKey($id)->lockForUpdate()->firstOrFail() : new InvestmentZone;
|
||||
if ($id && $zone->category === 'industrial_park') {
|
||||
$prepared=app(InvestmentZoneOperationalProfile::class)->prepare($zone,$data,$actor);
|
||||
$this->storePendingLocations($zone,$data['locations'] ?? [],$actor);
|
||||
$prepared = app(InvestmentZoneOperationalProfile::class)->prepare($zone, $data, $actor);
|
||||
$this->storePendingLocations($zone, $data['locations'] ?? [], $actor);
|
||||
|
||||
return $prepared;
|
||||
}
|
||||
abort_if($id && (int) $zone->version_no !== (int) $data['version_no'], 409, 'Versão alterada. Recarregue antes de guardar.');
|
||||
$version = $id ? (int) $zone->version_no + 1 : 1;
|
||||
$locations = $data['locations'] ?? [];
|
||||
unset($data['locations'], $data['version_no']);
|
||||
abort_if(array_key_exists('operational_details',$data),422,'Prepare L1 numa ficha de parque existente; não cria outro cadastro.');
|
||||
abort_if(array_key_exists('operational_details', $data), 422, 'Prepare L1 numa ficha de parque existente; não cria outro cadastro.');
|
||||
$zone->fill([...$data, 'editor_id' => $actor, 'reviewer_id' => null, 'published_at' => null,
|
||||
'version_no' => $version, 'publication_status' => 'draft', 'validation_status' => 'pending'])->save();
|
||||
foreach ($locations as $location) {
|
||||
@@ -127,8 +128,11 @@ class InvestmentZones
|
||||
return DB::transaction(function () use ($id, $version, $actor) {
|
||||
$zone = InvestmentZone::whereKey($id)->lockForUpdate()->firstOrFail();
|
||||
$profile = app(InvestmentZoneOperationalProfile::class);
|
||||
if ($zone->category === 'industrial_park') { $profile->promote($zone,$version); }
|
||||
else { abort_if((int) $zone->version_no !== $version, 409, 'Versão alterada. Recarregue antes de aprovar.'); }
|
||||
if ($zone->category === 'industrial_park') {
|
||||
$profile->promote($zone, $version);
|
||||
} else {
|
||||
abort_if((int) $zone->version_no !== $version, 409, 'Versão alterada. Recarregue antes de aprovar.');
|
||||
}
|
||||
$source = DB::table('investment_legal_sources')->where('id', $zone->legal_source_id)->lockForUpdate()->first();
|
||||
$evidence = $zone->source_evidence ?? [];
|
||||
$officialEvidence = collect($evidence)->contains(fn ($item) => in_array($item['source_type'] ?? null, ['government', 'institutional'], true)
|
||||
@@ -166,8 +170,8 @@ class InvestmentZones
|
||||
{
|
||||
return DB::transaction(function () use ($id, $version, $actor) {
|
||||
$zone = InvestmentZone::whereKey($id)->lockForUpdate()->firstOrFail();
|
||||
$profile=app(InvestmentZoneOperationalProfile::class);
|
||||
abort_if(($zone->category === 'industrial_park' ? $profile->workingVersion($zone) : (int)$zone->version_no) !== $version, 409, 'Versão alterada.');
|
||||
$profile = app(InvestmentZoneOperationalProfile::class);
|
||||
abort_if(($zone->category === 'industrial_park' ? $profile->workingVersion($zone) : (int) $zone->version_no) !== $version, 409, 'Versão alterada.');
|
||||
$zone->fill(['version_no' => $zone->category === 'industrial_park' ? $profile->nextVersion($zone) : $version + 1, 'publication_status' => 'suspended'])->save();
|
||||
$this->snapshot($zone, $actor, 'suspended');
|
||||
|
||||
@@ -201,7 +205,7 @@ class InvestmentZones
|
||||
|
||||
private function snapshot(InvestmentZone $zone, int $actor, string $action): void
|
||||
{
|
||||
$json = json_encode([...$zone->toArray(),'operational_details'=>app(InvestmentZoneOperationalDetails::class)->stored($zone)],JSON_THROW_ON_ERROR);
|
||||
$json = json_encode([...$zone->toArray(), 'operational_details' => app(InvestmentZoneOperationalDetails::class)->stored($zone)], JSON_THROW_ON_ERROR);
|
||||
DB::table('investment_zone_revisions')->insert(['id' => (string) Str::uuid(), 'zone_id' => $zone->id,
|
||||
'version_no' => $zone->version_no, 'actor_id' => $actor, 'action' => $action,
|
||||
'snapshot' => Crypt::encryptString($json), 'sha256' => hash('sha256', $json)]);
|
||||
@@ -210,14 +214,16 @@ class InvestmentZones
|
||||
'after_data' => json_encode(['version_no' => $zone->version_no, 'sha256' => hash('sha256', $json)], JSON_THROW_ON_ERROR)]);
|
||||
}
|
||||
|
||||
/** Existing explicit territorial control: new associations remain pending, never promoted with the profile. */
|
||||
private function storePendingLocations(InvestmentZone $zone,array $locations,int $actor): void
|
||||
/** Existing explicit territorial control: new associations remain pending, never promoted with the profile.
|
||||
* @param array<array-key, mixed> $locations
|
||||
*/
|
||||
private function storePendingLocations(InvestmentZone $zone, array $locations, int $actor): void
|
||||
{
|
||||
foreach($locations as $location){
|
||||
foreach ($locations as $location) {
|
||||
DB::table('investment_zone_locations')->insertOrIgnore([
|
||||
'id'=>(string)Str::uuid(),'zone_id'=>$zone->id,'geo_location_id'=>$location['geo_location_id'],
|
||||
'scope_type'=>$location['scope_type'] ?? 'administrative','source_reference'=>$location['source_reference'],
|
||||
'legal_source_id'=>$location['legal_source_id'] ?? null,'validation_status'=>'pending','editor_id'=>$actor,'version_no'=>1,'created_at'=>now(),'updated_at'=>now(),
|
||||
'id' => (string) Str::uuid(), 'zone_id' => $zone->id, 'geo_location_id' => $location['geo_location_id'],
|
||||
'scope_type' => $location['scope_type'] ?? 'administrative', 'source_reference' => $location['source_reference'],
|
||||
'legal_source_id' => $location['legal_source_id'] ?? null, 'validation_status' => 'pending', 'editor_id' => $actor, 'version_no' => 1, 'created_at' => now(), 'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user