forge
Explore
Log in

Don't you dare go Hollow

🎟️ ask about a whole slot series in one query, not 52

Browse files at this commit

57df6b3e2827a51ecaa1e0276c7e6104043fbde3Ahmet Kaan GümüşParent: 14334bc
  • fix
    • 🎟️ ask about a whole slot series in one query, not 52
Show full message
fix: 🎟️ ask about a whole slot series in one query, not 52

Deleting a weekly publish checked each occurrence for a live booking
with its own round-trip, all while holding APPOINTMENT_LOCK. One
`slot IN $slots` query answers the same question; the 409, its message,
and the all-or-nothing series refusal are unchanged.
src/domain/appointment.rsmodified+28 -0
@@ -306,6 +306,34 @@ impl Appointment {
306 Ok(!result.take::<Vec<Appointment>>(0)?.is_empty())306 Ok(!result.take::<Vec<Appointment>>(0)?.is_empty())
307 }307 }
308 308
309+ /// Same question as [`has_live_booking`](Self::has_live_booking) asked of a
310+ /// whole series at once: does *any* of `slots` still hold an occupied
311+ /// booking? One round-trip, because a weekly publish is up to 52 slots and
312+ /// the caller asks while holding the lock. Caller must hold
313+ /// [`APPOINTMENT_LOCK`], for the same reason: the answer is only meaningful
314+ /// while no booking can be written.
315+ ///
316+ /// No slots, no bookings — answered without a query, since an empty `IN`
317+ /// list is a needless round-trip and must never read as "occupied".
318+ pub async fn any_live_booking(
319+ slots: &[AppointmentSlotId],
320+ db: &Database,
321+ ) -> Result<bool, AppError> {
322+ if slots.is_empty() {
323+ return Ok(false);
324+ }
325+ let records: Vec<RecordId> = slots.iter().map(AppointmentSlotId::record).collect();
326+ let mut result = db
327+ .query(
328+ "SELECT * FROM appointment WHERE slot IN $slots \
329+ AND status IN ['pending', 'approved'] LIMIT 1",
330+ )
331+ .bind(("slots", records))
332+ .await?
333+ .check()?;
334+ Ok(!result.take::<Vec<Appointment>>(0)?.is_empty())
335+ }
336+
309 /// Request `slot`. Lands `pending`: publishing availability is not consent337 /// Request `slot`. Lands `pending`: publishing availability is not consent
310 /// to a specific person and topic.338 /// to a specific person and topic.
311 ///339 ///
src/domain/appointment_slot.rsmodified+4 -6
@@ -459,12 +459,10 @@ impl AppointmentSlot {
459 /// [`APPOINTMENT_LOCK`]: the live-booking check is only meaningful while459 /// [`APPOINTMENT_LOCK`]: the live-booking check is only meaningful while
460 /// no booking can be written.460 /// no booking can be written.
461 async fn delete_locked(ids: &[AppointmentSlotId], db: &Database) -> Result<(), AppError> {461 async fn delete_locked(ids: &[AppointmentSlotId], db: &Database) -> Result<(), AppError> {
462- for id in ids {462+ if Appointment::any_live_booking(ids, db).await? {
463- if Appointment::has_live_booking(id, db).await? {463+ return Err(AppError::Conflict(
464- return Err(AppError::Conflict(464+ "the slot has a pending or approved booking",
465- "the slot has a pending or approved booking",465+ ));
466- ));
467- }
468 }466 }
469 let records: Vec<RecordId> = ids.iter().map(|id| id.record()).collect();467 let records: Vec<RecordId> = ids.iter().map(|id| id.record()).collect();
470 let mut result = db468 let mut result = db