118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
// ============================================
|
|
// handlers/gps_handlers.go
|
|
// Gestion des liens GPS et visualisation
|
|
// ============================================
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"gestion/db"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GET /api/v2/admin/protected/delivery-persons/:username/map-links
|
|
func GetDeliveryPersonMapLinks(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
|
|
return
|
|
}
|
|
|
|
username := c.Param("username")
|
|
if username == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Username requis"})
|
|
return
|
|
}
|
|
|
|
log.Printf("🗺️ [MAP_LINKS] Demande pour livreur: %s", username)
|
|
|
|
lat, lon, err := database.GetDeliveryPersonLocation(username)
|
|
if err != nil {
|
|
log.Printf("❌ [MAP_LINKS] Erreur position: %v", err)
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"success": false,
|
|
"error": "Position GPS non disponible pour ce livreur",
|
|
"message": "Le livreur n'a pas encore partagé sa position ou est hors ligne",
|
|
})
|
|
return
|
|
}
|
|
|
|
if lat == 0 && lon == 0 {
|
|
log.Printf("⚠️ [MAP_LINKS] Coordonnées invalides (0,0) pour %s", username)
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"success": false,
|
|
"error": "Coordonnées GPS invalides (0,0)",
|
|
"message": "Le livreur doit mettre à jour sa position",
|
|
})
|
|
return
|
|
}
|
|
|
|
mapLinks := database.GenerateMapLinks(lat, lon, username)
|
|
|
|
log.Printf("✅ [MAP_LINKS] Liens générés pour %s: (%.6f, %.6f)", username, lat, lon)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"deliveryman": gin.H{
|
|
"username": username,
|
|
},
|
|
"location": gin.H{
|
|
"latitude": lat,
|
|
"longitude": lon,
|
|
"valid": true,
|
|
},
|
|
"map_links": mapLinks,
|
|
})
|
|
}
|
|
|
|
func GetLivreurNavLink(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
username := c.GetString("username")
|
|
|
|
commandID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalide"})
|
|
return
|
|
}
|
|
|
|
command, err := database.GetCommandByID(commandID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Commande non trouvée"})
|
|
return
|
|
}
|
|
|
|
livreurAssign, _ := command["livreur_assign"].(string)
|
|
if livreurAssign != username {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Cette commande ne vous est pas assignée"})
|
|
return
|
|
}
|
|
|
|
var wazeLink string
|
|
destLat, hasLat := command["dest_latitude"].(float64)
|
|
destLon, hasLon := command["dest_longitude"].(float64)
|
|
if hasLat && hasLon && destLat != 0 && destLon != 0 {
|
|
wazeLink = fmt.Sprintf("waze://?ll=%.6f,%.6f&navigate=yes", destLat, destLon)
|
|
log.Printf("🗺️ [NAV_LINK] Lien coords pour cmd %d: %s", commandID, wazeLink)
|
|
} else if adresse, _ := command["adresse"].(string); adresse != "" {
|
|
wazeLink = fmt.Sprintf("waze://?q=%s&navigate=yes", url.QueryEscape(adresse))
|
|
log.Printf("🗺️ [NAV_LINK] Lien adresse pour cmd %d: %s", commandID, wazeLink)
|
|
} else {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Aucune destination disponible pour cette commande"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"command_id": commandID,
|
|
"waze_app": wazeLink,
|
|
})
|
|
}
|