From 1f332643a2ca06a6620cd46845eefde6687e2657 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Sat, 28 Feb 2026 13:28:46 +0100 Subject: [PATCH] chore: add clock middleware --- backend/gestion/main.go | 2 +- .../gestion/middleware/clock_middleware.go | 37 +++++++++++++++++++ backend/gestion/routes/routes.go | 4 +- frontend-prep/src/api/api.ts | 24 +++++++++--- frontend-prep/vite.config.ts | 10 ++++- 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 backend/gestion/middleware/clock_middleware.go diff --git a/backend/gestion/main.go b/backend/gestion/main.go index 14c1e5cc..68a77c30 100644 --- a/backend/gestion/main.go +++ b/backend/gestion/main.go @@ -96,7 +96,7 @@ func main() { // Configuration CORS r.Use(cors.New(cors.Config{ - AllowOrigins: []string{"https://uber-stup.club"}, + AllowOrigins: []string{"https://uber-stup.club", "http://localhost:5173"}, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, ExposeHeaders: []string{"Content-Length"}, diff --git a/backend/gestion/middleware/clock_middleware.go b/backend/gestion/middleware/clock_middleware.go new file mode 100644 index 00000000..599e3f28 --- /dev/null +++ b/backend/gestion/middleware/clock_middleware.go @@ -0,0 +1,37 @@ +package middleware + +import ( + "log" + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +// OrderHoursMiddleware bloque les commandes en dehors de la plage 13h55 - 23h30. +func OrderHoursMiddleware(c *gin.Context) { + now := time.Now() + hour := now.Hour() + min := now.Minute() + + // Convertir l'heure courante en minutes depuis minuit pour faciliter la comparaison + currentMinutes := hour*60 + min + openMinutes := 13*60 + 55 // 13h55 + closeMinutes := 23*60 + 30 // 23h30 + + if currentMinutes < openMinutes || currentMinutes >= closeMinutes { + log.Printf("❌ [CLOCK-MWARE] Commande refusée à %02d:%02d (plage autorisée: 13h55 - 23h30)", hour, min) + c.JSON(http.StatusForbidden, gin.H{ + "error": "Commandes non disponibles à cette heure", + "message": "Vous pouvez commander entre 13h55 et 23h30", + "current_time": now.Format("15:04"), + "open_at": "13:55", + "close_at": "23:30", + }) + c.Abort() + return + } + + log.Printf("✅ [CLOCK-MWARE] Commande autorisée à %02d:%02d", hour, min) + c.Next() +} diff --git a/backend/gestion/routes/routes.go b/backend/gestion/routes/routes.go index 6355a6b3..e9ec0710 100644 --- a/backend/gestion/routes/routes.go +++ b/backend/gestion/routes/routes.go @@ -61,8 +61,8 @@ func SetupRoutes(router *gin.Engine, database *db.Database, geoService *services cartGroupV1.DELETE("/panier/clear", handlers.ClearBasket) // ✅ CORRIGÉ - Sans :username // Commandes - cartGroupV1.POST("/checkout", handlers.ValidateBasket) // ✅ Auto-assign GPS - cartGroupV1.GET("/my-commands", handlers.GetMyCommandsWithTracking) // ✅ Avec suivi + cartGroupV1.POST("/checkout", middleware.OrderHoursMiddleware, handlers.ValidateBasket) // ✅ Auto-assign GPS + cartGroupV1.GET("/my-commands", handlers.GetMyCommandsWithTracking) // ✅ Avec suivi // ⭐ NOUVEAUX - SUIVI CLIENT TEMPS RÉEL cartGroupV1.GET("/commands/:id/eta", handlers.GetOrderETA) // ✅ AJOUTÉ diff --git a/frontend-prep/src/api/api.ts b/frontend-prep/src/api/api.ts index 3681e2ca..56fba264 100644 --- a/frontend-prep/src/api/api.ts +++ b/frontend-prep/src/api/api.ts @@ -174,11 +174,17 @@ export const loginUser = async ( }); if (!response.ok) { - const errorData = await response.json(); - console.error("❌ [LOGIN] Erreur API:", errorData); + let errorMessage = "Erreur de connexion"; + try { + const errorData = await response.json(); + errorMessage = errorData.error || errorData.message || errorMessage; + } catch { + // body vide ou non-JSON + } + console.error("❌ [LOGIN] Erreur API:", response.status, errorMessage); return { success: false, - message: errorData.error || "Erreur de connexion", + message: errorMessage, }; } @@ -259,11 +265,17 @@ export const registerUser = async ( }); if (!response.ok) { - const errorData = await response.json(); - console.error("❌ [REGISTER] Erreur API:", errorData); + let errorMessage = "Erreur d'inscription"; + try { + const errorData = await response.json(); + errorMessage = errorData.error || errorData.message || errorMessage; + } catch { + // body vide ou non-JSON + } + console.error("❌ [REGISTER] Erreur API:", response.status, errorMessage); return { success: false, - message: errorData.error || "Erreur d'inscription", + message: errorMessage, }; } diff --git a/frontend-prep/vite.config.ts b/frontend-prep/vite.config.ts index 0e0b1a05..24d9ff9d 100644 --- a/frontend-prep/vite.config.ts +++ b/frontend-prep/vite.config.ts @@ -5,5 +5,13 @@ export default defineConfig({ plugins: [react()], build: { chunkSizeWarningLimit: 1000, // Augmenter la limite à 1000 KB - } + }, + server: { + proxy: { + '/api': { + target: 'http://localhost:8080', + changeOrigin: true, + }, + }, + }, })