74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"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": fmt.Sprintf("Commande bloquée : vous avez une amende de %.0f€ en attente de paiement. Prenez attache avec Milieu Nantais sur signal pour régulariser votre situation..", amende),
|
|
"amende": amende,
|
|
"blocked": true,
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|