Derive slot validation from config instead of hardcoded values

- Created _get_valid_minute_boundaries() helper that derives valid minutes from SLOT_DURATION_MINUTES
- Replaced hardcoded (0, 15, 30, 45) with dynamic calculation
- Error message now includes valid minute values for better clarity
This commit is contained in:
counterweight 2025-12-21 17:53:35 +01:00
parent 40b193238e
commit a14405a998
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -46,6 +46,19 @@ def _to_appointment_response(
)
def _get_valid_minute_boundaries() -> tuple[int, ...]:
"""Get valid minute boundaries based on SLOT_DURATION_MINUTES.
Assumes SLOT_DURATION_MINUTES divides 60 evenly (e.g., 15 minutes = 0, 15, 30, 45).
"""
boundaries: list[int] = []
minute = 0
while minute < 60:
boundaries.append(minute)
minute += SLOT_DURATION_MINUTES
return tuple(boundaries)
def _get_bookable_date_range() -> tuple[date, date]:
"""Get the valid date range for booking (tomorrow to +30 days)."""
today = date.today()
@ -148,11 +161,12 @@ async def create_booking(
slot_date = request.slot_start.date()
_validate_booking_date(slot_date)
# Validate slot is on 15-minute boundary
if request.slot_start.minute not in (0, 15, 30, 45):
# Validate slot is on the correct minute boundary (derived from SLOT_DURATION_MINUTES)
valid_minutes = _get_valid_minute_boundaries()
if request.slot_start.minute not in valid_minutes:
raise HTTPException(
status_code=400,
detail="Slot start time must be on 15-minute boundary",
detail=f"Slot start time must be on {SLOT_DURATION_MINUTES}-minute boundary (valid minutes: {valid_minutes})",
)
if request.slot_start.second != 0 or request.slot_start.microsecond != 0:
raise HTTPException(