Skip to content

Firefly III: a personal finances manager

Returns accounts consistently.

James Colee06736c2546d
diff --git a/app/Api/V2/Controllers/JsonApi/AccountController.php b/app/Api/V2/Controllers/JsonApi/AccountController.php
index 612149e996..f376e4da09 100644
--- a/app/Api/V2/Controllers/JsonApi/AccountController.php
+++ b/app/Api/V2/Controllers/JsonApi/AccountController.php
@@ -26,7 +26,10 @@ namespace FireflyIII\Api\V2\Controllers\JsonApi;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\JsonApi\V2\Accounts\AccountCollectionQuery;
use FireflyIII\JsonApi\V2\Accounts\AccountSchema;
-use FireflyIII\JsonApi\V2\Accounts\Capabilities\AccountQuery;
+use FireflyIII\JsonApi\V2\Accounts\AccountSingleQuery;
+use FireflyIII\Models\Account;
+use Illuminate\Contracts\Support\Responsable;
+use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Core\Responses\DataResponse;
use LaravelJsonApi\Laravel\Http\Controllers\Actions;
@@ -45,7 +48,7 @@ class AccountController extends Controller
use Actions\DetachRelationship;
// use Actions\FetchMany;
- use Actions\FetchOne;
+ //use Actions\FetchOne;
use Actions\FetchRelated;
use Actions\FetchRelationship;
use Actions\Store;
@@ -56,9 +59,9 @@ class AccountController extends Controller
* Fetch zero to many JSON API resources.
*
* @param AccountSchema $schema
- * @param AccountQuery $request
+ * @param AccountCollectionQuery $request
*
- * @return \Illuminate\Contracts\Support\Responsable|\Illuminate\Http\Response
+ * @return Responsable|Response
*/
public function index(AccountSchema $schema, AccountCollectionQuery $request)
{
@@ -74,6 +77,28 @@ class AccountController extends Controller
return new DataResponse($models);
}
+ /**
+ * Fetch zero to one JSON API resource by id.
+ *
+ * @param AccountSchema $schema
+ * @param AccountSingleQuery $request
+ * @param Account $account
+ *
+ * @return Responsable|Response
+ */
+ public function show(AccountSchema $schema, AccountSingleQuery $request, Account $account)
+ {
+ $model = $schema
+ ->repository()
+ ->queryOne($account)
+ ->withRequest($request)
+ ->first();
+
+ // do something custom...
+
+ return new DataResponse($model);
+ }
+
// public function readAccountBalances(AnonymousQuery $query, AccountBalanceSchema $schema, Account $account): Responsable
// {
diff --git a/app/JsonApi/V2/Accounts/AccountRepository.php b/app/JsonApi/V2/Accounts/AccountRepository.php
index e6b9216db8..67d96dd426 100644
--- a/app/JsonApi/V2/Accounts/AccountRepository.php
+++ b/app/JsonApi/V2/Accounts/AccountRepository.php
@@ -25,9 +25,12 @@ namespace FireflyIII\JsonApi\V2\Accounts;
use FireflyIII\Models\Account;
use FireflyIII\Support\JsonApi\Concerns\UsergroupAware;
+use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
+use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Contracts\Store\QueriesAll;
use LaravelJsonApi\NonEloquent\AbstractRepository;
use LaravelJsonApi\NonEloquent\Capabilities\CrudRelations;
+use LaravelJsonApi\NonEloquent\Concerns\HasCrudCapability;
use LaravelJsonApi\NonEloquent\Concerns\HasRelationsCapability;
/**
@@ -44,18 +47,36 @@ class AccountRepository extends AbstractRepository implements QueriesAll
{
use UsergroupAware;
use HasRelationsCapability;
+ use HasCrudCapability;
/**
* SiteRepository constructor.
*/
public function __construct() {}
+ public function exists(string $resourceId): bool
+ {
+ Log::debug(__METHOD__);
+ return null !== Account::find((int) $resourceId);
+ }
+
public function find(string $resourceId): ?object
{
- return Account::find((int) $resourceId);
+ Log::debug(__METHOD__);
+// throw new \RuntimeException('trace me');
+ $account = Account::find((int) $resourceId);
+ if(null === $account) {
+ return null;
+ }
+ // enrich the collected data
+ $enrichment = new AccountEnrichment();
+ return $enrichment->enrichSingle($account);
}
+
+
public function queryAll(): Capabilities\AccountQuery
{
+ Log::debug(__METHOD__);
return Capabilities\AccountQuery::make()
->withUserGroup($this->userGroup)
->withServer($this->server)
@@ -63,6 +84,14 @@ class AccountRepository extends AbstractRepository implements QueriesAll
;
}
+ /**
+ * @inheritDoc
+ */
+ protected function crud(): Capabilities\CrudAccount
+ {
+ return Capabilities\CrudAccount::make();
+ }
+
/**
* @inheritDoc
*/
diff --git a/app/JsonApi/V2/Accounts/AccountResource.php b/app/JsonApi/V2/Accounts/AccountResource.php
index 398f1c81b0..99e9344052 100644
--- a/app/JsonApi/V2/Accounts/AccountResource.php
+++ b/app/JsonApi/V2/Accounts/AccountResource.php
@@ -19,6 +19,7 @@ class AccountResource extends JsonApiResource
*/
public function id(): string
{
+ Log::debug(__METHOD__);
return (string) $this->resource->id;
}
@@ -34,13 +35,13 @@ class AccountResource extends JsonApiResource
{
Log::debug(__METHOD__);
return [
- '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,
+ '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
@@ -51,10 +52,10 @@ class AccountResource extends JsonApiResource
'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,
+ '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/AccountSchema.php b/app/JsonApi/V2/Accounts/AccountSchema.php
index d5eae3b4b8..e879ffa680 100644
--- a/app/JsonApi/V2/Accounts/AccountSchema.php
+++ b/app/JsonApi/V2/Accounts/AccountSchema.php
@@ -9,7 +9,7 @@ use LaravelJsonApi\Core\Schema\Schema;
use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
use LaravelJsonApi\NonEloquent\Fields\Attribute;
use LaravelJsonApi\NonEloquent\Fields\ID;
-use LaravelJsonApi\NonEloquent\Fields\Relation;
+use LaravelJsonApi\NonEloquent\Filters\Filter;
class AccountSchema extends Schema
@@ -34,10 +34,34 @@ class AccountSchema extends Schema
Log::debug(__METHOD__);;
return [
ID::make(),
+ Attribute::make('created_at'),
+ Attribute::make('updated_at'),
+
+ // basic info and meta data
Attribute::make('name'),
Attribute::make('active'),
Attribute::make('order'),
+ Attribute::make('type'),
+ Attribute::make('account_role'),
+ Attribute::make('account_number'),
+
+ // currency
+ Attribute::make('currency_id'),
+ Attribute::make('currency_name'),
+ Attribute::make('currency_code'),
+ Attribute::make('currency_symbol'),
+ Attribute::make('currency_decimal_places'),
+
+ // liability things
+ Attribute::make('liability_direction'),
+ Attribute::make('interest'),
+ Attribute::make('interest_period'),
+ Attribute::make('current_debt'),
+
+ // dynamic data
Attribute::make('last_activity'),
+
+
HasOne::make('user')->readOnly(),
];
}
@@ -51,16 +75,13 @@ class AccountSchema extends Schema
{
Log::debug(__METHOD__);;
return [
- // Filter::make('id'),
+ Filter::make('id'),
];
}
public function repository(): AccountRepository
{
- Log::debug(__METHOD__);;
- // to access the repository, you need to have the necessary rights.
-
-
+ Log::debug(__METHOD__);
$this->setUserGroup($this->server->getUsergroup());
return AccountRepository::make()
->withServer($this->server)
diff --git a/app/JsonApi/V2/Accounts/AccountQuery.php b/app/JsonApi/V2/Accounts/AccountSingleQuery.php
similarity index 95%
rename from app/JsonApi/V2/Accounts/AccountQuery.php
rename to app/JsonApi/V2/Accounts/AccountSingleQuery.php
index 050baa3fc2..3279e3681d 100644
--- a/app/JsonApi/V2/Accounts/AccountQuery.php
+++ b/app/JsonApi/V2/Accounts/AccountSingleQuery.php
@@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Laravel\Http\Requests\ResourceQuery;
use LaravelJsonApi\Validation\Rule as JsonApiRule;
-class AccountQuery extends ResourceQuery
+class AccountSingleQuery extends ResourceQuery
{
/**
diff --git a/app/JsonApi/V2/Accounts/Capabilities/CrudAccount.php b/app/JsonApi/V2/Accounts/Capabilities/CrudAccount.php
new file mode 100644
index 0000000000..1230536d98
--- /dev/null
+++ b/app/JsonApi/V2/Accounts/Capabilities/CrudAccount.php
@@ -0,0 +1,46 @@
+<?php
+/*
+ * CrudAccount.php
+ * Copyright (c) 2024 [email protected].
+ *
+ * This file is part of Firefly III (https://github.com/firefly-iii).
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see https://www.gnu.org/licenses/.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\JsonApi\V2\Accounts\Capabilities;
+
+use FireflyIII\Models\Account;
+use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
+use LaravelJsonApi\NonEloquent\Capabilities\CrudResource;
+
+class CrudAccount extends CrudResource
+{
+ /**
+ * Read the supplied site.
+ *
+ * @param Account $account
+ *
+ * @return Account|null
+ */
+ public function read(Account $account): ?Account
+ {
+ // enrich the collected data
+ $enrichment = new AccountEnrichment();
+ return $enrichment->enrichSingle($account);
+ }
+
+}
diff --git a/app/JsonApi/V2/Server.php b/app/JsonApi/V2/Server.php
index e59c629ac2..248f71a53f 100644
--- a/app/JsonApi/V2/Server.php
+++ b/app/JsonApi/V2/Server.php
@@ -8,6 +8,7 @@ use FireflyIII\JsonApi\V2\Accounts\AccountSchema;
use FireflyIII\JsonApi\V2\Users\UserSchema;
use FireflyIII\Support\JsonApi\Concerns\UsergroupAware;
use FireflyIII\Support\JsonApi\Concerns\UserGroupDetectable;
+use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Core\Server\Server as BaseServer;
/**
@@ -30,6 +31,7 @@ class Server extends BaseServer
*/
public function serving(): void
{
+ Log::debug(__METHOD__);
// at this point the user may not actually have access to this user group.
$res = $this->detectUserGroup();
$this->setUserGroup($res);
@@ -40,6 +42,7 @@ class Server extends BaseServer
*/
protected function allSchemas(): array
{
+ Log::debug(__METHOD__);
return [
AccountSchema::class,
UserSchema::class,
diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php
index 4447ed75f0..9e8bf2132b 100644
--- a/app/Models/Transaction.php
+++ b/app/Models/Transaction.php
@@ -97,7 +97,7 @@ class Transaction extends Model
use HasFactory;
use ReturnsIntegerIdTrait;
use SoftDeletes;
- //use Cachable;
+ use Cachable;
protected $casts
= [
diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
index 05daf2676d..a51e5dcab9 100644
--- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
@@ -29,6 +29,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
+use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -157,4 +158,12 @@ class AccountEnrichment implements EnrichmentInterface
return $account;
});
}
+
+ #[\Override] public function enrichSingle(Model $model): Model
+ {
+ Log::debug(__METHOD__);
+ $collection = new Collection([$model]);
+ $collection = $this->enrich($collection);
+ return $collection->first();
+ }
}
diff --git a/app/Support/JsonApi/Enrichments/EnrichmentInterface.php b/app/Support/JsonApi/Enrichments/EnrichmentInterface.php
index 827a62b453..ced98325bc 100644
--- a/app/Support/JsonApi/Enrichments/EnrichmentInterface.php
+++ b/app/Support/JsonApi/Enrichments/EnrichmentInterface.php
@@ -23,9 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Support\JsonApi\Enrichments;
+use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
interface EnrichmentInterface
{
public function enrich(Collection $collection): Collection;
+
+ public function enrichSingle(Model $model): Model;
}