Skip to content

Commit 650e02b

Browse files
committed
feat: add webhooks:cleanup command with configurable retention
- New artisan command: webhooks:cleanup [--days=N] - Config option retention_days (null = keep forever) - Force-deletes webhooks older than retention period - Registered via Spatie package tools hasCommand()
1 parent 6ff9466 commit 650e02b

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

config/filament-webhooks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
'model' => InboundWebhook::class,
1717
'providers_enum' => InboundWebhookSource::class,
1818
'view_any' => true,
19+
20+
'retention_days' => null, // null = keep forever, e.g. 30 = delete after 30 days
1921
];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Basement\Webhooks\Commands;
6+
7+
use Basement\Webhooks\Models\InboundWebhook;
8+
use Illuminate\Console\Command;
9+
10+
final class CleanupWebhooksCommand extends Command
11+
{
12+
protected $signature = 'webhooks:cleanup {--days= : Number of days to retain}';
13+
14+
protected $description = 'Delete inbound webhooks older than the configured retention period';
15+
16+
public function handle(): int
17+
{
18+
$days = $this->option('days') ?? config('filament-webhooks.retention_days');
19+
20+
if ($days === null) {
21+
$this->info('No retention period configured. Skipping cleanup.');
22+
23+
return self::SUCCESS;
24+
}
25+
26+
$days = (int) $days;
27+
28+
/** @var class-string<InboundWebhook> $modelClass */
29+
$modelClass = config('filament-webhooks.model', InboundWebhook::class);
30+
31+
$count = $modelClass::where('created_at', '<', now()->subDays($days))->forceDelete();
32+
33+
$this->info("Deleted {$count} webhooks older than {$days} days.");
34+
35+
return self::SUCCESS;
36+
}
37+
}

src/WebhooksServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Basement\Webhooks;
66

77
use Basement\Webhooks\Actions\StoreInboundWebhook;
8+
use Basement\Webhooks\Commands\CleanupWebhooksCommand;
89
use Basement\Webhooks\Contracts\StoresInboundWebhook;
910
use Basement\Webhooks\Models\InboundWebhook;
1011
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -31,6 +32,7 @@ public function configurePackage(Package $package): void
3132
$package
3233
->name('filament-webhooks')
3334
->hasConfigFile()
35+
->hasCommand(CleanupWebhooksCommand::class)
3436
->discoversMigrations();
3537
}
3638

0 commit comments

Comments
 (0)