This commit is contained in:
Vehikl 2025-08-21 17:24:22 -04:00
parent dd2fdd15a1
commit c948314e21
5 changed files with 139 additions and 10 deletions

View File

@ -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')
CheckboxList::make('webhookEvents')
->relationship('webhookEvents')
->live()
->options(fn () => WebhookConfiguration::filamentCheckboxList())
->searchable()
->bulkToggleable()
->columns(3)
->columnSpanFull()
->required(),
]),
->required()
->before(function (array $state) {
dd($state);
}),
]),*/
]);
}
@ -343,4 +349,12 @@ class WebhookResource extends Resource
'edit' => Pages\EditWebhookConfiguration::route('/{record}/edit'),
];
}
/** @return class-string<RelationManager>[] */
public static function getDefaultRelations(): array
{
return [
EventsRelationManager::class,
];
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Filament\Admin\Resources\WebhookResource\RelationManagers;
use App\Models\WebhookConfiguration;
use App\Models\WebhookEvent;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
/**
* @method WebhookConfiguration getOwnerRecord()
*/
class EventsRelationManager extends RelationManager
{
protected static string $relationship = 'webhookEvents';
public function table(Table $table): Table
{
return $table
->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)),
]);
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* @property string $name
*/
class WebhookEvent extends Model
{
public $timestamps = false;
protected $fillable = [
'name',
];
public function webhookConfigurationEvent(): BelongsToMany
{
return $this->belongsToMany(WebhookConfiguration::class, 'webhook_configurations_events', 'event_id', 'configuration_id');
}
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
/*Schema::table('webhook_configurations', function (Blueprint $table) {
$table->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');
}
};