From 57df6b3e2827a51ecaa1e0276c7e6104043fbde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 26 Jul 2026 22:53:50 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=8E=9F=EF=B8=8F=20ask=20about=20a?= =?UTF-8?q?=20whole=20slot=20series=20in=20one=20query,=20not=2052?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 28 ++++++++++++++++++++++++++++ src/domain/appointment_slot.rs | 10 ++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/domain/appointment.rs b/src/domain/appointment.rs index a67275d..22e96df 100644 --- a/src/domain/appointment.rs +++ b/src/domain/appointment.rs @@ -306,6 +306,34 @@ impl Appointment { Ok(!result.take::>(0)?.is_empty()) } + /// Same question as [`has_live_booking`](Self::has_live_booking) asked of a + /// whole series at once: does *any* of `slots` still hold an occupied + /// booking? One round-trip, because a weekly publish is up to 52 slots and + /// the caller asks while holding the lock. Caller must hold + /// [`APPOINTMENT_LOCK`], for the same reason: the answer is only meaningful + /// while no booking can be written. + /// + /// No slots, no bookings — answered without a query, since an empty `IN` + /// list is a needless round-trip and must never read as "occupied". + pub async fn any_live_booking( + slots: &[AppointmentSlotId], + db: &Database, + ) -> Result { + if slots.is_empty() { + return Ok(false); + } + let records: Vec = slots.iter().map(AppointmentSlotId::record).collect(); + let mut result = db + .query( + "SELECT * FROM appointment WHERE slot IN $slots \ + AND status IN ['pending', 'approved'] LIMIT 1", + ) + .bind(("slots", records)) + .await? + .check()?; + Ok(!result.take::>(0)?.is_empty()) + } + /// Request `slot`. Lands `pending`: publishing availability is not consent /// to a specific person and topic. /// diff --git a/src/domain/appointment_slot.rs b/src/domain/appointment_slot.rs index 7bf3084..89a3b9b 100644 --- a/src/domain/appointment_slot.rs +++ b/src/domain/appointment_slot.rs @@ -459,12 +459,10 @@ impl AppointmentSlot { /// [`APPOINTMENT_LOCK`]: the live-booking check is only meaningful while /// no booking can be written. async fn delete_locked(ids: &[AppointmentSlotId], db: &Database) -> Result<(), AppError> { - for id in ids { - if Appointment::has_live_booking(id, db).await? { - return Err(AppError::Conflict( - "the slot has a pending or approved booking", - )); - } + if Appointment::any_live_booking(ids, db).await? { + return Err(AppError::Conflict( + "the slot has a pending or approved booking", + )); } let records: Vec = ids.iter().map(|id| id.record()).collect(); let mut result = db