-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathSendDigestNotifications.php
More file actions
56 lines (45 loc) · 1.93 KB
/
SendDigestNotifications.php
File metadata and controls
56 lines (45 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace App\Class;
use App\Models\User;
use Filament\Notifications\Notification;
use App\Notifications\SummaryNotification;
class SendDigestNotifications
{
public function __invoke()
{
$users = User::all(); // Or a more specific query to target users with pending notifications
foreach ($users as $user) {
$notifications = $user->unreadNotifications; // Get unread notifications
$temp = $notifications;
// Aggregate notifications based on your criteria
$groupedNotifications = $notifications->groupBy(function ($notification) {
return $notification->type;
});
foreach ($groupedNotifications as $type => $temp) {
if($temp->first()->type == "App\\Notifications\\SummaryNotification"){
continue;
}
// // Create a summary notification for each group
$summary = $this->createSummary($temp);
// // // Send the summary notification
$user->notify(new SummaryNotification($summary));
// // // Mark the individual notifications as read
}
foreach ($user->unreadNotifications as $notification) {
if($notification->type != "App\\Notifications\\SummaryNotification"){
$notification->delete();
}
}
}
}
protected function createSummary($notifications)
{
// Logic to create a summary from a group of notifications
$summary = [
'count' => $notifications->count(),
'type' => $notifications->first()->type,
'messages' => $notifications->pluck('data')->toArray(), // Assuming 'message' is an attribute in notification data
];
return $summary;
}
}