chore: update
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"gestion/db"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// BlockClientIfPenalty bloque le checkout si le client a une amende non payée.
|
||||
// Lit d'abord les paramètres globaux (penalties_enabled), puis le PenaltyCache Redis, fallback DB.
|
||||
func BlockClientIfPenalty(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
// Vérifier si les amendes sont activées dans les paramètres globaux
|
||||
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
|
||||
}
|
||||
|
||||
// 1. Tenter le cache Redis via la session
|
||||
if id, ok := clientID.(int); ok {
|
||||
if session, err := database.GetClientSession(id); err == nil {
|
||||
if 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
|
||||
}
|
||||
// Cache présent et amende = 0 → on laisse passer sans requête DB
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback DB si session Redis absente/expirée
|
||||
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)
|
||||
// En cas d'erreur DB on laisse passer pour ne pas bloquer l'utilisateur injustement
|
||||
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()
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gestion/db"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -14,21 +16,67 @@ func OrderHoursMiddleware(c *gin.Context) {
|
||||
loc = time.UTC
|
||||
}
|
||||
now := time.Now().In(loc)
|
||||
weekday := now.Weekday()
|
||||
hour := now.Hour()
|
||||
min := now.Minute()
|
||||
|
||||
// Récupérer le planning depuis les settings DB
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
settings, err := database.GetSettings()
|
||||
if err != nil {
|
||||
log.Printf("⚠️ [CLOCK-MWARE] Erreur lecture settings: %v — accès autorisé par défaut", err)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
sched := settings.DeliverySchedule
|
||||
var day db.DaySchedule
|
||||
switch weekday {
|
||||
case time.Monday:
|
||||
day = sched.Monday
|
||||
case time.Tuesday:
|
||||
day = sched.Tuesday
|
||||
case time.Wednesday:
|
||||
day = sched.Wednesday
|
||||
case time.Thursday:
|
||||
day = sched.Thursday
|
||||
case time.Friday:
|
||||
day = sched.Friday
|
||||
case time.Saturday:
|
||||
day = sched.Saturday
|
||||
case time.Sunday:
|
||||
day = sched.Sunday
|
||||
}
|
||||
|
||||
if !day.Enabled {
|
||||
log.Printf("❌ [CLOCK-MWARE] Commande refusée — jour fermé (%s)", weekday)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Commandes non disponibles aujourd'hui",
|
||||
"message": "La livraison n'est pas disponible ce jour",
|
||||
"current_time": now.Format("15:04"),
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
parseTime := func(t string) int {
|
||||
var h, m int
|
||||
fmt.Sscanf(t, "%d:%d", &h, &m)
|
||||
return h*60 + m
|
||||
}
|
||||
|
||||
currentMinutes := hour*60 + min
|
||||
openMinutes := 13*60 + 55
|
||||
closeMinutes := 23*60 + 30
|
||||
openMinutes := parseTime(day.OpenTime)
|
||||
closeMinutes := parseTime(day.CloseTime)
|
||||
|
||||
if currentMinutes < openMinutes || currentMinutes >= closeMinutes {
|
||||
log.Printf("❌ [CLOCK-MWARE] Commande refusée à %02d:%02d (plage autorisée: 13h55 - 23h30)", hour, min)
|
||||
log.Printf("❌ [CLOCK-MWARE] Commande refusée à %02d:%02d (plage autorisée: %s - %s)", hour, min, day.OpenTime, day.CloseTime)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Commandes non disponibles à cette heure",
|
||||
"message": "Vous pouvez commander entre 13h55 et 23h30",
|
||||
"message": fmt.Sprintf("Vous pouvez commander entre %s et %s", day.OpenTime, day.CloseTime),
|
||||
"current_time": now.Format("15:04"),
|
||||
"open_at": "13:55",
|
||||
"close_at": "23:30",
|
||||
"open_at": day.OpenTime,
|
||||
"close_at": day.CloseTime,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user