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>
This commit is contained in:
2026-04-13 09:54:08 +02:00
parent 8f1b53130b
commit 838bee4d60
2 changed files with 31 additions and 4 deletions

View File

@@ -5,23 +5,49 @@ use App\Mail\RegistrationApprovedMail;
use App\Mail\RegistrationConfirmationMail;
use App\Mail\RegistrationRejectedMail;
use App\Models\Event;
use App\Models\Organisation;
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) {
$org = Organisation::first();
$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.'),
'invitation' => new InvitationMail(UserInvitation::first()),
default => abort(404),
};
});
}