Skip to content

Commit fbef6d4

Browse files
authored
Merge pull request #3446 from codeeu/dev
Dev
2 parents d5e48d5 + f8d94db commit fbef6d4

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ function ($view) {
154154

155155
//Livewire::paginationView('vendor.livewire.pagination');
156156

157+
$this->commands([\App\Console\Commands\CertificateReassignUser::class]);
158+
157159
$this->bootAuth();
158160
$this->bootEvent();
159161
$this->bootNovaMainDashboardRoute();

0 commit comments

Comments
 (0)