chore: refacto

This commit is contained in:
2026-03-27 22:23:46 +01:00
parent 75bf4caaa1
commit 5380abe8ed
67 changed files with 1751 additions and 2573 deletions
+1 -36
View File
@@ -2,7 +2,6 @@ package db
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"os"
@@ -197,37 +196,6 @@ func InitDB() *Database {
log.Fatalf("❌ Erreur migration clients.points_extra: %v", err)
}
// Migration: copier point/point_zipette → points_extra[pool.Key] selon les clés admin
{
var poolsJSON string
_ = database.QueryRow(`SELECT value FROM app_settings WHERE key = 'points_pools'`).Scan(&poolsJSON)
if poolsJSON != "" {
var pools []struct {
Key string `json:"key"`
}
if jsonErr := json.Unmarshal([]byte(poolsJSON), &pools); jsonErr == nil {
if len(pools) > 0 && pools[0].Key != "" {
k0 := pools[0].Key
if _, mErr := database.Exec(`UPDATE clients SET points_extra = points_extra || jsonb_build_object($1::text, point) WHERE point > 0 AND NOT (points_extra ? $1)`, k0); mErr != nil {
log.Printf("⚠️ Migration points_extra pool[0] (%s): %v", k0, mErr)
} else {
log.Printf("✅ Migration points_extra pool[0] key=%s", k0)
}
}
if len(pools) > 1 && pools[1].Key != "" {
k1 := pools[1].Key
if _, mErr := database.Exec(`UPDATE clients SET points_extra = points_extra || jsonb_build_object($1::text, point_zipette) WHERE point_zipette > 0 AND NOT (points_extra ? $1)`, k1); mErr != nil {
log.Printf("⚠️ Migration points_extra pool[1] (%s): %v", k1, mErr)
} else {
log.Printf("✅ Migration points_extra pool[1] key=%s", k1)
}
}
}
} else {
log.Printf("️ Migration points_extra: aucun pool configuré (app_settings vide)")
}
}
// Lancer le nettoyage périodique des tokens expirés
go database.cleanExpiredTokensPeriodically()
@@ -267,8 +235,6 @@ func (db *Database) createTables() error {
prenom VARCHAR(100) NOT NULL,
telephone VARCHAR(20) NOT NULL UNIQUE,
command INTEGER DEFAULT 0,
point INTEGER DEFAULT 0,
point_zipette INTEGER DEFAULT 0,
amende NUMERIC(10,2) DEFAULT 0.0,
cancel_commande INTEGER DEFAULT 0,
cancellations_count INTEGER DEFAULT 0 NOT NULL,
@@ -436,8 +402,7 @@ func (db *Database) createTables() error {
`CREATE INDEX IF NOT EXISTS idx_command_items_client_username ON command_items(client_username);`,
`CREATE INDEX IF NOT EXISTS idx_command_items_status ON command_items(status);`,
`CREATE INDEX IF NOT EXISTS idx_command_items_produit ON command_items(produit);`,
`CREATE INDEX IF NOT EXISTS idx_clients_point_zipette ON clients(point_zipette) WHERE point_zipette > 0;`,
`CREATE INDEX IF NOT EXISTS idx_jwt_user_id ON jwt_tokens(user_id);`,
`CREATE INDEX IF NOT EXISTS idx_jwt_user_id ON jwt_tokens(user_id);`,
`CREATE INDEX IF NOT EXISTS idx_jwt_user_type ON jwt_tokens(user_type);`,
`CREATE INDEX IF NOT EXISTS idx_jwt_token ON jwt_tokens(token);`,
`CREATE INDEX IF NOT EXISTS idx_jwt_date_fin ON jwt_tokens(date_fin);`,