//! Appointment workflows: booking, the approve/reject/cancel decisions, and //! counter-proposals. Reads, listings, the overlap probe, the seat claim, //! and the compare-and-set live in [`crate::db::appointment`]. //! //! There is no lock anymore. What the old APPOINTMENT_LOCK serialized is //! now three database guarantees: //! //! - **Occupancy** — booking claims the slot's seat and writes the row in //! one statement ([`crate::db::appointment::claim_slot_and_create`]); two //! racers cannot both find the slot free. //! - **Publish overlap** — the `appointment_slot_teacher_span` exclusion //! constraint refuses two overlapping published windows at the store. //! - **Double-booking** — the approval's "is anyone already committed at //! that time" is a predicate over *other* rows, which no single-record //! write can decide and no row lock can stand in for (the rows that would //! collide may not exist yet). That one decision runs **serializable** — //! the lane's single documented isolation escalation: two concurrent //! approvals of overlapping proposals read the same approved set, and SSI //! aborts the loser (`40001`), which [`tx_with_retry`] re-sends; the //! re-run sees the winner committed and refuses with the ordinary 409. use crate::constant::CAS_UPDATE_RETRIES; use crate::database::{Database, tx_with_retry}; use crate::db::appointment; use crate::db::appointment_slot; use crate::db::cap::Claimed; use crate::domain::appointment::{ Appointment, AppointmentId, AppointmentReason, AppointmentStatus, }; use crate::domain::appointment_slot::AppointmentSlotId; use crate::domain::timestamp::Timestamp; use crate::domain::user::UserId; use crate::error::{AppError, ValidationError}; pub use crate::db::appointment::RequesterListParams; /// Request `slot`. Lands `pending`: publishing availability is not consent /// to a specific person and topic. /// /// Refused (409) when the slot's window has already opened, when it is /// already taken, or when the requester is already committed elsewhere at /// that time. The slot is taken by the seat claim — a conditional /// single-record write the database decides — so two racing requests /// cannot both find it free however they interleave, and a crash between /// the two can no longer leave the slot occupied by a booking that does /// not exist. The requester's own overlap check is advisory here, exactly /// as under the lock: only *approved* bookings block, so two pending /// requests may queue on overlapping times, and the approval is where the /// authoritative refusal lands. pub async fn book( db: &Database, slot: &AppointmentSlotId, requester: &UserId, reason: AppointmentReason, ) -> Result { let slot_row = appointment_slot::read(db, slot) .await? .ok_or(AppError::NotFound)?; // A window that has opened is history, not a plan: `cancel` refuses to // call off a meeting that started, so such a booking would be stuck // forever. `list_upcoming` is bounded on this same `starts_at`, so the // calendar never offers one — but a slot whose start passed while the // page was open, or an id kept from an earlier read, still reaches here. // The publish-time 60s skew grace does not apply on either side. if slot_row.get_starts_at().as_millis() <= Timestamp::now().as_millis() { return Err(AppError::Conflict("the slot has already started")); } // The teacher's own overlaps are not checked here — an approved // meeting of theirs elsewhere is the *approval*'s business, and // refusing the request outright would hide a slot they published. if appointment::conflicts( db, requester, requester, slot_row.get_starts_at(), slot_row.get_ends_at(), None, ) .await? { return Err(AppError::Conflict( "you already have an appointment at that time", )); } // Last, so nothing above can refuse *after* the slot was taken. A slot // deleted since the read above is gone, not full — the claim's `WHERE` // finds no row either way, so the distinction is re-read rather than // guessed. let appointment_row = Appointment { id: AppointmentId::generate(), slot: Some(slot.clone()), requester: *requester, status: AppointmentStatus::Pending, reason, proposed_starts_at: None, proposed_ends_at: None, proposed_by: None, decided_by: None, cancelled_by: None, cancel_reason: None, reject_reason: None, created_at: Timestamp::now(), }; match appointment::claim_slot_and_create(db, appointment_row).await? { Claimed::Made(created) => Ok(created), Claimed::Full => { if appointment_slot::read(db, slot).await?.is_none() { return Err(AppError::NotFound); } Err(AppError::Conflict("the slot is already booked")) } // The id is freshly minted on a table keyed by it: no rival can // have aimed at it. Claimed::Duplicate => Err(AppError::Internal("appointment id collided".into())), } } /// The row, for callers that only inspect it — the web layer's authorization /// gates read through here. pub async fn read(db: &Database, id: &AppointmentId) -> Result, AppError> { appointment::read(db, id).await } /// The requester's bookings, newest first — the web layer's paging read, /// narrowed by the door's optional `status`, `starts_after`/`starts_before` /// (the meeting's effective start, `[after, before)`) and `teacher` (the /// booked slot's teacher) filters. pub async fn list_for_requester( db: &Database, requester: &UserId, params: RequesterListParams<'_>, ) -> Result<(Vec, i64), AppError> { appointment::list_for_requester(db, requester, params).await } /// Every booking aimed at `teacher`, newest first — the request inbox, /// narrowed by the door's optional `status` and `starts_after`/`starts_before` /// filters. The inbox is the teacher's own universe already, so it takes no /// teacher filter: narrowing is the web layer's call. pub async fn list_for_teacher( db: &Database, teacher: &UserId, status: Option, starts_after: Option, starts_before: Option, limit: Option, offset: i64, ) -> Result<(Vec, i64), AppError> { appointment::list_for_teacher(db, teacher, status, starts_after, starts_before, limit, offset) .await } /// Confirm the meeting at the slot's own window. Re-runs the double-booking /// guard for both teacher and requester *inside the decision transaction* — /// the authoritative check, since only approval commits anyone. Refused /// (409) when the effective window has already started; that also covers /// [`accept_proposal`], which lands here. /// /// Also refused (409) while a counter-proposal stands: the effective window /// would then be the *proposed* one, and the deciding side here is the /// teacher's, so approving would commit the requester to a time only the /// teacher ever named. That move belongs to [`accept_proposal`], which the /// web layer opens to the requester alone. pub async fn approve( db: &Database, id: &AppointmentId, decided_by: &UserId, ) -> Result { approve_inner(db, id, decided_by, None).await } /// The one approval path. `accepting` carries the window the caller is /// answering *for*, which is the only way a proposed one may be committed: /// it both marks the caller as the proposal's counterparty and pins which /// proposal they saw. /// /// Read, gate, overlap probe and write are ONE serializable transaction: /// the overlap probe is a predicate over other approved rows, the one /// invariant in this lane no row lock can carry (see the module doc). The /// proposal pin is re-checked inside that same snapshot, so a proposal that /// moved between the requester's read and their accept is refused rather /// than committed. async fn approve_inner( db: &Database, id: &AppointmentId, decided_by: &UserId, accepting: Option<(Timestamp, Timestamp)>, ) -> Result { // Owned captures (`Send` rule of `tx_with_retry` closures). let id = id.clone(); let decided_by = *decided_by; tx_with_retry(db, false, async move |tx| { // Serializable for the overlap decision below (module doc). `SET // TRANSACTION` is legal only before the first query, so it goes // first; a `40001` from SSI is re-sent by the retry loop, and the // re-run re-reads everything against the committed winner. sqlx::query!("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE") .execute(&mut *tx) .await?; let expected = appointment::read_pending_on(&mut *tx, &id).await?; match accepting { None if expected.has_proposal() => { return Err(AppError::Conflict( "a counter-proposal is standing; only the requester can accept it", )); } Some(_) if !expected.has_proposal() => { return Err(AppError::Conflict("no time has been proposed")); } // The proposal moved between the requester reading it and // accepting it. Committing here would bind them to a window the // teacher swapped in after they clicked, which is the whole // thing the requester-only accept exists to prevent. Some(accepted) if !expected.proposes(accepted) => { return Err(AppError::Conflict( "the proposed time has changed; re-read the booking and accept the new one", )); } _ => {} } let slot_id = expected.get_slot().ok_or(AppError::NotFound)?.clone(); let slot = appointment_slot::read_on(&mut *tx, &slot_id) .await? .ok_or(AppError::NotFound)?; let (starts_at, ends_at) = expected.window(&slot); // The effective window — the proposal's when one stands — must still // be ahead. Mutual agreement does not help: `cancel` refuses a // meeting that started, so committing to a passed window would mint // a booking nobody can ever undo. Refused, the row stays `pending` // and the teacher can propose a time that can actually happen. if starts_at.as_millis() <= Timestamp::now().as_millis() { return Err(AppError::Conflict("that time has already started")); } if appointment::conflicts_on( &mut *tx, slot.get_teacher(), expected.get_requester(), starts_at, ends_at, Some(&id), ) .await? { return Err(AppError::Conflict( "that time collides with another approved appointment", )); } let mut approved = expected.clone(); approved.status = AppointmentStatus::Approved; approved.decided_by = Some(decided_by); match appointment::decision_cas(&mut *tx, &expected, approved).await? { Some(saved) => Ok(saved), // Unreachable under the row lock this transaction holds — kept // for the shape's honesty: a miss means re-decide, and the // caller's answer to that is this 409. None => Err(contended()), } }) .await } /// Turn the request down. Frees the slot — the seat goes back with the /// status flip, in one transaction. No lock needed and none taken: this /// decides nothing about anyone's calendar, and the write is its own guard. pub async fn reject( db: &Database, id: &AppointmentId, decided_by: &UserId, reason: Option, ) -> Result { for _ in 0..CAS_UPDATE_RETRIES { let expected = appointment::read_pending(db, id).await?; let mut rejected = expected.clone(); rejected.status = AppointmentStatus::Rejected; rejected.decided_by = Some(*decided_by); rejected.reject_reason = reason.clone(); if let Some(saved) = appointment::save_if_unchanged(db, &expected, rejected).await? { return Ok(saved); } } Err(contended()) } /// Call the meeting off. Legal from either live state, so an approved /// meeting can still be dropped; the slot frees up. The web layer opens /// this to the **requester only** — a teacher's way out is `reject` while /// the booking is pending, or `reschedule` (back to `pending`) then /// `reject` for an approved one — and `decline_reschedule`, the other /// caller, is the requester's too. /// /// Refused (409) once the *effective* window has opened — a meeting that /// began is history, not a plan. The guard lives here rather than in the /// handler so every caller inherits it: `decline_reschedule` cancels /// through this same door and used to slip past a handler-only check. pub async fn cancel( db: &Database, id: &AppointmentId, cancelled_by: &UserId, reason: Option, ) -> Result { for _ in 0..CAS_UPDATE_RETRIES { let expected = appointment::read(db, id).await?.ok_or(AppError::NotFound)?; if !expected.get_status().is_live() { return Err(AppError::Conflict("the appointment is already settled")); } let slot = appointment_slot::read(db, expected.get_slot().ok_or(AppError::NotFound)?) .await? .ok_or(AppError::NotFound)?; if expected.window(&slot).0.as_millis() <= Timestamp::now().as_millis() { return Err(AppError::Conflict("the appointment has already started")); } let mut cancelled = expected.clone(); cancelled.status = AppointmentStatus::Cancelled; cancelled.cancelled_by = Some(*cancelled_by); cancelled.cancel_reason = reason.clone(); if let Some(saved) = appointment::save_if_unchanged(db, &expected, cancelled).await? { return Ok(saved); } } Err(contended()) } /// Counter-propose another time on the same booking (no second row, so the /// reason and the history stay in one place). /// /// The proposal sends the booking back to `pending`: a previously approved /// meeting is *not* committed at the new time until the other side accepts, /// and leaving it approved would silently move a confirmed appointment. /// /// Refused (409) when the proposed window has already opened. The handler's /// `check_not_past` carries a 60s clock-skew grace, which is enough to /// propose a window that is already underway; `cancel` would then refuse to /// undo the booking, so it must not be offered in the first place. pub async fn propose( db: &Database, id: &AppointmentId, starts_at: Timestamp, ends_at: Timestamp, proposed_by: &UserId, ) -> Result { if starts_at.as_millis() >= ends_at.as_millis() { return Err(ValidationError::Invalid { field: "proposed_ends_at", reason: "must be after proposed_starts_at", } .into()); } // No lock: a proposal commits nobody's calendar (the booking goes back // to `pending`), and it neither takes nor frees the slot's seat. for _ in 0..CAS_UPDATE_RETRIES { let expected = appointment::read(db, id).await?.ok_or(AppError::NotFound)?; if !expected.get_status().is_live() { return Err(AppError::Conflict("the appointment is already settled")); } if starts_at.as_millis() <= Timestamp::now().as_millis() { return Err(AppError::Conflict("that time has already started")); } let mut proposed = expected.clone(); proposed.status = AppointmentStatus::Pending; proposed.decided_by = None; proposed.proposed_starts_at = Some(starts_at); proposed.proposed_ends_at = Some(ends_at); proposed.proposed_by = Some(*proposed_by); if let Some(saved) = appointment::save_if_unchanged(db, &expected, proposed).await? { return Ok(saved); } } Err(contended()) } /// Accept the standing proposal — approval at the proposed time. The /// overlap guard runs again because the time moved. /// /// `starts_at`/`ends_at` are the proposal the caller is answering, as they /// read it. They are not a request to move the meeting: they *pin* the one /// being accepted, and a proposal that has since been superseded is refused /// (409) rather than committed. `propose` takes no lock and the two calls /// are minutes apart, so nothing but this pin can tell "the requester /// agreed to 10:00" from "the requester's click landed on 23:00". pub async fn accept_proposal( db: &Database, id: &AppointmentId, decided_by: &UserId, starts_at: Timestamp, ends_at: Timestamp, ) -> Result { // The same approval path re-reads inside its serializable transaction // and reads the proposed window off the row, so accepting is approval — // no second code path. Only this door passes `accepting`, and the web // layer opens it to the requester alone. approve_inner(db, id, decided_by, Some((starts_at, ends_at))).await } /// Every retry lost the row to another decision. A 409 rather than a 500: the /// caller's request was refused, nothing was half-applied, and re-reading the /// booking shows what happened to it. fn contended() -> AppError { AppError::Conflict("the appointment is being decided elsewhere") } #[cfg(test)] mod tests { use super::*; use crate::database::{Database, init_test_db}; use crate::service::appointment_slot; fn at(millis: i64) -> Timestamp { Timestamp::from_millis(millis) } /// A real `app_user` row: teachers and students are foreign keys now. The /// label names the row's username; the id is minted, so repeated calls are /// new people, not the same row. async fn a_person(db: &Database, label: &str, role: &str) -> UserId { let user = UserId::generate(); sqlx::query( "INSERT INTO app_user (id, username, created_at, role) \ VALUES ($1, $2, 0, $3)", ) .bind(user.uuid()) .bind(format!("{label}-{}", &user.key()[30..])) .bind(role) .execute(db) .await .unwrap(); user } /// A window that has not opened yet — `book` refuses started ones. fn soon(offset: i64) -> Timestamp { at(Timestamp::now().as_millis() + offset) } #[tokio::test] async fn window_prefers_a_proposal_over_the_slot() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let (starts_at, ends_at) = (soon(60_000), soon(120_000)); let slot = appointment_slot::create(&db, &teacher, starts_at, ends_at, None) .await .unwrap(); let mut appointment_row = book( &db, slot.get_id(), &a_person(&db, "s1", "student").await, AppointmentReason::try_new("ödev").unwrap(), ) .await .unwrap(); assert_eq!(appointment_row.window(&slot), (starts_at, ends_at)); appointment_row.proposed_starts_at = Some(at(5_000)); appointment_row.proposed_ends_at = Some(at(6_000)); assert_eq!(appointment_row.window(&slot), (at(5_000), at(6_000))); } /// A slot whose window already opened is unbookable: `cancel` would refuse /// to undo the resulting meeting, so the booking would be stuck for good. #[tokio::test] async fn a_started_slot_cannot_be_booked() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let reason = || AppointmentReason::try_new("görüşme").unwrap(); let started = appointment_slot::create(&db, &teacher, soon(-30_000), soon(60_000), None) .await .unwrap(); assert!(matches!( book( &db, started.get_id(), &a_person(&db, "s1", "student").await, reason() ) .await, Err(AppError::Conflict("the slot has already started")) )); let upcoming = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); assert!( book( &db, upcoming.get_id(), &a_person(&db, "s1", "student").await, reason() ) .await .is_ok() ); } /// Approval judges the *effective* window, so a proposal whose time has /// come and gone cannot be accepted — the meeting would be uncancellable. #[tokio::test] async fn a_started_proposal_cannot_be_accepted() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let student = a_person(&db, "s1", "student").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); let booking = book( &db, slot.get_id(), &student, AppointmentReason::try_new("görüşme").unwrap(), ) .await .unwrap(); // Proposing a window that already opened is refused up front — the // handler's 60s skew grace would otherwise let one through. assert!(matches!( propose(&db, booking.get_id(), soon(-30_000), soon(30_000), &teacher).await, Err(AppError::Conflict("that time has already started")) )); // And approval judges the effective window again, for a proposal whose // time simply passed while it stood: forced onto the row directly, // since `propose` no longer mints one. let current = appointment::read(&db, booking.get_id()) .await .unwrap() .unwrap(); let (passed_starts_at, passed_ends_at) = (soon(-30_000), soon(30_000)); let mut standing = current.clone(); standing.proposed_starts_at = Some(passed_starts_at); standing.proposed_ends_at = Some(passed_ends_at); appointment::save_if_unchanged(&db, ¤t, standing) .await .unwrap() .unwrap(); assert!(matches!( accept_proposal( &db, booking.get_id(), &student, passed_starts_at, passed_ends_at ) .await, Err(AppError::Conflict("that time has already started")) )); // Refused, not half-applied: still pending, still decidable at a time // that can actually happen. let after = appointment::read(&db, booking.get_id()).await.unwrap(); assert_eq!(after.unwrap().get_status(), AppointmentStatus::Pending); let (starts_at, ends_at) = (soon(60_000), soon(120_000)); propose(&db, booking.get_id(), starts_at, ends_at, &teacher) .await .unwrap(); // Accepting a window that is *not* the standing proposal is refused — // the pin is what the requester saw, not a request to move the meeting. assert!(matches!( accept_proposal( &db, booking.get_id(), &student, passed_starts_at, passed_ends_at ) .await, Err(AppError::Conflict( "the proposed time has changed; \ re-read the booking and accept the new one" )) )); let accepted = accept_proposal(&db, booking.get_id(), &student, starts_at, ends_at) .await .unwrap(); assert_eq!(accepted.get_status(), AppointmentStatus::Approved); } /// The compare-and-set every decision writes through — the guard against a /// decision built on a snapshot that has since moved, which no lock taken /// after the read can catch. The interleaving that matters is the one a /// status-only guard would miss, since `propose` leaves a pending booking /// pending. /// /// Deliberately sequential: the mem engine answers `Ok` to a write it then /// drops, so a `join!` of two decisions proves nothing. This drives the /// primitive itself with the exact stale snapshot the race produces. #[tokio::test] async fn a_decision_built_on_a_stale_snapshot_never_lands() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let student = a_person(&db, "s1", "student").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); let booking = book( &db, slot.get_id(), &student, AppointmentReason::try_new("görüşme").unwrap(), ) .await .unwrap(); // One transaction counter-proposes. A second concurrent transaction // is still holding the snapshot it read before that — same id, same // `pending` status. let stale = booking.clone(); let (proposed_starts_at, proposed_ends_at) = (soon(180_000), soon(240_000)); propose( &db, booking.get_id(), proposed_starts_at, proposed_ends_at, &teacher, ) .await .unwrap(); let mut late = stale.clone(); late.status = AppointmentStatus::Approved; late.decided_by = Some(teacher); assert!( appointment::save_if_unchanged(&db, &stale, late) .await .unwrap() .is_none() ); // The proposal survived: the row was not confirmed at the slot's old // window behind the requester's back. let after = appointment::read(&db, booking.get_id()) .await .unwrap() .unwrap(); assert_eq!(after.get_status(), AppointmentStatus::Pending); assert_eq!(after.get_proposed_starts_at(), Some(proposed_starts_at)); assert_eq!(after.get_proposed_ends_at(), Some(proposed_ends_at)); assert_eq!(after.get_decided_by(), None); // And the same decision, re-validated against the row as it now // stands, does land — a miss is a retry signal, not a wall. let mut fresh = after.clone(); fresh.status = AppointmentStatus::Approved; fresh.decided_by = Some(teacher); let saved = appointment::save_if_unchanged(&db, &after, fresh) .await .unwrap() .expect("a current snapshot must write"); assert_eq!(saved.get_status(), AppointmentStatus::Approved); assert_eq!(saved.get_proposed_starts_at(), Some(proposed_starts_at)); } /// What the slot row itself says about being taken — the authority every /// gate now reads. async fn occupied(slot: &AppointmentSlotId, db: &Database) -> i64 { sqlx::query_scalar::<_, i64>("SELECT occupied FROM appointment_slot WHERE id = $1") .bind(slot.uuid()) .fetch_one(db) .await .unwrap() } /// The seat claim `book` rides, asked directly: the conditional /// `UPDATE … WHERE occupied < $cap` on the slot row is the whole /// primitive, so this asks it the exact question the race asks. async fn claim_seat(slot: &AppointmentSlotId, cap: i64, db: &Database) -> bool { sqlx::query_scalar::<_, i32>( "UPDATE appointment_slot SET occupied = occupied + 1 \ WHERE id = $1 AND occupied < $2 RETURNING 1", ) .bind(slot.uuid()) .bind(cap) .fetch_optional(db) .await .unwrap() .is_some() } /// The double-book race with the lock taken out of the picture: a second /// booking arrives at the database with nothing between it and the slot but /// the claim's own `WHERE`, which is what must refuse it. Driven /// through [`cap::claim`] directly rather than through a `join!`, since the /// in-memory engine can drop one of two concurrent writes to a record and /// still answer `Ok` — this asks the primitive the exact question the race /// asks it. /// /// Deleting the `WHERE (occupied ?? 0) < $cap` from `cap::claim` makes the /// second claim succeed and this fail (mutation-proved). #[tokio::test] async fn a_concurrent_claim_cannot_take_a_booked_slot() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 0); book( &db, slot.get_id(), &a_person(&db, "s1", "student").await, AppointmentReason::try_new("görüşme").unwrap(), ) .await .unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 1); assert!( !claim_seat(slot.get_id(), 1, &db).await, "the slot's seat must already be taken" ); // And the racer's refusal left the stored state alone: one booking, one // seat — not two of either. assert_eq!(occupied(slot.get_id(), &db).await, 1); assert_eq!( appointment::list_for_slot(&db, slot.get_id()) .await .unwrap() .len(), 1 ); } /// The seat and the booking row now land in one transaction, so the stored /// state must move together: a taken slot refuses the next requester with /// the counter *and* the row list untouched — a refusal that had already /// bumped the counter would strand the slot forever. #[tokio::test] async fn a_refused_booking_moves_nothing() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); let reason = || AppointmentReason::try_new("görüşme").unwrap(); book( &db, slot.get_id(), &a_person(&db, "s1", "student").await, reason(), ) .await .unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 1); let refused = book( &db, slot.get_id(), &a_person(&db, "s2", "student").await, reason(), ) .await; assert!( matches!(refused, Err(AppError::Conflict(_))), "the slot is taken" ); assert_eq!( occupied(slot.get_id(), &db).await, 1, "the refusal took no seat" ); assert_eq!( appointment::list_for_slot(&db, slot.get_id()) .await .unwrap() .len(), 1, "and wrote no row" ); } /// Cancelling gives the seat back in the same transaction as the status /// flip — the counter is the only thing that says a slot is free, so a /// cancel that moved one without the other would strand the slot forever. #[tokio::test] async fn cancelling_hands_the_seat_back_with_the_status() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let student = a_person(&db, "s1", "student").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); let booking = book( &db, slot.get_id(), &student, AppointmentReason::try_new("görüşme").unwrap(), ) .await .unwrap(); approve(&db, booking.get_id(), &teacher).await.unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 1, "approval holds it"); cancel(&db, booking.get_id(), &student, None).await.unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 0); // Cancelling twice is refused, so the seat cannot go back twice either. assert!(cancel(&db, booking.get_id(), &student, None).await.is_err()); assert_eq!(occupied(slot.get_id(), &db).await, 0); assert!( book( &db, slot.get_id(), &a_person(&db, "s2", "student").await, AppointmentReason::try_new("görüşme").unwrap() ) .await .is_ok() ); assert_eq!(occupied(slot.get_id(), &db).await, 1); } /// Occupancy frees on reject, so the slot can be re-booked. #[tokio::test] async fn rejecting_frees_the_slot_for_re_booking() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) .await .unwrap(); let reason = || AppointmentReason::try_new("görüşme").unwrap(); let first = book( &db, slot.get_id(), &a_person(&db, "s1", "student").await, reason(), ) .await .unwrap(); assert!(matches!( book( &db, slot.get_id(), &a_person(&db, "s2", "student").await, reason() ) .await, Err(AppError::Conflict(_)) )); // The live booking also blocks the slot delete. assert!(matches!( appointment_slot::delete(&db, slot.clone()).await, Err(AppError::Conflict(_)) )); reject(&db, first.get_id(), &teacher, None).await.unwrap(); assert_eq!(occupied(slot.get_id(), &db).await, 0); let second = book( &db, slot.get_id(), &a_person(&db, "s2", "student").await, reason(), ) .await .unwrap(); assert_eq!(second.get_status(), AppointmentStatus::Pending); assert_eq!(occupied(slot.get_id(), &db).await, 1); } /// The listing filters narrow each side's own universe: `status` to one /// state, `starts_after` to meetings whose effective start (a standing /// proposal, the slot's window otherwise) is at or after the instant, /// `teacher` to the booked slot's teacher — and the paging window applies /// after all of it, with `total` counting the filtered set, not the page. /// Absent filters, the listings answer exactly as before, dangling rows /// included. #[tokio::test] async fn listings_filter_by_status_start_and_teacher() { let (db, _leases) = init_test_db().await; let ali = a_person(&db, "ali", "teacher").await; let ayse = a_person(&db, "ayse", "teacher").await; let veli = a_person(&db, "veli", "student").await; let reason = AppointmentReason::try_new("görüşme").unwrap(); let now = Timestamp::now().as_millis(); let at = |millis: i64| Timestamp::from_millis(millis); let ali_slot = appointment_slot::create(&db, &ali, at(now + 60_000), at(now + 120_000), None) .await .unwrap(); let ayse_slot = appointment_slot::create(&db, &ayse, at(now + 200_000), at(now + 260_000), None) .await .unwrap(); let pending = book(&db, ali_slot.get_id(), &veli, reason.clone()) .await .unwrap(); let approved = book(&db, ayse_slot.get_id(), &veli, reason.clone()) .await .unwrap(); approve(&db, approved.get_id(), &ayse).await.unwrap(); let settled_slot = appointment_slot::create(&db, &ali, at(now + 300_000), at(now + 360_000), None) .await .unwrap(); let cancelled = book(&db, settled_slot.get_id(), &veli, reason.clone()) .await .unwrap(); cancel(&db, cancelled.get_id(), &veli, None).await.unwrap(); // Absent: all three, newest first — the shape today's callers see. let (rows, total) = list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); assert_eq!((rows.len(), total), (3, 3)); // status: only the approved one, and total is the filtered count. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { status: Some(AppointmentStatus::Approved), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), approved.get_id()); // No state matches: an empty page, not an error. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { status: Some(AppointmentStatus::Rejected), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (0, 0)); // teacher joins through the slot: only the bookings on ali's slots. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { teacher: Some(&ali), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (2, 2)); assert!(rows.iter().all(|row| row.get_id() != approved.get_id())); // starts_after judges the effective start: the pending meeting (at // now + 60_000) is out; the approved and cancelled ones stay. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_after: Some(at(now + 150_000)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (2, 2)); assert!(rows.iter().all(|row| row.get_id() != pending.get_id())); // The window applies after the filter; total stays the filtered count. let (page1, total) = list_for_requester( &db, &veli, RequesterListParams { limit: Some(1), ..Default::default() }, ) .await .unwrap(); assert_eq!((page1.len(), total), (1, 3)); let (page2, total) = list_for_requester( &db, &veli, RequesterListParams { limit: Some(1), offset: 1, ..Default::default() }, ) .await .unwrap(); assert_eq!((page2.len(), total), (1, 3)); assert_ne!(page1[0].get_id(), page2[0].get_id()); // A standing proposal moves the meeting's start for the cut: the // pending booking proposes itself to now + 500_000, so it alone // clears a cut at now + 400_000. propose(&db, pending.get_id(), at(now + 500_000), at(now + 560_000), &ali) .await .unwrap(); let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_after: Some(at(now + 400_000)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), pending.get_id()); // The inbox: ali's two bookings and never ayse's — the join's // teacher predicate is the whole visibility universe here. let (rows, total) = list_for_teacher(&db, &ali, None, None, None, None, 0).await.unwrap(); assert_eq!((rows.len(), total), (2, 2)); let (rows, total) = list_for_teacher(&db, &ali, Some(AppointmentStatus::Cancelled), None, None, None, 0, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), cancelled.get_id()); let (_, total) = list_for_teacher(&db, &ali, None, Some(at(now + 400_000)), None, None, 0) .await .unwrap(); assert_eq!(total, 1); let (rows, total) = list_for_teacher(&db, &ali, None, None, None, Some(1), 0).await.unwrap(); assert_eq!((rows.len(), total), (1, 2)); // A settled booking whose slot is withdrawn still lists unfiltered — // today's shape — and the time and teacher filters drop it: it has // neither a meeting window nor a teacher any more. The guarded // delete sweeps its settled bookings along, so the dangling shape is // forced here the way a demotion sweep leaves it: the slot row goes, // and the slot's `ON DELETE SET NULL` strands the booking. sqlx::query("DELETE FROM appointment_slot WHERE id = $1") .bind(settled_slot.get_id().uuid()) .execute(&db) .await .unwrap(); let (rows, total) = list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); assert_eq!((rows.len(), total), (3, 3), "the windowless row still lists"); let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_after: Some(at(now)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (2, 2), "the windowless row has no start to match"); assert!(rows.iter().all(|row| row.get_id() != cancelled.get_id())); let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { teacher: Some(&ali), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1), "a windowless row names no teacher"); assert_eq!(rows[0].get_id(), pending.get_id()); } /// `starts_before` is the upper edge of a half-open month window: a row /// starting before the bound is out, a row inside it stays, the bound /// instant itself is excluded, and absent the set is unchanged. #[tokio::test] async fn starts_before_bounds_the_meeting_exclusively() { let (db, _leases) = init_test_db().await; let teacher = a_person(&db, "t1", "teacher").await; let veli = a_person(&db, "veli", "student").await; let reason = AppointmentReason::try_new("görüşme").unwrap(); let now = Timestamp::now().as_millis(); let at = |millis: i64| Timestamp::from_millis(millis); // Two bookings: one before the bound, one after it. let early_slot = appointment_slot::create(&db, &teacher, at(now + 60_000), at(now + 120_000), None) .await .unwrap(); let late_slot = appointment_slot::create(&db, &teacher, at(now + 200_000), at(now + 260_000), None) .await .unwrap(); let early = book(&db, early_slot.get_id(), &veli, reason.clone()) .await .unwrap(); let late = book(&db, late_slot.get_id(), &veli, reason.clone()) .await .unwrap(); // Absent: both — the set is unchanged without the bound. let (rows, total) = list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); assert_eq!((rows.len(), total), (2, 2)); // before: only the early booking survives. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_before: Some(at(now + 150_000)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), early.get_id()); // Half-open: the bound instant itself is outside the window, so a // cut exactly at the late meeting's start keeps only the early one — // and the inbox answers the same way. let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_before: Some(at(now + 200_000)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), early.get_id()); let (rows, total) = list_for_teacher(&db, &teacher, None, None, Some(at(now + 150_000)), None, 0) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), early.get_id()); // Composed with starts_after, the pair is the month window: only the // late meeting sits in [now + 150_000, now + 260_000). let (rows, total) = list_for_requester( &db, &veli, RequesterListParams { starts_after: Some(at(now + 150_000)), starts_before: Some(at(now + 260_000)), ..Default::default() }, ) .await .unwrap(); assert_eq!((rows.len(), total), (1, 1)); assert_eq!(rows[0].get_id(), late.get_id()); } }