Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions app/Console/Commands/CertificateGenerateWindow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace App\Console\Commands;

use App\CertificateExcellence;
use App\Excellence;
use Illuminate\Console\Command;

class CertificateGenerateWindow extends Command
{
protected $signature = 'certificate:generate-window
{--edition=2025 : Target edition year}
{--type=excellence : excellence|super-organiser}
{--limit=500 : Max recipients to process in this run}
{--include-failed : Include rows with previous generation errors}';

protected $description = 'Generate certificates in controlled windows (e.g. 500 at a time)';

public function handle(): int
{
$edition = (int) $this->option('edition');
$limit = max(1, (int) $this->option('limit'));
$includeFailed = (bool) $this->option('include-failed');
$typeInput = strtolower(trim((string) $this->option('type')));
$type = match ($typeInput) {
'excellence' => 'Excellence',
'super-organiser', 'superorganiser' => 'SuperOrganiser',
default => null,
};

if ($type === null) {
$this->error("Invalid --type value: {$typeInput}. Use 'excellence' or 'super-organiser'.");
return self::FAILURE;
}

$query = Excellence::query()
->where('edition', $edition)
->where('type', $type)
->where(function ($q) use ($includeFailed) {
$q->whereNull('certificate_url');
if (! $includeFailed) {
$q->whereNull('certificate_generation_error');
}
})
->with('user')
->orderBy('id')
->limit($limit);

$rows = $query->get();
if ($rows->isEmpty()) {
$this->info('No pending recipients found for this window.');
return self::SUCCESS;
}

$this->info("Generating {$rows->count()} {$type} certificates for edition {$edition}...");
$bar = $this->output->createProgressBar($rows->count());
$bar->start();

$ok = 0;
$failed = 0;

foreach ($rows as $excellence) {
$bar->advance();
$user = $excellence->user;
if (! $user) {
$failed++;
$excellence->update(['certificate_generation_error' => 'User missing.']);
continue;
}

$name = $excellence->name_for_certificate ?? trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? ''));
$name = $name !== '' ? $name : 'Unknown';
$certType = $type === 'SuperOrganiser' ? 'super-organiser' : 'excellence';
$numberOfActivities = $type === 'SuperOrganiser' ? (int) $user->activities($edition) : 0;

try {
$cert = new CertificateExcellence(
$edition,
$name,
$certType,
$numberOfActivities,
(int) $user->id,
(string) ($user->email ?? '')
);
$url = $cert->generate();
$excellence->update([
'certificate_url' => $url,
'certificate_generation_error' => null,
]);
$ok++;
} catch (\Throwable $e) {
$failed++;
$excellence->update([
'certificate_generation_error' => $e->getMessage(),
]);
}
}

$bar->finish();
$this->newLine(2);
$this->info("Window complete. Success: {$ok}, Failed: {$failed}.");
$this->line('Run the same command again to process the next window.');

return self::SUCCESS;
}
}
98 changes: 98 additions & 0 deletions app/Console/Commands/CertificateSendWindow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Console\Commands;

use App\Excellence;
use App\Mail\NotifySuperOrganiser;
use App\Mail\NotifyWinner;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class CertificateSendWindow extends Command
{
protected $signature = 'certificate:send-window
{--edition=2025 : Target edition year}
{--type=excellence : excellence|super-organiser}
{--limit=500 : Max recipients to send in this run}
{--include-send-failed : Include rows that previously failed sending}';

protected $description = 'Send certificate emails in controlled windows (e.g. 500 at a time)';

public function handle(): int
{
$edition = (int) $this->option('edition');
$limit = max(1, (int) $this->option('limit'));
$includeSendFailed = (bool) $this->option('include-send-failed');
$typeInput = strtolower(trim((string) $this->option('type')));
$type = match ($typeInput) {
'excellence' => 'Excellence',
'super-organiser', 'superorganiser' => 'SuperOrganiser',
default => null,
};

if ($type === null) {
$this->error("Invalid --type value: {$typeInput}. Use 'excellence' or 'super-organiser'.");
return self::FAILURE;
}

$query = Excellence::query()
->where('edition', $edition)
->where('type', $type)
->where(function ($q) use ($includeSendFailed) {
$q->whereNull('notified_at');
if ($includeSendFailed) {
$q->orWhereNotNull('certificate_sent_error');
}
})
->with('user')
->orderBy('id')
->limit($limit);

$rows = $query->get();
if ($rows->isEmpty()) {
$this->info('No recipients found for this send window.');
return self::SUCCESS;
}

$this->info("Queueing {$rows->count()} {$type} emails for edition {$edition}...");
$bar = $this->output->createProgressBar($rows->count());
$bar->start();

$queued = 0;
$failed = 0;

foreach ($rows as $excellence) {
$bar->advance();
$user = $excellence->user;
if (! $user || ! $user->email) {
$failed++;
$excellence->update(['certificate_sent_error' => 'No user or email']);
continue;
}

try {
if ($type === 'SuperOrganiser') {
Mail::to($user->email)->queue(new NotifySuperOrganiser($user, $edition, $excellence->certificate_url));
} else {
Mail::to($user->email)->queue(new NotifyWinner($user, $edition, $excellence->certificate_url));
}
$excellence->update([
'notified_at' => Carbon::now(),
'certificate_sent_error' => null,
]);
$queued++;
} catch (\Throwable $e) {
$failed++;
$excellence->update(['certificate_sent_error' => $e->getMessage()]);
}
}

$bar->finish();
$this->newLine(2);
$this->info("Send window complete. Queued: {$queued}, Failed: {$failed}.");
$this->line('Run again to process the next send window.');

return self::SUCCESS;
}
}
Loading