This commit is contained in:
James Cole
2025-02-22 08:03:42 +01:00
parent 56470bfbba
commit 48c1b525be
4 changed files with 50 additions and 37 deletions

View File

@@ -65,24 +65,7 @@ class StoreRequest extends FormRequest
return $data; return $data;
} }
private function parseAccounts(mixed $array): array
{
if (!is_array($array)) {
return [];
}
$return = [];
foreach ($array as $entry) {
if (!is_array($entry)) {
continue;
}
$return[] = [
'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')),
'current_amount' => $this->clearString((string) ($entry['current_amount'] ?? '0')),
];
}
return $return;
}
/** /**
* The rules that the incoming request must be matched against. * The rules that the incoming request must be matched against.

View File

@@ -25,8 +25,8 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Models\PiggyBank; namespace FireflyIII\Api\V1\Requests\Models\PiggyBank;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Rules\IsAssetAccountId;
use FireflyIII\Rules\IsValidPositiveAmount; use FireflyIII\Rules\IsValidPositiveAmount;
use FireflyIII\Rules\IsValidZeroOrMoreAmount;
use FireflyIII\Rules\LessThanPiggyTarget; use FireflyIII\Rules\LessThanPiggyTarget;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\ConvertsDataTypes;
@@ -47,18 +47,21 @@ class UpdateRequest extends FormRequest
{ {
$fields = [ $fields = [
'name' => ['name', 'convertString'], 'name' => ['name', 'convertString'],
'account_id' => ['account_id', 'convertInteger'], 'target_amount' => ['target_amount', 'convertString'],
'targetamount' => ['target_amount', 'convertString'], 'start_date' => ['start_date', 'convertDateTime'],
'current_amount' => ['current_amount', 'convertString'], 'target_date' => ['target_date', 'convertDateTime'],
'startdate' => ['start_date', 'convertDateTime'],
'targetdate' => ['target_date', 'convertDateTime'],
'notes' => ['notes', 'stringWithNewlines'], 'notes' => ['notes', 'stringWithNewlines'],
'order' => ['order', 'convertInteger'], 'order' => ['order', 'convertInteger'],
'object_group_title' => ['object_group_title', 'convertString'], 'object_group_title' => ['object_group_title', 'convertString'],
'object_group_id' => ['object_group_id', 'convertInteger'], 'object_group_id' => ['object_group_id', 'convertInteger'],
'transaction_currency_code' => ['transaction_currency_code', 'convertString'],
'transaction_currency_id' => ['transaction_currency_id', 'convertInteger'],
]; ];
return $this->getAllData($fields); $result = $this->getAllData($fields);
$result['accounts'] = $this->parseAccounts($this->get('accounts'));
return $result;
} }
/** /**
@@ -72,11 +75,18 @@ class UpdateRequest extends FormRequest
return [ return [
'name' => 'min:1|max:255|uniquePiggyBankForUser:' . $piggyBank->id, 'name' => 'min:1|max:255|uniquePiggyBankForUser:' . $piggyBank->id,
'current_amount' => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()], 'current_amount' => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()],
'target_amount' => ['nullable', new IsValidPositiveAmount()], 'target_amount' => ['nullable', new IsValidZeroOrMoreAmount()],
'start_date' => 'date|nullable', 'start_date' => 'date|nullable',
'target_date' => 'date|nullable|after:start_date', 'target_date' => 'date|nullable|after:start_date',
'notes' => 'max:65000', 'notes' => 'max:65000',
'account_id' => ['belongsToUser:accounts', new IsAssetAccountId()], 'accounts' => 'required',
'accounts.*' => 'array|required',
'accounts.*.account_id' => ['required', 'numeric', 'belongsToUser:accounts,id'],
'accounts.*.current_amount' => ['numeric', new IsValidZeroOrMoreAmount()],
'object_group_id' => 'numeric|belongsToUser:object_groups,id',
'object_group_title' => ['min:1', 'max:255'],
'transaction_currency_id' => 'exists:transaction_currencies,id|nullable',
'transaction_currency_code' => 'exists:transaction_currencies,code|nullable',
]; ];
} }
} }

View File

@@ -222,7 +222,8 @@ trait ModifiesPiggyBanks
// update the accounts // update the accounts
$factory = new PiggyBankFactory(); $factory = new PiggyBankFactory();
$factory->user = $this->user; $factory->user = $this->user;
$factory->linkToAccountIds($piggyBank, $data['accounts']);
$factory->linkToAccountIds($piggyBank, $data['accounts'] ?? []);
// if the piggy bank is now smaller than the sum of the money saved, // if the piggy bank is now smaller than the sum of the money saved,

View File

@@ -390,6 +390,25 @@ trait ConvertsDataTypes
return (int) $string; return (int) $string;
} }
protected function parseAccounts(mixed $array): array
{
if (!is_array($array)) {
return [];
}
$return = [];
foreach ($array as $entry) {
if (!is_array($entry)) {
continue;
}
$return[] = [
'account_id' => $this->integerFromValue((string) ($entry['account_id'] ?? '0')),
'current_amount' => $this->clearString((string) ($entry['current_amount'] ?? '0')),
];
}
return $return;
}
protected function floatFromValue(?string $string): ?float protected function floatFromValue(?string $string): ?float
{ {
if (null === $string) { if (null === $string) {