55use App \Models \Result ;
66use App \Services \SpeedtestFakeResultGenerator ;
77use Filament \Notifications \Notification ;
8+ use Illuminate \Support \Facades \Event ;
89use Illuminate \Support \Str ;
910use Lorisleiva \Actions \Concerns \AsAction ;
11+ use Spatie \WebhookServer \Events \WebhookCallFailedEvent ;
12+ use Spatie \WebhookServer \Events \WebhookCallSucceededEvent ;
1013use Spatie \WebhookServer \WebhookCall ;
1114
1215class SendWebhookTestNotification
@@ -27,9 +30,28 @@ public function handle(array $webhooks)
2730 // Generate a fake Result (NOT saved to database)
2831 $ fakeResult = SpeedtestFakeResultGenerator::completed ();
2932
33+ $ hasFailure = false ;
34+
3035 foreach ($ webhooks as $ webhook ) {
36+ $ url = $ webhook ['url ' ];
37+ $ succeeded = false ;
38+
39+ // Listen for success/failure events for this specific webhook
40+ Event::listen (WebhookCallSucceededEvent::class, function ($ event ) use ($ url , &$ succeeded ) {
41+ if ($ event ->webhookUrl === $ url ) {
42+ $ succeeded = true ;
43+ }
44+ });
45+
46+ Event::listen (WebhookCallFailedEvent::class, function ($ event ) use ($ url , &$ succeeded ) {
47+ if ($ event ->webhookUrl === $ url ) {
48+ $ succeeded = false ;
49+ }
50+ });
51+
52+ // Send webhook synchronously to get immediate result
3153 WebhookCall::create ()
32- ->url ($ webhook [ ' url ' ] )
54+ ->url ($ url )
3355 ->payload ([
3456 'result_id ' => Str::uuid (),
3557 'site_name ' => __ ('settings/notifications.test_notifications.webhook.payload ' ),
@@ -42,12 +64,25 @@ public function handle(array $webhooks)
4264 'url ' => url ('/admin/results ' ),
4365 ])
4466 ->doNotSign ()
45- ->dispatch ();
67+ ->dispatchSync ();
68+
69+ if (! $ succeeded ) {
70+ $ hasFailure = true ;
71+ }
4672 }
4773
48- Notification::make ()
49- ->title (__ ('settings/notifications.test_notifications.webhook.sent ' ))
50- ->success ()
51- ->send ();
74+ // Show appropriate notification based on results
75+ if (! $ hasFailure ) {
76+ Notification::make ()
77+ ->title (__ ('settings/notifications.test_notifications.webhook.sent ' ))
78+ ->success ()
79+ ->send ();
80+ } else {
81+ Notification::make ()
82+ ->title (__ ('settings/notifications.test_notifications.webhook.failed ' ))
83+ ->body (__ ('settings/notifications.test_notifications.webhook.failed_body ' ))
84+ ->danger ()
85+ ->send ();
86+ }
5287 }
5388}
0 commit comments