From 0d65e396d465e031f9a37198adeb93d4af1b56e9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 22 Oct 2023 07:16:05 +0200 Subject: [PATCH] Update get() methods. --- .../Currency/CurrencyRepository.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index c977e730b0..591b4ae146 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -166,11 +166,25 @@ class CurrencyRepository implements CurrencyRepositoryInterface } /** + * Returns ALL currencies, regardless of whether they are enabled or not. + * * @return Collection */ public function getAll(): Collection { - return TransactionCurrency::orderBy('code', 'ASC')->get(); + $all = TransactionCurrency::orderBy('code', 'ASC')->get(); + $local = $this->get(); + return $all->map(function (TransactionCurrency $current) use ($local) { + $hasId = $local->contains(function (TransactionCurrency $entry) use ($current) { + return (int)$entry->id === (int)$current->id; + }); + $isDefault = $local->contains(function (TransactionCurrency $entry) use ($current) { + return 1 === (int)$entry->pivot->user_default && (int)$entry->id === (int)$current->id; + }); + $current->userEnabled = $hasId; + $current->userDefault = $isDefault; + return $current; + }); } /** @@ -178,7 +192,13 @@ class CurrencyRepository implements CurrencyRepositoryInterface */ public function get(): Collection { - return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get(); + $all = $this->user->currencies()->orderBy('code', 'ASC')->withPivot(['user_default'])->get(); + $all->map(function (TransactionCurrency $current) { + $current->userEnabled = true; + $current->userDefault = 1 === (int)$current->pivot->user_default; + return $current; + }); + return $all; } /**