Files
crewli/api/routes/web.php
bert.hausmans 838bee4d60 fix: mail preview endpoint not loading and crashing on null data
Register web.php in bootstrap/app.php (was missing, so the route was
never loaded). Add null checks for all model queries with helpful error
messages instead of TypeErrors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 09:54:08 +02:00

54 lines
1.8 KiB
PHP

<?php
use App\Mail\InvitationMail;
use App\Mail\RegistrationApprovedMail;
use App\Mail\RegistrationConfirmationMail;
use App\Mail\RegistrationRejectedMail;
use App\Models\Event;
use App\Models\Person;
use App\Models\UserInvitation;
use Illuminate\Support\Facades\Route;
if (app()->environment('local', 'staging')) {
Route::get('/mail-preview/{type}', function (string $type) {
$supportedTypes = [
'registration-confirmation',
'registration-approved',
'registration-rejected',
'invitation',
];
if (! in_array($type, $supportedTypes)) {
abort(404, "Unknown mail type '{$type}'. Supported types: " . implode(', ', $supportedTypes));
}
if ($type === 'invitation') {
$invitation = UserInvitation::first();
if (! $invitation) {
return response('No UserInvitation found in the database. Create one first to preview this mail.', 422);
}
return new InvitationMail($invitation);
}
$event = Event::first();
$person = Person::first();
if (! $event || ! $person) {
$missing = collect([
'Event' => $event,
'Person' => $person,
])->filter(fn ($v) => $v === null)->keys()->implode(', ');
return response("No test data found. Missing: {$missing}. Seed the database first.", 422);
}
return match ($type) {
'registration-confirmation' => new RegistrationConfirmationMail($person, $event),
'registration-approved' => new RegistrationApprovedMail($person, $event),
'registration-rejected' => new RegistrationRejectedMail($person, $event, 'Helaas geen plek meer.'),
};
});
}