diff --git a/backend/gestion/db/db_product.go b/backend/gestion/db/db_product.go index 0c7c8d83..e8cb11b8 100644 --- a/backend/gestion/db/db_product.go +++ b/backend/gestion/db/db_product.go @@ -115,6 +115,27 @@ func (d *Database) GetProductByID(id int) (models.Product, error) { return p, nil } +// GetProductNamesByIDs retourne un map id→name pour une liste d'IDs. +func (d *Database) GetProductNamesByIDs(ids []int) (map[int]string, error) { + result := make(map[int]string, len(ids)) + if len(ids) == 0 { + return result, nil + } + rows, err := d.GDB.Raw(`SELECT id, name FROM products WHERE id IN ?`, ids).Rows() + if err != nil { + return result, err + } + defer rows.Close() + for rows.Next() { + var id int + var name string + if err := rows.Scan(&id, &name); err == nil { + result[id] = name + } + } + return result, nil +} + func (d *Database) GetAllProducts() ([]models.Product, error) { log.Println("đŸ“¦ [GetAllProducts] START") diff --git a/backend/gestion/handlers/points.go b/backend/gestion/handlers/points.go index f9d3aa0d..0a1193f8 100644 --- a/backend/gestion/handlers/points.go +++ b/backend/gestion/handlers/points.go @@ -40,16 +40,35 @@ func GetMyPointsRewards(c *gin.Context) { reward := settings.PointsReward - type PoolInfo struct { - Key string `json:"key"` - Name string `json:"name"` - Points int `json:"points"` - RewardsEarned int `json:"rewards_earned"` - RewardsClaimed int `json:"rewards_claimed"` - RewardsAvailable int `json:"rewards_available"` - EligibleConfigs []models.RewardCategoryConfig `json:"eligible_configs"` + type EligibleConfigResponse struct { + Category string `json:"category"` + AllProducts bool `json:"all_products"` + ProductIDs []int `json:"product_ids"` + ProductNames []string `json:"product_names"` + Amount float64 `json:"amount"` } + type PoolInfo struct { + Key string `json:"key"` + Name string `json:"name"` + Points int `json:"points"` + RewardsEarned int `json:"rewards_earned"` + RewardsClaimed int `json:"rewards_claimed"` + RewardsAvailable int `json:"rewards_available"` + EligibleConfigs []EligibleConfigResponse `json:"eligible_configs"` + } + + // Collecter tous les product_ids nĂ©cessaires en un seul passage + allProductIDs := make([]int, 0) + if reward != nil { + for _, cfg := range reward.CategoryConfigs { + if !cfg.AllProducts { + allProductIDs = append(allProductIDs, cfg.ProductIDs...) + } + } + } + productNames, _ := database.GetProductNamesByIDs(allProductIDs) + pools := make([]PoolInfo, 0, len(settings.PointsPools)) for _, pool := range settings.PointsPools { pts := pointsExtra[pool.Key] @@ -69,12 +88,25 @@ func GetMyPointsRewards(c *gin.Context) { for _, c := range pool.Categories { poolCats[c] = true } - eligibleConfigs := make([]models.RewardCategoryConfig, 0) + eligibleConfigs := make([]EligibleConfigResponse, 0) if reward != nil { for _, cfg := range reward.CategoryConfigs { - if poolCats[cfg.Category] { - eligibleConfigs = append(eligibleConfigs, cfg) + if !poolCats[cfg.Category] { + continue } + names := make([]string, 0, len(cfg.ProductIDs)) + for _, pid := range cfg.ProductIDs { + if n, ok := productNames[pid]; ok { + names = append(names, n) + } + } + eligibleConfigs = append(eligibleConfigs, EligibleConfigResponse{ + Category: cfg.Category, + AllProducts: cfg.AllProducts, + ProductIDs: cfg.ProductIDs, + ProductNames: names, + Amount: cfg.Amount, + }) } } diff --git a/frontend-prep/src/api/api.ts b/frontend-prep/src/api/api.ts index e075adf1..51fa076d 100644 --- a/frontend-prep/src/api/api.ts +++ b/frontend-prep/src/api/api.ts @@ -2145,6 +2145,7 @@ export type RewardCategoryConfig = { category: string; all_products: boolean; product_ids: number[]; + product_names: string[]; amount: number; }; diff --git a/frontend-prep/src/pages/User/ConsultationHistorique.tsx b/frontend-prep/src/pages/User/ConsultationHistorique.tsx index 527ee941..c01f242c 100644 --- a/frontend-prep/src/pages/User/ConsultationHistorique.tsx +++ b/frontend-prep/src/pages/User/ConsultationHistorique.tsx @@ -315,13 +315,21 @@ function ConsultationHistorique() { {pool.name} {pool.points} pts - {pool.eligible_configs.some((cfg) => cfg.all_products) && ( + {pool.eligible_configs.length > 0 && (