Skip to content

Firefly III: a personal finances manager

Fix #8802

James Cole01a897b5e17e
diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php
index 6f99f697a2..831f9242f9 100644
--- a/app/Api/V2/Controllers/Chart/AccountController.php
+++ b/app/Api/V2/Controllers/Chart/AccountController.php
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V2\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Api\V2\Controllers\Controller;
+use FireflyIII\Api\V2\Request\Chart\DashboardChartRequest;
use FireflyIII\Api\V2\Request\Generic\DateRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
@@ -35,6 +36,7 @@ use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Support\Http\Api\CleansChartData;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Collection;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -78,7 +80,7 @@ class AccountController extends Controller
*
* TODO validate and set user_group_id from request
*
- * @param DateRequest $request
+ * @param DashboardChartRequest $request
*
* @return JsonResponse
* @throws ContainerExceptionInterface
@@ -86,7 +88,7 @@ class AccountController extends Controller
* @throws FireflyException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
- public function dashboard(DateRequest $request): JsonResponse
+ public function dashboard(DashboardChartRequest $request): JsonResponse
{
/** @var Carbon $start */
$start = $this->parameters->get('start');
@@ -94,19 +96,25 @@ class AccountController extends Controller
$end = $this->parameters->get('end');
$end->endOfDay();
+ /** @var TransactionCurrency $default */
+ $default = app('amount')->getDefaultCurrency();
+ $params = $request->getAll();
+ /** @var Collection $preSet */
+ $accounts = $params['accounts'];
+ $chartData = [];
+
// user's preferences
- $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
- $frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
+ if (0 === $accounts->count()) {
+ $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
+ $frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
- if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) {
- $frontPage->data = $defaultSet;
- $frontPage->save();
- }
+ if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) {
+ $frontPage->data = $defaultSet;
+ $frontPage->save();
+ }
- /** @var TransactionCurrency $default */
- $default = app('amount')->getDefaultCurrency();
- $accounts = $this->repository->getAccountsById($frontPage->data);
- $chartData = [];
+ $accounts = $this->repository->getAccountsById($frontPage->data);
+ }
/** @var Account $account */
foreach ($accounts as $account) {
diff --git a/app/Api/V2/Request/Chart/DashboardChartRequest.php b/app/Api/V2/Request/Chart/DashboardChartRequest.php
new file mode 100644
index 0000000000..60c0812b3c
--- /dev/null
+++ b/app/Api/V2/Request/Chart/DashboardChartRequest.php
@@ -0,0 +1,88 @@
+<?php
+/*
+ * DashboardChartRequest.php
+ * Copyright (c) 2023 [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\Api\V2\Request\Chart;
+
+use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
+use FireflyIII\Support\Request\ChecksLogin;
+use FireflyIII\Support\Request\ConvertsDataTypes;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Validation\Validator;
+
+/**
+* Class DashboardChartRequest
+ */
+class DashboardChartRequest extends FormRequest
+{
+ use ChecksLogin;
+ use ConvertsDataTypes;
+ use ValidatesUserGroupTrait;
+
+ /**
+ * Get all data from the request.
+ *
+ * @return array
+ */
+ public function getAll(): array
+ {
+ return [
+ 'accounts' => $this->getAccountList(),
+ ];
+ }
+
+ /**
+ * The rules that the incoming request must be matched against.
+ *
+ * @return array
+ */
+ public function rules(): array
+ {
+ return [
+ 'start' => 'required|date|after:1900-01-01|before:2099-12-31',
+ 'end' => 'required|date|after_or_equal:start|before:2099-12-31|after:1900-01-01',
+ 'accounts.*' => 'exists:accounts,id',
+ ];
+ }
+
+ /**
+ * @param Validator $validator
+ *
+ * @return void
+ */
+ public function withValidator(Validator $validator): void
+ {
+ $validator->after(
+ static function (Validator $validator) {
+ // validate transaction query data.
+ $data = $validator->getData();
+ if (!array_key_exists('accounts', $data)) {
+ //$validator->errors()->add('accounts', trans('validation.filled', ['attribute' => 'accounts']));
+ return;
+ }
+ if (!is_array($data['accounts'])) {
+ $validator->errors()->add('accounts', trans('validation.filled', ['attribute' => 'accounts']));
+ }
+ }
+ );
+ }
+}