|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use ProcessMaker\Jobs\EvaluateProcessRetentionJob; |
| 7 | +use ProcessMaker\Models\Process; |
| 8 | +use ProcessMaker\Models\ProcessCategory; |
| 9 | +use ProcessMaker\Services\CaseRetentionTierService; |
| 10 | + |
| 11 | +class EvaluateCaseRetention extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'cases:retention:evaluate'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Evaluate and delete cases past their retention period'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Execute the console command. |
| 29 | + */ |
| 30 | + public function handle() |
| 31 | + { |
| 32 | + // Only run if case retention policy is enabled |
| 33 | + $enabled = config('app.case_retention_policy_enabled', false); |
| 34 | + if (!$enabled) { |
| 35 | + $this->info('Case retention policy is disabled'); |
| 36 | + $this->error('Skipping case retention evaluation'); |
| 37 | + |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $this->info('Case retention policy is enabled'); |
| 42 | + $this->info('Dispatching retention evaluation jobs for all processes'); |
| 43 | + // Get the allowed periods for the current tier (support for downgrading to a lower tier) |
| 44 | + $tierAllowedPeriods = CaseRetentionTierService::allowedPeriodsForCurrentTier(); |
| 45 | + |
| 46 | + // Get system category IDs to exclude |
| 47 | + $systemCategoryIds = ProcessCategory::where('is_system', true)->pluck('id'); |
| 48 | + |
| 49 | + // Exclude processes that are templates or in system categories |
| 50 | + $jobCount = 0; |
| 51 | + $query = Process::where('is_template', '!=', 1); |
| 52 | + |
| 53 | + // Exclude processes in system categories |
| 54 | + if ($systemCategoryIds->isNotEmpty()) { |
| 55 | + $query->where(function ($q) use ($systemCategoryIds) { |
| 56 | + $q->where(function ($subQuery) use ($systemCategoryIds) { |
| 57 | + $subQuery->whereNotIn('process_category_id', $systemCategoryIds) |
| 58 | + ->orWhereNull('process_category_id'); |
| 59 | + }); |
| 60 | + }) |
| 61 | + ->whereDoesntHave('categories', function ($q) use ($systemCategoryIds) { |
| 62 | + // Exclude processes with any category assignment to system categories |
| 63 | + $q->whereIn('process_categories.id', $systemCategoryIds); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + $query->chunkById(100, function ($processes) use (&$jobCount, $tierAllowedPeriods) { |
| 68 | + foreach ($processes as $process) { |
| 69 | + dispatch(new EvaluateProcessRetentionJob($process->id, $tierAllowedPeriods)); |
| 70 | + $jobCount++; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + $this->info("Dispatched {$jobCount} retention evaluation job(s) to the queue"); |
| 75 | + $this->info('Jobs will be processed asynchronously by queue workers'); |
| 76 | + } |
| 77 | +} |
0 commit comments