package db import ( "encoding/json" "fmt" "gestion/models" "gestion/services" "log" "time" ) func (d *Database) NotifyClient(username string, commandID int, notifType, message string) error { notifKey := fmt.Sprintf("notifications:%s", username) notification := map[string]any{ "command_id": commandID, "type": notifType, "message": message, "created_at": time.Now().Format(time.RFC3339), "read": false, } notifJSON, _ := json.Marshal(notification) Redis.LPush(RedisCtx, notifKey, notifJSON) Redis.Expire(RedisCtx, notifKey, 7*24*time.Hour) if services.TelegramBot != nil && services.TelegramBot.IsConfigured() && services.TelegramBot.IsNotificationsEnabled() { if chatID, ok, err := d.GetClientTelegramChatID(username); err == nil && ok { go services.TelegramBot.SendMessage(chatID, fmt.Sprintf("🔔 Notification\n\n%s", message)) } } log.Printf("📬 Notification envoyée à %s: %s", username, message) return nil } // NotifyLivreur envoie une notification in-app (Redis) à un livreur func (d *Database) NotifyLivreur(username string, commandID int, notifType, message string) error { notifKey := fmt.Sprintf("notifications:%s", username) notification := map[string]any{ "command_id": commandID, "type": notifType, "message": message, "created_at": time.Now().Format(time.RFC3339), "read": false, } notifJSON, _ := json.Marshal(notification) Redis.LPush(RedisCtx, notifKey, notifJSON) Redis.Expire(RedisCtx, notifKey, 7*24*time.Hour) if services.TelegramBot != nil && services.TelegramBot.IsConfigured() && services.TelegramBot.IsNotificationsEnabled() { if chatID, ok, err := d.GetUserTelegramChatID(username); err == nil && ok { go services.TelegramBot.SendMessage(chatID, fmt.Sprintf("🔔 Notification\n\n%s", message)) } } log.Printf("📬 [LIVREUR_NOTIF] Notification envoyée à %s: %s", username, message) return nil } // NotifyAllAdminCabine stocke une notification Redis pour tous les admins/cabines func (d *Database) NotifyAllAdminCabine(commandID int, clientUsername, deliveryAddr string) { var users []struct { Username string `gorm:"column:username"` } if err := d.GDB.Model(&models.User{}).Select("username").Where("role IN ?", []string{"admin", "cabine"}).Scan(&users).Error; err != nil { log.Printf("❌ [ADMIN_NOTIF] Erreur lecture users admin/cabine: %v", err) return } msg := fmt.Sprintf("Nouvelle commande #%d de %s — %s", commandID, clientUsername, deliveryAddr) notification := map[string]any{ "command_id": commandID, "type": "new_order", "message": msg, "created_at": time.Now().Format(time.RFC3339), "read": false, } notifJSON, _ := json.Marshal(notification) count := 0 for _, u := range users { notifKey := fmt.Sprintf("notifications:%s", u.Username) Redis.LPush(RedisCtx, notifKey, notifJSON) Redis.Expire(RedisCtx, notifKey, 7*24*time.Hour) if services.TelegramBot != nil && services.TelegramBot.IsConfigured() && services.TelegramBot.IsNotificationsEnabled() { if chatID, ok, err := d.GetUserTelegramChatID(u.Username); err == nil && ok { capturedChatID := chatID capturedMsg := msg go services.TelegramBot.SendMessage(capturedChatID, fmt.Sprintf("🔔 Nouvelle commande\n\n%s", capturedMsg)) } } count++ } log.Printf("📬 [ADMIN_NOTIF] Notif Redis (%d users) pour commande #%d", count, commandID) } // NotifyAllAdminCabineAlert envoie une notification Redis à tous les admins/cabines lors d'une alerte func (d *Database) NotifyAllAdminCabineAlert(alertID int, livreurUsername, alertMessage string) { var users []struct { Username string `gorm:"column:username"` } if err := d.GDB.Model(&models.User{}).Select("username").Where("role IN ?", []string{"admin", "cabine"}).Scan(&users).Error; err != nil { log.Printf("❌ [ALERT_NOTIF] Erreur lecture users admin/cabine: %v", err) return } body := fmt.Sprintf("%s — livreur : %s", alertMessage, livreurUsername) notification := map[string]any{ "alert_id": alertID, "type": "alert", "message": body, "created_at": time.Now().Format(time.RFC3339), "read": false, } notifJSON, _ := json.Marshal(notification) count := 0 for _, u := range users { notifKey := fmt.Sprintf("notifications:%s", u.Username) Redis.LPush(RedisCtx, notifKey, notifJSON) Redis.Expire(RedisCtx, notifKey, 7*24*time.Hour) if services.TelegramBot != nil && services.TelegramBot.IsConfigured() && services.TelegramBot.IsNotificationsEnabled() { if chatID, ok, err := d.GetUserTelegramChatID(u.Username); err == nil && ok { capturedChatID := chatID capturedBody := body go services.TelegramBot.SendMessage(capturedChatID, fmt.Sprintf("🚨 Alerte livreur\n\n%s", capturedBody)) } } count++ } log.Printf("🚨 [ALERT_NOTIF] Notif Redis (%d users) pour alerte #%d de %s", count, alertID, livreurUsername) }