Files
projet_gestion_commande/backend/gestion/db/db_clients.go
T
Xor290 3ce40dd2be
Backend - Build & Lint / build (push) Successful in 41m26s
Frontend Admin - EAS Build / build (push) Canceled after 38m58s
chore: build
2026-11-15 20:44:32 +01:00

949 lines
32 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package db
import (
"encoding/json"
"fmt"
"gestion/models"
"log"
"strings"
"time"
"gorm.io/gorm"
)
func (d *Database) CreateClient(client *models.Client) error {
var result struct {
ID int `gorm:"column:id"`
CreatedAt time.Time `gorm:"column:created_at"`
}
err := d.GDB.Raw(`
INSERT INTO clients (username, password, nom, prenom, telephone, command, amende, must_change_password, created_at)
VALUES (?, ?, ?, ?, ?, 0, 0.0, ?, CURRENT_TIMESTAMP)
RETURNING id, created_at`,
client.Username, client.Password, client.Nom, client.Prenom, client.Telephone, client.MustChangePassword,
).Scan(&result).Error
if err != nil {
return fmt.Errorf("erreur lors de la création du client: %w", err)
}
client.ID = result.ID
client.CreatedAt = result.CreatedAt
log.Printf("✅ Client créé avec succès: %s %s (ID: %d)", client.Prenom, client.Nom, client.ID)
return nil
}
// GetClientByID récupère un client par son ID
func (d *Database) GetClientByID(id int) (*models.Client, error) {
var row struct {
ID int `gorm:"column:id"`
Username string `gorm:"column:username"`
Password string `gorm:"column:password"`
Nom string `gorm:"column:nom"`
Prenom string `gorm:"column:prenom"`
Telephone string `gorm:"column:telephone"`
Command int `gorm:"column:command"`
Amende float64 `gorm:"column:amende"`
PointsExtraJSON []byte `gorm:"column:points_extra"`
CreatedAt time.Time `gorm:"column:created_at"`
}
err := d.GDB.Raw(`
SELECT id, username, password, nom, prenom, telephone, command, amende,
COALESCE(points_extra, '{}'::jsonb) as points_extra, created_at
FROM clients WHERE id = ?`, id).Scan(&row).Error
if err != nil {
return nil, fmt.Errorf("erreur lors de la récupération du client: %w", err)
}
if row.ID == 0 {
return nil, fmt.Errorf("client non trouvé")
}
client := &models.Client{
ID: row.ID,
Username: row.Username,
Password: row.Password,
Nom: row.Nom,
Prenom: row.Prenom,
Telephone: row.Telephone,
Command: row.Command,
Amende: row.Amende,
CreatedAt: row.CreatedAt,
}
client.PointsExtra = map[string]int{}
if len(row.PointsExtraJSON) > 0 {
json.Unmarshal(row.PointsExtraJSON, &client.PointsExtra)
}
return client, nil
}
// GetAllClients récupère une page de clients, triée par date de création
// descendante, ainsi que le nombre total de clients en base.
func (d *Database) GetAllClients(limit, offset int) ([]*models.Client, int64, error) {
var rows []struct {
ID int `gorm:"column:id"`
Username string `gorm:"column:username"`
Password string `gorm:"column:password"`
Nom string `gorm:"column:nom"`
Prenom string `gorm:"column:prenom"`
Telephone string `gorm:"column:telephone"`
Command int `gorm:"column:command"`
Amende float64 `gorm:"column:amende"`
ReferralBalance float64 `gorm:"column:referral_balance"`
CancellationsCount int `gorm:"column:cancellations_count"`
LastPenaltyReason string `gorm:"column:last_penalty_reason"`
Parrain string `gorm:"column:parrain"`
PointsExtraJSON []byte `gorm:"column:points_extra"`
CreatedAt time.Time `gorm:"column:created_at"`
}
err := d.GDB.Raw(`
SELECT id, username, password, nom, prenom, telephone, command, amende, referral_balance,
COALESCE(cancellations_count, 0) as cancellations_count,
COALESCE(last_penalty_reason, '') as last_penalty_reason,
COALESCE(parrain, '') as parrain,
COALESCE(points_extra, '{}'::jsonb) as points_extra, created_at
FROM clients ORDER BY created_at DESC LIMIT ? OFFSET ?`, limit, offset).Scan(&rows).Error
if err != nil {
return nil, 0, fmt.Errorf("erreur lors de la récupération des clients: %w", err)
}
var total int64
if err := d.GDB.Table("clients").Count(&total).Error; err != nil {
return nil, 0, fmt.Errorf("erreur lors du comptage des clients: %w", err)
}
clients := make([]*models.Client, 0, len(rows))
for _, row := range rows {
client := &models.Client{
ID: row.ID,
Username: row.Username,
Password: row.Password,
Nom: row.Nom,
Prenom: row.Prenom,
Telephone: row.Telephone,
Command: row.Command,
Amende: row.Amende,
ReferralBalance: row.ReferralBalance,
CancellationsCount: row.CancellationsCount,
LastPenaltyReason: row.LastPenaltyReason,
Parrain: row.Parrain,
CreatedAt: row.CreatedAt,
}
client.PointsExtra = map[string]int{}
if len(row.PointsExtraJSON) > 0 {
json.Unmarshal(row.PointsExtraJSON, &client.PointsExtra)
}
clients = append(clients, client)
}
return clients, total, nil
}
// UpdateClient met à jour un client existant
func (d *Database) UpdateClient(client *models.Client) error {
result := d.GDB.Model(&models.Client{}).Where("id = ?", client.ID).Updates(map[string]any{
"username": client.Username,
"nom": client.Nom,
"prenom": client.Prenom,
"telephone": client.Telephone,
"command": client.Command,
"amende": client.Amende,
})
if result.Error != nil {
return fmt.Errorf("erreur lors de la mise à jour du client: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
return nil
}
// DeleteClient supprime un client
func (d *Database) DeleteClient(id int) error {
_ = d.RevokeAllUserTokens(id, "client")
result := d.GDB.Delete(&models.Client{}, id)
if result.Error != nil {
return fmt.Errorf("erreur lors de la suppression du client: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
return nil
}
// UpdateClientPassword met à jour le mot de passe d'un client
func (d *Database) UpdateClientPassword(clientID int, hashedPassword string) error {
result := d.GDB.Model(&models.Client{}).Where("id = ?", clientID).Update("password", hashedPassword)
if result.Error != nil {
return fmt.Errorf("erreur lors de la mise à jour du mot de passe: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
return nil
}
// UpdateClientPasswordAndClearFlag met à jour le mot de passe et remet must_change_password à false
func (d *Database) UpdateClientPasswordAndClearFlag(clientID int, hashedPassword string) error {
result := d.GDB.Model(&models.Client{}).Where("id = ?", clientID).Updates(map[string]any{
"password": hashedPassword,
"must_change_password": false,
})
if result.Error != nil {
return fmt.Errorf("erreur lors de la mise à jour du mot de passe: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
return nil
}
func (d *Database) GetClientAmende(username string) (float64, error) {
var result struct {
Amende float64 `gorm:"column:amende"`
}
err := d.GDB.Raw(`SELECT COALESCE(amende, 0) as amende FROM clients WHERE username = ?`, username).Scan(&result).Error
if err != nil {
log.Printf("❌ [GetClientAmende] Erreur pour %s: %v", username, err)
return 0, fmt.Errorf("erreur récupération pénalités: %w", err)
}
log.Printf("💰 [GetClientAmende] Client %s: %.2f points", username, result.Amende)
return result.Amende, nil
}
// IncrementClientCommandCount incrémente le compteur de commandes du client
func (d *Database) IncrementClientCommandCount(username string) error {
result := d.GDB.Model(&models.Client{}).Where("username = ?", username).UpdateColumn("command", gorm.Expr("command + 1"))
if result.Error != nil {
return fmt.Errorf("erreur lors de l'incrémentation du compteur: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
return nil
}
func (d *Database) AddClientPointsByCategory(username string, points int, poolKey string) error {
if poolKey == "" {
poolKey = "pool_0"
}
result := d.GDB.Exec(`
UPDATE clients
SET points_extra = jsonb_set(
COALESCE(points_extra, '{}'::jsonb),
ARRAY[?],
to_jsonb(COALESCE((points_extra->>?)::int, 0) + ?)
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?`,
poolKey, poolKey, points, username)
if result.Error != nil {
return fmt.Errorf("erreur lors de l'ajout de points: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
log.Printf("✅ %d points (key=%s) ajoutés au client %s", points, poolKey, username)
return nil
}
func (d *Database) CalculateAndAddPointsForCommand(commandID int, username string) (int, error) {
var totalPoints int
err := d.GDB.Transaction(func(tx *gorm.DB) error {
points, _, err := d.CalculateAndAddPointsForCommandTx(tx, commandID, username)
if err != nil {
return err
}
totalPoints = points
return nil
})
if err != nil {
return 0, err
}
return totalPoints, nil
}
func (d *Database) GetClientByTelephone(telephone string) (*models.Client, error) {
var row struct {
ID int `gorm:"column:id"`
Username string `gorm:"column:username"`
Password string `gorm:"column:password"`
Nom string `gorm:"column:nom"`
Prenom string `gorm:"column:prenom"`
Telephone string `gorm:"column:telephone"`
Command int `gorm:"column:command"`
Amende float64 `gorm:"column:amende"`
CreatedAt time.Time `gorm:"column:created_at"`
}
err := d.GDB.Raw(`
SELECT id, username, password, nom, prenom, telephone, command, amende, created_at
FROM clients WHERE telephone = ?`, telephone).Scan(&row).Error
if err != nil {
return nil, fmt.Errorf("erreur lors de la récupération du client: %w", err)
}
if row.ID == 0 {
return nil, nil
}
return &models.Client{
ID: row.ID,
Username: row.Username,
Password: row.Password,
Nom: row.Nom,
Prenom: row.Prenom,
Telephone: row.Telephone,
Command: row.Command,
Amende: row.Amende,
CreatedAt: row.CreatedAt,
}, nil
}
// GetClientByUsername récupère un client par son username
// GetClientsByUsernames charge plusieurs clients en une seule requête.
// Retourne map[username]*Client ; les usernames sans correspondance sont absents de la map.
func (d *Database) GetClientsByUsernames(usernames []string) (map[string]*models.Client, error) {
result := make(map[string]*models.Client, len(usernames))
if len(usernames) == 0 {
return result, nil
}
var rows []struct {
ID int `gorm:"column:id"`
Username string `gorm:"column:username"`
Nom string `gorm:"column:nom"`
Prenom string `gorm:"column:prenom"`
}
if err := d.GDB.Raw(`SELECT id, username, nom, prenom FROM clients WHERE username IN ?`, usernames).Scan(&rows).Error; err != nil {
return nil, err
}
for _, r := range rows {
result[r.Username] = &models.Client{ID: r.ID, Username: r.Username, Nom: r.Nom, Prenom: r.Prenom}
}
return result, nil
}
func (d *Database) GetClientByUsername(username string) (*models.Client, error) {
var row struct {
ID int `gorm:"column:id"`
Username string `gorm:"column:username"`
Password string `gorm:"column:password"`
Nom string `gorm:"column:nom"`
Prenom string `gorm:"column:prenom"`
Telephone string `gorm:"column:telephone"`
Command int `gorm:"column:command"`
Amende float64 `gorm:"column:amende"`
MustChangePassword bool `gorm:"column:must_change_password"`
PointsExtraJSON []byte `gorm:"column:points_extra"`
CreatedAt time.Time `gorm:"column:created_at"`
TwoFAEnabled bool `gorm:"column:two_fa_enabled"`
}
err := d.GDB.Raw(`
SELECT id, username, password, nom, prenom, telephone, command, amende,
must_change_password, COALESCE(points_extra, '{}'::jsonb) as points_extra, created_at,
two_fa_enabled
FROM clients WHERE username = ?`, username).Scan(&row).Error
if err != nil {
return nil, fmt.Errorf("erreur lors de la récupération du client: %w", err)
}
if row.ID == 0 {
return nil, nil
}
client := &models.Client{
ID: row.ID,
Username: row.Username,
Password: row.Password,
Nom: row.Nom,
Prenom: row.Prenom,
Telephone: row.Telephone,
Command: row.Command,
Amende: row.Amende,
MustChangePassword: row.MustChangePassword,
CreatedAt: row.CreatedAt,
TwoFAEnabled: row.TwoFAEnabled,
}
client.PointsExtra = map[string]int{}
if len(row.PointsExtraJSON) > 0 {
json.Unmarshal(row.PointsExtraJSON, &client.PointsExtra)
}
return client, nil
}
func (d *Database) SetClientTwoFAEnabled(clientID int, enabled bool) error {
return d.GDB.Model(&models.Client{}).Where("id = ?", clientID).Update("two_fa_enabled", enabled).Error
}
func (d *Database) GetClientPenaltiesInfo(username string) (map[string]any, error) {
amende, err := d.GetClientAmende(username)
if err != nil {
return nil, err
}
cancellationsCount, err := d.GetClientCancellationsCount(username)
if err != nil {
log.Printf("⚠️ [GetClientPenaltiesInfo] Erreur récup annulations: %v", err)
cancellationsCount = 0
}
cancellationHistory, err := d.GetClientCancellationHistory(username)
if err != nil {
log.Printf("⚠️ [GetClientPenaltiesInfo] Erreur récup historique: %v", err)
cancellationHistory = map[string]any{
"cancellations_count": cancellationsCount,
"next_penalty": 20,
}
}
info := map[string]any{
"username": username,
"total_penalty": amende,
"cancellations_count": cancellationsCount,
"cancellation_history": cancellationHistory,
"has_penalties": amende > 0,
}
return info, nil
}
// ResetClientPoint réinitialise les points d'un client.
// extraPoolKey != "" → reset points_extra[extraPoolKey] uniquement
// extraPoolKey == "" (poolIdx=-1) → reset total points_extra
func (d *Database) ResetClientPoint(username string, poolIdx int, extraPoolKey string) error {
if extraPoolKey != "" {
err := d.GDB.Exec(`
UPDATE clients SET points_extra = points_extra - ?, updated_at = CURRENT_TIMESTAMP
WHERE username = ?`, extraPoolKey, username).Error
if err != nil {
log.Printf("❌ [ResetClientPointAdmin] Erreur UPDATE extra: %v", err)
} else {
cacheKey := fmt.Sprintf("client:%s", username)
Redis.Del(RedisCtx, cacheKey)
}
return err
}
// -1 ou poolIdx sans clé → reset total
result := d.GDB.Exec(`
UPDATE clients SET points_extra = '{}'::jsonb, updated_at = CURRENT_TIMESTAMP
WHERE username = ?`, username)
if result.Error != nil {
log.Printf("❌ [ResetClientPointAdmin] Erreur UPDATE: %v", result.Error)
return fmt.Errorf("erreur reset points: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
cacheKey := fmt.Sprintf("client:%s", username)
Redis.Del(RedisCtx, cacheKey)
return nil
}
func (d *Database) ResetClientPenalties(username string, _ bool) error {
log.Printf("🔄 [ResetClientPenalties] Reset amende + cancellations_count pour %s", username)
result := d.GDB.Exec(
`UPDATE clients SET amende = 0, cancellations_count = 0, updated_at = CURRENT_TIMESTAMP WHERE username = ?`,
username,
)
if result.Error != nil {
log.Printf("❌ [ResetClientPenalties] Erreur UPDATE: %v", result.Error)
return fmt.Errorf("erreur reset pénalités: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("client non trouvé")
}
cacheKey := fmt.Sprintf("client:%s", username)
Redis.Del(RedisCtx, cacheKey)
return nil
}
func (d *Database) GetAllClientsWithPenalties() ([]map[string]any, error) {
var rows []struct {
Username string `gorm:"column:username"`
Amende float64 `gorm:"column:amende"`
CancellationsCount int `gorm:"column:cancellations_count"`
UpdatedAt any `gorm:"column:updated_at"`
}
err := d.GDB.Raw(`
SELECT username, amende, COALESCE(cancellations_count, 0) as cancellations_count, updated_at
FROM clients
WHERE amende > 0
ORDER BY amende DESC`).Scan(&rows).Error
if err != nil {
log.Printf("❌ [GetAllClientsWithPenalties] Erreur query: %v", err)
return nil, fmt.Errorf("erreur récupération clients: %w", err)
}
clients := make([]map[string]any, 0, len(rows))
for _, row := range rows {
clients = append(clients, map[string]any{
"username": row.Username,
"total_penalty": row.Amende,
"cancellations_count": row.CancellationsCount,
"last_updated": row.UpdatedAt,
})
}
log.Printf("📊 [GetAllClientsWithPenalties] %d clients avec pénalités", len(clients))
return clients, nil
}
func (d *Database) GetClientPenaltiesStats() (map[string]any, error) {
var result struct {
ClientsWithPenalties int `gorm:"column:clients_with_penalties"`
TotalPenalties float64 `gorm:"column:total_penalties"`
AvgPenalty float64 `gorm:"column:avg_penalty"`
MaxPenalty float64 `gorm:"column:max_penalty"`
TotalClients int `gorm:"column:total_clients"`
}
err := d.GDB.Raw(`
SELECT
COUNT(CASE WHEN amende > 0 THEN 1 END) as clients_with_penalties,
COALESCE(SUM(amende), 0) as total_penalties,
COALESCE(AVG(amende), 0) as avg_penalty,
COALESCE(MAX(amende), 0) as max_penalty,
COUNT(*) as total_clients
FROM clients`).Scan(&result).Error
if err != nil {
log.Printf("❌ [GetClientPenaltiesStats] Erreur: %v", err)
return nil, fmt.Errorf("erreur récupération stats: %w", err)
}
stats := map[string]any{
"clients_with_penalties": result.ClientsWithPenalties,
"total_penalties": result.TotalPenalties,
"average_penalty": result.AvgPenalty,
"max_penalty": result.MaxPenalty,
"total_clients": result.TotalClients,
}
log.Printf("📊 [GetClientPenaltiesStats] Stats: %d/%d clients avec pénalités",
result.ClientsWithPenalties, result.TotalClients)
return stats, nil
}
func (d *Database) CalculateAndAddPointsForCommandTx(tx *gorm.DB, commandID int, username string) (int, string, error) {
log.Printf("💰 [CalcPointsTx] START - cmd=%d, user=%s", commandID, username)
// Charger les paramètres globaux
settings, err := d.GetSettings()
if err != nil {
log.Printf("⚠️ [CalcPointsTx] Erreur lecture settings, utilisation des défauts: %v", err)
settings = DefaultSettings()
}
pools := settings.PointsPools
if len(pools) == 0 {
log.Printf("ℹ️ [CalcPointsTx] Aucun pool configuré → 0 points")
return 0, "", nil
}
// Construire la map catégorie → index de pool
catToPool := make(map[string]int)
for i, pool := range pools {
for _, cat := range pool.Categories {
catToPool[strings.ToLower(cat)] = i
}
}
if len(catToPool) == 0 {
log.Printf("ℹ️ [CalcPointsTx] Aucune catégorie assignée aux pools → 0 points")
return 0, "", nil
}
// ✅ ÉTAPE 1: Récupérer tous les items de la commande avec leurs catégories
var items []struct {
Quantite float64 `gorm:"column:quantite"`
Prix float64 `gorm:"column:prix"`
Category string `gorm:"column:category"`
}
if err := tx.Raw(`
SELECT ci.quantite, ci.prix, COALESCE(p.category, '') as category
FROM command_items ci
LEFT JOIN products p ON ci.product_id = p.id
WHERE ci.command_id = ?
`, commandID).Scan(&items).Error; err != nil {
log.Printf("❌ [CalcPointsTx] Erreur query items: %v", err)
return 0, "", fmt.Errorf("erreur récupération items: %w", err)
}
if len(items) == 0 {
log.Printf("⚠️ [CalcPointsTx] Aucun item trouvé pour cmd %d", commandID)
return 0, "", nil
}
poolTotals := make([]float64, len(pools))
for _, item := range items {
catLower := strings.ToLower(item.Category)
if poolIdx, ok := catToPool[catLower]; ok {
poolTotals[poolIdx] += item.Prix
}
}
for i, t := range poolTotals {
log.Printf("📊 [CalcPointsTx] Pool[%d] (%s): %.2f€", i, pools[i].Name, t)
}
// ✅ ÉTAPE 2: Calculer les points pour tous les pools
var totalPoints int
var pointCategory string
poolPts := make([]int, len(pools))
var categoryParts []string
for i, pool := range pools {
poolPts[i] = CalcPointsFromTiers(poolTotals[i], pool.Tiers)
totalPoints += poolPts[i]
if poolPts[i] > 0 {
categoryParts = append(categoryParts, pool.Name)
}
}
log.Printf("💰 [CalcPointsTx] points par pool: %v, total=%d", poolPts, totalPoints)
// totalPoints == 0 ne doit sauter que le crédit (rien à créditer) — pas
// l'ÉTAPE 3 plus bas, qui doit tourner même sans aucun point gagné sur
// cette commande : sinon un article récompense d'une commande dont le
// reste du panier n'atteint aucun palier de points ne verrait jamais sa
// réservation libérée/son solde débité à l'approbation.
if totalPoints > 0 {
if len(categoryParts) > 0 {
pointCategory = strings.Join(categoryParts, " & ")
} else {
pointCategory = "points"
}
for i, pool := range pools {
if poolPts[i] == 0 {
continue
}
if err := tx.Exec(`
UPDATE clients
SET points_extra = jsonb_set(
COALESCE(points_extra, '{}'::jsonb),
ARRAY[?],
to_jsonb(COALESCE((points_extra->>?)::int, 0) + ?)
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?
`, pool.Key, pool.Key, poolPts[i], username).Error; err != nil {
log.Printf("❌ [CalcPointsTx] Erreur UPDATE points_extra pool[%d] (%s): %v", i, pool.Key, err)
return 0, "", fmt.Errorf("erreur mise à jour points pool[%d]: %w", i, err)
}
log.Printf("💰 [CalcPointsTx] pool[%d] (%s / key=%s): +%d pts", i, pool.Name, pool.Key, poolPts[i])
}
log.Printf("🎉 [CalcPointsTx] SUCCÈS - %d points [%s] → %s", totalPoints, pointCategory, username)
}
// ✅ ÉTAPE 3: Déduire les points des récompenses reçues dans cette commande.
// DISTINCT sur reward_pool_key : une seule réclamation (un seul claim, un
// seul threshold consommé) peut avoir ajouté plusieurs lignes pour le même
// pool (toutes les catégories/produits éligibles) — sans ce DISTINCT, une
// commande avec plusieurs lignes récompense pour le même pool débiterait
// threshold points plusieurs fois pour une seule récompense réellement
// accordée.
var poolKeys []string
if err := tx.Raw(`
SELECT DISTINCT reward_pool_key FROM command_items
WHERE command_id = ? AND is_reward = true AND reward_pool_key != ''
`, commandID).Scan(&poolKeys).Error; err != nil {
log.Printf("⚠️ [CalcPointsTx] Erreur query reward items: %v", err)
}
for _, poolKey := range poolKeys {
if settings.PointsReward == nil || settings.PointsReward.Threshold <= 0 {
break
}
threshold := settings.PointsReward.Threshold
// Déduire threshold points de points_extra[poolKey] (plancher à 0)
if err := tx.Exec(`
UPDATE clients
SET points_extra = jsonb_set(
COALESCE(points_extra, '{}'::jsonb),
ARRAY[?],
to_jsonb(GREATEST(0, COALESCE((points_extra->>?)::int, 0) - ?))
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?
`, poolKey, poolKey, threshold, username).Error; err != nil {
log.Printf("⚠️ [CalcPointsTx] Erreur déduction points reward pool=%s: %v", poolKey, err)
} else {
log.Printf("🎁 [CalcPointsTx] Récompense reçue: -%d pts pool=%s → %s", threshold, poolKey, username)
}
// Décrémenter points_redeemed[poolKey] (plancher à 0)
if err := tx.Exec(`
UPDATE clients
SET points_redeemed = jsonb_set(
COALESCE(points_redeemed, '{}'::jsonb),
ARRAY[?],
to_jsonb(GREATEST(0, COALESCE((points_redeemed->>?)::int, 0) - 1))
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?
`, poolKey, poolKey, username).Error; err != nil {
log.Printf("⚠️ [CalcPointsTx] Erreur décrément redeemed pool=%s: %v", poolKey, err)
}
}
return totalPoints, pointCategory, nil
}
func (d *Database) CanUserAccessCommand(
commandID int,
username string,
role string,
) (bool, error) {
// 👑 Admin : accès total
if role == "admin" {
return true, nil
}
var exists bool
// 🚚 Livreur : seulement commandes assignées
if role == "livreur" {
err := d.GDB.Raw(`
SELECT EXISTS(
SELECT 1 FROM commandes
WHERE id = ? AND livreur_assign = ?
)
`, commandID, username).Scan(&exists).Error
return exists, err
}
// 👤 User : seulement SES commandes
err := d.GDB.Raw(`
SELECT EXISTS(
SELECT 1 FROM commandes
WHERE id = ? AND username = ?
)
`, commandID, username).Scan(&exists).Error
return exists, err
}
// GetClientPointsAndRewards retourne les points cumulés et les récompenses réclamées pour un client.
func (d *Database) GetClientPointsAndRewards(username string) (pointsExtra map[string]int, pointsRedeemed map[string]int, err error) {
var row struct {
PointsExtraJSON []byte `gorm:"column:points_extra"`
PointsRedeemedJSON []byte `gorm:"column:points_redeemed"`
}
if err = d.GDB.Raw(`
SELECT COALESCE(points_extra, '{}'::jsonb) as points_extra,
COALESCE(points_redeemed, '{}'::jsonb) as points_redeemed
FROM clients WHERE username = ?`, username).Scan(&row).Error; err != nil {
return nil, nil, fmt.Errorf("erreur lecture points client: %w", err)
}
pointsExtra = map[string]int{}
pointsRedeemed = map[string]int{}
if len(row.PointsExtraJSON) > 0 {
json.Unmarshal(row.PointsExtraJSON, &pointsExtra)
}
if len(row.PointsRedeemedJSON) > 0 {
json.Unmarshal(row.PointsRedeemedJSON, &pointsRedeemed)
}
return pointsExtra, pointsRedeemed, nil
}
// claimPoolRewardTx vérifie l'éligibilité et consomme une récompense pour un
// pool donné, dans la transaction fournie — factorisée pour être appelée
// seule (ClaimPoolReward) ou combinée avec la livraison du produit dans la
// même transaction (ClaimPoolRewardAndAddToBasket), afin qu'une récompense
// ne soit jamais consommée sans que son produit soit effectivement livré.
func claimPoolRewardTx(tx *gorm.DB, username, poolKey string, threshold int) (remainingAvailable int, err error) {
var row struct {
Points int `gorm:"column:pts"`
Redeemed int `gorm:"column:redeemed"`
}
if err := tx.Raw(`
SELECT
COALESCE((points_extra->>?)::int, 0) as pts,
COALESCE((points_redeemed->>?)::int, 0) as redeemed
FROM clients WHERE username = ? FOR UPDATE`,
poolKey, poolKey, username).Scan(&row).Error; err != nil {
return 0, fmt.Errorf("erreur lecture: %w", err)
}
earned := row.Points / threshold
available := earned - row.Redeemed
if available <= 0 {
return 0, fmt.Errorf("pas de récompense disponible pour ce pool")
}
if err := tx.Exec(`
UPDATE clients
SET points_redeemed = jsonb_set(
COALESCE(points_redeemed, '{}'::jsonb),
ARRAY[?],
to_jsonb(COALESCE((points_redeemed->>?)::int, 0) + 1)
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?`,
poolKey, poolKey, username).Error; err != nil {
return 0, err
}
return earned - (row.Redeemed + 1), nil
}
func releaseRewardReservationTx(tx *gorm.DB, username, poolKey string) error {
return tx.Exec(`
UPDATE clients
SET points_redeemed = jsonb_set(
COALESCE(points_redeemed, '{}'::jsonb),
ARRAY[?],
to_jsonb(GREATEST(0, COALESCE((points_redeemed->>?)::int, 0) - 1))
), updated_at = CURRENT_TIMESTAMP
WHERE username = ?`,
poolKey, poolKey, username).Error
}
func (d *Database) ReleaseRewardReservationsForCommandTx(tx *gorm.DB, commandID int, username string) error {
var poolKeys []string
if err := tx.Raw(`
SELECT DISTINCT reward_pool_key FROM command_items
WHERE command_id = ? AND is_reward = true AND reward_pool_key != ''
`, commandID).Scan(&poolKeys).Error; err != nil {
return fmt.Errorf("erreur lecture récompenses commande: %w", err)
}
for _, poolKey := range poolKeys {
if err := releaseRewardReservationTx(tx, username, poolKey); err != nil {
return fmt.Errorf("erreur libération réservation récompense pool=%s: %w", poolKey, err)
}
}
return nil
}
func (d *Database) ClaimPoolReward(username, poolKey string, threshold int) (remainingAvailable int, err error) {
err = d.GDB.Transaction(func(tx *gorm.DB) error {
var err error
remainingAvailable, err = claimPoolRewardTx(tx, username, poolKey, threshold)
return err
})
if err != nil {
return 0, err
}
return remainingAvailable, nil
}
func (d *Database) ClaimPoolRewardAndAddToBasket(username, poolKey string, threshold int, items []models.RewardItem) (remainingAvailable int, added []models.Panier, err error) {
err = d.GDB.Transaction(func(tx *gorm.DB) error {
var err error
remainingAvailable, err = claimPoolRewardTx(tx, username, poolKey, threshold)
if err != nil {
return err
}
if len(items) > 0 {
added, err = addRewardsToBasketTx(tx, username, items, poolKey)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return 0, nil, err
}
return remainingAvailable, added, nil
}
// ResetClientRedeemed remet à zéro les récompenses réclamées (admin).
func (d *Database) ResetClientRedeemed(username, poolKey string) error {
if poolKey != "" {
return d.GDB.Exec(`
UPDATE clients SET points_redeemed = points_redeemed - ?, updated_at = CURRENT_TIMESTAMP
WHERE username = ?`, poolKey, username).Error
}
return d.GDB.Exec(`
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(&current).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
}