chore: build
This commit is contained in:
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gestion/db"
|
||||
"gestion/models"
|
||||
@@ -184,6 +185,13 @@ func GetDeliveryDetails(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// Le livreur avance dans l'ordre : assignée → en route → arrivé → livrée.
|
||||
var livreurNextStatus = map[string]string{
|
||||
"assigned": "en_route",
|
||||
"en_route": "arrived",
|
||||
"arrived": "livre",
|
||||
}
|
||||
|
||||
func UpdateDeliveryStatus(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
@@ -248,6 +256,22 @@ func UpdateDeliveryStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
currentStatus, _ := command["status"].(string)
|
||||
if req.Status != "cancelled" {
|
||||
if currentStatus == req.Status {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Statut déjà à jour", "command_id": commandID, "status": currentStatus})
|
||||
return
|
||||
}
|
||||
if livreurNextStatus[currentStatus] != req.Status {
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": "Transition de statut non autorisée",
|
||||
"current_status": currentStatus,
|
||||
"received": req.Status,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.Status == "livre" {
|
||||
if req.Latitude == 0 || req.Longitude == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Coordonnées GPS requises pour confirmer la livraison"})
|
||||
@@ -273,6 +297,10 @@ func UpdateDeliveryStatus(c *gin.Context) {
|
||||
// double appel (double-tap, retry réseau, commande déjà annulée ailleurs).
|
||||
if req.Status == "cancelled" {
|
||||
alreadyCancelled, prevStatus, cancelErr := database.CancelDeliveryByLivreurAtomic(commandID)
|
||||
if errors.Is(cancelErr, db.ErrCommandAlreadyDelivered) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Commande déjà livrée : annulation impossible"})
|
||||
return
|
||||
}
|
||||
if cancelErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Erreur mise à jour",
|
||||
@@ -306,6 +334,10 @@ func UpdateDeliveryStatus(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
} else if err := database.UpdateCommandStatus(commandID, req.Status); err != nil {
|
||||
if errors.Is(err, db.ErrCommandLocked) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Commande annulée ou approuvée : statut non modifiable"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Erreur mise à jour",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user