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()
|
||||
|
||||
|
||||
@@ -50,6 +50,15 @@ func GetMyCompletedOrders(c *gin.Context) {
|
||||
// ✅ Récupérer les infos client pour statistiques
|
||||
client, err := database.GetClientByUsername(usernameStr)
|
||||
|
||||
// ✅ Récupérer les noms des pools de points
|
||||
poolNames := []string{"Pool 1", "Pool 2"}
|
||||
if settings, sErr := database.GetSettings(); sErr == nil && len(settings.PointsPools) > 0 {
|
||||
poolNames = make([]string, len(settings.PointsPools))
|
||||
for i, p := range settings.PointsPools {
|
||||
poolNames[i] = p.Name
|
||||
}
|
||||
}
|
||||
|
||||
response := gin.H{
|
||||
"success": true,
|
||||
"commands": commands,
|
||||
@@ -57,10 +66,21 @@ func GetMyCompletedOrders(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err == nil && client != nil {
|
||||
// Construire le tableau générique des valeurs par pool
|
||||
poolPoints := make([]int, len(poolNames))
|
||||
if len(poolPoints) > 0 {
|
||||
poolPoints[0] = client.Point
|
||||
}
|
||||
if len(poolPoints) > 1 {
|
||||
poolPoints[1] = client.PointZipette
|
||||
}
|
||||
|
||||
response["client_stats"] = gin.H{
|
||||
"username": client.Username,
|
||||
"total_commands": client.Command,
|
||||
"points": client.Point,
|
||||
"pool_points": poolPoints,
|
||||
"pool_names": poolNames,
|
||||
"penalties": client.Amende,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -952,16 +952,23 @@ func ResetClientPointAdmin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
ResetCancellationsPoints bool `json:"reset_cancellations_points"`
|
||||
Pool int `json:"pool"` // 0=pool principal, 1=pool secondaire, -1=tous (défaut)
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
req.ResetCancellationsPoints = false
|
||||
req.Pool = -1 // défaut : reset total
|
||||
if err := c.ShouldBindJSON(&req); err == nil {
|
||||
// valeur reçue correctement
|
||||
}
|
||||
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
err := database.ResetClientPoint(username, req.ResetCancellationsPoints)
|
||||
extraPoolKey := ""
|
||||
if req.Pool >= 2 {
|
||||
if settings, err := database.GetSettings(); err == nil && req.Pool < len(settings.PointsPools) {
|
||||
extraPoolKey = settings.PointsPools[req.Pool].Key
|
||||
}
|
||||
}
|
||||
|
||||
err := database.ResetClientPoint(username, req.Pool, extraPoolKey)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Erreur lors de la réinitialisation",
|
||||
|
||||
@@ -19,12 +19,21 @@ func GetPublicSettings(c *gin.Context) {
|
||||
settings = db.DefaultSettings()
|
||||
}
|
||||
|
||||
poolNames := make([]string, len(settings.PointsPools))
|
||||
poolKeys := make([]string, len(settings.PointsPools))
|
||||
for i, p := range settings.PointsPools {
|
||||
poolNames[i] = p.Name
|
||||
poolKeys[i] = p.Key
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"penalties_enabled": settings.PenaltiesEnabled,
|
||||
"show_amende_score": settings.ShowAmendeScore,
|
||||
"points_enabled": settings.PointsEnabled,
|
||||
"points_separated": len(settings.PointsPools) > 1,
|
||||
"pool_names": poolNames,
|
||||
"pool_keys": poolKeys,
|
||||
"referral_enabled": settings.ReferralEnabled,
|
||||
"delivery_schedule": settings.DeliverySchedule,
|
||||
})
|
||||
|
||||
@@ -11,8 +11,9 @@ type Client struct {
|
||||
Prenom string `json:"prenom"`
|
||||
Telephone string `json:"telephone"`
|
||||
Command int `json:"command"`
|
||||
Point int `json:"point"`
|
||||
PointZipette int `json:"points_zipette"`
|
||||
Point int `json:"point"`
|
||||
PointZipette int `json:"points_zipette"`
|
||||
PointsExtra map[string]int `json:"points_extra"` // pools[2+]
|
||||
Amende float64 `json:"amende"`
|
||||
CancellationsCount int `json:"cancellations_count"`
|
||||
LastPenaltyReason string `json:"last_penalty_reason"`
|
||||
|
||||
Reference in New Issue
Block a user