diff --git a/app/Http/Controllers/CompanyComparisonController.php b/app/Http/Controllers/CompanyComparisonController.php index a6677db4..c077ea4e 100644 --- a/app/Http/Controllers/CompanyComparisonController.php +++ b/app/Http/Controllers/CompanyComparisonController.php @@ -3,8 +3,10 @@ namespace App\Http\Controllers; use App\Services\CompanyComparison; +use App\Services\InvestorPath; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; use Inertia\Inertia; use Inertia\Response; @@ -13,8 +15,14 @@ final class CompanyComparisonController extends Controller public function index(Request $request, CompanyComparison $service): Response|JsonResponse { $answers = $service->answers($request->all()); + // Comparisons already kept in the investor's own dossiers, to resume them from the general page. + $contexts = $request->user() ? collect(app(InvestorPath::class)->contexts($request->user())) : collect(); + $kept = $contexts->isEmpty() ? collect() : DB::table('desk_events')->whereIn('case_id', $contexts->pluck('id')) + ->where('action', CompanyComparison::EVENT)->selectRaw('case_id, max(created_at) as at')->groupBy('case_id')->pluck('at', 'case_id'); $data = ['definition' => $service->definition(), 'answers' => $answers, 'result' => $service->evaluate($answers), - 'case' => null, 'saved' => null, 'canWrite' => false, 'history' => [], 'historyNext' => null, 'historical' => false, 'workingCopy' => false]; + 'case' => null, 'saved' => null, 'canWrite' => false, 'history' => [], 'historyNext' => null, 'historical' => false, 'workingCopy' => false, + 'savedCases' => $contexts->filter(fn (array $c) => $kept->has($c['id'])) + ->map(fn (array $c) => ['id' => $c['id'], 'title' => $c['title'], 'at' => (string) $kept[$c['id']]])->values()->all()]; return $request->expectsJson() ? response()->json($data) : Inertia::render('investment/company-comparison', $data); } diff --git a/resources/js/pages/investment/company-comparison.tsx b/resources/js/pages/investment/company-comparison.tsx index 422e3b0b..8b07c880 100644 --- a/resources/js/pages/investment/company-comparison.tsx +++ b/resources/js/pages/investment/company-comparison.tsx @@ -56,6 +56,8 @@ type Props = { answers: CompanyAnswers; result: Result; saved: { revision: number; at: string; result: Result } | null; + /** Dossiers of the investor that already keep a comparison (general page only). */ + savedCases?: { id: string; title: string; at: string }[]; history: { id: string; revision: number; @@ -148,6 +150,25 @@ function Comparator(props: Props) {

)}

{comparisonIntroduction}

+ {!dossier && (props.savedCases?.length ?? 0) > 0 && ( +
+

+ Comparações guardadas nos seus dossiers +

+ +
+ )} {saved && (

{historical @@ -183,7 +204,18 @@ function Comparator(props: Props) { ) : ( -

+ { + // In a dossier the comparison is always kept: comparing also saves. + if (!dossier || !props.canWrite) return; + event.preventDefault(); + void save(); + }} + >
{Object.entries(definition.questions).map( ([key, question]) => @@ -239,21 +271,23 @@ function Comparator(props: Props) { )}
- - {dossier ? ( - props.canWrite && ( - - ) + {dossier && props.canWrite ? ( + ) : ( + + )} + {dossier ? null : (