Files
omnex/control-plane/api/internal/alerts/recipients.go
T
Xor290 c5ee5636ef
ci-api / test (push) Successful in 22m17s
ci-web / test (push) Successful in 13m44s
chore: update
2026-08-02 18:34:59 +02:00

39 lines
1.3 KiB
Go

package alerts
import (
"context"
"gorm.io/gorm"
"github.com/omnex/control-plane/api/internal/auth"
)
// GormRecipients construit, à partir de la table des utilisateurs, la liste
// des notifiers actifs : chaque admin ayant renseigné un webhook Discord
// et/ou un bot Telegram (dans son profil) reçoit les alertes. Interrogée à
// chaque notification nécessaire — un admin qui change ses réglages est pris
// en compte sans redémarrage de l'API.
func GormRecipients(gdb *gorm.DB) func(ctx context.Context) ([]Notifier, error) {
return func(ctx context.Context) ([]Notifier, error) {
var admins []auth.User
if err := gdb.WithContext(ctx).Where("role = ?", auth.RoleAdmin).Find(&admins).Error; err != nil {
return nil, err
}
var notifiers []Notifier
for _, a := range admins {
if a.AlertDiscordWebhookURL != nil && *a.AlertDiscordWebhookURL != "" {
notifiers = append(notifiers, &DiscordNotifier{WebhookURL: *a.AlertDiscordWebhookURL})
}
if a.AlertTelegramBotToken != nil && *a.AlertTelegramBotToken != "" &&
a.AlertTelegramChatID != nil && *a.AlertTelegramChatID != "" {
notifiers = append(notifiers, &TelegramNotifier{
BotToken: *a.AlertTelegramBotToken,
ChatID: *a.AlertTelegramChatID,
})
}
}
return notifiers, nil
}
}