Skip to content

Commit

Permalink
feat(api.py): add conflict detection for class bookings to prevent ov…
Browse files Browse the repository at this point in the history
…erlapping bookings

feat(exceptions.py): introduce ConflictingBookingError to handle booking conflicts
  • Loading branch information
NodeJSmith committed Jan 7, 2025
1 parent 1ad516c commit 7cad494
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ def book_class(self, otf_class: str | models.OtfClass) -> models.Booking:
f"Class {class_uuid} is already booked.", booking_uuid=existing_booking.class_booking_uuid
)

if isinstance(otf_class, models.OtfClass):
all_bookings = self.get_bookings()
booking_starts_at_map = {b.otf_class.starts_at_local: b for b in all_bookings.bookings}
if otf_class.starts_at_local in booking_starts_at_map:
conflicting_booking = booking_starts_at_map[otf_class.starts_at_local]
conflicting_class_uuid = conflicting_booking.otf_class.class_uuid
raise exc.ConflictingBookingError(
f"You already have a booking that conflicts with this class ({conflicting_class_uuid}).",
booking_uuid=conflicting_booking.class_booking_uuid,
)

body = {"classUUId": class_uuid, "confirmed": False, "waitlist": False}

try:
Expand Down
4 changes: 4 additions & 0 deletions src/otf_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class AlreadyBookedError(BookingError):
"""Raised when attempting to book a class that is already booked."""


class ConflictingBookingError(BookingError):
"""Raised when attempting to book a class that conflicts with an existing booking."""


class BookingAlreadyCancelledError(BookingError):
"""Raised when attempting to cancel a booking that is already cancelled."""

Expand Down

0 comments on commit 7cad494

Please sign in to comment.