|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Excellence; |
| 6 | +use App\User; |
| 7 | +use Illuminate\Console\Command; |
| 8 | + |
| 9 | +class CertificateReassignUser extends Command |
| 10 | +{ |
| 11 | + protected $signature = 'certificate:reassign-user |
| 12 | + {--from-email= : Current account email (certificates will be moved FROM this user)} |
| 13 | + {--to-email= : Target account email (certificates will be moved TO this user)} |
| 14 | + {--dry-run : List what would be moved, do not update}'; |
| 15 | + |
| 16 | + protected $description = 'Reassign all Excellence/Super Organiser certificate rows from one user to another (e.g. after email change)'; |
| 17 | + |
| 18 | + public function handle(): int |
| 19 | + { |
| 20 | + $fromEmail = trim((string) $this->option('from-email')); |
| 21 | + $toEmail = trim((string) $this->option('to-email')); |
| 22 | + $dryRun = (bool) $this->option('dry-run'); |
| 23 | + |
| 24 | + if ($fromEmail === '' || $toEmail === '') { |
| 25 | + $this->error('Provide both --from-email and --to-email.'); |
| 26 | + return self::FAILURE; |
| 27 | + } |
| 28 | + |
| 29 | + if (strtolower($fromEmail) === strtolower($toEmail)) { |
| 30 | + $this->error('From and to email must be different.'); |
| 31 | + return self::FAILURE; |
| 32 | + } |
| 33 | + |
| 34 | + $fromUser = User::where('email', $fromEmail)->first(); |
| 35 | + $toUser = User::where('email', $toEmail)->first(); |
| 36 | + |
| 37 | + if (! $fromUser) { |
| 38 | + $this->error("User not found: {$fromEmail}"); |
| 39 | + return self::FAILURE; |
| 40 | + } |
| 41 | + if (! $toUser) { |
| 42 | + $this->error("User not found: {$toEmail}"); |
| 43 | + return self::FAILURE; |
| 44 | + } |
| 45 | + |
| 46 | + $rows = Excellence::where('user_id', $fromUser->id)->orderBy('edition')->orderBy('type')->get(); |
| 47 | + |
| 48 | + if ($rows->isEmpty()) { |
| 49 | + $this->info("No certificate rows found for {$fromEmail} (user_id {$fromUser->id}). Nothing to move."); |
| 50 | + return self::SUCCESS; |
| 51 | + } |
| 52 | + |
| 53 | + $this->info("Found {$rows->count()} certificate row(s) for {$fromEmail} (user_id {$fromUser->id})."); |
| 54 | + $this->info("Target: {$toEmail} (user_id {$toUser->id})."); |
| 55 | + $this->newLine(); |
| 56 | + |
| 57 | + $table = $rows->map(fn (Excellence $e) => [ |
| 58 | + $e->id, |
| 59 | + $e->edition, |
| 60 | + $e->type, |
| 61 | + $e->certificate_url ? 'Yes' : 'No', |
| 62 | + $e->notified_at ? 'Yes' : 'No', |
| 63 | + ])->toArray(); |
| 64 | + $this->table(['id', 'edition', 'type', 'has PDF', 'sent'], $table); |
| 65 | + |
| 66 | + if ($dryRun) { |
| 67 | + $this->newLine(); |
| 68 | + $this->warn('Dry run: no changes made. Run without --dry-run to reassign.'); |
| 69 | + return self::SUCCESS; |
| 70 | + } |
| 71 | + |
| 72 | + if (! $this->confirm('Reassign these rows to ' . $toEmail . '?')) { |
| 73 | + $this->info('Aborted.'); |
| 74 | + return self::SUCCESS; |
| 75 | + } |
| 76 | + |
| 77 | + $updated = Excellence::where('user_id', $fromUser->id)->update(['user_id' => $toUser->id]); |
| 78 | + |
| 79 | + $this->info("Done. Reassigned {$updated} row(s) to {$toEmail}. Certificates will now appear under the target account."); |
| 80 | + return self::SUCCESS; |
| 81 | + } |
| 82 | +} |
0 commit comments