From 7b396b7a9dff6f8a44f77464c8f73b1453c32884 Mon Sep 17 00:00:00 2001 From: Stephen Adamson Date: Thu, 30 Apr 2026 16:06:51 +0100 Subject: [PATCH] fix: resolve guest booking failures from router conflict, reservation self-block, and closed-day miscalculation - backend/main.go: Flatten /bookings/* sub-Route to explicit paths to prevent RequireAuth middleware from bleeding into OptionalAuth POST /bookings - backend/handlers/scheduling/time-blockers.go: Exclude RESERVATION:* entries from GetTimeBlockersInRange so overlap checks dont reject the users own reservation before CreateBookingHandler can delete it - local-dev-2.sh: Fix open_day to skip Saturday (6) not Monday (1), matching working_hours schema; move guest booking dates to +16/+20/+22 days beyond the upcoming loop range; add reserve-then-book step mirroring frontend flow --- backend/handlers/scheduling/time-blockers.go | 1 + backend/main.go | 28 ++++--- local-dev-2.sh | 81 ++++++++++++++------ 3 files changed, 70 insertions(+), 40 deletions(-) diff --git a/backend/handlers/scheduling/time-blockers.go b/backend/handlers/scheduling/time-blockers.go index 1036090..8f03089 100644 --- a/backend/handlers/scheduling/time-blockers.go +++ b/backend/handlers/scheduling/time-blockers.go @@ -196,6 +196,7 @@ func GetTimeBlockersInRange(ctx context.Context, start, end time.Time) ([]TimeBl SELECT id, start_time, duration_minutes, description, cron_expression, created_at, created_by FROM time_blockers WHERE cron_expression IS NULL + AND description NOT LIKE 'RESERVATION:%' AND start_time >= $1 AND start_time <= $2 ORDER BY start_time `, start, end) diff --git a/backend/main.go b/backend/main.go index 4147831..cce6d9e 100644 --- a/backend/main.go +++ b/backend/main.go @@ -137,15 +137,16 @@ func main() { }) }) - // Public booking endpoints (optional auth for slot reservation) - r.With(mw.RateLimit(30, time.Minute), mw.OptionalAuth).Post("/bookings/reserve", bookings.ReserveSlotHandler) + // Public booking endpoints (optional auth for slot reservation and guest bookings) + r.Group(func(r chi.Router) { + r.Use(mw.RateLimit(30, time.Minute), mw.OptionalAuth) + r.Post("/bookings/reserve", bookings.ReserveSlotHandler) + r.Post("/bookings", bookings.CreateBookingHandler) + }) // Guest user creation (public, no auth required) r.With(mw.RateLimit(10, time.Minute)).Post("/users/guest", user.CreateGuestUserHandler) - // Booking creation (accepts both authenticated and guest users) - r.With(mw.RateLimit(30, time.Minute), mw.OptionalAuth).Post("/bookings", bookings.CreateBookingHandler) - // Authenticated users r.Group(func(r chi.Router) { r.Use(mw.RequireAuth) @@ -160,16 +161,13 @@ func main() { r.Delete("/user/account", user.DeleteAccountHandler) r.Get("/user/loyalty", user.GetLoyaltyHandler) - r.Route("/bookings", func(r chi.Router) { - r.Get("/", bookings.GetAllUserBookingsHandler) - r.Get("/{id}", bookings.GetBookingHandler) - r.Get("/{id}/calendar", bookings.GetBookingCalendarHandler) - r.Put("/{id}", bookings.EditBookingHandler) - r.Delete("/{id}", bookings.DeleteBookingHandler) - // Edit request endpoints - r.Post("/{id}/edit-request", bookings.RequestEditHandler) - r.Delete("/{id}/edit-request", bookings.DeleteEditRequestHandler) - }) + r.Get("/bookings", bookings.GetAllUserBookingsHandler) + r.Get("/bookings/{id}", bookings.GetBookingHandler) + r.Get("/bookings/{id}/calendar", bookings.GetBookingCalendarHandler) + r.Put("/bookings/{id}", bookings.EditBookingHandler) + r.Delete("/bookings/{id}", bookings.DeleteBookingHandler) + r.Post("/bookings/{id}/edit-request", bookings.RequestEditHandler) + r.Delete("/bookings/{id}/edit-request", bookings.DeleteEditRequestHandler) }) // Admin-only (no rate limit - trusted users with authenticated sessions) diff --git a/local-dev-2.sh b/local-dev-2.sh index 5926d92..437121e 100755 --- a/local-dev-2.sh +++ b/local-dev-2.sh @@ -464,23 +464,21 @@ create_admin_booking() { } # Returns the nearest OPEN business day at or after the given date. -# Schema: Monday=0 closed, Sunday=6 closed. Tue-Sat open. -# dow: date's day-of-week as 0=Sun,1=Mon,...,6=Sat (GNU date %w) +# dow: date's day-of-week as 0=Sun,...,6=Sat (GNU date %w) open_day() { local d="$1" local max=7 for ((i=0; i