39 lines
1.6 KiB
PHP
39 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
use IriksIT\Manager\Models\Lead;
|
|
use IriksIT\Manager\Models\LeadBlock;
|
|
|
|
/**
|
|
* Removes what a privacy statement promises to remove: a request that did not become a job after `keep_days` (a year), spam after
|
|
* `keep_spam_days` (30), and blocks that ran out. A request with the outcome "won" is a customer file: it is never removed here.
|
|
*/
|
|
class LeadsPruneCommand extends Command
|
|
{
|
|
protected $signature = 'manager:leads-prune {--dry-run : Only count}';
|
|
|
|
protected $description = 'Remove old requests, old spam and expired blocks';
|
|
|
|
public function handle(): int
|
|
{
|
|
$old = Lead::query()->where(fn ($q) => $q->whereNull('outcome')->orWhere('outcome', '!=', Lead::WON))
|
|
->where('folder', '!=', Lead::SPAM)->where('created_at', '<', now()->subDays(max(1, (int) config('manager.leads.keep_days', 365))));
|
|
$spam = Lead::query()->where('folder', Lead::SPAM)->where('created_at', '<', now()->subDays(max(1, (int) config('manager.leads.keep_spam_days', 30))));
|
|
$blocks = LeadBlock::query()->whereNotNull('expires_at')->where('expires_at', '<', now());
|
|
|
|
$counts = ['requests' => $old->count(), 'spam' => $spam->count(), 'blocks' => $blocks->count()];
|
|
|
|
if (! $this->option('dry-run')) {
|
|
$old->delete();
|
|
$spam->delete();
|
|
$blocks->delete();
|
|
}
|
|
|
|
$this->components->info(($this->option('dry-run') ? 'Would remove' : 'Removed')." {$counts['requests']} requests, {$counts['spam']} spam, {$counts['blocks']} blocks.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|