mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 09:22:33 +00:00
Expand webhooks to support multiple delivery payloads, event triggers and responses.
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
private const TABLE_ALREADY_EXISTS = 'If this table exists already (see the error message), this is not a problem. Other errors? Please open a discussion on GitHub.';
|
||||
private const TABLE_ERROR = 'Could not create table "%s": %s';
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (!Schema::hasTable('webhook_triggers')) {
|
||||
Schema::create('webhook_triggers', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->smallInteger('key')->unsigned();
|
||||
$table->string('title', 100);
|
||||
$table->unique(['key', 'title']);
|
||||
});
|
||||
}
|
||||
if (!Schema::hasTable('webhook_responses')) {
|
||||
Schema::create('webhook_responses', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->smallInteger('key')->unsigned();
|
||||
$table->string('title', 100);
|
||||
$table->unique(['key', 'title']);
|
||||
});
|
||||
}
|
||||
if (!Schema::hasTable('webhook_deliveries')) {
|
||||
Schema::create('webhook_deliveries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->smallInteger('key')->unsigned();
|
||||
$table->string('title', 100);
|
||||
$table->unique(['key', 'title']);
|
||||
});
|
||||
}
|
||||
|
||||
// webhook_webhook_trigger
|
||||
if (!Schema::hasTable('webhook_webhook_trigger')) {
|
||||
try {
|
||||
Schema::create(
|
||||
'webhook_webhook_trigger',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('webhook_id', false, true);
|
||||
$table->bigInteger('webhook_trigger_id', false, true);
|
||||
$table->foreign('webhook_id')->references('id')->on('webhooks')->onDelete('cascade');
|
||||
$table->foreign('webhook_trigger_id','link_to_trigger')->references('id')->on('webhook_triggers')->onDelete('cascade');
|
||||
|
||||
// unique combi:
|
||||
$table->unique(['webhook_id', 'webhook_trigger_id']);
|
||||
}
|
||||
);
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error(sprintf(self::TABLE_ERROR, 'webhook_webhook_trigger', $e->getMessage()));
|
||||
app('log')->error(self::TABLE_ALREADY_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// webhook_webhook_response
|
||||
if (!Schema::hasTable('webhook_webhook_response')) {
|
||||
try {
|
||||
Schema::create(
|
||||
'webhook_webhook_response',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('webhook_id', false, true);
|
||||
$table->bigInteger('webhook_response_id', false, true);
|
||||
$table->foreign('webhook_id')->references('id')->on('webhooks')->onDelete('cascade');
|
||||
$table->foreign('webhook_response_id','link_to_response')->references('id')->on('webhook_responses')->onDelete('cascade');
|
||||
|
||||
// unique combi:
|
||||
$table->unique(['webhook_id', 'webhook_response_id']);
|
||||
}
|
||||
);
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error(sprintf(self::TABLE_ERROR, 'webhook_webhook_response', $e->getMessage()));
|
||||
app('log')->error(self::TABLE_ALREADY_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
// webhook_webhook_delivery
|
||||
if (!Schema::hasTable('webhook_webhook_delivery')) {
|
||||
try {
|
||||
Schema::create(
|
||||
'webhook_webhook_delivery',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('webhook_id', false, true);
|
||||
$table->bigInteger('webhook_delivery_id', false, true);
|
||||
$table->foreign('webhook_id')->references('id')->on('webhooks')->onDelete('cascade');
|
||||
$table->foreign('webhook_delivery_id','link_to_delivery')->references('id')->on('webhook_deliveries')->onDelete('cascade');
|
||||
|
||||
// unique combi:
|
||||
$table->unique(['webhook_id', 'webhook_delivery_id']);
|
||||
}
|
||||
);
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error(sprintf(self::TABLE_ERROR, 'webhook_webhook_delivery', $e->getMessage()));
|
||||
app('log')->error(self::TABLE_ALREADY_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('webhook_webhook_delivery');
|
||||
Schema::dropIfExists('webhook_webhook_trigger');
|
||||
Schema::dropIfExists('webhook_webhook_response');
|
||||
|
||||
Schema::dropIfExists('webhook_triggers');
|
||||
Schema::dropIfExists('webhook_responses');
|
||||
Schema::dropIfExists('webhook_deliveries');
|
||||
}
|
||||
};
|
@@ -35,25 +35,10 @@ class AccountTypeSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$types = [
|
||||
AccountTypeEnum::DEFAULT->value,
|
||||
AccountTypeEnum::CASH->value,
|
||||
AccountTypeEnum::ASSET->value,
|
||||
AccountTypeEnum::EXPENSE->value,
|
||||
AccountTypeEnum::REVENUE->value,
|
||||
AccountTypeEnum::INITIAL_BALANCE->value,
|
||||
AccountTypeEnum::BENEFICIARY->value,
|
||||
AccountTypeEnum::IMPORT->value,
|
||||
AccountTypeEnum::LOAN->value,
|
||||
AccountTypeEnum::RECONCILIATION->value,
|
||||
AccountTypeEnum::DEBT->value,
|
||||
AccountTypeEnum::MORTGAGE->value,
|
||||
AccountTypeEnum::LIABILITY_CREDIT->value,
|
||||
];
|
||||
foreach ($types as $type) {
|
||||
if (null === AccountType::where('type', $type)->first()) {
|
||||
foreach(AccountTypeEnum::cases() as $type) {
|
||||
if (null === AccountType::where('type', $type->value)->first()) {
|
||||
try {
|
||||
AccountType::create(['type' => $type]);
|
||||
AccountType::create(['type' => $type->value]);
|
||||
} catch (PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
|
@@ -43,5 +43,6 @@ class DatabaseSeeder extends Seeder
|
||||
$this->call(ConfigSeeder::class);
|
||||
$this->call(UserRoleSeeder::class);
|
||||
$this->call(ExchangeRateSeeder::class);
|
||||
$this->call(WebhookDataSeeder::class);
|
||||
}
|
||||
}
|
||||
|
@@ -35,24 +35,17 @@ class TransactionTypeSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$types = [
|
||||
TransactionTypeEnum::WITHDRAWAL->value,
|
||||
TransactionTypeEnum::DEPOSIT->value,
|
||||
TransactionTypeEnum::TRANSFER->value,
|
||||
TransactionTypeEnum::OPENING_BALANCE->value,
|
||||
TransactionTypeEnum::RECONCILIATION->value,
|
||||
TransactionTypeEnum::INVALID->value,
|
||||
TransactionTypeEnum::LIABILITY_CREDIT->value,
|
||||
];
|
||||
|
||||
foreach ($types as $type) {
|
||||
if (null === TransactionType::where('type', $type)->first()) {
|
||||
/** @var TransactionTypeEnum $type */
|
||||
foreach (TransactionTypeEnum::cases() as $type) {
|
||||
if (null === TransactionType::where('type', $type->value)->first()) {
|
||||
try {
|
||||
TransactionType::create(['type' => $type]);
|
||||
TransactionType::create(['type' => $type->value]);
|
||||
} catch (PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -39,16 +39,11 @@ class UserRoleSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$roles = [];
|
||||
/** @var UserRoleEnum $role */
|
||||
foreach (UserRoleEnum::cases() as $role) {
|
||||
$roles[] = $role->value;
|
||||
}
|
||||
|
||||
/** @var string $role */
|
||||
foreach ($roles as $role) {
|
||||
if (null === UserRole::where('title', $role)->first()) {
|
||||
if (null === UserRole::where('title', $role->value)->first()) {
|
||||
try {
|
||||
UserRole::create(['title' => $role]);
|
||||
UserRole::create(['title' => $role->value]);
|
||||
} catch (PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
|
48
database/seeders/WebhookDataSeeder.php
Normal file
48
database/seeders/WebhookDataSeeder.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use FireflyIII\Enums\WebhookTrigger;
|
||||
use FireflyIII\Enums\WebhookResponse;
|
||||
use FireflyIII\Enums\WebhookDelivery;
|
||||
use FireflyIII\Models\WebhookTrigger as WebhookTriggerModel;
|
||||
use FireflyIII\Models\WebhookResponse as WebhookResponseModel;
|
||||
use FireflyIII\Models\WebhookDelivery as WebhookDeliveryModel;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class WebhookDataSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
foreach (WebhookTrigger::cases() as $trigger) {
|
||||
if (null === WebhookTriggerModel::where('key', $trigger->value)->where('title', $trigger->name)->first()) {
|
||||
try {
|
||||
WebhookTriggerModel::create(['key' => $trigger->value, 'title' => $trigger->name]);
|
||||
} catch (\PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (WebhookResponse::cases() as $response) {
|
||||
if (null === WebhookResponseModel::where('key', $response->value)->where('title', $response->name)->first()) {
|
||||
try {
|
||||
WebhookResponseModel::create(['key' => $response->value, 'title' => $response->name]);
|
||||
} catch (\PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (WebhookDelivery::cases() as $delivery) {
|
||||
if (null === WebhookDeliveryModel::where('key', $delivery->value)->where('title', $delivery->name)->first()) {
|
||||
try {
|
||||
WebhookDeliveryModel::create(['key' => $delivery->value, 'title' => $delivery->name]);
|
||||
} catch (\PDOException $e) {
|
||||
// @ignoreException
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user