Notifications change

This commit is contained in:
Simon Pocrnjič
2025-10-23 00:10:38 +02:00
parent 67ebe4b225
commit 3a2eed7dda
5 changed files with 312 additions and 6 deletions
+54 -2
View File
@@ -86,8 +86,20 @@ public function share(Request $request): array
'contract' => function ($q) {
$q->select(['contracts.id', 'contracts.uuid', 'contracts.reference', 'contracts.client_case_id'])
->with([
// Include client (via case) so the UI can render client.person.full_name
'clientCase' => function ($qq) {
$qq->select(['client_cases.id', 'client_cases.uuid']);
// Include person_id to ensure nested person loads correctly and to avoid null clientCase due to narrow selects
$qq->select(['client_cases.id', 'client_cases.uuid', 'client_cases.client_id', 'client_cases.person_id'])
->with([
'client' => function ($qqq) {
$qqq->select(['clients.id', 'clients.person_id'])
->with([
'person' => function ($qqqq) {
$qqqq->select(['person.id', 'person.full_name']);
},
]);
},
]);
},
'account' => function ($qq) {
$qq->select(['accounts.id', 'accounts.contract_id', 'accounts.balance_amount', 'accounts.initial_amount']);
@@ -95,15 +107,55 @@ public function share(Request $request): array
]);
},
'clientCase' => function ($q) {
$q->select(['client_cases.id', 'client_cases.uuid', 'client_cases.person_id'])
$q->select(['client_cases.id', 'client_cases.uuid', 'client_cases.person_id', 'client_cases.client_id'])
->with([
'person' => function ($qq) {
$qq->select(['person.id', 'person.full_name']);
},
'client' => function ($qq) {
$qq->select(['clients.id', 'clients.person_id'])
->with([
'person' => function ($qqq) {
$qqq->select(['person.id', 'person.full_name']);
},
]);
},
]);
},
]);
// For convenience on the frontend, mirror client onto the contract so it can be accessed as contract.client.person
// 1) Build a map of contract_id -> client_id using a lightweight join
$contractIds = $activities->pluck('contract_id')->filter()->unique()->values();
if ($contractIds->isNotEmpty()) {
$mapContractToClient = \App\Models\Contract::query()
->whereIn('contracts.id', $contractIds)
->join('client_cases', 'client_cases.id', '=', 'contracts.client_case_id')
->pluck('client_cases.client_id', 'contracts.id');
// 2) Load all needed clients with their person
$clientIds = $mapContractToClient->filter()->unique()->values();
$clientsById = $clientIds->isNotEmpty()
? \App\Models\Client::query()
->whereIn('clients.id', $clientIds)
->with(['person:id,full_name'])
->get(['clients.id', 'clients.person_id'])
->keyBy('id')
: collect();
// 3) Attach client relation on each contract instance
foreach ($activities as $act) {
$contract = $act->getRelation('contract');
if (! $contract) {
continue;
}
$cid = $mapContractToClient->get($contract->id);
if ($cid && $clientsById->has($cid)) {
$contract->setRelation('client', $clientsById->get($cid));
}
}
}
return [
'dueToday' => [
'count' => $activities->count(),