Skip to content
Merged

Dev #3401

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
14 changes: 14 additions & 0 deletions app/CertificateExcellence.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public function generate()
return $s3path;
}

/**
* Dry-run style preflight: compile the certificate locally without S3 upload.
* Cleans up temp files regardless of success/failure.
*/
public function preflight(): void
{
try {
$this->customize_and_save_latex();
$this->run_pdf_creation();
} finally {
$this->clean_temp_files();
}
}

/**
* Clean up LaTeX artifacts for the generated file.
*/
Expand Down
170 changes: 170 additions & 0 deletions app/Console/Commands/CertificatePreflight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace App\Console\Commands;

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

class CertificatePreflight extends Command
{
protected $signature = 'certificate:preflight
{--edition=2025 : Target edition year}
{--type=all : excellence|super-organiser|all}
{--limit=0 : Max records to test (0 = all)}
{--only-pending : Test only rows without certificate_url}
{--export= : Optional CSV path for failures}';

protected $description = 'Dry-run compile certificates (no S3 upload, no DB updates) and report failures';

public function handle(): int
{
$edition = (int) $this->option('edition');
$typeOption = strtolower(trim((string) $this->option('type')));
$limit = max(0, (int) $this->option('limit'));
$onlyPending = (bool) $this->option('only-pending');
$exportPath = trim((string) $this->option('export'));

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

$query = Excellence::query()
->where('edition', $edition)
->whereIn('type', $types)
->with('user')
->orderBy('type')
->orderBy('id');

if ($onlyPending) {
$query->whereNull('certificate_url');
}

if ($limit > 0) {
$query->limit($limit);
}

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

$failures = [];
$ok = 0;
$bar = $this->output->createProgressBar($rows->count());
$bar->start();

foreach ($rows as $e) {
$bar->advance();
$user = $e->user;
if (! $user) {
$failures[] = $this->failureRow($e, '', 'Missing related user record.');
continue;
}
if (! $user->email) {
$failures[] = $this->failureRow($e, (string) $user->email, 'Missing user email.');
continue;
}

$name = $e->name_for_certificate ?? trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? ''));
if ($name === '') {
$failures[] = $this->failureRow($e, (string) $user->email, 'Empty certificate holder name.');
continue;
}

$certType = $e->type === 'SuperOrganiser' ? 'super-organiser' : 'excellence';
$numberOfActivities = $e->type === 'SuperOrganiser' ? (int) $user->activities($edition) : 0;

try {
$cert = new CertificateExcellence(
$edition,
$name,
$certType,
$numberOfActivities,
(int) $user->id,
(string) $user->email
);
$cert->preflight();
$ok++;
} catch (\Throwable $ex) {
$failures[] = $this->failureRow($e, (string) $user->email, $ex->getMessage());
}
}
$bar->finish();
$this->newLine(2);

$this->info("Preflight complete. Tested: {$rows->count()}, Passed: {$ok}, Failed: " . count($failures));

if (! empty($failures)) {
$show = array_slice($failures, 0, 20);
$this->table(['id', 'type', 'user_id', 'email', 'name', 'error'], $show);
if (count($failures) > 20) {
$this->line('Showing first 20 failures. Use --export for full list.');
}
}

if ($exportPath !== '') {
$path = $this->resolvePath($exportPath);
$dir = dirname($path);
if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->error("Failed to create export directory: {$dir}");
return self::FAILURE;
}
$fh = @fopen($path, 'wb');
if (! $fh) {
$this->error("Failed to open export file: {$path}");
return self::FAILURE;
}
fputcsv($fh, ['id', 'type', 'edition', 'user_id', 'email', 'name_for_certificate', 'error']);
foreach ($failures as $row) {
fputcsv($fh, [
$row['id'],
$row['type'],
$edition,
$row['user_id'],
$row['email'],
$row['name'],
$row['error'],
]);
}
fclose($fh);
$this->info("Exported failures CSV: {$path}");
}

return self::SUCCESS;
}

private function resolveTypes(string $typeOption): ?array
{
return match ($typeOption) {
'all' => ['Excellence', 'SuperOrganiser'],
'excellence' => ['Excellence'],
'super-organiser', 'superorganiser' => ['SuperOrganiser'],
default => null,
};
}

private function resolvePath(string $path): string
{
if (str_starts_with($path, '/')) {
return $path;
}
return base_path($path);
}

private function failureRow(Excellence $e, string $email, string $error): array
{
$name = $e->name_for_certificate ?? ($e->user ? trim(($e->user->firstname ?? '') . ' ' . ($e->user->lastname ?? '')) : '');
return [
'id' => $e->id,
'type' => $e->type,
'user_id' => (int) $e->user_id,
'email' => $email,
'name' => (string) $name,
'error' => $error,
];
}
}
8 changes: 4 additions & 4 deletions resources/views/certificates.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

@if(!$excellence->isEmpty())
@foreach($excellence as $certificate_of_excellence)
@if(!is_null($certificate_of_excellence->name_for_certificate))
@if(!empty($certificate_of_excellence->certificate_url))
<tr class="{{ $loop->even ? 'bg-[#F5F2FA]' : 'bg-white' }}">
<td class="border-r border-[#B399D6] px-6 py-4 font-semibold text-xl">
<span class="text-slate-500 font-semibold">Excellence</span>
Expand Down Expand Up @@ -112,7 +112,7 @@

@if(!$superOrganiser->isEmpty())
@foreach($superOrganiser as $super_organiser_certificate)
@if(!is_null($super_organiser_certificate->name_for_certificate))
@if(!empty($super_organiser_certificate->certificate_url))
<tr class="{{ $loop->even ? 'bg-[#F5F2FA]' : 'bg-white' }}">
<td class="border-r border-[#B399D6] px-6 py-4 font-semibold text-xl">
<span class="text-slate-500 font-semibold">Super Organiser</span>
Expand Down Expand Up @@ -222,7 +222,7 @@

@if(!$excellence->isEmpty())
@foreach($excellence as $certificate_of_excellence)
@if(!is_null($certificate_of_excellence->name_for_certificate))
@if(!empty($certificate_of_excellence->certificate_url))
<div class="border-2 border-[#B399D6] rounded-lg overflow-hidden">
<div class="flex">
<div class="flex items-center px-4 py-5 bg-[#410098] border-r border-b border-[#B399D6] font-['Montserrat'] font-semibold text-base text-white w-[108px]">
Expand Down Expand Up @@ -272,7 +272,7 @@

@if(!$superOrganiser->isEmpty())
@foreach($superOrganiser as $super_organiser_certificate)
@if(!is_null($super_organiser_certificate->name_for_certificate))
@if(!empty($super_organiser_certificate->certificate_url))
<div class="border-2 border-[#B399D6] rounded-lg overflow-hidden">
<div class="flex">
<div class="flex items-center px-4 py-5 bg-[#410098] border-r border-b border-[#B399D6] font-['Montserrat'] font-semibold text-base text-white w-[108px]">
Expand Down
Loading