From ec1c91fb75f23727f5ed91fdcd2cfd46f6932ee3 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:03:40 +0100 Subject: [PATCH] feat: notification trait --- app/Traits/HasNotificationSettings.php | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/Traits/HasNotificationSettings.php diff --git a/app/Traits/HasNotificationSettings.php b/app/Traits/HasNotificationSettings.php new file mode 100644 index 000000000..64ea85a55 --- /dev/null +++ b/app/Traits/HasNotificationSettings.php @@ -0,0 +1,71 @@ + $this->emailNotificationSettings, + 'discord' => $this->discordNotificationSettings, + 'telegram' => $this->telegramNotificationSettings, + 'slack' => $this->slackNotificationSettings, + default => null, + }; + } + + /** + * Check if a notification channel is enabled + */ + public function isNotificationEnabled(string $channel): bool + { + $settings = $this->getNotificationSettings($channel); + + return $settings?->isEnabled() ?? false; + } + + /** + * Get all enabled notification channels for an event + */ + public function getEnabledChannels(string $event): array + { + $alwaysSendEvents = [ + 'server_force_enabled', + 'server_force_disabled', + 'general', + ]; + + $channels = []; + + $channelMap = [ + 'email' => EmailChannel::class, + 'discord' => DiscordChannel::class, + 'telegram' => TelegramChannel::class, + 'slack' => SlackChannel::class, + ]; + + 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; + } + } + } + + return $channels; + } +}