Skip to content

Commit 596f152

Browse files
authored
Add user database notifications for completed and failed speedtests (alexjustesen#2432)
Co-authored-by: Alex Justesen <[email protected]>
1 parent f751def commit 596f152

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Models\Result;
7+
use App\Models\User;
8+
use Filament\Actions\Action;
9+
use Filament\Notifications\Notification;
10+
11+
class ProcessCompletedSpeedtest
12+
{
13+
/**
14+
* Handle the event.
15+
*/
16+
public function handle(SpeedtestCompleted $event): void
17+
{
18+
$result = $event->result;
19+
20+
if ($result->dispatched_by && ! $result->scheduled) {
21+
$this->notifyDispatchingUser($result);
22+
}
23+
}
24+
25+
/**
26+
* Notify the user who dispatched the speedtest.
27+
*/
28+
private function notifyDispatchingUser(Result $result): void
29+
{
30+
$user = User::find($result->dispatched_by);
31+
32+
$user->notify(
33+
Notification::make()
34+
->title(__('results.speedtest_completed'))
35+
->actions([
36+
Action::make('view')
37+
->label(__('general.view'))
38+
->url(route('filament.admin.resources.results.index')),
39+
])
40+
->success()
41+
->toDatabase(),
42+
);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\SpeedtestFailed;
6+
use App\Models\Result;
7+
use App\Models\User;
8+
use Filament\Actions\Action;
9+
use Filament\Notifications\Notification;
10+
11+
class ProcessFailedSpeedtest
12+
{
13+
/**
14+
* Handle the event.
15+
*/
16+
public function handle(SpeedtestFailed $event): void
17+
{
18+
$result = $event->result;
19+
20+
if ($result->dispatched_by && ! $result->scheduled) {
21+
$this->notifyDispatchingUser($result);
22+
}
23+
}
24+
25+
/**
26+
* Notify the user who dispatched the speedtest.
27+
*/
28+
private function notifyDispatchingUser(Result $result): void
29+
{
30+
$user = User::find($result->dispatched_by);
31+
32+
$user->notify(
33+
Notification::make()
34+
->title(__('results.speedtest_failed'))
35+
->actions([
36+
Action::make('view')
37+
->label(__('general.view'))
38+
->url(route('filament.admin.resources.results.index')),
39+
])
40+
->warning()
41+
->toDatabase(),
42+
);
43+
}
44+
}

lang/en/general.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'no' => 'No',
1717
'options' => 'Options',
1818
'details' => 'Details',
19+
'view' => 'View',
1920

2021
// Common labels
2122
'name' => 'Name',

lang/en/results.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
// Notifications
6464
'speedtest_started' => 'Speedtest started',
6565
'speedtest_completed' => 'Speedtest completed',
66+
'speedtest_failed' => 'Speedtest failed',
6667
'download_threshold_breached' => 'Download threshold breached!',
6768
'upload_threshold_breached' => 'Upload threshold breached!',
6869
'ping_threshold_breached' => 'Ping threshold breached!',

0 commit comments

Comments
 (0)