fix: approved by client
Backend - Build & Lint / build (push) Has been cancelled

This commit is contained in:
2026-07-01 19:57:12 +02:00
parent 211df2e8d6
commit 2311631825
+28 -4
View File
@@ -2,6 +2,7 @@ package db
import (
"encoding/json"
"errors"
"fmt"
"gestion/models"
"log"
@@ -12,6 +13,9 @@ import (
"gorm.io/gorm"
)
// errAlreadyApproved est retournée quand le client tente d'approuver une commande déjà approuvée.
var errAlreadyApproved = errors.New("already_approved")
func sanitizeString(s string) string {
sanitized := strings.Map(func(r rune) rune {
if r < 32 || r == 127 {
@@ -118,9 +122,15 @@ func (d *Database) CreateCommand(username string) (*models.Command, error) {
commandID := cmdResult.ID
productIDs := make([]int, 0, len(basketItems))
for _, item := range basketItems {
productName, err := d.GetProductNameByID(item.ProductID)
if err != nil {
productIDs = append(productIDs, item.ProductID)
}
productNames, _ := d.GetProductNamesByIDs(productIDs)
for _, item := range basketItems {
productName := productNames[item.ProductID]
if productName == "" {
productName = "Produit inconnu"
}
@@ -213,9 +223,15 @@ func (d *Database) CreateCommandWithAddress(username, deliveryAddress string) (*
commandID := cmdResult.ID
productIDs2 := make([]int, 0, len(basketItems))
for _, item := range basketItems {
productName, err := d.GetProductNameByID(item.ProductID)
if err != nil || productName == "" {
productIDs2 = append(productIDs2, item.ProductID)
}
productNames2, _ := d.GetProductNamesByIDs(productIDs2)
for _, item := range basketItems {
productName := productNames2[item.ProductID]
if productName == "" {
productName = fmt.Sprintf("Produit #%d", item.ProductID)
}
@@ -796,6 +812,11 @@ func (d *Database) ApproveDeliveryAtomic(commandID int, username string) (int, s
return fmt.Errorf("cette commande ne vous appartient pas")
}
if cmd.Status == "approved" {
log.Printf("️ [ApproveAtomic] Commande %d déjà approuvée — réponse idempotente", commandID)
return errAlreadyApproved
}
if cmd.Status != "livre" {
log.Printf("❌ [ApproveAtomic] Statut invalide: %s (attendu: livre)", cmd.Status)
return fmt.Errorf("commande doit être en statut 'livre' (statut actuel: %s)", cmd.Status)
@@ -845,6 +866,9 @@ func (d *Database) ApproveDeliveryAtomic(commandID int, username string) (int, s
})
if err != nil {
if errors.Is(err, errAlreadyApproved) {
return 0, "", nil
}
return 0, "", err
}