Update get() methods.

This commit is contained in:
James Cole 2023-10-22 07:16:05 +02:00
parent 704fc24d20
commit 0d65e396d4
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -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;
}
/**