[chore] various code cleanup.

This commit is contained in:
James Cole
2025-01-21 06:34:39 +01:00
parent 15ac69bfad
commit d57327fd11
7 changed files with 62 additions and 6 deletions

View File

@@ -86,7 +86,7 @@ class PreferencesController extends Controller
$manager = $this->getManager(); $manager = $this->getManager();
if ('currencyPreference' === $preference->name) { if ('currencyPreference' === $preference->name) {
throw new FireflyException('Please use api/v1/currencies/default instead.'); throw new FireflyException('Please use api/v1/currencies/native instead.');
} }
/** @var PreferenceTransformer $transformer */ /** @var PreferenceTransformer $transformer */
@@ -161,7 +161,7 @@ class PreferencesController extends Controller
public function update(PreferenceUpdateRequest $request, Preference $preference): JsonResponse public function update(PreferenceUpdateRequest $request, Preference $preference): JsonResponse
{ {
if ('currencyPreference' === $preference->name) { if ('currencyPreference' === $preference->name) {
throw new FireflyException('Please use api/v1/currencies/default instead.'); throw new FireflyException('Please use api/v1/currencies/native instead.');
} }
$manager = $this->getManager(); $manager = $this->getManager();

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Requests\Models\Account;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\Location; use FireflyIII\Models\Location;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\UniqueAccountNumber; use FireflyIII\Rules\UniqueAccountNumber;
use FireflyIII\Rules\UniqueIban; use FireflyIII\Rules\UniqueIban;
@@ -33,6 +34,8 @@ use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/** /**
* Class UpdateRequest * Class UpdateRequest
@@ -86,7 +89,7 @@ class UpdateRequest extends FormRequest
$types = implode(',', array_keys(config('firefly.subTitlesByIdentifier'))); $types = implode(',', array_keys(config('firefly.subTitlesByIdentifier')));
$ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes'))); $ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes')));
$rules = [ $rules = [
'name' => sprintf('min:1|max:1024|uniqueAccountForUser:%d', $account->id), 'name' => sprintf('min:1|max:1024|uniqueAccountForUser:%d', $account->id),
'type' => sprintf('in:%s', $types), 'type' => sprintf('in:%s', $types),
'iban' => ['iban', 'nullable', new UniqueIban($account, $this->convertString('type'))], 'iban' => ['iban', 'nullable', new UniqueIban($account, $this->convertString('type'))],
@@ -112,4 +115,34 @@ class UpdateRequest extends FormRequest
return Location::requestRules($rules); return Location::requestRules($rules);
} }
/**
* Configure the validator instance with special rules for after the basic validation rules.
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator): void {
// validate start before end only if both are there.
$data = $validator->getData();
/** @var Account $account */
$account = $this->route()->parameter('account');
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account);
// how many piggies are attached?
$piggyBanks = $account->piggyBanks()->count();
if($piggyBanks > 0 && array_key_exists('currency_code', $data) && $data['currency_code'] !== $currency->code) {
$validator->errors()->add('currency_code', (string) trans('validation.piggy_no_change_currency'));
}
if($piggyBanks > 0 && array_key_exists('currency_id', $data) && (int) $data['currency_id'] !== $currency->id) {
$validator->errors()->add('currency_id', (string) trans('validation.piggy_no_change_currency'));
}
}
);
if ($validator->fails()) {
Log::channel('audit')->error(sprintf('Validation errors in %s', __CLASS__), $validator->errors()->toArray());
}
}
} }

View File

@@ -63,7 +63,6 @@ class UpdateRequest extends FormRequest
{ {
/** @var Tag $tag */ /** @var Tag $tag */
$tag = $this->route()->parameter('tagOrId'); $tag = $this->route()->parameter('tagOrId');
// TODO check if uniqueObjectForUser is obsolete
$rules = [ $rules = [
'tag' => 'min:1|max:1024|uniqueObjectForUser:tags,tag,'.$tag->id, 'tag' => 'min:1|max:1024|uniqueObjectForUser:tags,tag,'.$tag->id,
'description' => 'min:1|nullable|max:32768', 'description' => 'min:1|nullable|max:32768',

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Models\Transaction; namespace FireflyIII\Api\V1\Requests\Models\Transaction;
use FireflyIII\Models\Location;
use FireflyIII\Rules\BelongsUser; use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime; use FireflyIII\Rules\IsDateOrTime;
@@ -89,6 +90,11 @@ class StoreRequest extends FormRequest
'currency_id' => $this->integerFromValue((string) $object['currency_id']), 'currency_id' => $this->integerFromValue((string) $object['currency_id']),
'currency_code' => $this->clearString((string) $object['currency_code']), 'currency_code' => $this->clearString((string) $object['currency_code']),
// location
'latitude' => $this->floatFromValue((string) $object['latitude']),
'longitude' => $this->floatFromValue((string) $object['longitude']),
'zoom_level' => $this->integerFromValue((string) $object['zoom_level']),
// foreign currency info: // foreign currency info:
'foreign_currency_id' => $this->integerFromValue((string) $object['foreign_currency_id']), 'foreign_currency_id' => $this->integerFromValue((string) $object['foreign_currency_id']),
'foreign_currency_code' => $this->clearString((string) $object['foreign_currency_code']), 'foreign_currency_code' => $this->clearString((string) $object['foreign_currency_code']),
@@ -171,13 +177,18 @@ class StoreRequest extends FormRequest
{ {
app('log')->debug('Collect rules of TransactionStoreRequest'); app('log')->debug('Collect rules of TransactionStoreRequest');
$validProtocols = config('firefly.valid_url_protocols'); $validProtocols = config('firefly.valid_url_protocols');
$locationRules = Location::requestRules([]);
return [ return [
// basic fields for group: // basic fields for group:
'group_title' => 'min:1|max:1000|nullable', 'group_title' => 'min:1|max:1000|nullable',
'error_if_duplicate_hash' => [new IsBoolean()], 'error_if_duplicate_hash' => [new IsBoolean()],
'apply_rules' => [new IsBoolean()], 'apply_rules' => [new IsBoolean()],
// location rules
'transactions.*.latitude' => $locationRules['latitude'],
'transactions.*.longitude' => $locationRules['longitude'],
'transactions.*.zoom_level' => $locationRules['zoom_level'],
// transaction rules (in array for splits): // transaction rules (in array for splits):
'transactions.*.type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation', 'transactions.*.type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation',
'transactions.*.date' => ['required', new IsDateOrTime()], 'transactions.*.date' => ['required', new IsDateOrTime()],

View File

@@ -576,7 +576,7 @@ class TransactionJournalFactory
private function storeLocation(TransactionJournal $journal, NullArrayObject $data): void private function storeLocation(TransactionJournal $journal, NullArrayObject $data): void
{ {
if (true === $data['store_location']) { if(null !== $data['longitude'] && null !== $data['latitude'] && null !== $data['zoom_level']) {
$location = new Location(); $location = new Location();
$location->longitude = $data['longitude']; $location->longitude = $data['longitude'];
$location->latitude = $data['latitude']; $location->latitude = $data['latitude'];

View File

@@ -386,6 +386,18 @@ trait ConvertsDataTypes
return (int) $string; return (int) $string;
} }
protected function floatFromValue(?string $string): ?float
{
if (null === $string) {
return null;
}
if ('' === $string) {
return null;
}
return (float) $string;
}
/** /**
* Return integer value, or NULL when it's not set. * Return integer value, or NULL when it's not set.
*/ */

View File

@@ -177,6 +177,7 @@ return [
'unique_existing_webhook' => 'You already have another webhook with this combination of URL, trigger, response and delivery.', 'unique_existing_webhook' => 'You already have another webhook with this combination of URL, trigger, response and delivery.',
'same_account_type' => 'Both accounts must be of the same account type', 'same_account_type' => 'Both accounts must be of the same account type',
'same_account_currency' => 'Both accounts must have the same currency setting', 'same_account_currency' => 'Both accounts must have the same currency setting',
'piggy_no_change_currency' => 'Because there are piggy banks linked to this account, you cannot change the currency of the account.',