diff --git a/app/Api/V1/Requests/Models/PiggyBank/StoreRequest.php b/app/Api/V1/Requests/Models/PiggyBank/StoreRequest.php index b4c416f1a3..729b5e9a83 100644 --- a/app/Api/V1/Requests/Models/PiggyBank/StoreRequest.php +++ b/app/Api/V1/Requests/Models/PiggyBank/StoreRequest.php @@ -65,24 +65,7 @@ class StoreRequest extends FormRequest 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. diff --git a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php index ca2062527a..f506b1c870 100644 --- a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php @@ -25,8 +25,8 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests\Models\PiggyBank; use FireflyIII\Models\PiggyBank; -use FireflyIII\Rules\IsAssetAccountId; use FireflyIII\Rules\IsValidPositiveAmount; +use FireflyIII\Rules\IsValidZeroOrMoreAmount; use FireflyIII\Rules\LessThanPiggyTarget; use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ConvertsDataTypes; @@ -46,19 +46,22 @@ class UpdateRequest extends FormRequest public function getAll(): array { $fields = [ - 'name' => ['name', 'convertString'], - 'account_id' => ['account_id', 'convertInteger'], - 'targetamount' => ['target_amount', 'convertString'], - 'current_amount' => ['current_amount', 'convertString'], - 'startdate' => ['start_date', 'convertDateTime'], - 'targetdate' => ['target_date', 'convertDateTime'], - 'notes' => ['notes', 'stringWithNewlines'], - 'order' => ['order', 'convertInteger'], - 'object_group_title' => ['object_group_title', 'convertString'], - 'object_group_id' => ['object_group_id', 'convertInteger'], + 'name' => ['name', 'convertString'], + 'target_amount' => ['target_amount', 'convertString'], + 'start_date' => ['start_date', 'convertDateTime'], + 'target_date' => ['target_date', 'convertDateTime'], + 'notes' => ['notes', 'stringWithNewlines'], + 'order' => ['order', 'convertInteger'], + 'object_group_title' => ['object_group_title', 'convertString'], + '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; } /** @@ -70,13 +73,20 @@ class UpdateRequest extends FormRequest $piggyBank = $this->route()->parameter('piggyBank'); return [ - 'name' => 'min:1|max:255|uniquePiggyBankForUser:'.$piggyBank->id, - 'current_amount' => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()], - 'target_amount' => ['nullable', new IsValidPositiveAmount()], - 'start_date' => 'date|nullable', - 'target_date' => 'date|nullable|after:start_date', - 'notes' => 'max:65000', - 'account_id' => ['belongsToUser:accounts', new IsAssetAccountId()], + 'name' => 'min:1|max:255|uniquePiggyBankForUser:' . $piggyBank->id, + 'current_amount' => ['nullable', new LessThanPiggyTarget(), new IsValidPositiveAmount()], + 'target_amount' => ['nullable', new IsValidZeroOrMoreAmount()], + 'start_date' => 'date|nullable', + 'target_date' => 'date|nullable|after:start_date', + 'notes' => 'max:65000', + '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', ]; } } diff --git a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php index d00b74c58b..568e37895d 100644 --- a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php +++ b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php @@ -222,7 +222,8 @@ trait ModifiesPiggyBanks // update the accounts $factory = new PiggyBankFactory(); $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, diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php index 6e4644250b..18e93a8343 100644 --- a/app/Support/Request/ConvertsDataTypes.php +++ b/app/Support/Request/ConvertsDataTypes.php @@ -390,6 +390,25 @@ trait ConvertsDataTypes 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 { if (null === $string) {