document bug where documents from other contracts are shown under client case with not contracts

This commit is contained in:
Simon Pocrnjič 2025-10-08 23:11:42 +02:00
parent 1b96b0d821
commit c177264b0b

View File

@ -1130,10 +1130,14 @@ public function show(ClientCase $clientCase)
// Merge client case and contract documents into a single array and include contract reference when applicable // Merge client case and contract documents into a single array and include contract reference when applicable
$contractIds = $contracts->pluck('id'); $contractIds = $contracts->pluck('id');
// Include 'uuid' so frontend can build document routes (was causing missing 'document' param error) // Include 'uuid' so frontend can build document routes (was causing missing 'document' param error)
// 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() $contractDocs = Document::query()
->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at']) ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at'])
->where('documentable_type', Contract::class) ->where('documentable_type', Contract::class)
->when($contractIds->isNotEmpty(), fn ($q) => $q->whereIn('documentable_id', $contractIds)) ->whereIn('documentable_id', $contractIds)
->orderByDesc('created_at') ->orderByDesc('created_at')
->limit(300) // cap to prevent excessive payload; add pagination later if needed ->limit(300) // cap to prevent excessive payload; add pagination later if needed
->get() ->get()
@ -1144,6 +1148,7 @@ public function show(ClientCase $clientCase)
return $arr; return $arr;
}); });
}
$caseDocs = $case->documents() $caseDocs = $case->documents()
->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at']) ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at'])