Expand v2 api

This commit is contained in:
James Cole
2024-07-31 20:19:17 +02:00
parent 3560f0388c
commit dafd99f155
13 changed files with 407 additions and 35 deletions

View File

@@ -6,6 +6,8 @@ namespace FireflyIII\JsonApi\V2\Accounts;
use FireflyIII\Models\Account;
use FireflyIII\Rules\IsAllowedGroupAction;
use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Rules\IsValidDateRange;
use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Laravel\Http\Requests\ResourceQuery;
use LaravelJsonApi\Validation\Rule as JsonApiRule;
@@ -25,11 +27,23 @@ class AccountCollectionQuery extends ResourceQuery
'array',
JsonApiRule::fieldSets(),
],
'user_group_id' => [
'userGroupId' => [
'nullable',
'integer',
new IsAllowedGroupAction(Account::class, request()->method()),
],
'startPeriod' => [
'nullable',
'date',
new IsDateOrTime(),
new isValidDateRange(),
],
'endPeriod' => [
'nullable',
'date',
new IsDateOrTime(),
new isValidDateRange(),
],
'filter' => [
'nullable',
'array',
@@ -45,6 +59,15 @@ class AccountCollectionQuery extends ResourceQuery
'array',
JsonApiRule::page(),
],
'page.number' => [
'integer',
'min:1',
],
'page.size' => [
'integer',
'min:1',
],
'sort' => [
'nullable',
'string',

View File

@@ -39,24 +39,46 @@ class AccountResource extends JsonApiResource
'name' => $this->resource->name,
'active' => $this->resource->active,
'order' => $this->resource->order,
'iban' => $this->resource->iban,
'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 (if the account has a currency setting, otherwise NULL).
'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,
'is_multi_currency' => '1' === $this->resource->is_multi_currency,
// balances
'balance' => $this->resource->balance,
'native_balance' => $this->resource->native_balance,
// liability things
'liability_direction' => $this->resource->liability_direction,
'interest' => $this->resource->interest,
'interest_period' => $this->resource->interest_period,
'current_debt' => $this->resource->current_debt,
'current_debt' => $this->resource->current_debt, // TODO may be removed in the future.
'last_activity' => $this->resource->last_activity,
// other things
'last_activity' => $this->resource->last_activity,
// still to do
// balance difference
// 'balance_difference' => $balanceDiff,
// 'native_balance_difference' => $nativeBalanceDiff,
// 'balance_difference_start' => $diffStart,
// 'balance_difference_end' => $diffEnd,
// object group
// 'object_group_id' => null !== $objectGroupId ? (string) $objectGroupId : null,
// 'object_group_order' => $objectGroupOrder,
// 'object_group_title' => $objectGroupTitle,
];
}

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\JsonApi\V2\Accounts\Capabilities;
use FireflyIII\Support\JsonApi\CollectsCustomParameters;
use FireflyIII\Support\JsonApi\Concerns\UsergroupAware;
use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
use FireflyIII\Support\JsonApi\ExpandsQuery;
@@ -42,6 +43,7 @@ class AccountQuery extends QueryAll implements HasPagination
use SortsCollection;
use UsergroupAware;
use ValidateSortParameters;
use CollectsCustomParameters;
#[\Override]
/**
@@ -51,17 +53,20 @@ class AccountQuery extends QueryAll implements HasPagination
{
Log::debug(__METHOD__);
// collect filters
$filters = $this->queryParameters->filter();
$filters = $this->queryParameters->filter();
// collect sort options
$sort = $this->queryParameters->sortFields();
$sort = $this->queryParameters->sortFields();
// collect pagination based on the page
$pagination = $this->filtersPagination($this->queryParameters->page());
// check if we need all accounts, regardless of pagination
// This is necessary when the user wants to sort on specific params.
$needsAll = $this->needsFullDataset('account', $sort);
$needsAll = $this->needsFullDataset('account', $sort);
// params that were not recognised, may be my own custom stuff.
$otherParams = $this->getOtherParams($this->queryParameters->unrecognisedParameters());
// start the query
$query = $this->userGroup->accounts();
$query = $this->userGroup->accounts();
// add pagination to the query, limiting the results.
if (!$needsAll) {
@@ -69,14 +74,16 @@ class AccountQuery extends QueryAll implements HasPagination
}
// add sort and filter parameters to the query.
$query = $this->addSortParams($query, $sort);
$query = $this->addFilterParams('account', $query, $filters);
$query = $this->addSortParams($query, $sort);
$query = $this->addFilterParams('account', $query, $filters);
// collect the result.
$collection = $query->get(['accounts.*']);
// enrich the collected data
$enrichment = new AccountEnrichment();
$enrichment->setStart($otherParams['start'] ?? null);
$enrichment->setEnd($otherParams['end'] ?? null);
$collection = $enrichment->enrich($collection);
// TODO add filters after the query, if there are filters that cannot be applied to the database but only