chore: build
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gestion/db"
|
||||
"gestion/services"
|
||||
@@ -1332,6 +1333,10 @@ func UpdateCommandStatusAdmin(c *gin.Context) {
|
||||
switch req.Status {
|
||||
case "cancelled":
|
||||
if err := database.CancelCommandByAdminAtomic(commandID); err != nil {
|
||||
if errors.Is(err, db.ErrCommandLocked) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Commande approuvée : annulation impossible"})
|
||||
return
|
||||
}
|
||||
utils.ServerErr(c, "Impossible d'annuler la commande", err)
|
||||
return
|
||||
}
|
||||
@@ -1349,6 +1354,10 @@ func UpdateCommandStatusAdmin(c *gin.Context) {
|
||||
}
|
||||
default:
|
||||
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
|
||||
}
|
||||
utils.ServerErr(c, "Impossible de mettre à jour le statut", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
})
|
||||
|
||||
@@ -510,7 +510,7 @@ func UpdateProduct(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.GetProductByID(id)
|
||||
current, err := database.GetProductByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Produit non trouvé"})
|
||||
return
|
||||
@@ -574,6 +574,25 @@ func UpdateProduct(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// L'unité n'est qu'un libellé : la changer réinterpréterait les quantités des paniers et commandes en cours.
|
||||
if current.Unit != "" && updateData.Unit != current.Unit {
|
||||
baskets, commands, err := database.CountOpenProductUsage(id)
|
||||
if err != nil {
|
||||
utils.ServerErr(c, "Erreur vérification paniers et commandes", err)
|
||||
return
|
||||
}
|
||||
if baskets > 0 || commands > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": fmt.Sprintf("Impossible de changer l'unité (%s → %s) : produit présent dans %d panier(s) et %d commande(s) en cours", current.Unit, updateData.Unit, baskets, commands),
|
||||
"open_baskets": baskets,
|
||||
"open_commands": commands,
|
||||
"current_unit": current.Unit,
|
||||
"requested_unit": updateData.Unit,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("🔄 [UpdateProduct] %s met à jour produit #%d", username, id)
|
||||
|
||||
comingSoon := false
|
||||
|
||||
Reference in New Issue
Block a user