Refactor methods that request the old currency preference.

This commit is contained in:
James Cole 2023-10-22 07:55:36 +02:00
parent 4cec0a9f97
commit 80237d8bc3
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
7 changed files with 46 additions and 62 deletions

View File

@ -31,6 +31,7 @@ use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\AccountFilter; use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\CurrencyTransformer; use FireflyIII\Transformers\CurrencyTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use JsonException; use JsonException;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
@ -79,12 +80,14 @@ class StoreController extends Controller
{ {
$currency = $this->repository->store($request->getAll()); $currency = $this->repository->store($request->getAll());
if (true === $request->boolean('default')) { if (true === $request->boolean('default')) {
app('preferences')->set('currencyPreference', $currency->code); $this->repository->makeDefault($currency);
app('preferences')->mark(); app('preferences')->mark();
} }
$manager = $this->getManager(); $manager = $this->getManager();
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$this->parameters->set('defaultCurrency', $defaultCurrency); /** @var User $user */
$user = auth()->user();
$currency->refreshForUser($user);
/** @var CurrencyTransformer $transformer */ /** @var CurrencyTransformer $transformer */
$transformer = app(CurrencyTransformer::class); $transformer = app(CurrencyTransformer::class);

View File

@ -56,13 +56,11 @@ class JavascriptController extends Controller
*/ */
public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
{ {
$accounts = $repository->getAccountsByType( $accounts = $repository->getAccountsByType(
[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD] [AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]
); );
$preference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR')); $default = app('amount')->getDefaultCurrency();
$default = $currencyRepository->findByCodeNull((string)$preference->data); $data = ['accounts' => []];
$data = ['accounts' => []];
/** @var Account $account */ /** @var Account $account */
foreach ($accounts as $account) { foreach ($accounts as $account) {

View File

@ -114,7 +114,7 @@ class NewUserController extends Controller
$this->createCashWalletAccount($currency, $language); // create cash wallet account $this->createCashWalletAccount($currency, $language); // create cash wallet account
// store currency preference: // store currency preference:
app('preferences')->set('currencyPreference', $currency->code); $currencyRepository->makeDefault($currency);
// store frontpage preferences: // store frontpage preferences:
$accounts = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray(); $accounts = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray();

View File

@ -418,10 +418,6 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
if (null !== $currencyPreference) { if (null !== $currencyPreference) {
$currency = TransactionCurrency::where('id', $currencyPreference->data)->first(); $currency = TransactionCurrency::where('id', $currencyPreference->data)->first();
} }
if (null === $currencyPreference) {
$currencyCode = app('preferences')->getForUser($this->user, 'currencyPreference', 'EUR')->data;
$currency = TransactionCurrency::where('code', $currencyCode)->first();
}
$journalId = (int)$row->transaction_journal_id; $journalId = (int)$row->transaction_journal_id;
$return[$journalId] = $return[$journalId] ?? []; $return[$journalId] = $return[$journalId] ?? [];

View File

@ -108,35 +108,10 @@ class Amount
*/ */
public function getCurrencies(): Collection public function getCurrencies(): Collection
{ {
throw new FireflyException(sprintf('Method "%s" needs a refactor', __METHOD__));
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get(); return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
} }
/**
* @return string
* @throws FireflyException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getCurrencyCode(): string
{
$cache = new CacheProperties();
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
return $cache->get();
}
$currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
if ($currency) {
$cache->store($currency->code);
return $currency->code;
}
$cache->store(config('firefly.default_currency', 'EUR'));
return (string)config('firefly.default_currency', 'EUR');
}
/** /**
* @return TransactionCurrency * @return TransactionCurrency
* @throws FireflyException * @throws FireflyException
@ -163,22 +138,14 @@ class Amount
if ($cache->has()) { if ($cache->has()) {
return $cache->get(); return $cache->get();
} }
$currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR')); $default = $user->currencies()->where('user_default', true)->first();
$currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR'; if(null === $default) {
$default = $this->getSystemCurrency();
// at this point the currency preference could be encrypted, if coming from an old version. $user->currencies()->sync([$default->id => ['user_default' => true]]);
$currencyCode = $this->tryDecrypt((string)$currencyPrefStr);
// could still be json encoded:
/** @var TransactionCurrency|null $currency */
$currency = TransactionCurrency::where('code', $currencyCode)->first();
if (null === $currency) {
// get EUR
$currency = TransactionCurrency::where('code', 'EUR')->first();
} }
$cache->store($currency); $cache->store($default);
return $currency; return $default;
} }
/** /**

View File

@ -60,6 +60,9 @@ class Preferences
*/ */
public function get(string $name, $default = null): ?Preference public function get(string $name, $default = null): ?Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
if (null === $user) { if (null === $user) {
@ -82,6 +85,9 @@ class Preferences
*/ */
public function getForUser(User $user, string $name, $default = null): ?Preference public function getForUser(User $user, string $name, $default = null): ?Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']); $preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $preference->data) { if (null !== $preference && null === $preference->data) {
$preference->delete(); $preference->delete();
@ -108,6 +114,9 @@ class Preferences
*/ */
public function delete(string $name): bool public function delete(string $name): bool
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$fullName = sprintf('preference%s%s', auth()->user()->id, $name); $fullName = sprintf('preference%s%s', auth()->user()->id, $name);
if (Cache::has($fullName)) { if (Cache::has($fullName)) {
Cache::forget($fullName); Cache::forget($fullName);
@ -123,6 +132,9 @@ class Preferences
*/ */
public function forget(User $user, string $name): void public function forget(User $user, string $name): void
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$key = sprintf('preference%s%s', $user->id, $name); $key = sprintf('preference%s%s', $user->id, $name);
Cache::forget($key); Cache::forget($key);
Cache::put($key, '', 5); Cache::put($key, '', 5);
@ -138,6 +150,9 @@ class Preferences
*/ */
public function setForUser(User $user, string $name, $value): Preference public function setForUser(User $user, string $name, $value): Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$fullName = sprintf('preference%s%s', $user->id, $name); $fullName = sprintf('preference%s%s', $user->id, $name);
Cache::forget($fullName); Cache::forget($fullName);
/** @var Preference|null $pref */ /** @var Preference|null $pref */
@ -185,6 +200,9 @@ class Preferences
*/ */
public function findByName(string $name): Collection public function findByName(string $name): Collection
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
return Preference::where('name', $name)->get(); return Preference::where('name', $name)->get();
} }
@ -220,6 +238,9 @@ class Preferences
*/ */
public function getFresh(string $name, $default = null): ?Preference public function getFresh(string $name, $default = null): ?Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
/** @var User|null $user */ /** @var User|null $user */
$user = auth()->user(); $user = auth()->user();
if (null === $user) { if (null === $user) {
@ -243,6 +264,9 @@ class Preferences
*/ */
public function getFreshForUser(User $user, string $name, $default = null): ?Preference public function getFreshForUser(User $user, string $name, $default = null): ?Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
return $this->getForUser($user, $name, $default); return $this->getForUser($user, $name, $default);
} }
@ -283,6 +307,9 @@ class Preferences
*/ */
public function set(string $name, $value): Preference public function set(string $name, $value): Preference
{ {
if('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$user = auth()->user(); $user = auth()->user();
if (null === $user) { if (null === $user) {
// make new preference, return it: // make new preference, return it:

View File

@ -34,23 +34,16 @@ class CurrencyTransformer extends AbstractTransformer
* Transform the currency. * Transform the currency.
* *
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
*
* @return array * @return array
*/ */
public function transform(TransactionCurrency $currency): array public function transform(TransactionCurrency $currency): array
{ {
$isDefault = false;
$defaultCurrency = $this->parameters->get('defaultCurrency');
if (null !== $defaultCurrency) {
$isDefault = (int)$defaultCurrency->id === (int)$currency->id;
}
return [ return [
'id' => (int)$currency->id, 'id' => (int)$currency->id,
'created_at' => $currency->created_at->toAtomString(), 'created_at' => $currency->created_at->toAtomString(),
'updated_at' => $currency->updated_at->toAtomString(), 'updated_at' => $currency->updated_at->toAtomString(),
'default' => $isDefault, 'default' => $currency->userDefault,
'enabled' => $currency->enabled, 'enabled' => $currency->userEnabled,
'name' => $currency->name, 'name' => $currency->name,
'code' => $currency->code, 'code' => $currency->code,
'symbol' => $currency->symbol, 'symbol' => $currency->symbol,