|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Excellence; |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +class CertificateFailuresReport extends Command |
| 9 | +{ |
| 10 | + protected $signature = 'certificate:failures-report |
| 11 | + {--edition=2025 : Target edition year} |
| 12 | + {--type=all : excellence|super-organiser|all} |
| 13 | + {--limit=0 : Show first N rows (0 = all)} |
| 14 | + {--export= : Optional CSV export path (absolute or relative to project root)}'; |
| 15 | + |
| 16 | + protected $description = 'List/export certificate generation failures for review before sending emails'; |
| 17 | + |
| 18 | + public function handle(): int |
| 19 | + { |
| 20 | + $edition = (int) $this->option('edition'); |
| 21 | + $typeOption = strtolower(trim((string) $this->option('type'))); |
| 22 | + $limit = max(0, (int) $this->option('limit')); |
| 23 | + $exportPath = trim((string) $this->option('export')); |
| 24 | + |
| 25 | + $types = $this->resolveTypes($typeOption); |
| 26 | + if ($types === null) { |
| 27 | + $this->error("Invalid --type value: {$typeOption}. Use 'excellence', 'super-organiser', or 'all'."); |
| 28 | + return self::FAILURE; |
| 29 | + } |
| 30 | + |
| 31 | + $rows = Excellence::query() |
| 32 | + ->where('edition', $edition) |
| 33 | + ->whereIn('type', $types) |
| 34 | + ->whereNotNull('certificate_generation_error') |
| 35 | + ->with('user') |
| 36 | + ->orderBy('type') |
| 37 | + ->orderBy('id') |
| 38 | + ->get(); |
| 39 | + |
| 40 | + if ($rows->isEmpty()) { |
| 41 | + $this->info("No generation failures found for edition {$edition}, type {$typeOption}."); |
| 42 | + return self::SUCCESS; |
| 43 | + } |
| 44 | + |
| 45 | + $displayRows = $limit > 0 ? $rows->take($limit) : $rows; |
| 46 | + $table = $displayRows->map(function (Excellence $e) { |
| 47 | + $name = $e->name_for_certificate; |
| 48 | + if (! $name && $e->user) { |
| 49 | + $name = trim(($e->user->firstname ?? '') . ' ' . ($e->user->lastname ?? '')); |
| 50 | + } |
| 51 | + return [ |
| 52 | + 'id' => $e->id, |
| 53 | + 'type' => $e->type, |
| 54 | + 'user_id' => $e->user_id, |
| 55 | + 'email' => $e->user?->email ?? '', |
| 56 | + 'name' => $name ?? '', |
| 57 | + 'error' => $e->certificate_generation_error, |
| 58 | + ]; |
| 59 | + })->values()->all(); |
| 60 | + |
| 61 | + $this->info('Generation failures: ' . $rows->count()); |
| 62 | + if ($limit > 0 && $rows->count() > $limit) { |
| 63 | + $this->line("Showing first {$limit} rows."); |
| 64 | + } |
| 65 | + $this->table(['id', 'type', 'user_id', 'email', 'name', 'error'], $table); |
| 66 | + |
| 67 | + if ($exportPath !== '') { |
| 68 | + $fullPath = $this->resolvePath($exportPath); |
| 69 | + $dir = dirname($fullPath); |
| 70 | + if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) { |
| 71 | + $this->error("Failed to create directory: {$dir}"); |
| 72 | + return self::FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + $fh = @fopen($fullPath, 'wb'); |
| 76 | + if (! $fh) { |
| 77 | + $this->error("Failed to open export file: {$fullPath}"); |
| 78 | + return self::FAILURE; |
| 79 | + } |
| 80 | + |
| 81 | + fputcsv($fh, ['id', 'type', 'edition', 'user_id', 'email', 'name_for_certificate', 'certificate_generation_error']); |
| 82 | + foreach ($rows as $e) { |
| 83 | + $name = $e->name_for_certificate; |
| 84 | + if (! $name && $e->user) { |
| 85 | + $name = trim(($e->user->firstname ?? '') . ' ' . ($e->user->lastname ?? '')); |
| 86 | + } |
| 87 | + fputcsv($fh, [ |
| 88 | + $e->id, |
| 89 | + $e->type, |
| 90 | + $e->edition, |
| 91 | + $e->user_id, |
| 92 | + $e->user?->email ?? '', |
| 93 | + $name ?? '', |
| 94 | + $e->certificate_generation_error, |
| 95 | + ]); |
| 96 | + } |
| 97 | + fclose($fh); |
| 98 | + $this->info("Exported CSV: {$fullPath}"); |
| 99 | + } |
| 100 | + |
| 101 | + $this->line('Note: send flows already exclude rows without certificate_url, so failed generation rows are not emailed.'); |
| 102 | + return self::SUCCESS; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<int, string>|null |
| 107 | + */ |
| 108 | + private function resolveTypes(string $typeOption): ?array |
| 109 | + { |
| 110 | + return match ($typeOption) { |
| 111 | + 'all' => ['Excellence', 'SuperOrganiser'], |
| 112 | + 'excellence' => ['Excellence'], |
| 113 | + 'super-organiser', 'superorganiser' => ['SuperOrganiser'], |
| 114 | + default => null, |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + private function resolvePath(string $path): string |
| 119 | + { |
| 120 | + if (str_starts_with($path, '/')) { |
| 121 | + return $path; |
| 122 | + } |
| 123 | + return base_path($path); |
| 124 | + } |
| 125 | +} |
0 commit comments