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,20 +1130,25 @@ 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)
$contractDocs = Document::query() // IMPORTANT: If there are no contracts for this case we must NOT return all contract documents from other cases.
->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at']) if ($contractIds->isEmpty()) {
->where('documentable_type', Contract::class) $contractDocs = collect();
->when($contractIds->isNotEmpty(), fn ($q) => $q->whereIn('documentable_id', $contractIds)) } else {
->orderByDesc('created_at') $contractDocs = Document::query()
->limit(300) // cap to prevent excessive payload; add pagination later if needed ->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at'])
->get() ->where('documentable_type', Contract::class)
->map(function ($d) use ($contractRefMap) { ->whereIn('documentable_id', $contractIds)
$arr = method_exists($d, 'toArray') ? $d->toArray() : (array) $d; ->orderByDesc('created_at')
$arr['contract_reference'] = $contractRefMap[$d->documentable_id] ?? null; ->limit(300) // cap to prevent excessive payload; add pagination later if needed
$arr['contract_uuid'] = optional(Contract::withTrashed()->find($d->documentable_id))->uuid; ->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() $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'])