From 2311631825b37014494d953ac1f3f8268bdb432e Mon Sep 17 00:00:00 2001 From: Xor290 Date: Wed, 1 Jul 2026 19:57:12 +0200 Subject: [PATCH] fix: approved by client --- backend/gestion/db/db_commands.go | 32 +++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/gestion/db/db_commands.go b/backend/gestion/db/db_commands.go index 328ac686..d42e8868 100644 --- a/backend/gestion/db/db_commands.go +++ b/backend/gestion/db/db_commands.go @@ -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 }