Fix methods and clean up code.

This commit is contained in:
James Cole
2024-03-09 19:46:16 +01:00
parent 487b65b669
commit 66b322e844
19 changed files with 153 additions and 139 deletions

View File

@@ -8,16 +8,16 @@
"packages": [ "packages": [
{ {
"name": "composer/pcre", "name": "composer/pcre",
"version": "3.1.1", "version": "3.1.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/composer/pcre.git", "url": "https://github.com/composer/pcre.git",
"reference": "00104306927c7a0919b4ced2aaa6782c1e61a3c9" "reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/composer/pcre/zipball/00104306927c7a0919b4ced2aaa6782c1e61a3c9", "url": "https://api.github.com/repos/composer/pcre/zipball/4775f35b2d70865807c89d32c8e7385b86eb0ace",
"reference": "00104306927c7a0919b4ced2aaa6782c1e61a3c9", "reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -59,7 +59,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/composer/pcre/issues", "issues": "https://github.com/composer/pcre/issues",
"source": "https://github.com/composer/pcre/tree/3.1.1" "source": "https://github.com/composer/pcre/tree/3.1.2"
}, },
"funding": [ "funding": [
{ {
@@ -75,7 +75,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2023-10-11T07:11:09+00:00" "time": "2024-03-07T15:38:35+00:00"
}, },
{ {
"name": "composer/semver", "name": "composer/semver",

View File

@@ -62,7 +62,6 @@ class IndexController extends Controller
*/ */
public function index(IndexRequest $request): JsonResponse public function index(IndexRequest $request): JsonResponse
{ {
$this->repository->resetAccountOrder(); $this->repository->resetAccountOrder();
$types = $request->getAccountTypes(); $types = $request->getAccountTypes();
$instructions = $request->getSortInstructions('accounts'); $instructions = $request->getSortInstructions('accounts');
@@ -78,7 +77,8 @@ class IndexController extends Controller
return response() return response()
->json($this->jsonApiList('accounts', $paginator, $transformer)) ->json($this->jsonApiList('accounts', $paginator, $transformer))
->header('Content-Type', self::CONTENT_TYPE); ->header('Content-Type', self::CONTENT_TYPE)
;
} }
public function infiniteList(InfiniteListRequest $request): JsonResponse public function infiniteList(InfiniteListRequest $request): JsonResponse
@@ -98,6 +98,7 @@ class IndexController extends Controller
return response() return response()
->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer)) ->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer))
->header('Content-Type', self::CONTENT_TYPE); ->header('Content-Type', self::CONTENT_TYPE)
;
} }
} }

View File

@@ -41,8 +41,8 @@ class InfiniteListRequest extends FormRequest
use AccountFilter; use AccountFilter;
use ChecksLogin; use ChecksLogin;
use ConvertsDataTypes; use ConvertsDataTypes;
use TransactionFilter;
use GetSortInstructions; use GetSortInstructions;
use TransactionFilter;
public function buildParams(): string public function buildParams(): string
{ {
@@ -99,7 +99,6 @@ class InfiniteListRequest extends FormRequest
return 0 === $page || $page > 65536 ? 1 : $page; return 0 === $page || $page > 65536 ? 1 : $page;
} }
public function getTransactionTypes(): array public function getTransactionTypes(): array
{ {
$type = (string)$this->get('type', 'default'); $type = (string)$this->get('type', 'default');

View File

@@ -74,20 +74,22 @@ class LoginController extends Controller
* *
* @throws ValidationException * @throws ValidationException
*/ */
public function login(Request $request): JsonResponse | RedirectResponse public function login(Request $request): JsonResponse|RedirectResponse
{ {
Log::channel('audit')->info(sprintf('User is trying to login using "%s"', $request->get($this->username()))); Log::channel('audit')->info(sprintf('User is trying to login using "%s"', $request->get($this->username())));
app('log')->debug('User is trying to login.'); app('log')->debug('User is trying to login.');
try { try {
$this->validateLogin($request); $this->validateLogin($request);
} catch (ValidationException $e) { } catch (ValidationException $e) {
return redirect(route('login')) return redirect(route('login'))
->withErrors( ->withErrors(
[ [
$this->username => trans('auth.failed') $this->username => trans('auth.failed'),
] ]
) )
->onlyInput($this->username); ->onlyInput($this->username)
;
} }
app('log')->debug('Login data is present.'); app('log')->debug('Login data is present.');
@@ -125,7 +127,6 @@ class LoginController extends Controller
$this->sendFailedLoginResponse($request); $this->sendFailedLoginResponse($request);
// @noinspection PhpUnreachableStatementInspection // @noinspection PhpUnreachableStatementInspection
return response()->json([]); return response()->json([]);
} }
@@ -235,7 +236,6 @@ class LoginController extends Controller
} }
$usernameField = $this->username(); $usernameField = $this->username();
return view('auth.login', compact('allowRegistration', 'email', 'remember', 'allowReset', 'title', 'usernameField')); return view('auth.login', compact('allowRegistration', 'email', 'remember', 'allowReset', 'title', 'usernameField'));
} }
} }

View File

@@ -181,7 +181,8 @@ class Account extends Model
/** @var null|AccountMeta $metaValue */ /** @var null|AccountMeta $metaValue */
$metaValue = $this->accountMeta() $metaValue = $this->accountMeta()
->where('name', 'account_number') ->where('name', 'account_number')
->first(); ->first()
;
return null !== $metaValue ? $metaValue->data : ''; return null !== $metaValue ? $metaValue->data : '';
} }
@@ -259,7 +260,7 @@ class Account extends Model
protected function accountId(): Attribute protected function accountId(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn($value) => (int)$value, get: static fn ($value) => (int)$value,
); );
} }
@@ -269,22 +270,21 @@ class Account extends Model
protected function accountTypeId(): Attribute protected function accountTypeId(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn($value) => (int)$value, get: static fn ($value) => (int)$value,
); );
} }
//
protected function iban(): Attribute protected function iban(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn($value) => null === $value ? null : trim(str_replace(' ', '', (string)$value)), get: static fn ($value) => null === $value ? null : trim(str_replace(' ', '', (string)$value)),
); );
} }
protected function order(): Attribute protected function order(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn($value) => (int)$value, get: static fn ($value) => (int)$value,
); );
} }
@@ -294,7 +294,7 @@ class Account extends Model
protected function virtualBalance(): Attribute protected function virtualBalance(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn($value) => (string)$value, get: static fn ($value) => (string)$value,
); );
} }
} }

View File

@@ -62,7 +62,8 @@ class AccountRepository implements AccountRepositoryInterface
$q1->where('account_meta.name', '=', 'account_number'); $q1->where('account_meta.name', '=', 'account_number');
$q1->where('account_meta.data', '=', $json); $q1->where('account_meta.data', '=', $json);
} }
); )
;
if (0 !== count($types)) { if (0 !== count($types)) {
$dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); $dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
@@ -273,7 +274,8 @@ class AccountRepository implements AccountRepositoryInterface
->orderBy('accounts.order', 'ASC') ->orderBy('accounts.order', 'ASC')
->orderBy('accounts.account_type_id', 'ASC') ->orderBy('accounts.account_type_id', 'ASC')
->orderBy('accounts.name', 'ASC') ->orderBy('accounts.name', 'ASC')
->with(['accountType']); ->with(['accountType'])
;
if ('' !== $query) { if ('' !== $query) {
// split query on spaces just in case: // split query on spaces just in case:
$parts = explode(' ', $query); $parts = explode(' ', $query);

View File

@@ -25,7 +25,6 @@ namespace FireflyIII\Support\Request;
trait GetSortInstructions trait GetSortInstructions
{ {
final public function getSortInstructions(string $key): array final public function getSortInstructions(string $key): array
{ {
$allowed = config(sprintf('firefly.sorting.allowed.%s', $key)); $allowed = config(sprintf('firefly.sorting.allowed.%s', $key));
@@ -50,5 +49,4 @@ trait GetSortInstructions
return $result; return $result;
} }
} }

View File

@@ -37,9 +37,6 @@ abstract class AbstractTransformer extends TransformerAbstract
/** /**
* This method is called exactly ONCE from FireflyIII\Api\V2\Controllers\Controller::jsonApiList * This method is called exactly ONCE from FireflyIII\Api\V2\Controllers\Controller::jsonApiList
*
* @param Collection $objects
* @return Collection
*/ */
abstract public function collectMetaData(Collection $objects): Collection; abstract public function collectMetaData(Collection $objects): Collection;

View File

@@ -32,7 +32,6 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/** /**
* Class AccountTransformer * Class AccountTransformer
@@ -64,9 +63,10 @@ class AccountTransformer extends AbstractTransformer
// get currencies: // get currencies:
$accountIds = $objects->pluck('id')->toArray(); $accountIds = $objects->pluck('id')->toArray();
$meta = AccountMeta::whereIn('account_id', $accountIds) $meta = AccountMeta::whereIn('account_id', $accountIds)
->whereIn('name', ['currency_id','account_role','account_number']) ->whereIn('name', ['currency_id', 'account_role', 'account_number'])
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']); ->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])
$currencyIds = $meta->where('name','currency_id')->pluck('data')->toArray(); ;
$currencyIds = $meta->where('name', 'currency_id')->pluck('data')->toArray();
$currencies = $repository->getByIds($currencyIds); $currencies = $repository->getByIds($currencyIds);
foreach ($currencies as $currency) { foreach ($currencies as $currency) {
@@ -81,7 +81,8 @@ class AccountTransformer extends AbstractTransformer
// select accounts.id, account_types.type from account_types left join accounts on accounts.account_type_id = account_types.id; // select accounts.id, account_types.type from account_types left join accounts on accounts.account_type_id = account_types.id;
$accountTypes = AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id') $accountTypes = AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
->whereIn('accounts.id', $accountIds) ->whereIn('accounts.id', $accountIds)
->get(['accounts.id', 'account_types.type']); ->get(['accounts.id', 'account_types.type'])
;
/** @var AccountType $row */ /** @var AccountType $row */
foreach ($accountTypes as $row) { foreach ($accountTypes as $row) {
@@ -96,15 +97,16 @@ class AccountTransformer extends AbstractTransformer
if ('iban' === $column) { if ('iban' === $column) {
$meta = $this->accountMeta; $meta = $this->accountMeta;
$objects = $objects->sort(function (Account $left, Account $right) use ($meta, $direction) { $objects = $objects->sort(function (Account $left, Account $right) use ($meta, $direction) {
$leftIban = trim(sprintf('%s%s', $left->iban, ($meta[$left->id]['account_number'] ?? ''))); $leftIban = trim(sprintf('%s%s', $left->iban, $meta[$left->id]['account_number'] ?? ''));
$rightIban = trim(sprintf('%s%s', $right->iban, ($meta[$right->id]['account_number'] ?? ''))); $rightIban = trim(sprintf('%s%s', $right->iban, $meta[$right->id]['account_number'] ?? ''));
if ('asc' === $direction) { if ('asc' === $direction) {
return strcasecmp($leftIban, $rightIban); return strcasecmp($leftIban, $rightIban);
} }
return strcasecmp($rightIban, $leftIban); return strcasecmp($rightIban, $leftIban);
}); });
} }
if('balance' === $column) { if ('balance' === $column) {
$balances = $this->convertedBalances; $balances = $this->convertedBalances;
$objects = $objects->sort(function (Account $left, Account $right) use ($balances, $direction) { $objects = $objects->sort(function (Account $left, Account $right) use ($balances, $direction) {
$leftBalance = (float)($balances[$left->id]['native_balance'] ?? 0); $leftBalance = (float)($balances[$left->id]['native_balance'] ?? 0);
@@ -112,13 +114,14 @@ class AccountTransformer extends AbstractTransformer
if ('asc' === $direction) { if ('asc' === $direction) {
return $leftBalance <=> $rightBalance; return $leftBalance <=> $rightBalance;
} }
return $rightBalance <=> $leftBalance; return $rightBalance <=> $leftBalance;
}); });
} }
} }
} }
//$objects = $objects->sortByDesc('name');
// $objects = $objects->sortByDesc('name');
return $objects; return $objects;
} }
@@ -206,7 +209,7 @@ class AccountTransformer extends AbstractTransformer
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/accounts/' . $account->id, 'uri' => '/accounts/'.$account->id,
], ],
], ],
]; ];

View File

@@ -54,7 +54,7 @@ class BillTransformer extends AbstractTransformer
* *
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/ */
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
$currencies = []; $currencies = [];
$bills = []; $bills = [];
@@ -175,6 +175,8 @@ class BillTransformer extends AbstractTransformer
]; ];
} }
} }
return $objects;
} }
/** /**

View File

@@ -37,9 +37,10 @@ class BudgetLimitTransformer extends AbstractTransformer
'budget', 'budget',
]; ];
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
// TODO: Implement collectMetaData() method. // TODO: Implement collectMetaData() method.
return $objects;
} }
/** /**

View File

@@ -45,9 +45,10 @@ class BudgetTransformer extends AbstractTransformer
$this->parameters = new ParameterBag(); $this->parameters = new ParameterBag();
} }
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
// TODO: Implement collectMetaData() method. // TODO: Implement collectMetaData() method.
return $objects;
} }
/** /**

View File

@@ -31,7 +31,10 @@ use Illuminate\Support\Collection;
*/ */
class CurrencyTransformer extends AbstractTransformer class CurrencyTransformer extends AbstractTransformer
{ {
public function collectMetaData(Collection $objects): void {} public function collectMetaData(Collection $objects): Collection
{
return $objects;
}
/** /**
* Transform the currency. * Transform the currency.

View File

@@ -69,7 +69,7 @@ class PiggyBankTransformer extends AbstractTransformer
// $this->piggyRepos = app(PiggyBankRepositoryInterface::class); // $this->piggyRepos = app(PiggyBankRepositoryInterface::class);
} }
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
// TODO move to repository (does not exist yet) // TODO move to repository (does not exist yet)
$piggyBanks = $objects->pluck('id')->toArray(); $piggyBanks = $objects->pluck('id')->toArray();
@@ -135,6 +135,8 @@ class PiggyBankTransformer extends AbstractTransformer
Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__)); Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__));
$this->default = app('amount')->getDefaultCurrencyByUserGroup(auth()->user()->userGroup); $this->default = app('amount')->getDefaultCurrencyByUserGroup(auth()->user()->userGroup);
$this->converter = new ExchangeRateConverter(); $this->converter = new ExchangeRateConverter();
return $objects;
} }
/** /**

View File

@@ -31,9 +31,10 @@ use Illuminate\Support\Collection;
*/ */
class PreferenceTransformer extends AbstractTransformer class PreferenceTransformer extends AbstractTransformer
{ {
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
// TODO: Implement collectMetaData() method. // TODO: Implement collectMetaData() method.
return $objects;
} }
/** /**

View File

@@ -64,7 +64,7 @@ class TransactionGroupTransformer extends AbstractTransformer
// private array $journalCurrencies = []; // private array $journalCurrencies = [];
// private array $foreignCurrencies = []; // private array $foreignCurrencies = [];
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
$collectForObjects = false; $collectForObjects = false;
@@ -94,6 +94,8 @@ class TransactionGroupTransformer extends AbstractTransformer
// source accounts // source accounts
// destination accounts // destination accounts
} }
return $objects;
} }
private function collectForArray(array $object): void private function collectForArray(array $object): void

View File

@@ -42,7 +42,7 @@ class UserGroupTransformer extends AbstractTransformer
$this->memberships = []; $this->memberships = [];
} }
public function collectMetaData(Collection $objects): void public function collectMetaData(Collection $objects): Collection
{ {
if (auth()->check()) { if (auth()->check()) {
// collect memberships so they can be listed in the group. // collect memberships so they can be listed in the group.
@@ -67,6 +67,8 @@ class UserGroupTransformer extends AbstractTransformer
} }
} }
} }
return $objects;
} }
/** /**