Expand account object api

This commit is contained in:
James Cole
2024-07-28 07:47:54 +02:00
parent 1515dea9fa
commit ec367e94ce
5 changed files with 50 additions and 44 deletions

View File

@@ -37,6 +37,27 @@ class AccountResource extends JsonApiResource
'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,
];
}

View File

@@ -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,

View File

@@ -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(),
];
}

View File

@@ -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()

View File

@@ -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,9 +116,7 @@ class AccountEnrichment implements EnrichmentInterface
*/
private function collectAccountTypes(): void
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$accountTypes = $accountRepository->getAccountTypes($this->collection);
$accountTypes = $this->repository->getAccountTypes($this->collection);
$types = [];
/** @var AccountType $row */
@@ -119,7 +124,7 @@ class AccountEnrichment implements EnrichmentInterface
$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);
/** @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']);
$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();
$currencies = [];
foreach ($repository->getByIds($currencyIds) as $currency) {
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;