From a082c2542a954ac560d164dbe6477261de4bf5c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 17:52:55 +0200 Subject: [PATCH] =?UTF-8?q?Criar=20Hotel:=20a=20actividade=20escolhida=20n?= =?UTF-8?q?o=20dossier=20mant=C3=A9m-se=20ao=20longo=20do=20percurso?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uma oportunidade, sector, parque ou território acrescentado depois só propõe outra actividade; o sector e a actividade do dossier deixam de ser trocados ou apagados sem o investidor o pedir. A pré-visualização mostra «conserva … ; a fonte propõe …» e há uma opção explícita para mudar. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM --- app/Services/InvestorContext.php | 9 +++-- resources/js/pages/investor-path/context.tsx | 34 +++++++++++++++++-- .../InvestmentContextContinuityTest.php | 31 +++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/app/Services/InvestorContext.php b/app/Services/InvestorContext.php index 6e972305..591525ef 100644 --- a/app/Services/InvestorContext.php +++ b/app/Services/InvestorContext.php @@ -26,6 +26,9 @@ final class InvestorContext public const FIELDS = ['promoter_entity_key', 'legal_name', 'legal_form', 'registration_number', 'products_services', 'investment_budget_min', 'investment_budget_max', 'project_title', 'project_summary', 'sector_id', 'sector', 'province_id', 'district_id', 'admin_post_id', 'locality_id', 'province', 'district', 'location', 'currency', 'expected_start_date', 'fiscal_period', 'fiscal_periodicity', 'fiscal_month', 'investor_origin', 'person_type', 'entity_stage', 'tax_residency_country', 'fiscal_residency_country', 'activity_id', 'project_type', 'maturity_stage', 'special_zone_id', 'investment_regime', ...PrivateInvestmentPreparation::FIELDS]; + /** The investor's primary intention: once chosen, later references may only propose a different one. */ + public const INTENTION_FIELDS = ['sector_id', 'sector', 'activity_id']; + public function __construct(private readonly VirtualDesk $desk) {} /** @@ -674,7 +677,9 @@ final class InvestorContext $provenance[$key] = ['kind' => 'process_declaration', 'actor_id' => $actor->id, 'context_revision' => $old['revision'] + 1]; } foreach ($proposed as $key => $value) { - if (($fields[$key] ?? '') === '' || (($provenance[$key]['kind'] ?? '') !== 'process_declaration' && ($provenance[$key]['kind'] ?? '') !== 'dossier') || in_array($key, $data['replace_fields'] ?? [], true)) { + // A hotel stays a hotel: another activity is only proposed, never swapped in silently. + $intention = in_array($key, self::INTENTION_FIELDS, true) && ($fields[$key] ?? '') !== ''; + if (($fields[$key] ?? '') === '' || (! $intention && ($provenance[$key]['kind'] ?? '') !== 'process_declaration' && ($provenance[$key]['kind'] ?? '') !== 'dossier') || in_array($key, $data['replace_fields'] ?? [], true)) { if (($fields[$key] ?? null) !== $value) { $replaced[] = ['field' => $key, 'before' => $fields[$key] ?? null, 'after' => $value]; }$fields[$key] = $value; @@ -685,7 +690,7 @@ final class InvestorContext } // Remove inherited subordinate geography only when its source was actually replaced. foreach ($fields as $key => $value) { - if (! array_key_exists($key, $proposed) && in_array($key, ['province_id', 'district_id', 'admin_post_id', 'locality_id', 'province', 'district', 'location', 'sector_id', 'sector', 'special_zone_id', 'investment_regime'], true) && isset($provenance[$key]['key'])) { + if (! array_key_exists($key, $proposed) && in_array($key, ['province_id', 'district_id', 'admin_post_id', 'locality_id', 'province', 'district', 'location', 'special_zone_id', 'investment_regime'], true) && isset($provenance[$key]['key'])) { unset($fields[$key],$provenance[$key]); } } diff --git a/resources/js/pages/investor-path/context.tsx b/resources/js/pages/investor-path/context.tsx index 95b727b7..c91deab4 100644 --- a/resources/js/pages/investor-path/context.tsx +++ b/resources/js/pages/investor-path/context.tsx @@ -1316,7 +1316,7 @@ function ScopedContextEditor({ Substituições explícitas de declarações

- Por defeito, uma seleção conserva o título e as + Por defeito, uma selecção conserva o título e as declarações do utilizador. Autorize somente os campos que pretende substituir pelo cadastro.

@@ -1337,10 +1337,40 @@ function ScopedContextEditor({ setPlan(null); }} />{' '} - Usar a fonte selecionada em{' '} + Usar a fonte seleccionada em{' '} {contextFieldNames[k].toLowerCase()} ))} + {view.context.fields.sector && ( + + )} {view.private_investment && ( postJson('/investimentos/'.$id.'/contexto/previa', ['version' => $view['case']['version'], 'selection' => [], 'declarations' => ['project_title' => 'Título revisto']]) ->assertOk()->assertJsonPath('dependency_changed', false)->assertJsonPath('review_steps', []); } + + public function test_the_primary_activity_is_kept_until_the_investor_explicitly_changes_it(): void + { + // Mapa de erros, "Criar Hotel": a hotel dossier is never moved to another activity by a later reference. + $owner = User::factory()->create(); + $id = $this->dossier($owner, 'Criar Hotel'); + // Synthetic sectors, rolled back with the transaction; never catalogue data. + [$tourism, $agro] = array_map(fn (string $name) => Sector::forceCreate(['name' => 'SINTÉTICO QA '.$name, 'slug' => 'sintetico-qa-'.Str::lower(Str::random(12)), 'is_active' => true]), ['Hotelaria', 'Agronegócio']); + $apply = function (array $selection, array $replace = []) use ($id): array { + $view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json(); + $body = ['version' => $view['case']['version'], 'selection' => $selection, 'replace_fields' => $replace]; + $plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json(); + $this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$body, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk(); + + return $plan; + }; + $apply(['sector' => 'sector:'.$tourism->id]); + $this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $tourism->id); + + $plan = $apply(['sector' => 'sector:'.$agro->id]); + $this->assertSame((string) $tourism->id, $plan['fields']['sector_id']); + $this->assertContains(['field' => 'sector', 'value' => $tourism->name, 'proposed' => $agro->name], $plan['maintained']); + $this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector', $tourism->name); + + $apply(['sector' => null]); + $this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $tourism->id); + + $apply(['sector' => 'sector:'.$agro->id], ['sector_id', 'sector']); + $this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $agro->id)->assertJsonPath('context.fields.sector', $agro->name); + } }