fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -60,7 +60,9 @@ final class SimulationFormDrafts
|
||||
throw new HttpException(409, 'Já existe um rascunho activo. Carregue-o antes de guardar novas alterações.');
|
||||
}
|
||||
|
||||
if ($case) $data['economic_snapshot'] = $this->landContext($data['economic_snapshot'], $case);
|
||||
if ($case) {
|
||||
$data['economic_snapshot'] = $this->landContext($data['economic_snapshot'], $case);
|
||||
}
|
||||
$snapshotHash = hash('sha256', json_encode($this->canonical($data), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
||||
$draft = SimulationFormDraft::query()->create([
|
||||
'id' => (string) Str::uuid(), 'owner_user_id' => $actorId, 'anonymous_token_hash' => $tokenHash,
|
||||
@@ -459,15 +461,30 @@ final class SimulationFormDrafts
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $economic
|
||||
* @param array<array-key, mixed> $previous
|
||||
* @return array<array-key, mixed>
|
||||
*/
|
||||
private function preserveInactiveLandStatus(SimulationFormDraft $draft, array $economic, array $previous, ?int $actorId): array
|
||||
{
|
||||
if (! $actorId || ! config('investor-path.enabled') || ! app(VirtualDesk::class)->installed()) return $economic;
|
||||
if (! $actorId || ! config('investor-path.enabled') || ! app(VirtualDesk::class)->installed()) {
|
||||
return $economic;
|
||||
}
|
||||
// Use the real owner-bound dossier even if a request omits its context parameter.
|
||||
$case = DB::table('desk_cases')->where('simulation_id', $draft->id)->where('user_id', $actorId)->first();
|
||||
if (! $case) return $economic;
|
||||
if (! $case) {
|
||||
return $economic;
|
||||
}
|
||||
|
||||
return $this->landContext($economic, $case, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $economic
|
||||
* @param array<array-key, mixed>|null $previous
|
||||
* @return array<array-key, mixed>
|
||||
*/
|
||||
private function landContext(array $economic, \stdClass $case, ?array $previous = null): array
|
||||
{
|
||||
$snapshot = json_decode($economic['context_applicability_snapshot'] ?? $previous['context_applicability_snapshot'] ?? '', true);
|
||||
@@ -475,10 +492,16 @@ final class SimulationFormDrafts
|
||||
$key = app(InvestmentConsumerScope::class)->key(app(InvestorContext::class)->state($case), is_string($key) ? $key : null);
|
||||
$decision = app(InvestorContext::class)->landPreparation($case, $key);
|
||||
if ($decision['need'] !== 'yes' && $previous !== null) {
|
||||
if (array_key_exists('land_status', $previous)) $economic['land_status'] = $previous['land_status'];
|
||||
else unset($economic['land_status']);
|
||||
if (isset($previous['selection_labels']['land_status'])) $economic['selection_labels']['land_status'] = $previous['selection_labels']['land_status'];
|
||||
else unset($economic['selection_labels']['land_status']);
|
||||
if (array_key_exists('land_status', $previous)) {
|
||||
$economic['land_status'] = $previous['land_status'];
|
||||
} else {
|
||||
unset($economic['land_status']);
|
||||
}
|
||||
if (isset($previous['selection_labels']['land_status'])) {
|
||||
$economic['selection_labels']['land_status'] = $previous['selection_labels']['land_status'];
|
||||
} else {
|
||||
unset($economic['selection_labels']['land_status']);
|
||||
}
|
||||
}
|
||||
$snapshot = json_decode($economic['context_applicability_snapshot'] ?? $previous['context_applicability_snapshot'] ?? '', true);
|
||||
if (! is_array($snapshot) || ($snapshot['version'] ?? null) !== 1) {
|
||||
@@ -486,7 +509,9 @@ final class SimulationFormDrafts
|
||||
}
|
||||
$snapshot['land_preparation'] = $decision;
|
||||
$snapshot['consumer_opportunity'] = $key;
|
||||
if ($key) $economic['context_opportunity'] = $key;
|
||||
if ($key) {
|
||||
$economic['context_opportunity'] = $key;
|
||||
}
|
||||
$economic['context_applicability_snapshot'] = json_encode($snapshot, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
|
||||
|
||||
return $economic;
|
||||
|
||||
Reference in New Issue
Block a user