From 3184a8536e97018a6b0a59e2020f979c290cbc71 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 22 Apr 2021 18:33:53 +0200 Subject: [PATCH] Add another command. --- .../Commands/Correction/CorrectDatabase.php | 1 + .../Correction/FixFrontpageAccounts.php | 77 +++++++++++++++++++ .../Commands/Upgrade/UpgradeDatabase.php | 1 + .../Controllers/System/InstallController.php | 1 + composer.json | 1 + 5 files changed, 81 insertions(+) create mode 100644 app/Console/Commands/Correction/FixFrontpageAccounts.php diff --git a/app/Console/Commands/Correction/CorrectDatabase.php b/app/Console/Commands/Correction/CorrectDatabase.php index 29cc6f4e2b..ee61a0bb72 100644 --- a/app/Console/Commands/Correction/CorrectDatabase.php +++ b/app/Console/Commands/Correction/CorrectDatabase.php @@ -76,6 +76,7 @@ class CorrectDatabase extends Command 'firefly-iii:fix-recurring-transactions', 'firefly-iii:restore-oauth-keys', 'firefly-iii:fix-transaction-types', + 'firefly-iii:fix-frontpage-accounts' ]; foreach ($commands as $command) { $this->line(sprintf('Now executing %s', $command)); diff --git a/app/Console/Commands/Correction/FixFrontpageAccounts.php b/app/Console/Commands/Correction/FixFrontpageAccounts.php new file mode 100644 index 0000000000..fe004b8d5c --- /dev/null +++ b/app/Console/Commands/Correction/FixFrontpageAccounts.php @@ -0,0 +1,77 @@ +fixPreference($preference); + } + } + + return 0; + } + + /** + * @param Preference $preference + */ + private function fixPreference(Preference $preference): void + { + $fixed = []; + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + if (null === $preference->user) { + return; + } + $repository->setUser($preference->user); + $data = $preference->data; + if (is_array($data)) { + /** @var string $accountId */ + foreach ($data as $accountId) { + $accountId = (int)$accountId; + $account = $repository->findNull($accountId); + if (null !== $account) { + if (in_array($account->accountType->type, [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE], true)) { + $fixed[] = $account->id; + continue; + } + } + } + } + Preferences::setForUser($preference->user, 'frontPageAccounts', $fixed); + } +} diff --git a/app/Console/Commands/Upgrade/UpgradeDatabase.php b/app/Console/Commands/Upgrade/UpgradeDatabase.php index 7dd43c5b48..8b8a813844 100644 --- a/app/Console/Commands/Upgrade/UpgradeDatabase.php +++ b/app/Console/Commands/Upgrade/UpgradeDatabase.php @@ -95,6 +95,7 @@ class UpgradeDatabase extends Command 'firefly-iii:fix-recurring-transactions', 'firefly-iii:unify-group-accounts', 'firefly-iii:fix-transaction-types', + 'firefly-iii:fix-frontpage-accounts', // two report commands 'firefly-iii:report-empty-objects', diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.php index ad808437e3..f74120e1c0 100644 --- a/app/Http/Controllers/System/InstallController.php +++ b/app/Http/Controllers/System/InstallController.php @@ -105,6 +105,7 @@ class InstallController extends Controller 'firefly-iii:fix-recurring-transactions' => [], 'firefly-iii:unify-group-accounts' => [], 'firefly-iii:fix-transaction-types' => [], + 'firefly-iii:fix-frontpage-accounts' => [], // final command to set latest version in DB 'firefly-iii:set-latest-version' => ['--james-is-cool' => true], diff --git a/composer.json b/composer.json index c324c9f230..1990999d97 100644 --- a/composer.json +++ b/composer.json @@ -192,6 +192,7 @@ "@php artisan firefly-iii:fix-recurring-transactions", "@php artisan firefly-iii:unify-group-accounts", "@php artisan firefly-iii:fix-transaction-types", + "@php artisan firefly-iii:fix-frontpage-accounts", "@php artisan firefly-iii:report-empty-objects", "@php artisan firefly-iii:report-sum",