From c70b4ca69c7bbba07498f524c30ce2ef949eaa4e Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sun, 4 Aug 2024 17:16:10 +0200 Subject: [PATCH] ottimizzazione in elenco utenti --- code/app/Http/Controllers/UsersController.php | 12 ++++++++++++ code/app/Models/Concerns/CreditableTrait.php | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/code/app/Http/Controllers/UsersController.php b/code/app/Http/Controllers/UsersController.php index 699fa3ea..49994eda 100644 --- a/code/app/Http/Controllers/UsersController.php +++ b/code/app/Http/Controllers/UsersController.php @@ -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]); }); } diff --git a/code/app/Models/Concerns/CreditableTrait.php b/code/app/Models/Concerns/CreditableTrait.php index d5d87f3a..1f95b228 100644 --- a/code/app/Models/Concerns/CreditableTrait.php +++ b/code/app/Models/Concerns/CreditableTrait.php @@ -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); }