diff --git a/app/Services/InvestorPath.php b/app/Services/InvestorPath.php index a8cdd376..0cb65e1f 100644 --- a/app/Services/InvestorPath.php +++ b/app/Services/InvestorPath.php @@ -352,8 +352,19 @@ final class InvestorPath $contexts = DB::table('desk_cases as c')->join('investor_journeys as j', 'j.id', '=', 'c.journey_id')->whereNotNull('j.path_definition_id') ->whereNull('j.deleted_at')->whereNull('c.closed_at')->where(fn ($q) => $q->where(fn ($p) => $p->where('c.user_id', $actor->id)->whereNull('c.organization_id'))->orWhereIn('c.organization_id', $organizations)) - ->orderByDesc('c.updated_at')->get(['c.id', 'c.title', 'c.version', 'c.updated_at']) - ->map(fn ($row) => ['id' => $row->id, 'title' => $row->title, 'version' => $row->version, 'updated_at' => $row->updated_at])->all(); + ->orderByDesc('c.updated_at')->get(['c.id', 'c.title', 'c.version', 'c.updated_at', 'c.payload']) + ->map(function ($row) { + // The dossier's primary activity, so a new reference never lands in the wrong initiative unseen. + try { + $fields = $this->desk->decode($row->payload)['investor_path']['context']['fields'] ?? []; + } catch (\Throwable) { + $fields = []; + } + + return ['id' => $row->id, 'title' => $row->title, 'version' => $row->version, 'updated_at' => $row->updated_at, + 'sector_id' => isset($fields['sector_id']) && $fields['sector_id'] !== '' ? (string) $fields['sector_id'] : null, + 'sector' => isset($fields['sector']) && $fields['sector'] !== '' ? (string) $fields['sector'] : null]; + })->all(); return array_values($contexts); } diff --git a/resources/js/pages/investor-path/context-start.tsx b/resources/js/pages/investor-path/context-start.tsx index eab31758..91a3c95a 100644 --- a/resources/js/pages/investor-path/context-start.tsx +++ b/resources/js/pages/investor-path/context-start.tsx @@ -22,7 +22,13 @@ export default function ContextStart({ entryComplements = [], entrySubmission = null, }: { - contexts: { id: string; title: string; version: number }[]; + contexts: { + id: string; + title: string; + version: number; + sector_id?: string | null; + sector?: string | null; + }[]; source: ContextReference | null; opportunityChoices?: OpportunityChoice[]; step?: string | null; @@ -31,6 +37,23 @@ export default function ContextStart({ entrySubmission?: { id: string; version: number } | null; }) { const [chosen, setChosen] = useState(''); + // The investor's first choice leads: dossiers of the same sector come + // first, and a different sector is only ever proposed, never swapped in. + const sourceSector = + source?.kind === 'sector' ? (source.fields.sector_id ?? null) : null; + const ordered = sourceSector + ? [ + ...contexts.filter((c) => c.sector_id === sourceSector), + ...contexts.filter((c) => c.sector_id !== sourceSector), + ] + : contexts; + const chosenContext = contexts.find((c) => c.id === chosen); + const otherActivity = + sourceSector && + chosenContext?.sector_id && + chosenContext.sector_id !== sourceSector + ? chosenContext + : null; const [title, setTitle] = useState( entryComplements[0]?.kind === 'need' ? `Preparação de ${entryComplements[0].title} — ${entryComplements[0].origin.title}`.slice( @@ -174,13 +197,26 @@ export default function ContextStart({ onChange={(e) => setChosen(e.target.value)} > - {contexts.map((c) => ( + {ordered.map((c) => ( ))} + {otherActivity && ( +
+ O dossier «{otherActivity.title}» é de{' '} + {otherActivity.sector}. Se continuar nele, {source?.title}{' '} + fica apenas como proposta e a actividade do dossier + mantém-se. Para preparar uma ideia em {source?.title}, + crie uma nova ideia abaixo. +
+ )} {chosen && entrySubmission && (