fix: notification trait

fix: some events must always be enabled, so a notification is sent all the time (user cannot choose to not receive this notification).
fix: check if the event is enabled before adding a channel to enabled
This commit is contained in:
peaklabs-dev
2024-12-09 19:47:17 +01:00
parent 96c970ca4e
commit dedfd72731

View File

@@ -10,6 +10,13 @@ use Illuminate\Database\Eloquent\Model;
trait HasNotificationSettings
{
protected $alwaysSendEvents = [
'server_force_enabled',
'server_force_disabled',
'general',
'test',
];
/**
* Get settings model for specific channel
*/
@@ -34,17 +41,31 @@ trait HasNotificationSettings
return $settings?->isEnabled() ?? false;
}
/**
* Check if a specific notification type is enabled for a channel
*/
public function isNotificationTypeEnabled(string $channel, string $event): bool
{
$settings = $this->getNotificationSettings($channel);
if (! $settings || ! $this->isNotificationEnabled($channel)) {
return false;
}
if (in_array($event, $this->alwaysSendEvents)) {
return true;
}
$settingKey = "{$event}_{$channel}_notifications";
return (bool) $settings->$settingKey;
}
/**
* Get all enabled notification channels for an event
*/
public function getEnabledChannels(string $event): array
{
$alwaysSendEvents = [
'server_force_enabled',
'server_force_disabled',
'general',
];
$channels = [];
$channelMap = [
@@ -55,14 +76,8 @@ trait HasNotificationSettings
];
foreach ($channelMap as $channel => $channelClass) {
if (in_array($event, $alwaysSendEvents)) {
if ($this->isNotificationEnabled($channel)) {
$channels[] = $channelClass;
}
} else {
if ($this->isNotificationEnabled($channel) && $this->isNotificationTypeEnabled($channel, $event)) {
$channels[] = $channelClass;
}
if ($this->isNotificationEnabled($channel) && $this->isNotificationTypeEnabled($channel, $event)) {
$channels[] = $channelClass;
}
}