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
+21 -50
View File
@@ -9,17 +9,14 @@ import (
"database/sql"
"fmt"
"log"
"maps"
"time"
)
// GetCompletedCommandsByUsername récupère toutes les commandes terminées (approved) d'un utilisateur
// ✅ Retourne uniquement les commandes avec status = "approved"
// ✅ Ordonnées par date de création décroissante (plus récentes en premier)
func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string]interface{}, error) {
log.Printf("📚 [GetCompletedCommands] START - username=%s", username)
func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string]any, error) {
query := `
SELECT
SELECT
id,
username,
status,
@@ -40,7 +37,7 @@ func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string
}
defer rows.Close()
var commands []map[string]interface{}
var commands []map[string]any
for rows.Next() {
var id int
@@ -64,7 +61,7 @@ func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string
return nil, fmt.Errorf("erreur lors du scan de la commande: %w", err)
}
command := map[string]interface{}{
command := map[string]any{
"id": id,
"username": username,
"status": status,
@@ -74,7 +71,6 @@ func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string
"updated_at": updatedAt,
}
// Ajouter livreur_assign seulement s'il n'est pas NULL
if livreurAssign.Valid {
command["livreur_assign"] = livreurAssign.String
} else {
@@ -89,23 +85,17 @@ func (d *Database) GetCompletedCommandsByUsername(username string) ([]map[string
return nil, fmt.Errorf("erreur lors de l'itération des résultats: %w", err)
}
log.Printf("✅ [GetCompletedCommands] %d commandes terminées trouvées pour %s", len(commands), username)
return commands, nil
}
// GetCompletedCommandsWithItems récupère les commandes terminées avec leurs items
// ✅ Retourne les commandes approved avec tous les détails
func (d *Database) GetCompletedCommandsWithItems(username string) ([]map[string]interface{}, error) {
log.Printf("📚 [GetCompletedWithItems] START - username=%s", username)
// 1. Récupérer les commandes terminées
func (d *Database) GetCompletedCommandsWithItems(username string) ([]map[string]any, error) {
commands, err := d.GetCompletedCommandsByUsername(username)
if err != nil {
return nil, err
}
// 2. Pour chaque commande, récupérer les items
var enrichedCommands []map[string]interface{}
var enrichedCommands []map[string]any
for _, command := range commands {
commandID, ok := command["id"].(int)
@@ -117,31 +107,24 @@ func (d *Database) GetCompletedCommandsWithItems(username string) ([]map[string]
items, err := d.GetCommandItems(commandID)
if err != nil {
log.Printf("⚠️ [GetCompletedWithItems] Erreur items pour cmd %d: %v", commandID, err)
items = []map[string]interface{}{}
items = []map[string]any{}
}
// Enrichir la commande
enrichedCommand := make(map[string]interface{})
for k, v := range command {
enrichedCommand[k] = v
}
enrichedCommand := make(map[string]any)
maps.Copy(enrichedCommand, command)
enrichedCommand["items"] = items
enrichedCommand["items_count"] = len(items)
enrichedCommands = append(enrichedCommands, enrichedCommand)
}
log.Printf("✅ [GetCompletedWithItems] %d commandes enrichies", len(enrichedCommands))
return enrichedCommands, nil
}
// GetCommandsStatsByUsername récupère les statistiques des commandes d'un utilisateur
// ✅ Compte total, approved, pending, cancelled, etc.
func (d *Database) GetCommandsStatsByUsername(username string) (map[string]interface{}, error) {
log.Printf("📊 [GetCommandsStats] START - username=%s", username)
func (d *Database) GetCommandsStatsByUsername(username string) (map[string]any, error) {
query := `
SELECT
SELECT
COUNT(*) FILTER (WHERE status = 'approved') as approved_count,
COUNT(*) FILTER (WHERE status = 'pending') as pending_count,
COUNT(*) FILTER (WHERE status = 'assigned') as assigned_count,
@@ -172,7 +155,7 @@ func (d *Database) GetCommandsStatsByUsername(username string) (map[string]inter
return nil, fmt.Errorf("erreur lors de la récupération des statistiques: %w", err)
}
stats := map[string]interface{}{
stats := map[string]any{
"approved_count": approvedCount,
"pending_count": pendingCount,
"assigned_count": assignedCount,
@@ -183,19 +166,15 @@ func (d *Database) GetCommandsStatsByUsername(username string) (map[string]inter
"total_spent": totalSpent,
}
log.Printf("✅ [GetCommandsStats] Stats calculées: total=%d, approved=%d, spent=%.2f€",
totalCount, approvedCount, totalSpent)
return stats, nil
}
// GetCommandsByStatus récupère les commandes d'un utilisateur par statut
// ✅ Permet de filtrer par un statut spécifique
func (d *Database) GetCommandsByStatus(username, status string) ([]map[string]interface{}, error) {
func (d *Database) GetCommandsByStatus(username, status string) ([]map[string]any, error) {
log.Printf("📋 [GetCommandsByStatus] START - username=%s, status=%s", username, status)
query := `
SELECT
SELECT
id,
username,
status,
@@ -211,12 +190,11 @@ func (d *Database) GetCommandsByStatus(username, status string) ([]map[string]in
rows, err := d.Query(query, username, status)
if err != nil {
log.Printf("❌ [GetCommandsByStatus] Erreur query: %v", err)
return nil, fmt.Errorf("erreur lors de la récupération des commandes: %w", err)
}
defer rows.Close()
var commands []map[string]interface{}
var commands []map[string]any
for rows.Next() {
var id int
@@ -240,7 +218,7 @@ func (d *Database) GetCommandsByStatus(username, status string) ([]map[string]in
return nil, fmt.Errorf("erreur lors du scan: %w", err)
}
command := map[string]interface{}{
command := map[string]any{
"id": id,
"username": username,
"status": status,
@@ -260,21 +238,16 @@ func (d *Database) GetCommandsByStatus(username, status string) ([]map[string]in
}
if err = rows.Err(); err != nil {
log.Printf("❌ [GetCommandsByStatus] Erreur itération: %v", err)
return nil, fmt.Errorf("erreur lors de l'itération: %w", err)
}
log.Printf("✅ [GetCommandsByStatus] %d commandes trouvées", len(commands))
return commands, nil
}
// GetRecentCompletedOrders récupère les N dernières commandes terminées d'un utilisateur
// ✅ Utile pour afficher les dernières commandes dans le dashboard
func (d *Database) GetRecentCompletedOrders(username string, limit int) ([]map[string]interface{}, error) {
log.Printf("📚 [GetRecentCompleted] START - username=%s, limit=%d", username, limit)
func (d *Database) GetRecentCompletedOrders(username string, limit int) ([]map[string]any, error) {
query := `
SELECT
SELECT
id,
username,
status,
@@ -296,7 +269,7 @@ func (d *Database) GetRecentCompletedOrders(username string, limit int) ([]map[s
}
defer rows.Close()
var commands []map[string]interface{}
var commands []map[string]any
for rows.Next() {
var id int
@@ -320,7 +293,7 @@ func (d *Database) GetRecentCompletedOrders(username string, limit int) ([]map[s
return nil, fmt.Errorf("erreur lors du scan: %w", err)
}
command := map[string]interface{}{
command := map[string]any{
"id": id,
"username": username,
"status": status,
@@ -340,10 +313,8 @@ func (d *Database) GetRecentCompletedOrders(username string, limit int) ([]map[s
}
if err = rows.Err(); err != nil {
log.Printf("❌ [GetRecentCompleted] Erreur itération: %v", err)
return nil, fmt.Errorf("erreur lors de l'itération: %w", err)
}
log.Printf("✅ [GetRecentCompleted] %d commandes récentes trouvées", len(commands))
return commands, nil
}