Skip to content

Firefly III: a personal finances manager

Restore previous stuff

James Colec6c8f282e27d
diff --git a/app/JsonApi/V3/Accounts/AccountRepository.php b/app/JsonApi/V3/Accounts/AccountRepository.php
index 248dd6fbff..abe23dbb87 100644
--- a/app/JsonApi/V3/Accounts/AccountRepository.php
+++ b/app/JsonApi/V3/Accounts/AccountRepository.php
@@ -40,17 +40,6 @@ class AccountRepository extends AbstractRepository implements QueriesAll
*/
public function __construct() {}
- /**
- * Get the tags relationship
- *
- * @param Account $account
- * @return iterable
- */
- public function getBalances(Account $account): iterable
- {
- die('here we are');
- }
-
/**
* @inheritDoc
*/
diff --git a/app/JsonApi/V3/Accounts/AccountResource.php b/app/JsonApi/V3/Accounts/AccountResource.php
index 36e97d061d..5448e803de 100644
--- a/app/JsonApi/V3/Accounts/AccountResource.php
+++ b/app/JsonApi/V3/Accounts/AccountResource.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\JsonApi\V3\Accounts;
use Illuminate\Http\Request;
+use Illuminate\Support\Collection;
use LaravelJsonApi\Core\Resources\JsonApiResource;
class AccountResource extends JsonApiResource
@@ -54,11 +55,17 @@ class AccountResource extends JsonApiResource
'name' => $this->resource->name,
'iban' => '' === $this->resource->iban ? null : $this->resource->iban,
'active' => $this->resource->active,
-// 'virtual_balance' => $this->resource->virtual_balance,
'last_activity' => $this->resource->last_activity,
-// 'native_balance' => $this->resource->native_balance,
'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' => []
//
@@ -152,9 +159,8 @@ class AccountResource extends JsonApiResource
public function relationships($request): iterable
{
return [
- $this->relation('user')->withData($this->resource->user_array),
- $this->relation('balances')->withData([]),
- //$this->relation('tags')->withData($this->resource->getTags()),
+ $this->relation('user')->withData($this->resource->user),
+ $this->relation('balances')->withData($this->resource->balances),
];
}
}
diff --git a/app/JsonApi/V3/Accounts/Capabilities/AccountQuery.php b/app/JsonApi/V3/Accounts/Capabilities/AccountQuery.php
index 4ebf4f247d..ea46fce8f1 100644
--- a/app/JsonApi/V3/Accounts/Capabilities/AccountQuery.php
+++ b/app/JsonApi/V3/Accounts/Capabilities/AccountQuery.php
@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\JsonApi\V3\Accounts\Capabilities;
-use FireflyIII\Models\Account;
use FireflyIII\Support\JsonApi\Concerns\UsergroupAware;
use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
use FireflyIII\Support\JsonApi\ExpandsQuery;
@@ -48,6 +47,7 @@ class AccountQuery extends QueryAll implements HasPagination
*/
#[\Override] public function get(): iterable
{
+
$filters = $this->queryParameters->filter();
$sort = $this->queryParameters->sortFields();
$pagination = $this->filtersPagination($this->queryParameters->page());
diff --git a/app/JsonApi/V3/Server.php b/app/JsonApi/V3/Server.php
index 78a5beec6a..b94c51d0cd 100644
--- a/app/JsonApi/V3/Server.php
+++ b/app/JsonApi/V3/Server.php
@@ -3,6 +3,7 @@
namespace FireflyIII\JsonApi\V3;
use FireflyIII\JsonApi\V3\Accounts\AccountSchema;
+use FireflyIII\JsonApi\V3\Balances\BalanceSchema;
use FireflyIII\JsonApi\V3\Users\UserSchema;
use LaravelJsonApi\Core\Server\Server as BaseServer;
@@ -36,6 +37,7 @@ class Server extends BaseServer
return [
AccountSchema::class,
UserSchema::class,
+ BalanceSchema::class,
];
}
diff --git a/app/JsonApi/V3/Users/UserSchema.php b/app/JsonApi/V3/Users/UserSchema.php
index 00dc8e0363..ccc307b9d4 100644
--- a/app/JsonApi/V3/Users/UserSchema.php
+++ b/app/JsonApi/V3/Users/UserSchema.php
@@ -7,6 +7,7 @@ use LaravelJsonApi\Eloquent\Contracts\Paginator;
use LaravelJsonApi\Eloquent\Fields\DateTime;
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Fields\Relations\HasMany;
+use LaravelJsonApi\Eloquent\Fields\Str;
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
use LaravelJsonApi\Eloquent\Schema;
@@ -31,7 +32,8 @@ class UserSchema extends Schema
return [
ID::make(),
DateTime::make('created_at')->sortable()->readOnly(),
- DateTime::make('created_at')->sortable()->readOnly(),
+ DateTime::make('updated_at')->sortable()->readOnly(),
+ Str::make('email'),
HasMany::make('accounts'),
];
}
diff --git a/app/Policies/AccountPolicy.php b/app/Policies/AccountPolicy.php
index c1ed85f04d..020a285470 100644
--- a/app/Policies/AccountPolicy.php
+++ b/app/Policies/AccountPolicy.php
@@ -51,4 +51,19 @@ class AccountPolicy
{
return auth()->check();
}
+
+ /**
+ * Everybody can do this, but selection should limit to user.
+ *
+ * @return true
+ */
+ public function viewUser(User $user, Account $account): bool
+ {
+ return $this->view($user, $account);
+ }
+
+ public function viewBalances(User $user, Account $account): bool
+ {
+ return $this->view($user, $account);
+ }
}
diff --git a/app/Policies/BalancePolicy.php b/app/Policies/BalancePolicy.php
new file mode 100644
index 0000000000..3571a0e870
--- /dev/null
+++ b/app/Policies/BalancePolicy.php
@@ -0,0 +1,53 @@
+<?php
+/*
+ * BalancePolicy.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\Policies;
+
+use FireflyIII\Models\Account;
+use FireflyIII\User;
+
+class BalancePolicy
+{
+ /**
+ * TODO needs better authentication.
+ *
+ * @param User $user
+ * @param Account $account
+ *
+ * @return bool
+ */
+ public function view(User $user, Account $account): bool
+ {
+ return auth()->check() && $user->id === $account->user_id;
+ }
+
+ /**
+ * Everybody can do this, but selection should limit to user.
+ *
+ * @return true
+ */
+ public function viewAny(): bool
+ {
+ return auth()->check();
+ }
+}
diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
index f827d9f528..cfc4fd0b97 100644
--- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
@@ -51,6 +51,11 @@ class AccountEnrichment implements EnrichmentInterface
$this->collection->transform(function (Account $account) {
$account->user_array = ['id' => 1,'bla bla' => 'bla'];
+ $account->balances = collect([
+ ['balance_id' => 1,'balance' => 5],
+ ['balance_id' => 2,'balance' => 5],
+ ['balance_id' => 3,'balance' => 5],
+ ]);
return $account;
});
diff --git a/routes/api.php b/routes/api.php
index 151f63eb4c..1cdb0c9f96 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -23,14 +23,18 @@
declare(strict_types=1);
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
-use LaravelJsonApi\Laravel\Routing\ResourceRegistrar;
use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;
+use LaravelJsonApi\Laravel\Routing\Relationships;
+use LaravelJsonApi\Laravel\Routing\ResourceRegistrar;
JsonApiRoute::server('v3')
->prefix('v3')
->resources(function (ResourceRegistrar $server) {
- $server->resource('accounts', JsonApiController::class);
+ $server->resource('accounts', JsonApiController::class)->readOnly()->relationships(function (Relationships $relations) {
+ $relations->hasOne('user')->readOnly();
+ $relations->hasMany('balances')->readOnly();
+ });
$server->resource('users', JsonApiController::class);
});