Verify sorting

This commit is contained in:
James Cole 2021-03-08 09:56:40 +01:00
parent e3161a8b9c
commit 4d34160ede
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
6 changed files with 60 additions and 82 deletions

View File

@ -71,6 +71,7 @@ class ShowController extends Controller
*/
public function index(): JsonResponse
{
$this->repository->correctOrder();
$bills = $this->repository->getBills();
$manager = $this->getManager();
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;

View File

@ -74,11 +74,20 @@ class UpdateRequest extends FormRequest
];
// this is the way.
$return = $this->getAllData($fields);
$return['trigger'] = $triggers[$return['trigger']] ?? 0;
$return['response'] = $responses[$return['response']] ?? 0;
$return['delivery'] = $deliveries[$return['delivery']] ?? 0;
$return['secret'] = null !== $this->get('secret');
$return = $this->getAllData($fields);
if (array_key_exists('trigger', $return)) {
$return['trigger'] = $triggers[$return['trigger']] ?? 0;
}
if (array_key_exists('response', $return)) {
$return['response'] = $responses[$return['response']] ?? 0;
}
if (array_key_exists('delivery', $return)) {
$return['delivery'] = $deliveries[$return['delivery']] ?? 0;
}
$return['secret'] = null !== $this->get('secret');
if (null !== $this->get('title')) {
$return['title'] = $this->string('title');
}
return $return;
}
@ -98,10 +107,10 @@ class UpdateRequest extends FormRequest
return [
'title' => sprintf('between:1,512|uniqueObjectForUser:webhooks,title,%d', $webhook->id),
'active' => [new IsBoolean],
'trigger' => sprintf('required|in:%s', $triggers),
'response' => sprintf('required|in:%s', $responses),
'delivery' => sprintf('required|in:%s', $deliveries),
'url' => ['required', 'url', 'starts_with:https://', sprintf('uniqueExistingWebhook:%d', $webhook->id)],
'trigger' => sprintf('in:%s', $triggers),
'response' => sprintf('in:%s', $responses),
'delivery' => sprintf('in:%s', $deliveries),
'url' => ['url', 'starts_with:https://', sprintf('uniqueExistingWebhook:%d', $webhook->id)],
];
}
}

View File

@ -102,6 +102,7 @@ class WebhookRepository implements WebhookRepositoryInterface
$webhook->trigger = $data['trigger'] ?? $webhook->trigger;
$webhook->response = $data['response'] ?? $webhook->response;
$webhook->delivery = $data['delivery'] ?? $webhook->delivery;
$webhook->title = $data['title'] ?? $webhook->title;
$webhook->url = $data['url'] ?? $webhook->url;
if (true === $data['secret']) {

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Location;
@ -263,54 +264,30 @@ class AccountUpdateService
return $account;
}
// get account type ID's because a join and an update is hard:
$list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]);
$oldOrder = (int)$account->order;
$newOrder = $data['order'];
$list = $this->getTypeIds([AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT]);
if (in_array($type, [AccountType::ASSET], true)) {
$list = $this->getTypeIds([AccountType::ASSET]);
}
if ($oldOrder > $newOrder) {
// say you move from 9 (old) to 3 (new)
// everything that's 3 or higher moves up one spot.
// that leaves a gap for nr 3 later on.
// 1 2 (!) 4 5 6 7 8 9 10 11 12 13 14
$this->user->accounts()
->whereIn('accounts.account_type_id', $list)
->where('accounts.order', '>=', $newOrder)
->update(['accounts.order' => \DB::raw('accounts.order + 1')]);
// update the account and save it:
// nummer 9 (now 10!) will move to nr 3.
// a gap appears on spot 10.
// 1 2 3 4 5 6 7 8 9 11 12 13 14
if ($newOrder > $oldOrder) {
$this->user->accounts()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('accounts.id', '!=', $account->id)
->whereIn('accounts.account_type_id', $list)
->update(['order' => DB::raw('accounts.order-1')]);
$account->order = $newOrder;
$account->save();
// everything over 9 (old) drops one spot
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14
$this->user->accounts()
->whereIn('accounts.account_type_id', $list)
->where('accounts.order', '>', $oldOrder)
->update(['accounts.order' => \DB::raw('accounts.order - 1')]);
return $account;
}
if ($oldOrder < $newOrder) {
// if it goes from 3 (old) to 9 (new),
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// everything that is between 3 and 9 (incl) - 1 spot
// 1 2 2 3 4 5 6 7 8 10 11 12 13 14
$this->user->accounts()
->whereIn('accounts.account_type_id', $list)
->where('accounts.order', '>=', $oldOrder)
->where('accounts.order', '<=', $newOrder)
->update(['accounts.order' => \DB::raw('accounts.order - 1')]);
// then set order to 9
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14
$account->order = $newOrder;
$account->save();
}
$this->user->accounts()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('accounts.id', '!=', $account->id)
->whereIn('accounts.account_type_id', $list)
->update(['order' => DB::raw('accounts.order+1')]);
$account->order = $newOrder;
$account->save();
return $account;
}

View File

@ -151,10 +151,6 @@ class Preferences
*/
public function getForUser(User $user, string $name, $default = null): ?Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
if (Cache::has($fullName)) {
return Cache::get($fullName);
}
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $preference->data) {
try {
@ -166,7 +162,6 @@ class Preferences
}
if (null !== $preference) {
Cache::forever($fullName, $preference);
return $preference;
}
@ -185,32 +180,11 @@ class Preferences
* @param null|string $default
*
* @return \FireflyIII\Models\Preference|null
* TODO remove me
*/
public function getFreshForUser(User $user, string $name, $default = null): ?Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $preference->data) {
try {
$preference->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()));
}
$preference = null;
}
if (null !== $preference) {
Cache::forever($fullName, $preference);
return $preference;
}
// no preference found and default is null:
if (null === $default) {
// return NULL
return null;
}
return $this->setForUser($user, $name, $default);
return $this->getForUser($user, $name, $default);
}
/**

View File

@ -634,17 +634,33 @@ class FireflyValidator extends Validator
public function validateUniqueExistingWebhook($value, $parameters, $something): bool
{
$existingId = (int)($something[0] ?? 0);
$trigger = 0;
$response = 0;
$delivery = 0;
$triggers = array_flip(config('firefly.webhooks.triggers'));
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
if (auth()->check()) {
// possible values
$triggers = array_flip(config('firefly.webhooks.triggers'));
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
// get existing webhook value:
if(0!== $existingId) {
/** @var Webhook $webhook */
$webhook = auth()->user()->webhooks()->find($existingId);
if(null === $webhook) {
return false;
}
// set triggers etc.
$trigger = $triggers[$webhook->trigger] ?? 0;
$response = $responses[$webhook->response] ?? 0;
$delivery = $deliveries[$webhook->delivery] ?? 0;
}
if(0=== $existingId) {
$trigger = $triggers[$this->data['trigger']] ?? 0;
$response = $responses[$this->data['response']] ?? 0;
$delivery = $deliveries[$this->data['delivery']] ?? 0;
}
// integers
$trigger = $triggers[$this->data['trigger']] ?? 0;
$response = $responses[$this->data['response']] ?? 0;
$delivery = $deliveries[$this->data['delivery']] ?? 0;
$url = $this->data['url'];
$userId = auth()->user()->id;