This commit is contained in:
@@ -2,6 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gestion/models"
|
"gestion/models"
|
||||||
"log"
|
"log"
|
||||||
@@ -12,6 +13,9 @@ import (
|
|||||||
"gorm.io/gorm"
|
"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 {
|
func sanitizeString(s string) string {
|
||||||
sanitized := strings.Map(func(r rune) rune {
|
sanitized := strings.Map(func(r rune) rune {
|
||||||
if r < 32 || r == 127 {
|
if r < 32 || r == 127 {
|
||||||
@@ -118,9 +122,15 @@ func (d *Database) CreateCommand(username string) (*models.Command, error) {
|
|||||||
|
|
||||||
commandID := cmdResult.ID
|
commandID := cmdResult.ID
|
||||||
|
|
||||||
|
productIDs := make([]int, 0, len(basketItems))
|
||||||
for _, item := range basketItems {
|
for _, item := range basketItems {
|
||||||
productName, err := d.GetProductNameByID(item.ProductID)
|
productIDs = append(productIDs, item.ProductID)
|
||||||
if err != nil {
|
}
|
||||||
|
productNames, _ := d.GetProductNamesByIDs(productIDs)
|
||||||
|
|
||||||
|
for _, item := range basketItems {
|
||||||
|
productName := productNames[item.ProductID]
|
||||||
|
if productName == "" {
|
||||||
productName = "Produit inconnu"
|
productName = "Produit inconnu"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,9 +223,15 @@ func (d *Database) CreateCommandWithAddress(username, deliveryAddress string) (*
|
|||||||
|
|
||||||
commandID := cmdResult.ID
|
commandID := cmdResult.ID
|
||||||
|
|
||||||
|
productIDs2 := make([]int, 0, len(basketItems))
|
||||||
for _, item := range basketItems {
|
for _, item := range basketItems {
|
||||||
productName, err := d.GetProductNameByID(item.ProductID)
|
productIDs2 = append(productIDs2, item.ProductID)
|
||||||
if err != nil || productName == "" {
|
}
|
||||||
|
productNames2, _ := d.GetProductNamesByIDs(productIDs2)
|
||||||
|
|
||||||
|
for _, item := range basketItems {
|
||||||
|
productName := productNames2[item.ProductID]
|
||||||
|
if productName == "" {
|
||||||
productName = fmt.Sprintf("Produit #%d", item.ProductID)
|
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")
|
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" {
|
if cmd.Status != "livre" {
|
||||||
log.Printf("❌ [ApproveAtomic] Statut invalide: %s (attendu: livre)", cmd.Status)
|
log.Printf("❌ [ApproveAtomic] Statut invalide: %s (attendu: livre)", cmd.Status)
|
||||||
return fmt.Errorf("commande doit être en statut 'livre' (statut actuel: %s)", 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 err != nil {
|
||||||
|
if errors.Is(err, errAlreadyApproved) {
|
||||||
|
return 0, "", nil
|
||||||
|
}
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user