2024-03-04 20:41:34 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* IndexController.php
|
|
|
|
* Copyright (c) 2024 james@firefly-iii.org.
|
|
|
|
*
|
|
|
|
* 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\Controllers\Model\Account;
|
|
|
|
|
|
|
|
use FireflyIII\Api\V2\Controllers\Controller;
|
2024-03-06 19:54:09 +01:00
|
|
|
use FireflyIII\Api\V2\Request\Model\Account\IndexRequest;
|
2024-04-07 06:06:40 +02:00
|
|
|
use FireflyIII\Enums\UserRoleEnum;
|
2024-03-04 20:41:34 +01:00
|
|
|
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Transformers\V2\AccountTransformer;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
|
|
|
|
class IndexController extends Controller
|
|
|
|
{
|
2024-04-07 06:51:34 +02:00
|
|
|
public const string RESOURCE_KEY = 'accounts';
|
2024-03-04 20:41:34 +01:00
|
|
|
|
|
|
|
private AccountRepositoryInterface $repository;
|
2024-04-07 06:06:40 +02:00
|
|
|
protected array $acceptedRoles = [UserRoleEnum::READ_ONLY, UserRoleEnum::MANAGE_TRANSACTIONS];
|
2024-03-04 20:41:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
$this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
// new way of user group validation
|
2024-04-07 06:51:34 +02:00
|
|
|
$userGroup = $this->validateUserGroup($request);
|
2024-04-07 06:06:40 +02:00
|
|
|
$this->repository->setUserGroup($userGroup);
|
2024-03-04 20:41:34 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-06 19:54:09 +01:00
|
|
|
/**
|
2024-04-20 16:18:41 +02:00
|
|
|
* TODO see autocomplete/account controller for list.
|
2024-03-06 19:54:09 +01:00
|
|
|
*/
|
|
|
|
public function index(IndexRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
$this->repository->resetAccountOrder();
|
2024-03-09 19:31:27 +01:00
|
|
|
$types = $request->getAccountTypes();
|
|
|
|
$instructions = $request->getSortInstructions('accounts');
|
|
|
|
$accounts = $this->repository->getAccountsByType($types, $instructions);
|
|
|
|
$pageSize = $this->parameters->get('limit');
|
|
|
|
$count = $accounts->count();
|
|
|
|
$accounts = $accounts->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
|
|
|
$paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page'));
|
|
|
|
$transformer = new AccountTransformer();
|
|
|
|
|
|
|
|
$this->parameters->set('sort', $instructions);
|
2024-03-06 19:54:09 +01:00
|
|
|
$transformer->setParameters($this->parameters); // give params to transformer
|
|
|
|
|
|
|
|
return response()
|
|
|
|
->json($this->jsonApiList('accounts', $paginator, $transformer))
|
2024-04-21 17:23:16 +02:00
|
|
|
->header('Content-Type', self::CONTENT_TYPE)
|
|
|
|
;
|
2024-03-04 20:41:34 +01:00
|
|
|
}
|
|
|
|
}
|