package middleware import ( "gestion/db" "log" "net/http" "github.com/gin-gonic/gin" ) func BlockClientIfPenalty(c *gin.Context) { database := c.MustGet("database").(*db.Database) if settings, err := database.GetSettings(); err == nil && !settings.PenaltiesEnabled { c.Next() return } clientID, exists := c.Get("client_id") if !exists { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentification requise"}) c.Abort() return } if id, ok := clientID.(int); ok { if session, err := database.GetClientSession(id); err == nil && session.PenaltyCache > 0 { log.Printf("đŸš« [PENALTY] Checkout bloquĂ© pour client_id=%d (amende=%.2f via cache)", id, session.PenaltyCache) c.JSON(http.StatusForbidden, gin.H{ "error": "Commande bloquĂ©e : vous avez une amende en attente de paiement", "amende": session.PenaltyCache, "blocked": true, }) c.Abort() return } } username, exists := c.Get("username") if !exists { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentification requise"}) c.Abort() return } usernameStr, ok := username.(string) if !ok || usernameStr == "" { c.JSON(http.StatusUnauthorized, gin.H{"error": "Username invalide"}) c.Abort() return } amende, err := database.GetClientAmende(usernameStr) if err != nil { log.Printf("❌ [PENALTY] Erreur vĂ©rification amende pour %s: %v", usernameStr, err) c.Next() return } if amende > 0 { log.Printf("đŸš« [PENALTY] Checkout bloquĂ© pour %s (amende=%.2f via DB)", usernameStr, amende) c.JSON(http.StatusForbidden, gin.H{ "error": "Commande bloquĂ©e : vous avez une amende en attente de paiement", "amende": amende, "blocked": true, }) c.Abort() return } c.Next() }