@@ -418,6 +418,106 @@ func ClaimMyReward(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// SwapMyRewardProduct change le produit choisi pour la récompense déjà en
|
||||
// panier, sans consommer une nouvelle réservation de points : le pool et
|
||||
// l'éligibilité sont redérivés côté serveur (jamais depuis une donnée
|
||||
// envoyée par le client), via la même logique de candidats que ClaimMyReward.
|
||||
func SwapMyRewardProduct(c *gin.Context) {
|
||||
username := c.GetString("username")
|
||||
if username == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
ProductID int `json:"product_id" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "product_id requis"})
|
||||
return
|
||||
}
|
||||
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
poolKey, err := database.GetActiveRewardPoolKey(username)
|
||||
if err != nil {
|
||||
utils.ServerErr(c, "Erreur lecture panier", err)
|
||||
return
|
||||
}
|
||||
if poolKey == "" {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Aucune récompense active dans le panier"})
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := database.GetSettings()
|
||||
if err != nil {
|
||||
utils.ServerErr(c, "Erreur lecture paramètres", err)
|
||||
return
|
||||
}
|
||||
if !settings.PointsEnabled {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Système de points désactivé"})
|
||||
return
|
||||
}
|
||||
reward := settings.PointsReward
|
||||
if reward == nil || reward.Threshold <= 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Aucune récompense configurée"})
|
||||
return
|
||||
}
|
||||
|
||||
var selectedPool *models.PointsPool
|
||||
for i := range settings.PointsPools {
|
||||
if settings.PointsPools[i].Key == poolKey {
|
||||
selectedPool = &settings.PointsPools[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if selectedPool == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Pool introuvable"})
|
||||
return
|
||||
}
|
||||
poolCategories := make(map[string]bool, len(selectedPool.Categories))
|
||||
for _, cat := range selectedPool.Categories {
|
||||
poolCategories[cat] = true
|
||||
}
|
||||
|
||||
candidates, err := resolveCategoryRewardCandidates(database, reward)
|
||||
if err != nil {
|
||||
utils.ServerErr(c, "Erreur résolution produits récompense", err)
|
||||
return
|
||||
}
|
||||
|
||||
var newItem *models.RewardItem
|
||||
for _, cand := range candidates {
|
||||
if cand.ProductID != req.ProductID || !poolCategories[cand.Category] {
|
||||
continue
|
||||
}
|
||||
price, err := effectiveRewardPrice(database, cand.ProductID, cand.Quantity, cand.Type)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Récompense momentanément indisponible, contactez le support"})
|
||||
return
|
||||
}
|
||||
newItem = &models.RewardItem{ProductID: cand.ProductID, Quantity: cand.Quantity, Price: price}
|
||||
break
|
||||
}
|
||||
if newItem == nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Ce produit n'est pas éligible pour cette récompense"})
|
||||
return
|
||||
}
|
||||
|
||||
added, err := database.AddRewardsToBasket(username, []models.RewardItem{*newItem}, poolKey)
|
||||
if err != nil {
|
||||
utils.ServerErr(c, "Erreur changement de produit récompense", err)
|
||||
return
|
||||
}
|
||||
|
||||
var productNames []string
|
||||
for _, item := range added {
|
||||
productNames = append(productNames, item.ProductName)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "product_names": productNames})
|
||||
}
|
||||
|
||||
func AdminResetClientRedeemed(c *gin.Context) {
|
||||
username := c.Param("username")
|
||||
poolKey := c.Query("pool_key")
|
||||
|
||||
Reference in New Issue
Block a user