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'])