chore: build
This commit is contained in:
@@ -867,3 +867,82 @@ func (d *Database) ResetClientRedeemed(username, poolKey string) error {
|
||||
UPDATE clients SET points_redeemed = '{}'::jsonb, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE username = ?`, username).Error
|
||||
}
|
||||
|
||||
type RewardOrderItem struct {
|
||||
Produit string `json:"produit"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Price float64 `json:"price"`
|
||||
RewardPoolKey string `json:"reward_pool_key"`
|
||||
}
|
||||
|
||||
type RewardOrder struct {
|
||||
CommandID int `json:"command_id"`
|
||||
Status string `json:"status"`
|
||||
Total float64 `json:"total"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Items []RewardOrderItem `json:"items"`
|
||||
}
|
||||
|
||||
// GetClientRewardOrders retourne les commandes du client contenant au moins un article récompense.
|
||||
func (d *Database) GetClientRewardOrders(username string) ([]RewardOrder, error) {
|
||||
var rows []struct {
|
||||
CommandID int `gorm:"column:command_id"`
|
||||
Status string `gorm:"column:status"`
|
||||
Total float64 `gorm:"column:total_prix"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
Produit string `gorm:"column:produit"`
|
||||
Quantity float64 `gorm:"column:quantite"`
|
||||
Price float64 `gorm:"column:prix"`
|
||||
RewardPoolKey string `gorm:"column:reward_pool_key"`
|
||||
}
|
||||
if err := d.GDB.Raw(`
|
||||
SELECT c.id AS command_id, c.status, c.total_prix::float8 AS total_prix, c.created_at,
|
||||
ci.produit, ci.quantite::float8 AS quantite, ci.prix::float8 AS prix,
|
||||
COALESCE(ci.reward_pool_key, '') AS reward_pool_key
|
||||
FROM command_items ci
|
||||
JOIN commandes c ON c.id = ci.command_id
|
||||
WHERE c.username = ? AND ci.is_reward = true
|
||||
ORDER BY c.created_at DESC, ci.id`, username).Scan(&rows).Error; err != nil {
|
||||
return nil, fmt.Errorf("erreur lecture commandes récompense: %w", err)
|
||||
}
|
||||
|
||||
orders := make([]RewardOrder, 0)
|
||||
index := make(map[int]int)
|
||||
for _, r := range rows {
|
||||
i, ok := index[r.CommandID]
|
||||
if !ok {
|
||||
orders = append(orders, RewardOrder{
|
||||
CommandID: r.CommandID, Status: r.Status, Total: r.Total, CreatedAt: r.CreatedAt,
|
||||
Items: []RewardOrderItem{},
|
||||
})
|
||||
i = len(orders) - 1
|
||||
index[r.CommandID] = i
|
||||
}
|
||||
orders[i].Items = append(orders[i].Items, RewardOrderItem{
|
||||
Produit: r.Produit, Quantity: r.Quantity, Price: r.Price, RewardPoolKey: r.RewardPoolKey,
|
||||
})
|
||||
}
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
// DecrementClientRedeemed annule une seule récompense réclamée sur un pool (admin).
|
||||
func (d *Database) DecrementClientRedeemed(username, poolKey string) (int, error) {
|
||||
var newCount int
|
||||
err := d.GDB.Transaction(func(tx *gorm.DB) error {
|
||||
var current int
|
||||
if err := tx.Raw(`
|
||||
SELECT COALESCE((points_redeemed->>?)::int, 0)
|
||||
FROM clients WHERE username = ? FOR UPDATE`, poolKey, username).Scan(¤t).Error; err != nil {
|
||||
return fmt.Errorf("erreur lecture: %w", err)
|
||||
}
|
||||
if current <= 0 {
|
||||
return fmt.Errorf("aucune récompense réclamée sur ce pool")
|
||||
}
|
||||
if err := releaseRewardReservationTx(tx, username, poolKey); err != nil {
|
||||
return err
|
||||
}
|
||||
newCount = current - 1
|
||||
return nil
|
||||
})
|
||||
return newCount, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user