diff --git a/app/JsonApi/V2/Accounts/AccountResource.php b/app/JsonApi/V2/Accounts/AccountResource.php index 437a7ce20c..398f1c81b0 100644 --- a/app/JsonApi/V2/Accounts/AccountResource.php +++ b/app/JsonApi/V2/Accounts/AccountResource.php @@ -34,9 +34,30 @@ class AccountResource extends JsonApiResource { Log::debug(__METHOD__); return [ - 'created_at' => $this->resource->created_at, - 'updated_at' => $this->resource->updated_at, - 'name' => $this->resource->name, + 'created_at' => $this->resource->created_at, + 'updated_at' => $this->resource->updated_at, + 'name' => $this->resource->name, + 'active' => $this->resource->active, + 'order' => $this->resource->order, + 'type' => $this->resource->account_type_string, + 'account_role' => $this->resource->account_role, + 'account_number' => '' === $this->resource->account_number ? null : $this->resource->account_number, + + // currency + 'currency_id' => $this->resource->currency_id, + 'currency_name' => $this->resource->currency_name, + 'currency_code' => $this->resource->currency_code, + 'currency_symbol' => $this->resource->currency_symbol, + 'currency_decimal_places' => $this->resource->currency_decimal_places, + + // liability things + 'liability_direction' => $this->resource->liability_direction, + 'interest' => $this->resource->interest, + 'interest_period' => $this->resource->interest_period, + 'current_debt' => $this->resource->current_debt, + + + 'last_activity' => $this->resource->last_activity, ]; } diff --git a/app/JsonApi/V2/Accounts/AccountResourceOld.php b/app/JsonApi/V2/Accounts/AccountResourceOld.php index add0e625d6..fdc06735fa 100644 --- a/app/JsonApi/V2/Accounts/AccountResourceOld.php +++ b/app/JsonApi/V2/Accounts/AccountResourceOld.php @@ -25,32 +25,23 @@ class AccountResourceOld extends JsonApiResource */ public function attributes($request): iterable { + // fields removed here have been migrated. return [ 'created_at' => $this->resource->created_at, 'updated_at' => $this->resource->updated_at, 'name' => $this->resource->name, -// 'iban' => '' === $this->resource->iban ? null : $this->resource->iban, -// 'active' => $this->resource->active, -// 'last_activity' => $this->resource->last_activity, -// 'type' => $this->resource->type, -// 'account_role' => $this->resource->account_role, // 'virtual_balance' => $this->resource->virtual_balance, // 'native_balance' => $this->resource->native_balance, // 'user' => $this->resource->user_array, // 'balances' => [] // - // currency - // 'currency_id' => $this->resource->currency_id, - // 'currency_code' => $this->resource->currency_code, - // 'currency_symbol' => $this->resource->currency_symbol, - // 'currency_decimal_places' => $this->resource->currency_decimal_places, // balance (in currency, on date) // 'current_balance' => $this->resource->current_balance, - // 'current_balance' => app('steam')->bcround(app('steam')->balance($account, $date), $decimalPlaces), // 'current_balance_date' => $date->toAtomString(), + // 'notes' => $this->repository->getNoteText($account), // 'monthly_payment_date' => $monthlyPaymentDate, // 'credit_card_type' => $creditCardType, @@ -70,11 +61,6 @@ class AccountResourceOld extends JsonApiResource // 'order' => $order, - // 'currency_id' => (string) $currency->id, - // 'currency_code' => $currency->code, - // 'currency_symbol' => $currency->symbol, - // 'currency_decimal_places' => $currency->decimal_places, - // // 'native_currency_id' => (string) $this->default->id, // 'native_currency_code' => $this->default->code, // 'native_currency_symbol' => $this->default->symbol, @@ -91,15 +77,9 @@ class AccountResourceOld extends JsonApiResource // 'balance_difference_start' => $diffStart, // 'balance_difference_end' => $diffEnd, // - // // more meta - // 'last_activity' => array_key_exists($id, $this->lastActivity) ? $this->lastActivity[$id]->toAtomString() : null, // // // liability stuff // 'liability_type' => $liabilityType, - // 'liability_direction' => $liabilityDirection, - // 'interest' => $interest, - // 'interest_period' => $interestPeriod, - // 'current_debt' => $currentDebt, // // // object group // 'object_group_id' => null !== $objectGroupId ? (string) $objectGroupId : null, diff --git a/app/JsonApi/V2/Accounts/AccountSchema.php b/app/JsonApi/V2/Accounts/AccountSchema.php index 2713400f3d..d5eae3b4b8 100644 --- a/app/JsonApi/V2/Accounts/AccountSchema.php +++ b/app/JsonApi/V2/Accounts/AccountSchema.php @@ -35,6 +35,9 @@ class AccountSchema extends Schema return [ ID::make(), Attribute::make('name'), + Attribute::make('active'), + Attribute::make('order'), + Attribute::make('last_activity'), HasOne::make('user')->readOnly(), ]; } diff --git a/app/Models/Account.php b/app/Models/Account.php index 8bfdc2a192..62131b899c 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -51,6 +51,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property null|Carbon $deleted_at * @property int $user_id * @property int $account_type_id + * @property string $account_type_string * @property string $name * @property string $virtual_balance * @property null|string $iban @@ -75,6 +76,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property Collection|Transaction[] $transactions * @property null|int $transactions_count * @property User $user + * @property string $last_activity * * @method static EloquentBuilder|Account accountTypeIn($types) * @method static EloquentBuilder|Account newModelQuery() diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php index cbe3aeb929..05daf2676d 100644 --- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php +++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php @@ -42,6 +42,15 @@ class AccountEnrichment implements EnrichmentInterface private Collection $collection; private array $currencies; + private AccountRepositoryInterface $repository; + private CurrencyRepositoryInterface $currencyRepository; + + public function __construct() + { + $this->repository = app(AccountRepositoryInterface::class); + $this->currencyRepository = app(CurrencyRepositoryInterface::class); + } + #[\Override] /** * Do the actual enrichment. @@ -55,9 +64,9 @@ class AccountEnrichment implements EnrichmentInterface // do everything here: $this->getLastActivity(); - // $this->getMetaBalances(); $this->collectAccountTypes(); $this->collectMetaData(); + // $this->getMetaBalances(); // $this->collection->transform(function (Account $account) { // $account->user_array = ['id' => 1, 'bla bla' => 'bla']; @@ -78,9 +87,7 @@ class AccountEnrichment implements EnrichmentInterface */ private function getLastActivity(): void { - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $lastActivity = $accountRepository->getLastActivity($this->collection); + $lastActivity = $this->repository->getLastActivity($this->collection); foreach ($lastActivity as $row) { $this->collection->where('id', $row['account_id'])->first()->last_activity = Carbon::parse($row['date_max'], config('app.timezone')); } @@ -109,17 +116,15 @@ class AccountEnrichment implements EnrichmentInterface */ private function collectAccountTypes(): void { - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $accountTypes = $accountRepository->getAccountTypes($this->collection); - $types = []; + $accountTypes = $this->repository->getAccountTypes($this->collection); + $types = []; /** @var AccountType $row */ foreach ($accountTypes as $row) { $types[$row->id] = $row->type; } $this->collection->transform(function (Account $account) use ($types) { - $account->type = $types[$account->id]; + $account->account_type_string = $types[$account->id]; return $account; }); @@ -127,17 +132,11 @@ class AccountEnrichment implements EnrichmentInterface private function collectMetaData(): void { - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); + $metaFields = $this->repository->getMetaValues($this->collection, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']); + $currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray(); - /** @var CurrencyRepositoryInterface $repository */ - $repository = app(CurrencyRepositoryInterface::class); - - $metaFields = $accountRepository->getMetaValues($this->collection, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']); - $currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray(); - - $currencies = []; - foreach ($repository->getByIds($currencyIds) as $currency) { + $currencies = []; + foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) { $id = $currency->id; $currencies[$id] = $currency; } @@ -148,6 +147,7 @@ class AccountEnrichment implements EnrichmentInterface $account->{$entry->name} = $entry->data; if ('currency_id' === $entry->name) { $id = (int) $entry->data; + $account->currency_name = $currencies[$id]?->name; $account->currency_code = $currencies[$id]?->code; $account->currency_symbol = $currencies[$id]?->symbol; $account->currency_decimal_places = $currencies[$id]?->decimal_places;