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

View File

@@ -62,7 +62,6 @@ class IndexController extends Controller
*/
public function index(IndexRequest $request): JsonResponse
{
$this->repository->resetAccountOrder();
$types = $request->getAccountTypes();
$instructions = $request->getSortInstructions('accounts');
@@ -78,7 +77,8 @@ class IndexController extends Controller
return response()
->json($this->jsonApiList('accounts', $paginator, $transformer))
->header('Content-Type', self::CONTENT_TYPE);
->header('Content-Type', self::CONTENT_TYPE)
;
}
public function infiniteList(InfiniteListRequest $request): JsonResponse
@@ -98,6 +98,7 @@ class IndexController extends Controller
return response()
->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 ChecksLogin;
use ConvertsDataTypes;
use TransactionFilter;
use GetSortInstructions;
use TransactionFilter;
public function buildParams(): string
{
@@ -99,7 +99,6 @@ class InfiniteListRequest extends FormRequest
return 0 === $page || $page > 65536 ? 1 : $page;
}
public function getTransactionTypes(): array
{
$type = (string)$this->get('type', 'default');

View File

@@ -74,20 +74,22 @@ class LoginController extends Controller
*
* @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())));
app('log')->debug('User is trying to login.');
try {
$this->validateLogin($request);
} catch (ValidationException $e) {
return redirect(route('login'))
->withErrors(
[
$this->username => trans('auth.failed')
$this->username => trans('auth.failed'),
]
)
->onlyInput($this->username);
->onlyInput($this->username)
;
}
app('log')->debug('Login data is present.');
@@ -125,7 +127,6 @@ class LoginController extends Controller
$this->sendFailedLoginResponse($request);
// @noinspection PhpUnreachableStatementInspection
return response()->json([]);
}
@@ -235,7 +236,6 @@ class LoginController extends Controller
}
$usernameField = $this->username();
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 */
$metaValue = $this->accountMeta()
->where('name', 'account_number')
->first();
->first()
;
return null !== $metaValue ? $metaValue->data : '';
}
@@ -259,7 +260,7 @@ class Account extends Model
protected function accountId(): Attribute
{
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
{
return Attribute::make(
get: static fn($value) => (int)$value,
get: static fn ($value) => (int)$value,
);
}
//
protected function iban(): Attribute
{
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
{
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
{
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.data', '=', $json);
}
);
)
;
if (0 !== count($types)) {
$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.account_type_id', 'ASC')
->orderBy('accounts.name', 'ASC')
->with(['accountType']);
->with(['accountType'])
;
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);

View File

@@ -25,7 +25,6 @@ namespace FireflyIII\Support\Request;
trait GetSortInstructions
{
final public function getSortInstructions(string $key): array
{
$allowed = config(sprintf('firefly.sorting.allowed.%s', $key));
@@ -50,5 +49,4 @@ trait GetSortInstructions
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
*
* @param Collection $objects
* @return Collection
*/
abstract public function collectMetaData(Collection $objects): Collection;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -69,7 +69,7 @@ class PiggyBankTransformer extends AbstractTransformer
// $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)
$piggyBanks = $objects->pluck('id')->toArray();
@@ -135,6 +135,8 @@ class PiggyBankTransformer extends AbstractTransformer
Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__));
$this->default = app('amount')->getDefaultCurrencyByUserGroup(auth()->user()->userGroup);
$this->converter = new ExchangeRateConverter();
return $objects;
}
/**

View File

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

View File

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

View File

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