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

@@ -34,9 +34,30 @@ class AccountResource extends JsonApiResource
{ {
Log::debug(__METHOD__); Log::debug(__METHOD__);
return [ return [
'created_at' => $this->resource->created_at, 'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at, 'updated_at' => $this->resource->updated_at,
'name' => $this->resource->name, '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 public function attributes($request): iterable
{ {
// fields removed here have been migrated.
return [ return [
'created_at' => $this->resource->created_at, 'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at, 'updated_at' => $this->resource->updated_at,
'name' => $this->resource->name, '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, // 'virtual_balance' => $this->resource->virtual_balance,
// 'native_balance' => $this->resource->native_balance, // 'native_balance' => $this->resource->native_balance,
// 'user' => $this->resource->user_array, // 'user' => $this->resource->user_array,
// 'balances' => [] // '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) // balance (in currency, on date)
// 'current_balance' => $this->resource->current_balance, // 'current_balance' => $this->resource->current_balance,
// 'current_balance' => app('steam')->bcround(app('steam')->balance($account, $date), $decimalPlaces), // 'current_balance' => app('steam')->bcround(app('steam')->balance($account, $date), $decimalPlaces),
// 'current_balance_date' => $date->toAtomString(), // 'current_balance_date' => $date->toAtomString(),
// 'notes' => $this->repository->getNoteText($account), // 'notes' => $this->repository->getNoteText($account),
// 'monthly_payment_date' => $monthlyPaymentDate, // 'monthly_payment_date' => $monthlyPaymentDate,
// 'credit_card_type' => $creditCardType, // 'credit_card_type' => $creditCardType,
@@ -70,11 +61,6 @@ class AccountResourceOld extends JsonApiResource
// 'order' => $order, // '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_id' => (string) $this->default->id,
// 'native_currency_code' => $this->default->code, // 'native_currency_code' => $this->default->code,
// 'native_currency_symbol' => $this->default->symbol, // 'native_currency_symbol' => $this->default->symbol,
@@ -91,15 +77,9 @@ class AccountResourceOld extends JsonApiResource
// 'balance_difference_start' => $diffStart, // 'balance_difference_start' => $diffStart,
// 'balance_difference_end' => $diffEnd, // 'balance_difference_end' => $diffEnd,
// //
// // more meta
// 'last_activity' => array_key_exists($id, $this->lastActivity) ? $this->lastActivity[$id]->toAtomString() : null,
// //
// // liability stuff // // liability stuff
// 'liability_type' => $liabilityType, // 'liability_type' => $liabilityType,
// 'liability_direction' => $liabilityDirection,
// 'interest' => $interest,
// 'interest_period' => $interestPeriod,
// 'current_debt' => $currentDebt,
// //
// // object group // // object group
// 'object_group_id' => null !== $objectGroupId ? (string) $objectGroupId : null, // 'object_group_id' => null !== $objectGroupId ? (string) $objectGroupId : null,

View File

@@ -35,6 +35,9 @@ class AccountSchema extends Schema
return [ return [
ID::make(), ID::make(),
Attribute::make('name'), Attribute::make('name'),
Attribute::make('active'),
Attribute::make('order'),
Attribute::make('last_activity'),
HasOne::make('user')->readOnly(), HasOne::make('user')->readOnly(),
]; ];
} }

View File

@@ -51,6 +51,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property null|Carbon $deleted_at * @property null|Carbon $deleted_at
* @property int $user_id * @property int $user_id
* @property int $account_type_id * @property int $account_type_id
* @property string $account_type_string
* @property string $name * @property string $name
* @property string $virtual_balance * @property string $virtual_balance
* @property null|string $iban * @property null|string $iban
@@ -75,6 +76,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property Collection|Transaction[] $transactions * @property Collection|Transaction[] $transactions
* @property null|int $transactions_count * @property null|int $transactions_count
* @property User $user * @property User $user
* @property string $last_activity
* *
* @method static EloquentBuilder|Account accountTypeIn($types) * @method static EloquentBuilder|Account accountTypeIn($types)
* @method static EloquentBuilder|Account newModelQuery() * @method static EloquentBuilder|Account newModelQuery()

View File

@@ -42,6 +42,15 @@ class AccountEnrichment implements EnrichmentInterface
private Collection $collection; private Collection $collection;
private array $currencies; private array $currencies;
private AccountRepositoryInterface $repository;
private CurrencyRepositoryInterface $currencyRepository;
public function __construct()
{
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
}
#[\Override] #[\Override]
/** /**
* Do the actual enrichment. * Do the actual enrichment.
@@ -55,9 +64,9 @@ class AccountEnrichment implements EnrichmentInterface
// do everything here: // do everything here:
$this->getLastActivity(); $this->getLastActivity();
// $this->getMetaBalances();
$this->collectAccountTypes(); $this->collectAccountTypes();
$this->collectMetaData(); $this->collectMetaData();
// $this->getMetaBalances();
// $this->collection->transform(function (Account $account) { // $this->collection->transform(function (Account $account) {
// $account->user_array = ['id' => 1, 'bla bla' => 'bla']; // $account->user_array = ['id' => 1, 'bla bla' => 'bla'];
@@ -78,9 +87,7 @@ class AccountEnrichment implements EnrichmentInterface
*/ */
private function getLastActivity(): void private function getLastActivity(): void
{ {
/** @var AccountRepositoryInterface $accountRepository */ $lastActivity = $this->repository->getLastActivity($this->collection);
$accountRepository = app(AccountRepositoryInterface::class);
$lastActivity = $accountRepository->getLastActivity($this->collection);
foreach ($lastActivity as $row) { foreach ($lastActivity as $row) {
$this->collection->where('id', $row['account_id'])->first()->last_activity = Carbon::parse($row['date_max'], config('app.timezone')); $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 private function collectAccountTypes(): void
{ {
/** @var AccountRepositoryInterface $accountRepository */ $accountTypes = $this->repository->getAccountTypes($this->collection);
$accountRepository = app(AccountRepositoryInterface::class); $types = [];
$accountTypes = $accountRepository->getAccountTypes($this->collection);
$types = [];
/** @var AccountType $row */ /** @var AccountType $row */
foreach ($accountTypes as $row) { foreach ($accountTypes as $row) {
$types[$row->id] = $row->type; $types[$row->id] = $row->type;
} }
$this->collection->transform(function (Account $account) use ($types) { $this->collection->transform(function (Account $account) use ($types) {
$account->type = $types[$account->id]; $account->account_type_string = $types[$account->id];
return $account; return $account;
}); });
@@ -127,17 +132,11 @@ class AccountEnrichment implements EnrichmentInterface
private function collectMetaData(): void private function collectMetaData(): void
{ {
/** @var AccountRepositoryInterface $accountRepository */ $metaFields = $this->repository->getMetaValues($this->collection, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']);
$accountRepository = app(AccountRepositoryInterface::class); $currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
/** @var CurrencyRepositoryInterface $repository */ $currencies = [];
$repository = app(CurrencyRepositoryInterface::class); foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) {
$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) {
$id = $currency->id; $id = $currency->id;
$currencies[$id] = $currency; $currencies[$id] = $currency;
} }
@@ -148,6 +147,7 @@ class AccountEnrichment implements EnrichmentInterface
$account->{$entry->name} = $entry->data; $account->{$entry->name} = $entry->data;
if ('currency_id' === $entry->name) { if ('currency_id' === $entry->name) {
$id = (int) $entry->data; $id = (int) $entry->data;
$account->currency_name = $currencies[$id]?->name;
$account->currency_code = $currencies[$id]?->code; $account->currency_code = $currencies[$id]?->code;
$account->currency_symbol = $currencies[$id]?->symbol; $account->currency_symbol = $currencies[$id]?->symbol;
$account->currency_decimal_places = $currencies[$id]?->decimal_places; $account->currency_decimal_places = $currencies[$id]?->decimal_places;