45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package db
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func (d *Database) NotifyClient(username string, commandID int, notifType, message string) error {
|
|
// Sauvegarder la notification dans Redis
|
|
notifKey := fmt.Sprintf("notifications:%s", username)
|
|
|
|
notification := map[string]interface{}{
|
|
"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) // Expire après 7 jours
|
|
|
|
log.Printf("📬 Notification envoyée à %s: %s", username, message)
|
|
return nil
|
|
}
|
|
|
|
// AddDeliveryRating ajoute une note pour un livreur
|
|
func (d *Database) AddDeliveryRating(livreurUsername string, commandID, rating int, comment string) error {
|
|
query := `
|
|
INSERT INTO delivery_ratings (livreur_username, command_id, rating, comment, created_at)
|
|
VALUES (?, ?, ?, ?, NOW())
|
|
`
|
|
_, err := d.Exec(query, livreurUsername, commandID, rating, comment)
|
|
if err != nil {
|
|
log.Printf("⚠️ Erreur sauvegarde note livreur: %v", err)
|
|
return err
|
|
}
|
|
|
|
log.Printf("⭐ Note %d/5 ajoutée pour livreur %s (commande %d)", rating, livreurUsername, commandID)
|
|
return nil
|
|
}
|