Fix some tests for account API

This commit is contained in:
James Cole
2021-03-13 12:01:01 +01:00
parent 668b169a5e
commit 7118abe28d
29 changed files with 992 additions and 315 deletions

View File

@@ -68,6 +68,7 @@ class StoreController extends Controller
public function store(StoreRequest $request): JsonResponse
{
$data = $request->getAllAccountData();
$this->repository->resetAccountOrder();
$account = $this->repository->store($data);
$manager = $this->getManager();

View File

@@ -80,7 +80,7 @@ class StoreRequest extends FormRequest
// append Location information.
$data = $this->appendLocationData($data, null);
if ('liability' === $data['account_type']) {
if ('liability' === $data['account_type'] || 'liabilities' === $data['account_type']) {
$data['opening_balance'] = bcmul($this->string('liability_amount'), '-1');
$data['opening_balance_date'] = $this->date('liability_start_date');
$data['account_type'] = $this->string('liability_type');

View File

@@ -195,7 +195,7 @@ class UpdateRequest extends FormRequest
{
$validator->after(
function (Validator $validator) {
$this->validateOneRecurrenceTransaction($validator);
//$this->validateOneRecurrenceTransaction($validator);
$this->validateOneRepetitionUpdate($validator);
$this->validateRecurrenceRepetition($validator);
$this->validateRepetitionMoment($validator);

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Console\Command;
@@ -62,16 +61,7 @@ class FixAccountOrder extends Command
$users = User::get();
foreach ($users as $user) {
$this->repository->setUser($user);
$sets = [
[AccountType::DEFAULT, AccountType::ASSET],
[AccountType::EXPENSE, AccountType::BENEFICIARY],
[AccountType::REVENUE],
[AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
[AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
];
foreach ($sets as $set) {
$this->repository->resetAccountOrder($set);
}
$this->repository->resetAccountOrder();
}
$end = round(microtime(true) - $start, 2);

View File

@@ -30,6 +30,7 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
use FireflyIII\Services\Internal\Support\LocationServiceTrait;
use FireflyIII\Services\Internal\Update\AccountUpdateService;
use FireflyIII\User;
use Log;
@@ -42,12 +43,12 @@ class AccountFactory
{
use AccountServiceTrait, LocationServiceTrait;
protected AccountRepositoryInterface $accountRepository;
protected array $validAssetFields;
protected array $validCCFields;
protected array $validFields;
private array $canHaveVirtual;
private User $user;
protected AccountRepositoryInterface $accountRepository;
protected array $validAssetFields;
protected array $validCCFields;
protected array $validFields;
private array $canHaveVirtual;
private User $user;
/**
* AccountFactory constructor.
@@ -73,9 +74,10 @@ class AccountFactory
public function create(array $data): Account
{
$type = $this->getAccountType($data['account_type_id'] ?? null, $data['account_type'] ?? null);
if (null === $type) {
throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'] ?? null, $data['account_type'] ?? null));
throw new FireflyException(
sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'] ?? null, $data['account_type'] ?? null)
);
}
$data['iban'] = $this->filterIban($data['iban'] ?? null);
@@ -85,8 +87,13 @@ class AccountFactory
$return = $this->find($data['name'], $type->type);
if (null === $return) {
$this->accountRepository->resetAccountOrder();
// create it:
$databaseData = ['user_id' => $this->user->id, 'account_type_id' => $type->id, 'name' => $data['name'], 'order' => $data['order'] ?? 0, 'virtual_balance' => $data['virtual_balance'] ?? null, 'active' => true === $data['active'], 'iban' => $data['iban'],];
$databaseData = ['user_id' => $this->user->id,
'account_type_id' => $type->id,
'name' => $data['name'], 'order' => 0,
'virtual_balance' => $data['virtual_balance'] ?? null, 'active' => true === $data['active'], 'iban' => $data['iban'],];
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
unset($data['currency_code']);
@@ -118,6 +125,15 @@ class AccountFactory
// store location
$this->storeNewLocation($return, $data);
// set new order:
if (array_key_exists('order', $data)) {
$maxOrder = $this->accountRepository->maxOrder([$type->type]);
$order = $data['order'] > $maxOrder ? $maxOrder+1 : $data['order'];
$update = new AccountUpdateService;
$update->setUser($return->user);
$return = $update->updateAccountOrder($return,['order' => $order]);
}
}
return $return;
@@ -152,7 +168,10 @@ class AccountFactory
if (null === $return) {
Log::debug('Found nothing. Will create a new one.');
$return = $this->create(['user_id' => $this->user->id, 'name' => $accountName, 'account_type_id' => $type->id, 'account_type' => null, 'virtual_balance' => '0', 'iban' => null, 'active' => true,]);
$return = $this->create(
['user_id' => $this->user->id, 'name' => $accountName, 'account_type_id' => $type->id, 'account_type' => null, 'virtual_balance' => '0',
'iban' => null, 'active' => true,]
);
}
return $return;

View File

@@ -136,7 +136,7 @@ class IndexController extends Controller
if (1 === random_int(0, 20)) {
Log::debug('Will reset order.');
$this->repository->resetAccountOrder($types);
$this->repository->resetAccountOrder();
}
$collection = $this->repository->getActiveAccountsByType($types);

View File

@@ -586,16 +586,25 @@ class AccountRepository implements AccountRepositoryInterface
/**
* @inheritDoc
*/
public function resetAccountOrder(array $types): void
public function resetAccountOrder(): void
{
$list = $this->getAccountsByType($types);
/**
* @var int $index
* @var Account $account
*/
foreach ($list as $index => $account) {
$account->order = $index + 1;
$account->save();
$sets = [
[AccountType::DEFAULT, AccountType::ASSET],
[AccountType::EXPENSE, AccountType::BENEFICIARY],
[AccountType::REVENUE],
[AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
[AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
];
foreach ($sets as $set) {
$list = $this->getAccountsByType($set);
$index = 1;
foreach ($list as $account) {
if ($index !== $account->order) {
$account->order = $index;
$account->save();
}
$index++;
}
}
}
@@ -753,4 +762,12 @@ class AccountRepository implements AccountRepositoryInterface
return $service->update($account, $data);
}
/**
* @inheritDoc
*/
public function maxOrder(array $types): int
{
return (int)$this->getAccountsByType($types)->max('order');
}
}

View File

@@ -47,6 +47,13 @@ interface AccountRepositoryInterface
*/
public function count(array $types): int;
/**
* @param array $types
*
* @return int
*/
public function maxOrder(array $types): int;
/**
* Moved here from account CRUD.
*
@@ -256,10 +263,8 @@ interface AccountRepositoryInterface
/**
* Reset order types of the mentioned accounts.
*
* @param array $types
*/
public function resetAccountOrder(array $types): void;
public function resetAccountOrder(): void;
/**
* @param string $query

View File

@@ -461,7 +461,8 @@ trait ModifiesPiggyBanks
$user = $this->user;
$user->piggyBanks()->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->update(['piggy_banks.order' => DB::raw('piggy_banks.order-1')]);
->decrement('piggybanks.order',1);
$piggyBank->order = $newOrder;
$piggyBank->save();
}
@@ -474,7 +475,8 @@ trait ModifiesPiggyBanks
$user = $this->user;
$user->piggyBanks()->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->update(['piggy_banks.order' => DB::raw('piggy_banks.order+1')]);
->increment('piggybanks.order',1);
$piggyBank->order = $newOrder;
$piggyBank->save();
}

View File

@@ -22,7 +22,6 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\RuleGroup;
use DB;
use Exception;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
@@ -458,14 +457,14 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
if ($newOrder > $oldOrder) {
$this->user->ruleGroups()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('rule_groups.id', '!=', $ruleGroup->id)
->update(['order' => DB::raw('rule_groups.order-1')]);
->decrement('rule_groups.order', 1);
$ruleGroup->order = $newOrder;
$ruleGroup->save();
}
if ($newOrder < $oldOrder) {
$this->user->ruleGroups()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('rule_groups.id', '!=', $ruleGroup->id)
->update(['order' => DB::raw('rule_groups.order+1')]);
->increment('rule_groups.order', 1);
$ruleGroup->order = $newOrder;
$ruleGroup->save();
}

View File

@@ -83,7 +83,6 @@ trait AccountServiceTrait
public function updateMetaData(Account $account, array $data): void
{
$fields = $this->validFields;
if ($account->accountType->type === AccountType::ASSET) {
$fields = $this->validAssetFields;
}

View File

@@ -52,9 +52,6 @@ class AccountUpdateService
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
// TODO move to configuration.
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
$this->accountRepository = app(AccountRepositoryInterface::class);
@@ -63,6 +60,14 @@ class AccountUpdateService
$this->validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Update account data.
*
@@ -73,6 +78,7 @@ class AccountUpdateService
*/
public function update(Account $account, array $data): Account
{
Log::debug(sprintf('Now in %s',__METHOD__));
$this->accountRepository->setUser($account->user);
$this->user = $account->user;
$account = $this->updateAccount($account, $data);
@@ -126,7 +132,7 @@ class AccountUpdateService
}
// update virtual balance (could be set to zero if empty string).
if (null !== $data['virtual_balance']) {
if (array_key_exists('virtual_balance', $data) && null !== $data['virtual_balance']) {
$account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
}
@@ -175,7 +181,7 @@ class AccountUpdateService
*
* @return Account
*/
private function updateAccountOrder(Account $account, array $data): Account
public function updateAccountOrder(Account $account, array $data): Account
{
// skip if no order info
if (!array_key_exists('order', $data) || $data['order'] === $account->order) {
@@ -195,20 +201,20 @@ class AccountUpdateService
}
if ($newOrder > $oldOrder) {
$this->user->accounts()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
$this->user->accounts()->where('accounts.order', '<=', $newOrder)->where('accounts.order', '>', $oldOrder)
->where('accounts.id', '!=', $account->id)
->whereIn('accounts.account_type_id', $list)
->update(['order' => DB::raw('accounts.order-1')]);
->decrement('order', 1);
$account->order = $newOrder;
$account->save();
return $account;
}
$this->user->accounts()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
$this->user->accounts()->where('accounts.order', '>=', $newOrder)->where('accounts.order', '<', $oldOrder)
->where('accounts.id', '!=', $account->id)
->whereIn('accounts.account_type_id', $list)
->update(['order' => DB::raw('accounts.order+1')]);
->increment('order',1);
$account->order = $newOrder;
$account->save();

View File

@@ -185,14 +185,14 @@ class BillUpdateService
if ($newOrder > $oldOrder) {
$this->user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order-1')]);
->decrement('bills.order',1);
$bill->order = $newOrder;
$bill->save();
}
if ($newOrder < $oldOrder) {
$this->user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('bills.id', '!=', $bill->id)
->update(['order' => DB::raw('bills.order+1')]);
->increment('bills.order',1);
$bill->order = $newOrder;
$bill->save();
}

View File

@@ -223,10 +223,6 @@ class Amount
*/
public function getAllCurrencies(): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
@@ -235,10 +231,6 @@ class Amount
*/
public function getCurrencies(): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
}
@@ -247,9 +239,6 @@ class Amount
*/
public function getCurrencyCode(): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$cache = new CacheProperties;
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
@@ -273,9 +262,6 @@ class Amount
*/
public function getDefaultCurrency(): TransactionCurrency
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
/** @var User $user */
$user = auth()->user();
@@ -287,10 +273,6 @@ class Amount
*/
public function getSystemCurrency(): TransactionCurrency
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return TransactionCurrency::where('code', 'EUR')->first();
}

View File

@@ -41,9 +41,6 @@ class FireflyConfig
*/
public function delete(string $name): void
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
}
$fullName = 'ff-config-' . $name;
if (Cache::has($fullName)) {
Cache::forget($fullName);
@@ -75,9 +72,6 @@ class FireflyConfig
*/
public function get(string $name, $default = null): ?Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
}
$fullName = 'ff-config-' . $name;
if (Cache::has($fullName)) {
return Cache::get($fullName);
@@ -111,9 +105,7 @@ class FireflyConfig
*/
public function getFresh(string $name, $default = null): ?Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
if ($config) {
@@ -135,9 +127,6 @@ class FireflyConfig
*/
public function put(string $name, $value): Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return $this->set($name, $value);
}
@@ -151,9 +140,6 @@ class FireflyConfig
*/
public function set(string $name, $value): Configuration
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
/** @var Configuration $config */
try {
$config = Configuration::whereName($name)->first();

View File

@@ -62,9 +62,6 @@ class TransactionGroupTwig extends AbstractExtension
return new TwigFunction(
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
Log::warning('Twig TransactionGroup::journalGetMetaDate should NOT be called in the TEST environment!');
}
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
@@ -87,9 +84,6 @@ class TransactionGroupTwig extends AbstractExtension
return new TwigFunction(
'journalGetMetaField',
static function (int $journalId, string $metaField) {
if ('testing' === config('app.env')) {
Log::warning('Twig TransactionGroup::journalGetMetaField should NOT be called in the TEST environment!');
}
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)

View File

@@ -91,7 +91,7 @@ class AccountTransformer extends AbstractTransformer
'created_at' => $account->created_at->toAtomString(),
'updated_at' => $account->updated_at->toAtomString(),
'active' => $account->active,
'order' => $account->order,
'order' => (int) $account->order,
'name' => $account->name,
'type' => strtolower($accountType),
'account_role' => $accountRole,