Refactor account search.

This commit is contained in:
James Cole
2021-12-18 12:35:17 +01:00
parent 0229fc243a
commit 36ecf25804
16 changed files with 206 additions and 128 deletions

View File

@@ -41,23 +41,24 @@ trait DepositValidation
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param int|null $accountId
* @param mixed $accountName
* @param array $array
*
* @return bool
*/
protected function validateDepositDestination(?int $accountId, $accountName): bool
protected function validateDepositDestination(array $array): bool
{
$result = null;
Log::debug(sprintf('Now in validateDepositDestination(%d, "%s")', $accountId, $accountName));
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateDepositDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
@@ -76,7 +77,7 @@ trait DepositValidation
if (null === $result) {
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
Log::debug('findExistingAccount() returned NULL, so the result is false.');
$this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
@@ -89,20 +90,23 @@ trait DepositValidation
}
}
$result = $result ?? false;
Log::debug(sprintf('validateDepositDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true)));
Log::debug(sprintf('validateDepositDestination will return %s', var_export($result, true)));
return $result;
}
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateDepositSource(?int $accountId, ?string $accountName): bool
protected function validateDepositSource(array $array): bool
{
Log::debug(sprintf('Now in validateDepositSource(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
Log::debug('Now in validateDepositSource', $array);
$result = null;
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
@@ -114,12 +118,32 @@ trait DepositValidation
$result = false;
}
// if the user submits an ID only but that ID is not of the correct type,
// if the user submits an ID, but that ID is not of the correct type,
// return false.
if (null !== $accountId && null === $accountName) {
if (null !== $accountId) {
$search = $this->accountRepository->find($accountId);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
Log::debug(sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
Log::debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
$result = false;
}
}
// if user submits an IBAN:
if (null !== $accountIban) {
$search = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
Log::debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type));
$result = false;
}
}
// if user submits a number:
if (null !== $accountNumber) {
$search = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
Log::debug(
sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type)
);
$result = false;
}
}

View File

@@ -36,14 +36,15 @@ trait LiabilityValidation
{
/**
* @param int|null $accountId
* @param array $array
*
* @return bool
*/
protected function validateLCDestination(?int $accountId): bool
protected function validateLCDestination(array $array): bool
{
Log::debug(sprintf('Now in validateLCDestination(%d)', $accountId));
Log::debug('Now in validateLCDestination', $array);
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$validTypes = config('firefly.valid_liabilities');
if (null === $accountId) {
@@ -74,14 +75,15 @@ trait LiabilityValidation
/**
* Source of an liability credit must be a liability.
*
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateLCSource(?string $accountName): bool
protected function validateLCSource(array $array): bool
{
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$result = true;
Log::debug(sprintf('Now in validateLCDestination("%s")', $accountName));
Log::debug('Now in validateLCDestination', $array);
if ('' === $accountName || null === $accountName) {
$result = false;
}

View File

@@ -41,15 +41,16 @@ trait OBValidation
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param int|null $accountId
* @param mixed $accountName
* @param array $array
*
* @return bool
*/
protected function validateOBDestination(?int $accountId, $accountName): bool
protected function validateOBDestination(array $array): bool
{
$result = null;
Log::debug(sprintf('Now in validateOBDestination(%d, "%s")', $accountId, $accountName));
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateOBDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
@@ -68,7 +69,7 @@ trait OBValidation
if (null === $result) {
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
Log::debug('findExistingAccount() returned NULL, so the result is false.', $validTypes);
$this->destError = (string)trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
@@ -90,15 +91,15 @@ trait OBValidation
* Source of an opening balance can either be an asset account
* or an "initial balance account". The latter can be created.
*
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateOBSource(?int $accountId, ?string $accountName): bool
protected function validateOBSource(array $array): bool
{
Log::debug(sprintf('Now in validateOBSource(%d, "%s")', $accountId, $accountName));
Log::debug(sprintf('The account name is null: %s', var_export(null === $accountName, true)));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateOBSource', $array);
$result = null;
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Log;
@@ -32,15 +33,17 @@ use Log;
*/
trait ReconciliationValidation
{
public ?Account $destination;
public ?Account $source;
/**
* @param int|null $accountId
* @param array $array
*
* @return bool
*/
protected function validateReconciliationDestination(?int $accountId): bool
protected function validateReconciliationDestination(array $array): bool
{
Log::debug('Now in validateReconciliationDestination');
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
Log::debug('Now in validateReconciliationDestination', $array);
if (null === $accountId) {
Log::debug('Return FALSE');
@@ -80,13 +83,14 @@ trait ReconciliationValidation
}
/**
* @param int|null $accountId
* @param array $array
*
* @return bool
*/
protected function validateReconciliationSource(?int $accountId): bool
protected function validateReconciliationSource(array $array): bool
{
Log::debug('In validateReconciliationSource');
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
Log::debug('In validateReconciliationSource', $array);
if (null === $accountId) {
Log::debug('Return FALSE');

View File

@@ -40,22 +40,22 @@ trait TransferValidation
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param int|null $accountId
* @param mixed $accountName
* @param array $array
*
* @return bool
*/
protected function validateTransferDestination(?int $accountId, $accountName): bool
protected function validateTransferDestination(array $array): bool
{
Log::debug(sprintf('Now in validateTransferDestination(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateTransferDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
@@ -68,7 +68,7 @@ trait TransferValidation
}
// or try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes,$array);
if (null === $search) {
$this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
@@ -88,14 +88,15 @@ trait TransferValidation
}
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateTransferSource(?int $accountId, ?string $accountName): bool
protected function validateTransferSource(array $array): bool
{
Log::debug(sprintf('Now in validateTransferSource(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateTransferSource', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
@@ -108,7 +109,7 @@ trait TransferValidation
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source, cant find it.', $validTypes);

View File

@@ -41,22 +41,22 @@ trait WithdrawalValidation
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateGenericSource(?int $accountId, ?string $accountName): bool
protected function validateGenericSource(array $array): bool
{
Log::debug(sprintf('Now in validateGenericSource(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateGenericSource', $array);
// source can be any of the following types.
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
@@ -69,7 +69,7 @@ trait WithdrawalValidation
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source. Cant find it.', $validTypes);
@@ -83,14 +83,15 @@ trait WithdrawalValidation
}
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateWithdrawalDestination(?int $accountId, ?string $accountName): bool
protected function validateWithdrawalDestination(array $array): bool
{
Log::debug(sprintf('Now in validateWithdrawalDestination(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateWithdrawalDestination()', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
@@ -120,14 +121,16 @@ trait WithdrawalValidation
}
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $array
*
* @return bool
*/
protected function validateWithdrawalSource(?int $accountId, ?string $accountName): bool
protected function validateWithdrawalSource(array $array): bool
{
Log::debug(sprintf('Now in validateWithdrawalSource(%d, "%s")', $accountId, $accountName));
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
Log::debug('Now in validateWithdrawalSource', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
@@ -140,7 +143,7 @@ trait WithdrawalValidation
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source. Cant find it.', $validTypes);