From 07ff2305fd7760349c61fd25245bf21b61c26a42 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 8 May 2025 20:22:01 +0200 Subject: [PATCH] Fix account list and remove sanctum --- .env.example | 7 -- app/Http/Kernel.php | 2 +- .../JsonApi/Enrichments/AccountEnrichment.php | 64 +++++++++++-------- app/Transformers/AccountTransformer.php | 1 + package-lock.json | 6 +- .../assets/v2/src/api/v1/model/account/get.js | 26 ++++++++ .../assets/v2/src/pages/accounts/index.js | 3 +- .../partials/dashboard/account-list.blade.php | 6 +- .../dashboard/subscriptions.blade.php | 2 +- 9 files changed, 73 insertions(+), 44 deletions(-) diff --git a/.env.example b/.env.example index 6ca952fe17..7c32e7c613 100644 --- a/.env.example +++ b/.env.example @@ -296,13 +296,6 @@ STATIC_CRON_TOKEN= # However if you know what you're doing you can significantly speed up container start times. # Set each value to true to enable, or false to disable. -# Set this to true to build all locales supported by Firefly III. -# This may take quite some time (several minutes) and is generally not recommended. -# If you wish to change or alter the list of locales, start your Docker container with -# `docker run -v locale.gen:/etc/locale.gen -e DKR_BUILD_LOCALE=true` -# and make sure your preferred locales are in your own locale.gen. -DKR_BUILD_LOCALE=false - # Check if the SQLite database exists. Can be skipped if you're not using SQLite. # Won't significantly speed up things. DKR_CHECK_SQLITE=true diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 139547582c..e11e92afbd 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -177,7 +177,7 @@ class Kernel extends HttpKernel // full API authentication 'api' => [ AcceptHeaders::class, - EnsureFrontendRequestsAreStateful::class, +// EnsureFrontendRequestsAreStateful::class, 'auth:api,sanctum', 'bindings', ], diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php index 33a3aa6d56..baa5b45e79 100644 --- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php +++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php @@ -50,7 +50,7 @@ class AccountEnrichment implements EnrichmentInterface private array $accountIds; private array $accountTypeIds; private array $accountTypes; - private Collection $collection; + private Collection $collection; private array $currencies; private array $locations; private array $meta; @@ -59,6 +59,7 @@ class AccountEnrichment implements EnrichmentInterface private array $openingBalances; private User $user; private UserGroup $userGroup; + private array $lastActivities; public function __construct() { @@ -69,6 +70,7 @@ class AccountEnrichment implements EnrichmentInterface $this->accountTypes = []; $this->meta = []; $this->notes = []; + $this->lastActivities = []; $this->locations = []; // $this->repository = app(AccountRepositoryInterface::class); // $this->currencyRepository = app(CurrencyRepositoryInterface::class); @@ -77,7 +79,7 @@ class AccountEnrichment implements EnrichmentInterface } #[\Override] - public function enrichSingle(array|Model $model): Account|array + public function enrichSingle(array | Model $model): Account | array { Log::debug(__METHOD__); $collection = new Collection([$model]); @@ -100,6 +102,7 @@ class AccountEnrichment implements EnrichmentInterface $this->getAccountTypes(); $this->collectMetaData(); $this->collectNotes(); + $this->collectLastActivities(); $this->collectLocations(); $this->collectOpeningBalances(); $this->appendCollectedData(); @@ -130,10 +133,9 @@ class AccountEnrichment implements EnrichmentInterface private function collectMetaData(): void { - $set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt']) - ->whereIn('account_id', $this->accountIds) - ->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray() - ; + $set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt']) + ->whereIn('account_id', $this->accountIds) + ->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray(); /** @var array $entry */ foreach ($set as $entry) { @@ -142,7 +144,7 @@ class AccountEnrichment implements EnrichmentInterface $this->currencies[(int) $entry['data']] = true; } } - $currencies = TransactionCurrency::whereIn('id', array_keys($this->currencies))->get(); + $currencies = TransactionCurrency::whereIn('id', array_keys($this->currencies))->get(); foreach ($currencies as $currency) { $this->currencies[(int) $currency->id] = $currency; } @@ -157,10 +159,9 @@ class AccountEnrichment implements EnrichmentInterface private function collectNotes(): void { $notes = Note::query()->whereIn('noteable_id', $this->accountIds) - ->whereNotNull('notes.text') - ->where('notes.text', '!=', '') - ->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray() - ; + ->whereNotNull('notes.text') + ->where('notes.text', '!=', '') + ->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray(); foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } @@ -170,15 +171,14 @@ class AccountEnrichment implements EnrichmentInterface private function collectLocations(): void { $locations = Location::query()->whereIn('locatable_id', $this->accountIds) - ->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray() - ; + ->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray(); foreach ($locations as $location) { $this->locations[(int) $location['locatable_id']] = [ - 'latitude' => (float) $location['latitude'], - 'longitude' => (float) $location['longitude'], - 'zoom_level' => (int) $location['zoom_level'], - ]; + 'latitude' => (float) $location['latitude'], + 'longitude' => (float) $location['longitude'], + 'zoom_level' => (int) $location['zoom_level'], + ]; } Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations))); } @@ -193,20 +193,19 @@ class AccountEnrichment implements EnrichmentInterface ->setUserGroup($this->userGroup) ->setAccounts($this->collection) ->withAccountInformation() - ->setTypes([TransactionTypeEnum::OPENING_BALANCE->value]) - ; - $journals = $collector->getExtractedJournals(); + ->setTypes([TransactionTypeEnum::OPENING_BALANCE->value]); + $journals = $collector->getExtractedJournals(); foreach ($journals as $journal) { $this->openingBalances[(int) $journal['source_account_id']] = [ - 'amount' => Steam::negative($journal['amount']), - 'date' => $journal['date'], - ]; + 'amount' => Steam::negative($journal['amount']), + 'date' => $journal['date'], + ]; $this->openingBalances[(int) $journal['destination_account_id']] = [ - 'amount' => Steam::positive($journal['amount']), - 'date' => $journal['date'], - ]; + 'amount' => Steam::positive($journal['amount']), + 'date' => $journal['date'], + ]; } } @@ -229,7 +228,8 @@ class AccountEnrichment implements EnrichmentInterface $notes = $this->notes; $openingBalances = $this->openingBalances; $locations = $this->locations; - $this->collection = $this->collection->map(function (Account $item) use ($accountTypes, $meta, $currencies, $notes, $openingBalances, $locations) { + $lastActivities = $this->lastActivities; + $this->collection = $this->collection->map(function (Account $item) use ($accountTypes, $meta, $currencies, $notes, $openingBalances, $locations, $lastActivities) { $item->full_account_type = $accountTypes[(int) $item->account_type_id] ?? null; $accountMeta = [ 'currency' => null, @@ -264,7 +264,10 @@ class AccountEnrichment implements EnrichmentInterface if (array_key_exists($item->id, $locations)) { $accountMeta['location'] = $locations[$item->id]; } - $item->meta = $accountMeta; + if (array_key_exists($item->id, $lastActivities)) { + $accountMeta['last_activity'] = $lastActivities[$item->id]; + } + $item->meta = $accountMeta; return $item; }); @@ -274,4 +277,9 @@ class AccountEnrichment implements EnrichmentInterface { $this->native = $native; } + + private function collectLastActivities(): void + { + $this->lastActivities = Steam::getLastActivities($this->accountIds); + } } diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 82c8fc9c04..5d959898a3 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -152,6 +152,7 @@ class AccountTransformer extends AbstractTransformer 'longitude' => $longitude, 'latitude' => $latitude, 'zoom_level' => $zoomLevel, + 'last_activity' => array_key_exists('last_activity', $account->meta) ? $account->meta['last_activity']->toAtomString() : null, 'links' => [ [ 'rel' => 'self', diff --git a/package-lock.json b/package-lock.json index 5ba613d7ec..23e6dd8ac3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4416,9 +4416,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001716", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001716.tgz", - "integrity": "sha512-49/c1+x3Kwz7ZIWt+4DvK3aMJy9oYXXG6/97JKsnjdCk/6n9vVyWL8NAwVt95Lwt9eigI10Hl782kDfZUUlRXw==", + "version": "1.0.30001717", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001717.tgz", + "integrity": "sha512-auPpttCq6BDEG8ZAuHJIplGw6GODhjw+/11e7IjpnYCxZcW/ONgPs0KVBJ0d1bY3e2+7PRe5RCLyP+PfwVgkYw==", "dev": true, "funding": [ { diff --git a/resources/assets/v2/src/api/v1/model/account/get.js b/resources/assets/v2/src/api/v1/model/account/get.js index a8d014a42c..8d8b8aed5b 100644 --- a/resources/assets/v2/src/api/v1/model/account/get.js +++ b/resources/assets/v2/src/api/v1/model/account/get.js @@ -20,6 +20,7 @@ import {api} from "../../../../boot/axios"; import format from "date-fns/format"; +import {getCacheKey} from "../../../../support/get-cache-key.js"; export default class Get { @@ -37,6 +38,31 @@ export default class Get { return api.get('/api/v1/accounts/' + identifier, {params: params}); } + /** + * + * @param params + * @returns {Promise>} + */ + index(params) { + // first, check API in some consistent manner. + // then, load if necessary. + const cacheKey = getCacheKey('/api/v1/accounts', params); + const cacheValid = window.store.get('cacheValid'); + let cachedData = window.store.get(cacheKey); + + if (cacheValid && typeof cachedData !== 'undefined') { + console.log('Cache is valid, return cache.'); + return Promise.resolve(cachedData); + } + + // if not, store in cache and then return res. + + return api.get('/api/v1/accounts', {params: params}).then(response => { + console.log('Cache is invalid, return fresh.'); + window.store.set(cacheKey, response.data); + return Promise.resolve({data: response.data.data, meta: response.data.meta}); + }); + } /** * diff --git a/resources/assets/v2/src/pages/accounts/index.js b/resources/assets/v2/src/pages/accounts/index.js index 80f4b34649..295aaa2902 100644 --- a/resources/assets/v2/src/pages/accounts/index.js +++ b/resources/assets/v2/src/pages/accounts/index.js @@ -383,7 +383,8 @@ let index = function () { // one page only.o (new Get()).index(params).then(response => { console.log(response); - this.totalPages = response.meta.lastPage; + this.totalPages = response.meta.pagination.total_pages; + console.log('a'); for (let i = 0; i < response.data.length; i++) { if (response.data.hasOwnProperty(i)) { let current = response.data[i]; diff --git a/resources/views/v2/partials/dashboard/account-list.blade.php b/resources/views/v2/partials/dashboard/account-list.blade.php index 9dafa4da16..a7b2f5078d 100644 --- a/resources/views/v2/partials/dashboard/account-list.blade.php +++ b/resources/views/v2/partials/dashboard/account-list.blade.php @@ -9,7 +9,7 @@

- @@ -42,7 +42,7 @@ -
+
    diff --git a/resources/views/v2/partials/dashboard/subscriptions.blade.php b/resources/views/v2/partials/dashboard/subscriptions.blade.php index d5102d6898..823e5ced30 100644 --- a/resources/views/v2/partials/dashboard/subscriptions.blade.php +++ b/resources/views/v2/partials/dashboard/subscriptions.blade.php @@ -30,7 +30,7 @@