|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Excellence; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | + |
| 9 | +class CertificateOrphanedExcellence extends Command |
| 10 | +{ |
| 11 | + protected $signature = 'certificate:orphaned-excellence |
| 12 | + {--edition=2025 : Target edition year} |
| 13 | + {--type=all : excellence|super-organiser|all} |
| 14 | + {--ids= : Comma-separated excellence IDs to check (optional)} |
| 15 | + {--export= : CSV path to export orphaned rows} |
| 16 | + {--delete : Delete orphaned excellence rows (cannot send cert without user)}'; |
| 17 | + |
| 18 | + protected $description = 'List or fix Excellence rows whose user record is missing (Missing related user record)'; |
| 19 | + |
| 20 | + public function handle(): int |
| 21 | + { |
| 22 | + $edition = (int) $this->option('edition'); |
| 23 | + $typeOption = strtolower(trim((string) $this->option('type'))); |
| 24 | + $idsOption = trim((string) $this->option('ids')); |
| 25 | + $exportPath = trim((string) $this->option('export')); |
| 26 | + $delete = (bool) $this->option('delete'); |
| 27 | + |
| 28 | + $types = $this->resolveTypes($typeOption); |
| 29 | + if ($types === null) { |
| 30 | + $this->error("Invalid --type value: {$typeOption}. Use 'excellence', 'super-organiser', or 'all'."); |
| 31 | + return self::FAILURE; |
| 32 | + } |
| 33 | + |
| 34 | + $query = Excellence::query() |
| 35 | + ->where('edition', $edition) |
| 36 | + ->whereIn('type', $types) |
| 37 | + ->whereNotExists(function ($q) { |
| 38 | + $q->select(DB::raw(1)) |
| 39 | + ->from('users') |
| 40 | + ->whereColumn('users.id', 'excellences.user_id'); |
| 41 | + }) |
| 42 | + ->orderBy('id'); |
| 43 | + |
| 44 | + if ($idsOption !== '') { |
| 45 | + $ids = array_filter(array_map('intval', explode(',', $idsOption))); |
| 46 | + if (! empty($ids)) { |
| 47 | + $query->whereIn('id', $ids); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + $rows = $query->get(); |
| 52 | + if ($rows->isEmpty()) { |
| 53 | + $this->info('No orphaned Excellence rows found (all have a related user).'); |
| 54 | + return self::SUCCESS; |
| 55 | + } |
| 56 | + |
| 57 | + $this->warn('Orphaned Excellence rows (user_id points to missing user): ' . $rows->count()); |
| 58 | + $this->table( |
| 59 | + ['id', 'user_id', 'type', 'edition', 'name_for_certificate'], |
| 60 | + $rows->map(fn ($e) => [$e->id, $e->user_id, $e->type, $e->edition, $e->name_for_certificate ?? ''])->toArray() |
| 61 | + ); |
| 62 | + |
| 63 | + if ($exportPath !== '') { |
| 64 | + $path = str_starts_with($exportPath, '/') ? $exportPath : base_path($exportPath); |
| 65 | + $dir = dirname($path); |
| 66 | + if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) { |
| 67 | + $this->error("Failed to create directory: {$dir}"); |
| 68 | + return self::FAILURE; |
| 69 | + } |
| 70 | + $fh = @fopen($path, 'wb'); |
| 71 | + if (! $fh) { |
| 72 | + $this->error("Failed to open: {$path}"); |
| 73 | + return self::FAILURE; |
| 74 | + } |
| 75 | + fputcsv($fh, ['excellence_id', 'user_id', 'type', 'edition', 'name_for_certificate']); |
| 76 | + foreach ($rows as $e) { |
| 77 | + fputcsv($fh, [$e->id, $e->user_id, $e->type, $e->edition, $e->name_for_certificate ?? '']); |
| 78 | + } |
| 79 | + fclose($fh); |
| 80 | + $this->info("Exported: {$path}"); |
| 81 | + } |
| 82 | + |
| 83 | + if ($delete) { |
| 84 | + $ids = $rows->pluck('id')->toArray(); |
| 85 | + Excellence::query()->whereIn('id', $ids)->delete(); |
| 86 | + $this->info('Deleted ' . count($ids) . ' orphaned Excellence row(s). They will no longer appear in preflight or send.'); |
| 87 | + } else { |
| 88 | + $this->line('Tip: Use --delete to remove these rows so they are excluded from certificate generation/send.'); |
| 89 | + } |
| 90 | + |
| 91 | + return self::SUCCESS; |
| 92 | + } |
| 93 | + |
| 94 | + private function resolveTypes(string $typeOption): ?array |
| 95 | + { |
| 96 | + return match ($typeOption) { |
| 97 | + 'all' => ['Excellence', 'SuperOrganiser'], |
| 98 | + 'excellence' => ['Excellence'], |
| 99 | + 'super-organiser', 'superorganiser' => ['SuperOrganiser'], |
| 100 | + default => null, |
| 101 | + }; |
| 102 | + } |
| 103 | +} |
0 commit comments