|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Notifier\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel; |
|
6
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationEvent; |
|
7
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationPreference; |
|
8
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationSetting; |
|
9
|
|
|
|
|
10
|
|
|
class PreferenceService |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Get user preferences for a specific event |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getUserPreferences($user, string $eventKey): array |
|
16
|
|
|
{ |
|
17
|
|
|
$event = NotificationEvent::where('key', $eventKey)->first(); |
|
18
|
|
|
|
|
19
|
|
|
if (!$event) { |
|
20
|
|
|
return []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$preference = NotificationPreference::where('user_id', $user->id) |
|
24
|
|
|
->where('notification_event_id', $event->id) |
|
25
|
|
|
->first(); |
|
26
|
|
|
|
|
27
|
|
|
if ($preference && isset($preference->channels)) { |
|
28
|
|
|
return $preference->channels; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this->getDefaultPreferences($event); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get channels configuration for an event (includes all active channels) |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getChannelsForEvent(NotificationEvent $event, ?NotificationPreference $preference): array |
|
38
|
|
|
{ |
|
39
|
|
|
if ($preference && isset($preference->channels)) { |
|
40
|
|
|
$channels = $preference->channels; |
|
41
|
|
|
} else { |
|
42
|
|
|
$channels = $this->getDefaultPreferences($event); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$activeChannels = NotificationChannel::where('is_active', true) |
|
46
|
|
|
->pluck('type') |
|
47
|
|
|
->toArray(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($activeChannels as $channelType) { |
|
50
|
|
|
if (!isset($channels[$channelType])) { |
|
51
|
|
|
$channels[$channelType] = false; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $channels; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get default preferences for an event based on settings |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getDefaultPreferences(NotificationEvent $event): array |
|
62
|
|
|
{ |
|
63
|
|
|
$defaultChannels = NotificationSetting::get( |
|
64
|
|
|
'preferences.default_channels', |
|
65
|
|
|
config('notifier.settings.preferences.default_channels', ['email']) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$preferences = []; |
|
69
|
|
|
foreach ($defaultChannels as $channel) { |
|
70
|
|
|
$preferences[$channel] = true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($event->settings['channels']) && is_array($event->settings['channels'])) { |
|
74
|
|
|
foreach ($event->settings['channels'] as $channel) { |
|
75
|
|
|
$preferences[$channel] = true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $preferences; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if notification should be sent to a specific channel |
|
84
|
|
|
*/ |
|
85
|
|
|
public function shouldSendToChannel($user, string $channelType, array $preferences): bool |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$channel = NotificationChannel::where('type', $channelType) |
|
88
|
|
|
->where('is_active', true) |
|
89
|
|
|
->first(); |
|
90
|
|
|
|
|
91
|
|
|
if (!$channel) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset($preferences[$channelType]) && !$preferences[$channelType]) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.