| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | use crate::constant::CAS_UPDATE_RETRIES; |
| 23 | use crate::database::{Database, tx_with_retry}; |
| 24 | use crate::db::appointment; |
| 25 | use crate::db::appointment_slot; |
| 26 | use crate::db::cap::Claimed; |
| 27 | use crate::domain::appointment::{ |
| 28 | Appointment, AppointmentId, AppointmentReason, AppointmentStatus, |
| 29 | }; |
| 30 | use crate::domain::appointment_slot::AppointmentSlotId; |
| 31 | use crate::domain::timestamp::Timestamp; |
| 32 | use crate::domain::user::UserId; |
| 33 | use crate::error::{AppError, ValidationError}; |
| 34 | |
| 35 | pub use crate::db::appointment::RequesterListParams; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | pub async fn book( |
| 51 | db: &Database, |
| 52 | slot: &AppointmentSlotId, |
| 53 | requester: &UserId, |
| 54 | reason: AppointmentReason, |
| 55 | ) -> Result<Appointment, AppError> { |
| 56 | let slot_row = appointment_slot::read(db, slot) |
| 57 | .await? |
| 58 | .ok_or(AppError::NotFound)?; |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | if slot_row.get_starts_at().as_millis() <= Timestamp::now().as_millis() { |
| 66 | return Err(AppError::Conflict("the slot has already started")); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | if appointment::conflicts( |
| 72 | db, |
| 73 | requester, |
| 74 | requester, |
| 75 | slot_row.get_starts_at(), |
| 76 | slot_row.get_ends_at(), |
| 77 | None, |
| 78 | ) |
| 79 | .await? |
| 80 | { |
| 81 | return Err(AppError::Conflict( |
| 82 | "you already have an appointment at that time", |
| 83 | )); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | let appointment_row = Appointment { |
| 90 | id: AppointmentId::generate(), |
| 91 | slot: Some(slot.clone()), |
| 92 | requester: *requester, |
| 93 | status: AppointmentStatus::Pending, |
| 94 | reason, |
| 95 | proposed_starts_at: None, |
| 96 | proposed_ends_at: None, |
| 97 | proposed_by: None, |
| 98 | decided_by: None, |
| 99 | cancelled_by: None, |
| 100 | cancel_reason: None, |
| 101 | reject_reason: None, |
| 102 | created_at: Timestamp::now(), |
| 103 | }; |
| 104 | match appointment::claim_slot_and_create(db, appointment_row).await? { |
| 105 | Claimed::Made(created) => Ok(created), |
| 106 | Claimed::Full => { |
| 107 | if appointment_slot::read(db, slot).await?.is_none() { |
| 108 | return Err(AppError::NotFound); |
| 109 | } |
| 110 | Err(AppError::Conflict("the slot is already booked")) |
| 111 | } |
| 112 | |
| 113 | |
| 114 | Claimed::Duplicate => Err(AppError::Internal("appointment id collided".into())), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | pub async fn read(db: &Database, id: &AppointmentId) -> Result<Option<Appointment>, AppError> { |
| 121 | appointment::read(db, id).await |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | pub async fn list_for_requester( |
| 129 | db: &Database, |
| 130 | requester: &UserId, |
| 131 | params: RequesterListParams<'_>, |
| 132 | ) -> Result<(Vec<Appointment>, i64), AppError> { |
| 133 | appointment::list_for_requester(db, requester, params).await |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | pub async fn list_for_teacher( |
| 141 | db: &Database, |
| 142 | teacher: &UserId, |
| 143 | status: Option<AppointmentStatus>, |
| 144 | starts_after: Option<Timestamp>, |
| 145 | starts_before: Option<Timestamp>, |
| 146 | limit: Option<i64>, |
| 147 | offset: i64, |
| 148 | ) -> Result<(Vec<Appointment>, i64), AppError> { |
| 149 | appointment::list_for_teacher(db, teacher, status, starts_after, starts_before, limit, offset) |
| 150 | .await |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | pub async fn approve( |
| 165 | db: &Database, |
| 166 | id: &AppointmentId, |
| 167 | decided_by: &UserId, |
| 168 | ) -> Result<Appointment, AppError> { |
| 169 | approve_inner(db, id, decided_by, None).await |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | async fn approve_inner( |
| 184 | db: &Database, |
| 185 | id: &AppointmentId, |
| 186 | decided_by: &UserId, |
| 187 | accepting: Option<(Timestamp, Timestamp)>, |
| 188 | ) -> Result<Appointment, AppError> { |
| 189 | |
| 190 | let id = id.clone(); |
| 191 | let decided_by = *decided_by; |
| 192 | tx_with_retry(db, false, async move |tx| { |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | sqlx::query!("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE") |
| 198 | .execute(&mut *tx) |
| 199 | .await?; |
| 200 | let expected = appointment::read_pending_on(&mut *tx, &id).await?; |
| 201 | match accepting { |
| 202 | None if expected.has_proposal() => { |
| 203 | return Err(AppError::Conflict( |
| 204 | "a counter-proposal is standing; only the requester can accept it", |
| 205 | )); |
| 206 | } |
| 207 | Some(_) if !expected.has_proposal() => { |
| 208 | return Err(AppError::Conflict("no time has been proposed")); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | Some(accepted) if !expected.proposes(accepted) => { |
| 215 | return Err(AppError::Conflict( |
| 216 | "the proposed time has changed; re-read the booking and accept the new one", |
| 217 | )); |
| 218 | } |
| 219 | _ => {} |
| 220 | } |
| 221 | let slot_id = expected.get_slot().ok_or(AppError::NotFound)?.clone(); |
| 222 | let slot = appointment_slot::read_on(&mut *tx, &slot_id) |
| 223 | .await? |
| 224 | .ok_or(AppError::NotFound)?; |
| 225 | let (starts_at, ends_at) = expected.window(&slot); |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | if starts_at.as_millis() <= Timestamp::now().as_millis() { |
| 232 | return Err(AppError::Conflict("that time has already started")); |
| 233 | } |
| 234 | if appointment::conflicts_on( |
| 235 | &mut *tx, |
| 236 | slot.get_teacher(), |
| 237 | expected.get_requester(), |
| 238 | starts_at, |
| 239 | ends_at, |
| 240 | Some(&id), |
| 241 | ) |
| 242 | .await? |
| 243 | { |
| 244 | return Err(AppError::Conflict( |
| 245 | "that time collides with another approved appointment", |
| 246 | )); |
| 247 | } |
| 248 | let mut approved = expected.clone(); |
| 249 | approved.status = AppointmentStatus::Approved; |
| 250 | approved.decided_by = Some(decided_by); |
| 251 | match appointment::decision_cas(&mut *tx, &expected, approved).await? { |
| 252 | Some(saved) => Ok(saved), |
| 253 | |
| 254 | |
| 255 | |
| 256 | None => Err(contended()), |
| 257 | } |
| 258 | }) |
| 259 | .await |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | pub async fn reject( |
| 266 | db: &Database, |
| 267 | id: &AppointmentId, |
| 268 | decided_by: &UserId, |
| 269 | reason: Option<AppointmentReason>, |
| 270 | ) -> Result<Appointment, AppError> { |
| 271 | for _ in 0..CAS_UPDATE_RETRIES { |
| 272 | let expected = appointment::read_pending(db, id).await?; |
| 273 | let mut rejected = expected.clone(); |
| 274 | rejected.status = AppointmentStatus::Rejected; |
| 275 | rejected.decided_by = Some(*decided_by); |
| 276 | rejected.reject_reason = reason.clone(); |
| 277 | if let Some(saved) = appointment::save_if_unchanged(db, &expected, rejected).await? { |
| 278 | return Ok(saved); |
| 279 | } |
| 280 | } |
| 281 | Err(contended()) |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | pub async fn cancel( |
| 296 | db: &Database, |
| 297 | id: &AppointmentId, |
| 298 | cancelled_by: &UserId, |
| 299 | reason: Option<AppointmentReason>, |
| 300 | ) -> Result<Appointment, AppError> { |
| 301 | for _ in 0..CAS_UPDATE_RETRIES { |
| 302 | let expected = appointment::read(db, id).await?.ok_or(AppError::NotFound)?; |
| 303 | if !expected.get_status().is_live() { |
| 304 | return Err(AppError::Conflict("the appointment is already settled")); |
| 305 | } |
| 306 | let slot = appointment_slot::read(db, expected.get_slot().ok_or(AppError::NotFound)?) |
| 307 | .await? |
| 308 | .ok_or(AppError::NotFound)?; |
| 309 | if expected.window(&slot).0.as_millis() <= Timestamp::now().as_millis() { |
| 310 | return Err(AppError::Conflict("the appointment has already started")); |
| 311 | } |
| 312 | let mut cancelled = expected.clone(); |
| 313 | cancelled.status = AppointmentStatus::Cancelled; |
| 314 | cancelled.cancelled_by = Some(*cancelled_by); |
| 315 | cancelled.cancel_reason = reason.clone(); |
| 316 | if let Some(saved) = appointment::save_if_unchanged(db, &expected, cancelled).await? { |
| 317 | return Ok(saved); |
| 318 | } |
| 319 | } |
| 320 | Err(contended()) |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | pub async fn propose( |
| 335 | db: &Database, |
| 336 | id: &AppointmentId, |
| 337 | starts_at: Timestamp, |
| 338 | ends_at: Timestamp, |
| 339 | proposed_by: &UserId, |
| 340 | ) -> Result<Appointment, AppError> { |
| 341 | if starts_at.as_millis() >= ends_at.as_millis() { |
| 342 | return Err(ValidationError::Invalid { |
| 343 | field: "proposed_ends_at", |
| 344 | reason: "must be after proposed_starts_at", |
| 345 | } |
| 346 | .into()); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | for _ in 0..CAS_UPDATE_RETRIES { |
| 351 | let expected = appointment::read(db, id).await?.ok_or(AppError::NotFound)?; |
| 352 | if !expected.get_status().is_live() { |
| 353 | return Err(AppError::Conflict("the appointment is already settled")); |
| 354 | } |
| 355 | if starts_at.as_millis() <= Timestamp::now().as_millis() { |
| 356 | return Err(AppError::Conflict("that time has already started")); |
| 357 | } |
| 358 | let mut proposed = expected.clone(); |
| 359 | proposed.status = AppointmentStatus::Pending; |
| 360 | proposed.decided_by = None; |
| 361 | proposed.proposed_starts_at = Some(starts_at); |
| 362 | proposed.proposed_ends_at = Some(ends_at); |
| 363 | proposed.proposed_by = Some(*proposed_by); |
| 364 | if let Some(saved) = appointment::save_if_unchanged(db, &expected, proposed).await? { |
| 365 | return Ok(saved); |
| 366 | } |
| 367 | } |
| 368 | Err(contended()) |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | pub async fn accept_proposal( |
| 381 | db: &Database, |
| 382 | id: &AppointmentId, |
| 383 | decided_by: &UserId, |
| 384 | starts_at: Timestamp, |
| 385 | ends_at: Timestamp, |
| 386 | ) -> Result<Appointment, AppError> { |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | approve_inner(db, id, decided_by, Some((starts_at, ends_at))).await |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | fn contended() -> AppError { |
| 398 | AppError::Conflict("the appointment is being decided elsewhere") |
| 399 | } |
| 400 | #[cfg(test)] |
| 401 | mod tests { |
| 402 | use super::*; |
| 403 | use crate::database::{Database, init_test_db}; |
| 404 | use crate::service::appointment_slot; |
| 405 | |
| 406 | fn at(millis: i64) -> Timestamp { |
| 407 | Timestamp::from_millis(millis) |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | async fn a_person(db: &Database, label: &str, role: &str) -> UserId { |
| 414 | let user = UserId::generate(); |
| 415 | sqlx::query( |
| 416 | "INSERT INTO app_user (id, username, created_at, role) \ |
| 417 | VALUES ($1, $2, 0, $3)", |
| 418 | ) |
| 419 | .bind(user.uuid()) |
| 420 | .bind(format!("{label}-{}", &user.key()[30..])) |
| 421 | .bind(role) |
| 422 | .execute(db) |
| 423 | .await |
| 424 | .unwrap(); |
| 425 | user |
| 426 | } |
| 427 | |
| 428 | |
| 429 | fn soon(offset: i64) -> Timestamp { |
| 430 | at(Timestamp::now().as_millis() + offset) |
| 431 | } |
| 432 | |
| 433 | #[tokio::test] |
| 434 | async fn window_prefers_a_proposal_over_the_slot() { |
| 435 | let (db, _leases) = init_test_db().await; |
| 436 | let teacher = a_person(&db, "t1", "teacher").await; |
| 437 | let (starts_at, ends_at) = (soon(60_000), soon(120_000)); |
| 438 | let slot = appointment_slot::create(&db, &teacher, starts_at, ends_at, None) |
| 439 | .await |
| 440 | .unwrap(); |
| 441 | let mut appointment_row = book( |
| 442 | &db, |
| 443 | slot.get_id(), |
| 444 | &a_person(&db, "s1", "student").await, |
| 445 | AppointmentReason::try_new("ödev").unwrap(), |
| 446 | ) |
| 447 | .await |
| 448 | .unwrap(); |
| 449 | |
| 450 | assert_eq!(appointment_row.window(&slot), (starts_at, ends_at)); |
| 451 | appointment_row.proposed_starts_at = Some(at(5_000)); |
| 452 | appointment_row.proposed_ends_at = Some(at(6_000)); |
| 453 | assert_eq!(appointment_row.window(&slot), (at(5_000), at(6_000))); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | |
| 458 | #[tokio::test] |
| 459 | async fn a_started_slot_cannot_be_booked() { |
| 460 | let (db, _leases) = init_test_db().await; |
| 461 | let teacher = a_person(&db, "t1", "teacher").await; |
| 462 | let reason = || AppointmentReason::try_new("görüşme").unwrap(); |
| 463 | let started = appointment_slot::create(&db, &teacher, soon(-30_000), soon(60_000), None) |
| 464 | .await |
| 465 | .unwrap(); |
| 466 | assert!(matches!( |
| 467 | book( |
| 468 | &db, |
| 469 | started.get_id(), |
| 470 | &a_person(&db, "s1", "student").await, |
| 471 | reason() |
| 472 | ) |
| 473 | .await, |
| 474 | Err(AppError::Conflict("the slot has already started")) |
| 475 | )); |
| 476 | |
| 477 | let upcoming = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 478 | .await |
| 479 | .unwrap(); |
| 480 | assert!( |
| 481 | book( |
| 482 | &db, |
| 483 | upcoming.get_id(), |
| 484 | &a_person(&db, "s1", "student").await, |
| 485 | reason() |
| 486 | ) |
| 487 | .await |
| 488 | .is_ok() |
| 489 | ); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | #[tokio::test] |
| 495 | async fn a_started_proposal_cannot_be_accepted() { |
| 496 | let (db, _leases) = init_test_db().await; |
| 497 | let teacher = a_person(&db, "t1", "teacher").await; |
| 498 | let student = a_person(&db, "s1", "student").await; |
| 499 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 500 | .await |
| 501 | .unwrap(); |
| 502 | let booking = book( |
| 503 | &db, |
| 504 | slot.get_id(), |
| 505 | &student, |
| 506 | AppointmentReason::try_new("görüşme").unwrap(), |
| 507 | ) |
| 508 | .await |
| 509 | .unwrap(); |
| 510 | |
| 511 | |
| 512 | |
| 513 | assert!(matches!( |
| 514 | propose(&db, booking.get_id(), soon(-30_000), soon(30_000), &teacher).await, |
| 515 | Err(AppError::Conflict("that time has already started")) |
| 516 | )); |
| 517 | |
| 518 | |
| 519 | |
| 520 | let current = appointment::read(&db, booking.get_id()) |
| 521 | .await |
| 522 | .unwrap() |
| 523 | .unwrap(); |
| 524 | let (passed_starts_at, passed_ends_at) = (soon(-30_000), soon(30_000)); |
| 525 | let mut standing = current.clone(); |
| 526 | standing.proposed_starts_at = Some(passed_starts_at); |
| 527 | standing.proposed_ends_at = Some(passed_ends_at); |
| 528 | appointment::save_if_unchanged(&db, ¤t, standing) |
| 529 | .await |
| 530 | .unwrap() |
| 531 | .unwrap(); |
| 532 | assert!(matches!( |
| 533 | accept_proposal( |
| 534 | &db, |
| 535 | booking.get_id(), |
| 536 | &student, |
| 537 | passed_starts_at, |
| 538 | passed_ends_at |
| 539 | ) |
| 540 | .await, |
| 541 | Err(AppError::Conflict("that time has already started")) |
| 542 | )); |
| 543 | |
| 544 | |
| 545 | let after = appointment::read(&db, booking.get_id()).await.unwrap(); |
| 546 | assert_eq!(after.unwrap().get_status(), AppointmentStatus::Pending); |
| 547 | let (starts_at, ends_at) = (soon(60_000), soon(120_000)); |
| 548 | propose(&db, booking.get_id(), starts_at, ends_at, &teacher) |
| 549 | .await |
| 550 | .unwrap(); |
| 551 | |
| 552 | |
| 553 | assert!(matches!( |
| 554 | accept_proposal( |
| 555 | &db, |
| 556 | booking.get_id(), |
| 557 | &student, |
| 558 | passed_starts_at, |
| 559 | passed_ends_at |
| 560 | ) |
| 561 | .await, |
| 562 | Err(AppError::Conflict( |
| 563 | "the proposed time has changed; \ |
| 564 | re-read the booking and accept the new one" |
| 565 | )) |
| 566 | )); |
| 567 | let accepted = accept_proposal(&db, booking.get_id(), &student, starts_at, ends_at) |
| 568 | .await |
| 569 | .unwrap(); |
| 570 | assert_eq!(accepted.get_status(), AppointmentStatus::Approved); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | #[tokio::test] |
| 583 | async fn a_decision_built_on_a_stale_snapshot_never_lands() { |
| 584 | let (db, _leases) = init_test_db().await; |
| 585 | let teacher = a_person(&db, "t1", "teacher").await; |
| 586 | let student = a_person(&db, "s1", "student").await; |
| 587 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 588 | .await |
| 589 | .unwrap(); |
| 590 | let booking = book( |
| 591 | &db, |
| 592 | slot.get_id(), |
| 593 | &student, |
| 594 | AppointmentReason::try_new("görüşme").unwrap(), |
| 595 | ) |
| 596 | .await |
| 597 | .unwrap(); |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | let stale = booking.clone(); |
| 603 | let (proposed_starts_at, proposed_ends_at) = (soon(180_000), soon(240_000)); |
| 604 | propose( |
| 605 | &db, |
| 606 | booking.get_id(), |
| 607 | proposed_starts_at, |
| 608 | proposed_ends_at, |
| 609 | &teacher, |
| 610 | ) |
| 611 | .await |
| 612 | .unwrap(); |
| 613 | |
| 614 | let mut late = stale.clone(); |
| 615 | late.status = AppointmentStatus::Approved; |
| 616 | late.decided_by = Some(teacher); |
| 617 | assert!( |
| 618 | appointment::save_if_unchanged(&db, &stale, late) |
| 619 | .await |
| 620 | .unwrap() |
| 621 | .is_none() |
| 622 | ); |
| 623 | |
| 624 | |
| 625 | |
| 626 | let after = appointment::read(&db, booking.get_id()) |
| 627 | .await |
| 628 | .unwrap() |
| 629 | .unwrap(); |
| 630 | assert_eq!(after.get_status(), AppointmentStatus::Pending); |
| 631 | assert_eq!(after.get_proposed_starts_at(), Some(proposed_starts_at)); |
| 632 | assert_eq!(after.get_proposed_ends_at(), Some(proposed_ends_at)); |
| 633 | assert_eq!(after.get_decided_by(), None); |
| 634 | |
| 635 | |
| 636 | |
| 637 | let mut fresh = after.clone(); |
| 638 | fresh.status = AppointmentStatus::Approved; |
| 639 | fresh.decided_by = Some(teacher); |
| 640 | let saved = appointment::save_if_unchanged(&db, &after, fresh) |
| 641 | .await |
| 642 | .unwrap() |
| 643 | .expect("a current snapshot must write"); |
| 644 | assert_eq!(saved.get_status(), AppointmentStatus::Approved); |
| 645 | assert_eq!(saved.get_proposed_starts_at(), Some(proposed_starts_at)); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | async fn occupied(slot: &AppointmentSlotId, db: &Database) -> i64 { |
| 651 | sqlx::query_scalar::<_, i64>("SELECT occupied FROM appointment_slot WHERE id = $1") |
| 652 | .bind(slot.uuid()) |
| 653 | .fetch_one(db) |
| 654 | .await |
| 655 | .unwrap() |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | async fn claim_seat(slot: &AppointmentSlotId, cap: i64, db: &Database) -> bool { |
| 662 | sqlx::query_scalar::<_, i32>( |
| 663 | "UPDATE appointment_slot SET occupied = occupied + 1 \ |
| 664 | WHERE id = $1 AND occupied < $2 RETURNING 1", |
| 665 | ) |
| 666 | .bind(slot.uuid()) |
| 667 | .bind(cap) |
| 668 | .fetch_optional(db) |
| 669 | .await |
| 670 | .unwrap() |
| 671 | .is_some() |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | #[tokio::test] |
| 685 | async fn a_concurrent_claim_cannot_take_a_booked_slot() { |
| 686 | let (db, _leases) = init_test_db().await; |
| 687 | let teacher = a_person(&db, "t1", "teacher").await; |
| 688 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 689 | .await |
| 690 | .unwrap(); |
| 691 | assert_eq!(occupied(slot.get_id(), &db).await, 0); |
| 692 | |
| 693 | book( |
| 694 | &db, |
| 695 | slot.get_id(), |
| 696 | &a_person(&db, "s1", "student").await, |
| 697 | AppointmentReason::try_new("görüşme").unwrap(), |
| 698 | ) |
| 699 | .await |
| 700 | .unwrap(); |
| 701 | assert_eq!(occupied(slot.get_id(), &db).await, 1); |
| 702 | |
| 703 | assert!( |
| 704 | !claim_seat(slot.get_id(), 1, &db).await, |
| 705 | "the slot's seat must already be taken" |
| 706 | ); |
| 707 | |
| 708 | |
| 709 | assert_eq!(occupied(slot.get_id(), &db).await, 1); |
| 710 | assert_eq!( |
| 711 | appointment::list_for_slot(&db, slot.get_id()) |
| 712 | .await |
| 713 | .unwrap() |
| 714 | .len(), |
| 715 | 1 |
| 716 | ); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | #[tokio::test] |
| 724 | async fn a_refused_booking_moves_nothing() { |
| 725 | let (db, _leases) = init_test_db().await; |
| 726 | let teacher = a_person(&db, "t1", "teacher").await; |
| 727 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 728 | .await |
| 729 | .unwrap(); |
| 730 | let reason = || AppointmentReason::try_new("görüşme").unwrap(); |
| 731 | |
| 732 | book( |
| 733 | &db, |
| 734 | slot.get_id(), |
| 735 | &a_person(&db, "s1", "student").await, |
| 736 | reason(), |
| 737 | ) |
| 738 | .await |
| 739 | .unwrap(); |
| 740 | assert_eq!(occupied(slot.get_id(), &db).await, 1); |
| 741 | |
| 742 | let refused = book( |
| 743 | &db, |
| 744 | slot.get_id(), |
| 745 | &a_person(&db, "s2", "student").await, |
| 746 | reason(), |
| 747 | ) |
| 748 | .await; |
| 749 | assert!( |
| 750 | matches!(refused, Err(AppError::Conflict(_))), |
| 751 | "the slot is taken" |
| 752 | ); |
| 753 | assert_eq!( |
| 754 | occupied(slot.get_id(), &db).await, |
| 755 | 1, |
| 756 | "the refusal took no seat" |
| 757 | ); |
| 758 | assert_eq!( |
| 759 | appointment::list_for_slot(&db, slot.get_id()) |
| 760 | .await |
| 761 | .unwrap() |
| 762 | .len(), |
| 763 | 1, |
| 764 | "and wrote no row" |
| 765 | ); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | #[tokio::test] |
| 772 | async fn cancelling_hands_the_seat_back_with_the_status() { |
| 773 | let (db, _leases) = init_test_db().await; |
| 774 | let teacher = a_person(&db, "t1", "teacher").await; |
| 775 | let student = a_person(&db, "s1", "student").await; |
| 776 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 777 | .await |
| 778 | .unwrap(); |
| 779 | let booking = book( |
| 780 | &db, |
| 781 | slot.get_id(), |
| 782 | &student, |
| 783 | AppointmentReason::try_new("görüşme").unwrap(), |
| 784 | ) |
| 785 | .await |
| 786 | .unwrap(); |
| 787 | approve(&db, booking.get_id(), &teacher).await.unwrap(); |
| 788 | assert_eq!(occupied(slot.get_id(), &db).await, 1, "approval holds it"); |
| 789 | |
| 790 | cancel(&db, booking.get_id(), &student, None).await.unwrap(); |
| 791 | assert_eq!(occupied(slot.get_id(), &db).await, 0); |
| 792 | |
| 793 | assert!(cancel(&db, booking.get_id(), &student, None).await.is_err()); |
| 794 | assert_eq!(occupied(slot.get_id(), &db).await, 0); |
| 795 | assert!( |
| 796 | book( |
| 797 | &db, |
| 798 | slot.get_id(), |
| 799 | &a_person(&db, "s2", "student").await, |
| 800 | AppointmentReason::try_new("görüşme").unwrap() |
| 801 | ) |
| 802 | .await |
| 803 | .is_ok() |
| 804 | ); |
| 805 | assert_eq!(occupied(slot.get_id(), &db).await, 1); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | #[tokio::test] |
| 810 | async fn rejecting_frees_the_slot_for_re_booking() { |
| 811 | let (db, _leases) = init_test_db().await; |
| 812 | let teacher = a_person(&db, "t1", "teacher").await; |
| 813 | let slot = appointment_slot::create(&db, &teacher, soon(60_000), soon(120_000), None) |
| 814 | .await |
| 815 | .unwrap(); |
| 816 | let reason = || AppointmentReason::try_new("görüşme").unwrap(); |
| 817 | |
| 818 | let first = book( |
| 819 | &db, |
| 820 | slot.get_id(), |
| 821 | &a_person(&db, "s1", "student").await, |
| 822 | reason(), |
| 823 | ) |
| 824 | .await |
| 825 | .unwrap(); |
| 826 | assert!(matches!( |
| 827 | book( |
| 828 | &db, |
| 829 | slot.get_id(), |
| 830 | &a_person(&db, "s2", "student").await, |
| 831 | reason() |
| 832 | ) |
| 833 | .await, |
| 834 | Err(AppError::Conflict(_)) |
| 835 | )); |
| 836 | |
| 837 | assert!(matches!( |
| 838 | appointment_slot::delete(&db, slot.clone()).await, |
| 839 | Err(AppError::Conflict(_)) |
| 840 | )); |
| 841 | |
| 842 | reject(&db, first.get_id(), &teacher, None).await.unwrap(); |
| 843 | assert_eq!(occupied(slot.get_id(), &db).await, 0); |
| 844 | let second = book( |
| 845 | &db, |
| 846 | slot.get_id(), |
| 847 | &a_person(&db, "s2", "student").await, |
| 848 | reason(), |
| 849 | ) |
| 850 | .await |
| 851 | .unwrap(); |
| 852 | assert_eq!(second.get_status(), AppointmentStatus::Pending); |
| 853 | assert_eq!(occupied(slot.get_id(), &db).await, 1); |
| 854 | } |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | #[tokio::test] |
| 864 | async fn listings_filter_by_status_start_and_teacher() { |
| 865 | let (db, _leases) = init_test_db().await; |
| 866 | let ali = a_person(&db, "ali", "teacher").await; |
| 867 | let ayse = a_person(&db, "ayse", "teacher").await; |
| 868 | let veli = a_person(&db, "veli", "student").await; |
| 869 | let reason = AppointmentReason::try_new("görüşme").unwrap(); |
| 870 | let now = Timestamp::now().as_millis(); |
| 871 | let at = |millis: i64| Timestamp::from_millis(millis); |
| 872 | |
| 873 | let ali_slot = |
| 874 | appointment_slot::create(&db, &ali, at(now + 60_000), at(now + 120_000), None) |
| 875 | .await |
| 876 | .unwrap(); |
| 877 | let ayse_slot = |
| 878 | appointment_slot::create(&db, &ayse, at(now + 200_000), at(now + 260_000), None) |
| 879 | .await |
| 880 | .unwrap(); |
| 881 | let pending = book(&db, ali_slot.get_id(), &veli, reason.clone()) |
| 882 | .await |
| 883 | .unwrap(); |
| 884 | let approved = book(&db, ayse_slot.get_id(), &veli, reason.clone()) |
| 885 | .await |
| 886 | .unwrap(); |
| 887 | approve(&db, approved.get_id(), &ayse).await.unwrap(); |
| 888 | let settled_slot = |
| 889 | appointment_slot::create(&db, &ali, at(now + 300_000), at(now + 360_000), None) |
| 890 | .await |
| 891 | .unwrap(); |
| 892 | let cancelled = book(&db, settled_slot.get_id(), &veli, reason.clone()) |
| 893 | .await |
| 894 | .unwrap(); |
| 895 | cancel(&db, cancelled.get_id(), &veli, None).await.unwrap(); |
| 896 | |
| 897 | |
| 898 | let (rows, total) = |
| 899 | list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 900 | assert_eq!((rows.len(), total), (3, 3)); |
| 901 | |
| 902 | |
| 903 | let (rows, total) = list_for_requester( |
| 904 | &db, |
| 905 | &veli, |
| 906 | RequesterListParams { |
| 907 | status: Some(AppointmentStatus::Approved), |
| 908 | ..Default::default() |
| 909 | }, |
| 910 | ) |
| 911 | .await |
| 912 | .unwrap(); |
| 913 | assert_eq!((rows.len(), total), (1, 1)); |
| 914 | assert_eq!(rows[0].get_id(), approved.get_id()); |
| 915 | |
| 916 | |
| 917 | let (rows, total) = list_for_requester( |
| 918 | &db, |
| 919 | &veli, |
| 920 | RequesterListParams { |
| 921 | status: Some(AppointmentStatus::Rejected), |
| 922 | ..Default::default() |
| 923 | }, |
| 924 | ) |
| 925 | .await |
| 926 | .unwrap(); |
| 927 | assert_eq!((rows.len(), total), (0, 0)); |
| 928 | |
| 929 | |
| 930 | let (rows, total) = list_for_requester( |
| 931 | &db, |
| 932 | &veli, |
| 933 | RequesterListParams { |
| 934 | teacher: Some(&ali), |
| 935 | ..Default::default() |
| 936 | }, |
| 937 | ) |
| 938 | .await |
| 939 | .unwrap(); |
| 940 | assert_eq!((rows.len(), total), (2, 2)); |
| 941 | assert!(rows.iter().all(|row| row.get_id() != approved.get_id())); |
| 942 | |
| 943 | |
| 944 | |
| 945 | let (rows, total) = list_for_requester( |
| 946 | &db, |
| 947 | &veli, |
| 948 | RequesterListParams { |
| 949 | starts_after: Some(at(now + 150_000)), |
| 950 | ..Default::default() |
| 951 | }, |
| 952 | ) |
| 953 | .await |
| 954 | .unwrap(); |
| 955 | assert_eq!((rows.len(), total), (2, 2)); |
| 956 | assert!(rows.iter().all(|row| row.get_id() != pending.get_id())); |
| 957 | |
| 958 | |
| 959 | let (page1, total) = list_for_requester( |
| 960 | &db, |
| 961 | &veli, |
| 962 | RequesterListParams { |
| 963 | limit: Some(1), |
| 964 | ..Default::default() |
| 965 | }, |
| 966 | ) |
| 967 | .await |
| 968 | .unwrap(); |
| 969 | assert_eq!((page1.len(), total), (1, 3)); |
| 970 | let (page2, total) = list_for_requester( |
| 971 | &db, |
| 972 | &veli, |
| 973 | RequesterListParams { |
| 974 | limit: Some(1), |
| 975 | offset: 1, |
| 976 | ..Default::default() |
| 977 | }, |
| 978 | ) |
| 979 | .await |
| 980 | .unwrap(); |
| 981 | assert_eq!((page2.len(), total), (1, 3)); |
| 982 | assert_ne!(page1[0].get_id(), page2[0].get_id()); |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | propose(&db, pending.get_id(), at(now + 500_000), at(now + 560_000), &ali) |
| 988 | .await |
| 989 | .unwrap(); |
| 990 | let (rows, total) = list_for_requester( |
| 991 | &db, |
| 992 | &veli, |
| 993 | RequesterListParams { |
| 994 | starts_after: Some(at(now + 400_000)), |
| 995 | ..Default::default() |
| 996 | }, |
| 997 | ) |
| 998 | .await |
| 999 | .unwrap(); |
| 1000 | assert_eq!((rows.len(), total), (1, 1)); |
| 1001 | assert_eq!(rows[0].get_id(), pending.get_id()); |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | let (rows, total) = |
| 1006 | list_for_teacher(&db, &ali, None, None, None, None, 0).await.unwrap(); |
| 1007 | assert_eq!((rows.len(), total), (2, 2)); |
| 1008 | let (rows, total) = list_for_teacher(&db, &ali, Some(AppointmentStatus::Cancelled), None, None, None, |
| 1009 | 0, |
| 1010 | ) |
| 1011 | .await |
| 1012 | .unwrap(); |
| 1013 | assert_eq!((rows.len(), total), (1, 1)); |
| 1014 | assert_eq!(rows[0].get_id(), cancelled.get_id()); |
| 1015 | let (_, total) = |
| 1016 | list_for_teacher(&db, &ali, None, Some(at(now + 400_000)), None, None, 0) |
| 1017 | .await |
| 1018 | .unwrap(); |
| 1019 | assert_eq!(total, 1); |
| 1020 | let (rows, total) = |
| 1021 | list_for_teacher(&db, &ali, None, None, None, Some(1), 0).await.unwrap(); |
| 1022 | assert_eq!((rows.len(), total), (1, 2)); |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | sqlx::query("DELETE FROM appointment_slot WHERE id = $1") |
| 1031 | .bind(settled_slot.get_id().uuid()) |
| 1032 | .execute(&db) |
| 1033 | .await |
| 1034 | .unwrap(); |
| 1035 | let (rows, total) = |
| 1036 | list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 1037 | assert_eq!((rows.len(), total), (3, 3), "the windowless row still lists"); |
| 1038 | let (rows, total) = list_for_requester( |
| 1039 | &db, |
| 1040 | &veli, |
| 1041 | RequesterListParams { |
| 1042 | starts_after: Some(at(now)), |
| 1043 | ..Default::default() |
| 1044 | }, |
| 1045 | ) |
| 1046 | .await |
| 1047 | .unwrap(); |
| 1048 | assert_eq!((rows.len(), total), (2, 2), "the windowless row has no start to match"); |
| 1049 | assert!(rows.iter().all(|row| row.get_id() != cancelled.get_id())); |
| 1050 | let (rows, total) = list_for_requester( |
| 1051 | &db, |
| 1052 | &veli, |
| 1053 | RequesterListParams { |
| 1054 | teacher: Some(&ali), |
| 1055 | ..Default::default() |
| 1056 | }, |
| 1057 | ) |
| 1058 | .await |
| 1059 | .unwrap(); |
| 1060 | assert_eq!((rows.len(), total), (1, 1), "a windowless row names no teacher"); |
| 1061 | assert_eq!(rows[0].get_id(), pending.get_id()); |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | #[tokio::test] |
| 1068 | async fn starts_before_bounds_the_meeting_exclusively() { |
| 1069 | let (db, _leases) = init_test_db().await; |
| 1070 | let teacher = a_person(&db, "t1", "teacher").await; |
| 1071 | let veli = a_person(&db, "veli", "student").await; |
| 1072 | let reason = AppointmentReason::try_new("görüşme").unwrap(); |
| 1073 | let now = Timestamp::now().as_millis(); |
| 1074 | let at = |millis: i64| Timestamp::from_millis(millis); |
| 1075 | |
| 1076 | |
| 1077 | let early_slot = |
| 1078 | appointment_slot::create(&db, &teacher, at(now + 60_000), at(now + 120_000), None) |
| 1079 | .await |
| 1080 | .unwrap(); |
| 1081 | let late_slot = |
| 1082 | appointment_slot::create(&db, &teacher, at(now + 200_000), at(now + 260_000), None) |
| 1083 | .await |
| 1084 | .unwrap(); |
| 1085 | let early = book(&db, early_slot.get_id(), &veli, reason.clone()) |
| 1086 | .await |
| 1087 | .unwrap(); |
| 1088 | let late = book(&db, late_slot.get_id(), &veli, reason.clone()) |
| 1089 | .await |
| 1090 | .unwrap(); |
| 1091 | |
| 1092 | |
| 1093 | let (rows, total) = |
| 1094 | list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 1095 | assert_eq!((rows.len(), total), (2, 2)); |
| 1096 | |
| 1097 | |
| 1098 | let (rows, total) = list_for_requester( |
| 1099 | &db, |
| 1100 | &veli, |
| 1101 | RequesterListParams { |
| 1102 | starts_before: Some(at(now + 150_000)), |
| 1103 | ..Default::default() |
| 1104 | }, |
| 1105 | ) |
| 1106 | .await |
| 1107 | .unwrap(); |
| 1108 | assert_eq!((rows.len(), total), (1, 1)); |
| 1109 | assert_eq!(rows[0].get_id(), early.get_id()); |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | let (rows, total) = list_for_requester( |
| 1115 | &db, |
| 1116 | &veli, |
| 1117 | RequesterListParams { |
| 1118 | starts_before: Some(at(now + 200_000)), |
| 1119 | ..Default::default() |
| 1120 | }, |
| 1121 | ) |
| 1122 | .await |
| 1123 | .unwrap(); |
| 1124 | assert_eq!((rows.len(), total), (1, 1)); |
| 1125 | assert_eq!(rows[0].get_id(), early.get_id()); |
| 1126 | |
| 1127 | let (rows, total) = |
| 1128 | list_for_teacher(&db, &teacher, None, None, Some(at(now + 150_000)), None, 0) |
| 1129 | .await |
| 1130 | .unwrap(); |
| 1131 | assert_eq!((rows.len(), total), (1, 1)); |
| 1132 | assert_eq!(rows[0].get_id(), early.get_id()); |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | let (rows, total) = list_for_requester( |
| 1137 | &db, |
| 1138 | &veli, |
| 1139 | RequesterListParams { |
| 1140 | starts_after: Some(at(now + 150_000)), |
| 1141 | starts_before: Some(at(now + 260_000)), |
| 1142 | ..Default::default() |
| 1143 | }, |
| 1144 | ) |
| 1145 | .await |
| 1146 | .unwrap(); |
| 1147 | assert_eq!((rows.len(), total), (1, 1)); |
| 1148 | assert_eq!(rows[0].get_id(), late.get_id()); |
| 1149 | } |
| 1150 | } |