diff --git a/src/otf_api/api.py b/src/otf_api/api.py index 91b0401..6b63a13 100644 --- a/src/otf_api/api.py +++ b/src/otf_api/api.py @@ -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: diff --git a/src/otf_api/exceptions.py b/src/otf_api/exceptions.py index 61a6d25..ed8c83f 100644 --- a/src/otf_api/exceptions.py +++ b/src/otf_api/exceptions.py @@ -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."""