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:
parent
40b193238e
commit
a14405a998
1 changed files with 17 additions and 3 deletions
|
|
@ -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]:
|
def _get_bookable_date_range() -> tuple[date, date]:
|
||||||
"""Get the valid date range for booking (tomorrow to +30 days)."""
|
"""Get the valid date range for booking (tomorrow to +30 days)."""
|
||||||
today = date.today()
|
today = date.today()
|
||||||
|
|
@ -148,11 +161,12 @@ async def create_booking(
|
||||||
slot_date = request.slot_start.date()
|
slot_date = request.slot_start.date()
|
||||||
_validate_booking_date(slot_date)
|
_validate_booking_date(slot_date)
|
||||||
|
|
||||||
# Validate slot is on 15-minute boundary
|
# Validate slot is on the correct minute boundary (derived from SLOT_DURATION_MINUTES)
|
||||||
if request.slot_start.minute not in (0, 15, 30, 45):
|
valid_minutes = _get_valid_minute_boundaries()
|
||||||
|
if request.slot_start.minute not in valid_minutes:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
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:
|
if request.slot_start.second != 0 or request.slot_start.microsecond != 0:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue