chore: fix
This commit is contained in:
@@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gestion/models"
|
||||
"log"
|
||||
@@ -57,7 +58,7 @@ func (d *Database) GetClientByID(id int) (*models.Client, error) {
|
||||
|
||||
// GetAllClients récupère tous les clients
|
||||
func (d *Database) GetAllClients() ([]*models.Client, error) {
|
||||
query := `SELECT id, username, password, nom, prenom, telephone, command, point, point_zipette, amende, referral_balance, created_at
|
||||
query := `SELECT id, username, password, nom, prenom, telephone, command, point, point_zipette, amende, referral_balance, COALESCE(points_extra, '{}'::jsonb), created_at
|
||||
FROM clients ORDER BY created_at DESC`
|
||||
|
||||
rows, err := d.Query(query)
|
||||
@@ -69,6 +70,7 @@ func (d *Database) GetAllClients() ([]*models.Client, error) {
|
||||
var clients []*models.Client
|
||||
for rows.Next() {
|
||||
client := &models.Client{}
|
||||
var pointsExtraJSON []byte
|
||||
err := rows.Scan(
|
||||
&client.ID,
|
||||
&client.Username,
|
||||
@@ -81,11 +83,16 @@ func (d *Database) GetAllClients() ([]*models.Client, error) {
|
||||
&client.PointZipette,
|
||||
&client.Amende,
|
||||
&client.ReferralBalance,
|
||||
&pointsExtraJSON,
|
||||
&client.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("erreur lors du scan du client: %w", err)
|
||||
}
|
||||
client.PointsExtra = map[string]int{}
|
||||
if len(pointsExtraJSON) > 0 {
|
||||
json.Unmarshal(pointsExtraJSON, &client.PointsExtra)
|
||||
}
|
||||
clients = append(clients, client)
|
||||
}
|
||||
|
||||
@@ -470,12 +477,24 @@ func (d *Database) CheckClientCanOrder(username string) (bool, float64, error) {
|
||||
return true, 0, nil
|
||||
}
|
||||
|
||||
func (d *Database) ResetClientPoint(username string, resetCancellationsPoint bool) error {
|
||||
// ResetClientPoint réinitialise les points d'un client.
|
||||
// poolIdx=0 → point, poolIdx=1 → point_zipette, poolIdx=-1 → tous
|
||||
// poolIdx>=2 → points_extra[extraPoolKey]
|
||||
func (d *Database) ResetClientPoint(username string, poolIdx int, extraPoolKey string) error {
|
||||
var query string
|
||||
if resetCancellationsPoint {
|
||||
query = `UPDATE clients SET point = 0, point_zipette = 0, updated_at = CURRENT_TIMESTAMP WHERE username = $1`
|
||||
} else {
|
||||
switch {
|
||||
case poolIdx == 0:
|
||||
query = `UPDATE clients SET point = 0, updated_at = CURRENT_TIMESTAMP WHERE username = $1`
|
||||
case poolIdx == 1:
|
||||
query = `UPDATE clients SET point_zipette = 0, updated_at = CURRENT_TIMESTAMP WHERE username = $1`
|
||||
case poolIdx >= 2 && extraPoolKey != "":
|
||||
_, err := d.Exec(
|
||||
`UPDATE clients SET points_extra = points_extra - $2, updated_at = CURRENT_TIMESTAMP WHERE username = $1`,
|
||||
username, extraPoolKey,
|
||||
)
|
||||
return err
|
||||
default: // -1 → reset total
|
||||
query = `UPDATE clients SET point = 0, point_zipette = 0, points_extra = '{}'::jsonb, updated_at = CURRENT_TIMESTAMP WHERE username = $1`
|
||||
}
|
||||
|
||||
result, err := d.Exec(query, username)
|
||||
@@ -943,18 +962,47 @@ func (d *Database) CalculateAndAddPointsForCommandTx(tx *sql.Tx, commandID int,
|
||||
}
|
||||
totalPoints = pts0 + pts1
|
||||
|
||||
log.Printf("💰 [CalcPointsTx] pool[0]=%d pts, pool[1]=%d pts", pts0, pts1)
|
||||
// Pools supplémentaires (index 2+) → points_extra JSONB
|
||||
for i := 2; i < len(pools); i++ {
|
||||
ptsExtra := CalcPointsFromTiers(poolTotals[i], pools[i].Tiers)
|
||||
if ptsExtra == 0 {
|
||||
continue
|
||||
}
|
||||
totalPoints += ptsExtra
|
||||
poolKey := pools[i].Key
|
||||
_, err = tx.Exec(`
|
||||
UPDATE clients
|
||||
SET points_extra = jsonb_set(
|
||||
COALESCE(points_extra, '{}'::jsonb),
|
||||
ARRAY[$2],
|
||||
to_jsonb(COALESCE((points_extra->>$2)::int, 0) + $3)
|
||||
), updated_at = CURRENT_TIMESTAMP
|
||||
WHERE username = $1
|
||||
`, username, poolKey, ptsExtra)
|
||||
if err != nil {
|
||||
log.Printf("❌ [CalcPointsTx] Erreur UPDATE points_extra pool[%d]: %v", i, err)
|
||||
return 0, "", fmt.Errorf("erreur mise à jour points pool[%d]: %w", i, err)
|
||||
}
|
||||
log.Printf("💰 [CalcPointsTx] pool[%d] (%s): +%d pts → points_extra", i, pools[i].Name, ptsExtra)
|
||||
}
|
||||
|
||||
log.Printf("💰 [CalcPointsTx] pool[0]=%d pts, pool[1]=%d pts, total=%d pts", pts0, pts1, totalPoints)
|
||||
|
||||
if totalPoints == 0 {
|
||||
return 0, "", nil
|
||||
}
|
||||
|
||||
if pts0 > 0 && pts1 > 0 {
|
||||
pointCategory = pools[0].Name + " & " + pools[1].Name
|
||||
} else if pts1 > 0 {
|
||||
pointCategory = pools[1].Name
|
||||
var categoryParts []string
|
||||
if pts0 > 0 {
|
||||
categoryParts = append(categoryParts, pools[0].Name)
|
||||
}
|
||||
if pts1 > 0 {
|
||||
categoryParts = append(categoryParts, pools[1].Name)
|
||||
}
|
||||
if len(categoryParts) > 0 {
|
||||
pointCategory = strings.Join(categoryParts, " & ")
|
||||
} else {
|
||||
pointCategory = pools[0].Name
|
||||
pointCategory = "points"
|
||||
}
|
||||
|
||||
if len(pools) == 1 || pts1 == 0 {
|
||||
|
||||
@@ -161,6 +161,11 @@ func InitDB() *Database {
|
||||
log.Fatalf("❌ Erreur migration commandes.referral_used: %v", err)
|
||||
}
|
||||
|
||||
// Migration: points extra pour les pools de points supplémentaires (pool[2+])
|
||||
if _, err = database.Exec(`ALTER TABLE clients ADD COLUMN IF NOT EXISTS points_extra JSONB NOT NULL DEFAULT '{}'::jsonb`); err != nil {
|
||||
log.Fatalf("❌ Erreur migration clients.points_extra: %v", err)
|
||||
}
|
||||
|
||||
// Lancer le nettoyage périodique des tokens expirés
|
||||
go database.cleanExpiredTokensPeriodically()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user