Update webhooks

This commit is contained in:
James Cole 2022-09-17 07:07:25 +02:00
parent f0416f5322
commit 10f2cf2481
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
13 changed files with 99 additions and 49 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Models\Webhook;
use FireflyIII\Models\Webhook;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
@ -40,9 +41,10 @@ class CreateRequest extends FormRequest
*/
public function getData(): array
{
$triggers = array_flip(config('firefly.webhooks.triggers'));
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
$triggers = array_flip(Webhook::getTriggers());
$responses = array_flip(Webhook::getResponses());
$deliveries = array_flip(Webhook::getDeliveries());
$fields = [
'title' => ['title', 'convertString'],
@ -69,9 +71,9 @@ class CreateRequest extends FormRequest
*/
public function rules(): array
{
$triggers = implode(',', array_values(config('firefly.webhooks.triggers')));
$responses = implode(',', array_values(config('firefly.webhooks.responses')));
$deliveries = implode(',', array_values(config('firefly.webhooks.deliveries')));
$triggers = implode(',', Webhook::getTriggers());
$responses = implode(',', Webhook::getResponses());
$deliveries = implode(',', Webhook::getDeliveries());
return [
'title' => 'required|between:1,512|uniqueObjectForUser:webhooks,title',
@ -79,7 +81,9 @@ class CreateRequest extends FormRequest
'trigger' => sprintf('required|in:%s', $triggers),
'response' => sprintf('required|in:%s', $responses),
'delivery' => sprintf('required|in:%s', $deliveries),
'url' => ['required', 'url', 'starts_with:https://', 'uniqueWebhook'],
'url' => ['required', 'url', 'uniqueWebhook'],
];
}
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Models\Webhook;
use FireflyIII\Models\Webhook;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
@ -40,9 +41,9 @@ class UpdateRequest extends FormRequest
*/
public function getData(): array
{
$triggers = array_flip(config('firefly.webhooks.triggers'));
$responses = array_flip(config('firefly.webhooks.responses'));
$deliveries = array_flip(config('firefly.webhooks.deliveries'));
$triggers = array_flip(Webhook::getTriggers());
$responses = array_flip(Webhook::getResponses());
$deliveries = array_flip(Webhook::getDeliveries());
$fields = [
'title' => ['title', 'convertString'],
@ -79,9 +80,10 @@ class UpdateRequest extends FormRequest
*/
public function rules(): array
{
$triggers = implode(',', array_values(config('firefly.webhooks.triggers')));
$responses = implode(',', array_values(config('firefly.webhooks.responses')));
$deliveries = implode(',', array_values(config('firefly.webhooks.deliveries')));
$triggers = implode(',', array_values(Webhook::getTriggers()));
$responses = implode(',', array_values(Webhook::getResponses()));
$deliveries = implode(',', array_values(Webhook::getDeliveries()));
$webhook = $this->route()->parameter('webhook');
return [

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* ListController.php
* Copyright (c) 2022 james@firefly-iii.org

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* ListController.php
* Copyright (c) 2022 james@firefly-iii.org

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* UpdateGroupInformation.php
* Copyright (c) 2022 james@firefly-iii.org

View File

@ -24,5 +24,6 @@ namespace FireflyIII\Enums;
enum WebhookDelivery: int
{
//case XML = 200;
case JSON = 300;
}

View File

@ -27,4 +27,5 @@ enum WebhookResponse: int
case TRANSACTIONS = 200;
case ACCOUNTS = 210;
case NONE = 220;
}

View File

@ -22,9 +22,15 @@ declare(strict_types=1);
namespace FireflyIII\Enums;
/**
* Class WebhookTrigger
*/
enum WebhookTrigger: int
{
case STORE_TRANSACTION = 100;
//case BEFORE_STORE_TRANSACTION = 101;
case UPDATE_TRANSACTION = 110;
//case BEFORE_UPDATE_TRANSACTION = 111;
case DESTROY_TRANSACTION = 120;
//case BEFORE_DESTROY_TRANSACTION = 121;
}

View File

@ -24,6 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Models;
use Eloquent;
use FireflyIII\Enums\WebhookDelivery;
use FireflyIII\Enums\WebhookResponse;
use FireflyIII\Enums\WebhookTrigger;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
@ -76,17 +79,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
*/
class Webhook extends Model
{
// dont forget to update the config in firefly.php
public const DELIVERY_JSON = 300;
public const RESPONSE_ACCOUNTS = 210;
public const RESPONSE_NONE = 220;
public const RESPONSE_TRANSACTIONS = 200;
public const TRIGGER_DESTROY_TRANSACTION = 120;
public const TRIGGER_STORE_TRANSACTION = 100;
public const TRIGGER_UPDATE_TRANSACTION = 110;
use SoftDeletes;
protected $casts
@ -138,4 +130,42 @@ class Webhook extends Model
{
return $this->hasMany(WebhookMessage::class);
}
/**
* @return array
*/
public static function getTriggers(): array
{
$array = [];
$set = WebhookTrigger::cases();
foreach ($set as $item) {
$array[$item->value] = $item->name;
}
return $array;
}
/**
* @return array
*/
public static function getResponses(): array
{
$array = [];
$set = WebhookResponse::cases();
foreach ($set as $item) {
$array[$item->value] = $item->name;
}
return $array;
}
/**
* @return array
*/
public static function getDeliveries(): array
{
$array = [];
$set = WebhookDelivery::cases();
foreach ($set as $item) {
$array[$item->value] = $item->name;
}
return $array;
}
}

View File

@ -24,6 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Enums\WebhookDelivery;
use FireflyIII\Enums\WebhookResponse;
use FireflyIII\Enums\WebhookTrigger;
use FireflyIII\Models\Webhook;
/**
@ -38,8 +41,6 @@ class WebhookTransformer extends AbstractTransformer
*/
public function __construct()
{
// array merge kills the keys
$this->enums = config('firefly.webhooks.triggers') + config('firefly.webhooks.responses') + config('firefly.webhooks.deliveries');
}
/**
@ -58,9 +59,9 @@ class WebhookTransformer extends AbstractTransformer
'active' => $webhook->active,
'title' => $webhook->title,
'secret' => $webhook->secret,
'trigger' => $this->getEnum($webhook->trigger),
'response' => $this->getEnum($webhook->response),
'delivery' => $this->getEnum($webhook->delivery),
'trigger' => $this->getEnum('trigger', $webhook->trigger),
'response' => $this->getEnum('response', $webhook->response),
'delivery' => $this->getEnum('delivery', $webhook->delivery),
'url' => $webhook->url,
'links' => [
[
@ -71,8 +72,19 @@ class WebhookTransformer extends AbstractTransformer
];
}
private function getEnum(int $value)
/**
* @param string $type
* @param int $value
* @return string
*/
private function getEnum(string $type, int $value): string
{
return $this->enums[$value] ?? 'UNKNOWN_VALUE';
if ('trigger' === $type) {
return WebhookTrigger::from($value)->name;
}
if ('response' === $type) {
return WebhookResponse::from($value)->name;
}
return WebhookDelivery::from($value)->name;
}
}

View File

@ -814,10 +814,10 @@ class FireflyValidator extends Validator
public function validateUniqueWebhook($value, $parameters): bool
{
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'));
$triggers = array_flip(Webhook::getTriggers());
$responses = array_flip(Webhook::getResponses());
$deliveries = array_flip(Webhook::getDeliveries());
// integers
$trigger = $triggers[$this->data['trigger']] ?? 0;

View File

@ -82,6 +82,9 @@ use FireflyIII\TransactionRules\Actions\SetNotes;
use FireflyIII\TransactionRules\Actions\SetSourceAccount;
use FireflyIII\TransactionRules\Actions\UpdatePiggybank;
use FireflyIII\User;
use FireflyIII\Enums\WebhookResponse as WebhookResponseEnum;
use FireflyIII\Enums\WebhookDelivery as WebhookDeliveryEnum;
use FireflyIII\Enums\WebhookTrigger as WebhookTriggerEnum;
/*
@ -727,20 +730,7 @@ return [
'recurrence_total', 'recurrence_count',
],
'webhooks' => [
'max_attempts' => env('WEBHOOK_MAX_ATTEMPTS', 3),
'triggers' => [
Webhook::TRIGGER_STORE_TRANSACTION => 'TRIGGER_STORE_TRANSACTION',
Webhook::TRIGGER_UPDATE_TRANSACTION => 'TRIGGER_UPDATE_TRANSACTION',
Webhook::TRIGGER_DESTROY_TRANSACTION => 'TRIGGER_DESTROY_TRANSACTION',
],
'responses' => [
Webhook::RESPONSE_TRANSACTIONS => 'RESPONSE_TRANSACTIONS',
Webhook::RESPONSE_ACCOUNTS => 'RESPONSE_ACCOUNTS',
Webhook::RESPONSE_NONE => 'RESPONSE_NONE',
],
'deliveries' => [
Webhook::DELIVERY_JSON => 'DELIVERY_JSON',
],
'max_attempts' => env('WEBHOOK_MAX_ATTEMPTS', 3)
],
'can_have_virtual_amounts' => [AccountType::ASSET],
'can_have_opening_balance' => [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD],

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;