diff --git a/app/Filament/Admin/Resources/WebhookResource.php b/app/Filament/Admin/Resources/WebhookResource.php index f11a854ba..15d44b943 100644 --- a/app/Filament/Admin/Resources/WebhookResource.php +++ b/app/Filament/Admin/Resources/WebhookResource.php @@ -4,6 +4,7 @@ namespace App\Filament\Admin\Resources; use App\Filament\Admin\Resources\WebhookResource\Pages; use App\Filament\Admin\Resources\WebhookResource\Pages\EditWebhookConfiguration; +use App\Filament\Admin\Resources\WebhookResource\RelationManagers\EventsRelationManager; use App\Livewire\AlertBanner; use App\Models\WebhookConfiguration; use App\Traits\Filament\CanCustomizePages; @@ -23,6 +24,7 @@ use Filament\Forms\Components\Actions\Action; use Filament\Forms\Form; use Filament\Resources\Pages\PageRegistration; use Filament\Forms\Get; +use Filament\Resources\RelationManagers\RelationManager; use Filament\Resources\Resource; use Filament\Forms\Set; use Filament\Tables\Actions\DeleteBulkAction; @@ -161,17 +163,21 @@ class WebhookResource extends Resource ->view('filament.components.webhooksection') ->aside() ->formBefore(), - Section::make(trans('admin/webhook.events')) + /*Section::make(trans('admin/webhook.events')) ->schema([ - CheckboxList::make('events') - ->live() - ->options(fn () => WebhookConfiguration::filamentCheckboxList()) - ->searchable() - ->bulkToggleable() - ->columns(3) - ->columnSpanFull() - ->required(), - ]), + CheckboxList::make('webhookEvents') + ->relationship('webhookEvents') + ->live() + ->options(fn () => WebhookConfiguration::filamentCheckboxList()) + ->searchable() + ->bulkToggleable() + ->columns(3) + ->columnSpanFull() + ->required() + ->before(function (array $state) { + dd($state); + }), + ]),*/ ]); } @@ -343,4 +349,12 @@ class WebhookResource extends Resource 'edit' => Pages\EditWebhookConfiguration::route('/{record}/edit'), ]; } + + /** @return class-string[] */ + public static function getDefaultRelations(): array + { + return [ + EventsRelationManager::class, + ]; + } } diff --git a/app/Filament/Admin/Resources/WebhookResource/RelationManagers/EventsRelationManager.php b/app/Filament/Admin/Resources/WebhookResource/RelationManagers/EventsRelationManager.php new file mode 100644 index 000000000..144c8d612 --- /dev/null +++ b/app/Filament/Admin/Resources/WebhookResource/RelationManagers/EventsRelationManager.php @@ -0,0 +1,49 @@ +heading('') + ->columns([ + TextColumn::make('id'), + TextColumn::make('name'), + ]) + ->headerActions([ + Action::make('create') + ->form(fn () => [ + TextInput::make('name') + ->inlineLabel() + ->live() + ->required(), + ]) + ->action(function (array $data) { + $id = WebhookEvent::firstOrCreate([ + 'name' => $data['name'], + ]); + $this->getOwnerRecord()->webhookEvents()->sync([$id]); + }), + ]) + ->actions([ + DeleteAction::make() + ->authorize(fn (WebhookConfiguration $config) => auth()->user()->can('delete', $config)), + ]); + } +} diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index 7c8a8fab0..b123cd04e 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Jobs\ProcessWebhook; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; @@ -231,4 +232,9 @@ class WebhookConfiguration extends Model 'id' => 2, ]; } + + public function webhookEvents(): BelongsToMany + { + return $this->belongsToMany(WebhookEvent::class, 'webhook_configurations_events', 'configuration_id', 'event_id'); + } } diff --git a/app/Models/WebhookEvent.php b/app/Models/WebhookEvent.php new file mode 100644 index 000000000..7905da8a4 --- /dev/null +++ b/app/Models/WebhookEvent.php @@ -0,0 +1,23 @@ +belongsToMany(WebhookConfiguration::class, 'webhook_configurations_events', 'event_id', 'configuration_id'); + } +} diff --git a/database/migrations/2025_08_21_194750_create_webhook_events_table.php b/database/migrations/2025_08_21_194750_create_webhook_events_table.php new file mode 100644 index 000000000..862bec661 --- /dev/null +++ b/database/migrations/2025_08_21_194750_create_webhook_events_table.php @@ -0,0 +1,37 @@ +dropColumn('events'); + });*/ // TODO: convert old format + + Schema::create('webhook_events', function (Blueprint $table) { + $table->id(); + $table->string('name'); + }); + Schema::create('webhook_configurations_events', function (Blueprint $table) { + $table->foreignId('event_id')->references('id')->on('webhook_events')->onDelete('cascade'); + $table->foreignId('configuration_id')->references('id')->on('webhook_configurations')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('webhook_events'); + Schema::dropIfExists('webhook_configurations_events'); + } +};