From c177264b0ba4c9f6f853f8b4d9967c8a1162f7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pocrnji=C4=8D?= Date: Wed, 8 Oct 2025 23:11:42 +0200 Subject: [PATCH] document bug where documents from other contracts are shown under client case with not contracts --- app/Http/Controllers/ClientCaseContoller.php | 31 ++++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/ClientCaseContoller.php b/app/Http/Controllers/ClientCaseContoller.php index 01889f0..1d1e563 100644 --- a/app/Http/Controllers/ClientCaseContoller.php +++ b/app/Http/Controllers/ClientCaseContoller.php @@ -1130,20 +1130,25 @@ public function show(ClientCase $clientCase) // Merge client case and contract documents into a single array and include contract reference when applicable $contractIds = $contracts->pluck('id'); // Include 'uuid' so frontend can build document routes (was causing missing 'document' param error) - $contractDocs = Document::query() - ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at']) - ->where('documentable_type', Contract::class) - ->when($contractIds->isNotEmpty(), fn ($q) => $q->whereIn('documentable_id', $contractIds)) - ->orderByDesc('created_at') - ->limit(300) // cap to prevent excessive payload; add pagination later if needed - ->get() - ->map(function ($d) use ($contractRefMap) { - $arr = method_exists($d, 'toArray') ? $d->toArray() : (array) $d; - $arr['contract_reference'] = $contractRefMap[$d->documentable_id] ?? null; - $arr['contract_uuid'] = optional(Contract::withTrashed()->find($d->documentable_id))->uuid; + // IMPORTANT: If there are no contracts for this case we must NOT return all contract documents from other cases. + if ($contractIds->isEmpty()) { + $contractDocs = collect(); + } else { + $contractDocs = Document::query() + ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at']) + ->where('documentable_type', Contract::class) + ->whereIn('documentable_id', $contractIds) + ->orderByDesc('created_at') + ->limit(300) // cap to prevent excessive payload; add pagination later if needed + ->get() + ->map(function ($d) use ($contractRefMap) { + $arr = method_exists($d, 'toArray') ? $d->toArray() : (array) $d; + $arr['contract_reference'] = $contractRefMap[$d->documentable_id] ?? null; + $arr['contract_uuid'] = optional(Contract::withTrashed()->find($d->documentable_id))->uuid; - return $arr; - }); + return $arr; + }); + } $caseDocs = $case->documents() ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at'])