43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\ProductUsageEvent;
|
|
use Illuminate\Console\Command;
|
|
|
|
class PruneProductUsageEventsCommand extends Command
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $signature = 'tenantpilot:product-usage:prune {--days= : Number of days to retain product usage events}';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $description = 'Delete product usage events older than the retention period';
|
|
|
|
public function handle(): int
|
|
{
|
|
$days = (int) ($this->option('days') ?: config('tenantpilot.product_usage_event_retention_days', 90));
|
|
|
|
if ($days < 1) {
|
|
$this->error('Retention days must be at least 1.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$cutoff = now()->subDays($days);
|
|
|
|
$deleted = ProductUsageEvent::query()
|
|
->where('occurred_at', '<', $cutoff)
|
|
->delete();
|
|
|
|
$this->info("Deleted {$deleted} product usage event(s) older than {$days} days.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|