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