Skip to content

Commit

Permalink
ottimizzazione in elenco utenti
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Aug 4, 2024
1 parent a553311 commit c70b4ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions code/app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public function index(Request $request)
return $this->easyExecute(function() use ($request) {
$user = $request->user();
$users = $this->service->list('', $user->can('users.admin', $user->gas));

/*
Il grosso collo di bottiglia nell'enumerazione degli utenti è
il recupero sullo stato del saldo di ciascuno, da cui dipendono
poi icone e filtri.
Qui pre-carico il saldo corrente di ognuno, premesso che nella
funzione CreditableTrait::currentBalance() verifico che
effettivamente esista o se è necessario allocare un nuovo saldo
corrente
*/
$users->loadMissing(['balances' => fn($query) => $query->where('current', true)]);

return view('pages.users', ['users' => $users]);
});
}
Expand Down
4 changes: 2 additions & 2 deletions code/app/Models/Concerns/CreditableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public function currentBalance($currency)
{
$proxy = $this->getActualObject();

$balance = $proxy->balances()->where('current', true)->where('currency_id', $currency->id)->orderBy('date', 'desc')->first();
$balance = $proxy->balances->where('current', true)->where('currency_id', $currency->id)->sortByDesc('date')->first();
if (is_null($balance)) {
$balance = $this->balances()->where('current', false)->where('currency_id', $currency->id)->orderBy('date', 'desc')->first();
$balance = $this->balances->where('current', false)->where('currency_id', $currency->id)->sortByDesc('date')->first();
if (is_null($balance)) {
$balance = $this->fixFirstBalance($currency);
}
Expand Down

0 comments on commit c70b4ca

Please sign in to comment.