Skip to content

Commit 6d6e052

Browse files
author
ShehabMohamedGamal
committed
fix(notify): protect listener against missing user
1 parent b54b2fd commit 6d6e052

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

app/Listeners/SendEnrollmentConfirmationEmail.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\Events\StudentEnrolled;
66
use App\Notifications\EnrollmentConfirmation;
77
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Support\Facades\Log;
9+
use Throwable;
810

911
class SendEnrollmentConfirmationEmail implements ShouldQueue
1012
{
@@ -22,8 +24,21 @@ class SendEnrollmentConfirmationEmail implements ShouldQueue
2224

2325
public function handle(StudentEnrolled $event): void
2426
{
25-
$event->enrollment->student->notify(
26-
new EnrollmentConfirmation($event->enrollment)
27-
);
27+
$student = $event->enrollment->student;
28+
29+
// Guard: user may have been deleted between dispatch and processing
30+
if (! $student) {
31+
return;
32+
}
33+
34+
$student->notify(new EnrollmentConfirmation($event->enrollment));
35+
}
36+
37+
public function failed(StudentEnrolled $event, Throwable $exception): void
38+
{
39+
Log::error('EnrollmentConfirmation notification failed after all retries', [
40+
'enrollment_id' => $event->enrollment->id,
41+
'exception' => $exception->getMessage(),
42+
]);
2843
}
2944
}

0 commit comments

Comments
 (0)