39 lines
1.3 KiB
Go
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
|
|
}
|
|
}
|