chore: refacto

This commit is contained in:
2026-03-27 22:23:46 +01:00
parent 75bf4caaa1
commit 5380abe8ed
67 changed files with 1751 additions and 2573 deletions
@@ -10,9 +10,8 @@ import (
)
func (d *Database) UpdateDeliveryPersonLocation(username string, lat, lon float64) error {
// 1️⃣ Mettre à jour la position GPS dans Redis
key := fmt.Sprintf("delivery:location:%s", username)
location := map[string]interface{}{
location := map[string]any{
"latitude": lat,
"longitude": lon,
"last_update": time.Now().Unix(),
@@ -25,25 +24,20 @@ func (d *Database) UpdateDeliveryPersonLocation(username string, lat, lon float6
log.Printf("📍 Position GPS mise à jour pour %s: (%.6f, %.6f)", username, lat, lon)
// 2️⃣ ✅ NOUVEAU: Auto-initialiser/synchroniser le statut
statusKey := fmt.Sprintf("delivery:status:%s", username)
statusData, err := Redis.Get(RedisCtx, statusKey).Result()
if err != nil || statusData == "" {
// ✅ Pas de statut → Créer "available" par défaut
log.Printf("🆕 [INIT_STATUS] Création statut 'available' pour %s (première position GPS)", username)
d.SetDeliveryPersonStatus(username, "available", 0)
} else {
// ✅ Statut existe → Vérifier s'il faut le réactiver
var status models.DeliveryPersonStatus
json.Unmarshal([]byte(statusData), &status)
if status.Status == "offline" {
// Si le livreur était offline et envoie sa position → Le remettre available
log.Printf("🔄 [REACTIVATE] %s passe de 'offline' à 'available' (position GPS reçue)", username)
d.SetDeliveryPersonStatus(username, "available", 0)
} else {
// ✅ Statut actif → Synchroniser basé sur la queue
go d.UpdateDeliverymanStatusBasedOnQueue(username)
}
}
@@ -66,12 +60,11 @@ func (d *Database) GetDeliveryPersonLocation(username string) (float64, float64,
return 0, 0, fmt.Errorf("aucune donnée de position pour %s", username)
}
var location map[string]interface{}
var location map[string]any
if err := json.Unmarshal([]byte(data), &location); err != nil {
return 0, 0, fmt.Errorf("erreur parsing JSON Redis: %w", err)
}
// Extraire latitude avec gestion de type robuste
var lat, lon float64
if v, ok := location["latitude"]; ok && v != nil {
switch val := v.(type) {
@@ -88,7 +81,6 @@ func (d *Database) GetDeliveryPersonLocation(username string) (float64, float64,
}
}
// Extraire longitude avec gestion de type robuste
if v, ok := location["longitude"]; ok && v != nil {
switch val := v.(type) {
case float64: