Record who cancelled or rejected and why
Browse files at this commit5d48a8d4350c220a04b1efec5bb7b228c9f5f165Parent: e9ffc5e- feat
- record who cancelled or rejected and why
4 changed files +155 -6
- src/database.rs +3 -0
- src/domain/appointment.rs +36 -2
- src/web/appointments.rs +53 -3
- tests/integration.rs +63 -1
src/database.rsmodified+3 -0
| @@ -436,6 +436,9 @@ const MIGRATION: &str = " | |||
| 436 | DEFINE FIELD IF NOT EXISTS proposed_ends_at ON appointment TYPE option<int>; | 436 | DEFINE FIELD IF NOT EXISTS proposed_ends_at ON appointment TYPE option<int>; |
| 437 | DEFINE FIELD IF NOT EXISTS proposed_by ON appointment TYPE option<record<user>>; | 437 | DEFINE FIELD IF NOT EXISTS proposed_by ON appointment TYPE option<record<user>>; |
| 438 | DEFINE FIELD IF NOT EXISTS decided_by ON appointment TYPE option<record<user>>; | 438 | DEFINE FIELD IF NOT EXISTS decided_by ON appointment TYPE option<record<user>>; |
| 439 | + DEFINE FIELD IF NOT EXISTS cancelled_by ON appointment TYPE option<record<user>>; | ||
| 440 | + DEFINE FIELD IF NOT EXISTS cancel_reason ON appointment TYPE option<string>; | ||
| 441 | + DEFINE FIELD IF NOT EXISTS reject_reason ON appointment TYPE option<string>; | ||
| 439 | DEFINE FIELD IF NOT EXISTS created_at ON appointment TYPE int; | 442 | DEFINE FIELD IF NOT EXISTS created_at ON appointment TYPE int; |
| 440 | DEFINE INDEX IF NOT EXISTS appointment_slot_ref ON appointment FIELDS slot; | 443 | DEFINE INDEX IF NOT EXISTS appointment_slot_ref ON appointment FIELDS slot; |
| 441 | DEFINE INDEX IF NOT EXISTS appointment_requester ON appointment FIELDS requester; | 444 | DEFINE INDEX IF NOT EXISTS appointment_requester ON appointment FIELDS requester; |
src/domain/appointment.rsmodified+36 -2
| @@ -153,6 +153,16 @@ pub struct Appointment { | |||
| 153 | proposed_ends_at: Option<Timestamp>, | 153 | proposed_ends_at: Option<Timestamp>, |
| 154 | proposed_by: Option<UserId>, | 154 | proposed_by: Option<UserId>, |
| 155 | decided_by: Option<UserId>, | 155 | decided_by: Option<UserId>, |
| 156 | + /// Who called the meeting off, and why, once it is `cancelled`. Both | ||
| 157 | + /// `#[surreal(default)]` so bookings written before cancellation records | ||
| 158 | + /// existed still read back (they resolve to `None`). | ||
| 159 | + #[surreal(default)] | ||
| 160 | + cancelled_by: Option<UserId>, | ||
| 161 | + #[surreal(default)] | ||
| 162 | + cancel_reason: Option<AppointmentReason>, | ||
| 163 | + /// Why the request was turned down; the rejecter is already on `decided_by`. | ||
| 164 | + #[surreal(default)] | ||
| 165 | + reject_reason: Option<AppointmentReason>, | ||
| 156 | created_at: Timestamp, | 166 | created_at: Timestamp, |
| 157 | } | 167 | } |
| 158 | 168 | ||
| @@ -193,6 +203,18 @@ impl Appointment { | |||
| 193 | self.decided_by.as_ref() | 203 | self.decided_by.as_ref() |
| 194 | } | 204 | } |
| 195 | 205 | ||
| 206 | + pub fn get_cancelled_by(&self) -> Option<&UserId> { | ||
| 207 | + self.cancelled_by.as_ref() | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + pub fn get_cancel_reason(&self) -> Option<&AppointmentReason> { | ||
| 211 | + self.cancel_reason.as_ref() | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + pub fn get_reject_reason(&self) -> Option<&AppointmentReason> { | ||
| 215 | + self.reject_reason.as_ref() | ||
| 216 | + } | ||
| 217 | + | ||
| 196 | pub fn get_created_at(&self) -> Timestamp { | 218 | pub fn get_created_at(&self) -> Timestamp { |
| 197 | self.created_at | 219 | self.created_at |
| 198 | } | 220 | } |
| @@ -339,6 +361,9 @@ impl Appointment { | |||
| 339 | proposed_ends_at: None, | 361 | proposed_ends_at: None, |
| 340 | proposed_by: None, | 362 | proposed_by: None, |
| 341 | decided_by: None, | 363 | decided_by: None, |
| 364 | + cancelled_by: None, | ||
| 365 | + cancel_reason: None, | ||
| 366 | + reject_reason: None, | ||
| 342 | created_at: Timestamp::now(), | 367 | created_at: Timestamp::now(), |
| 343 | }; | 368 | }; |
| 344 | let created: Option<Appointment> = db | 369 | let created: Option<Appointment> = db |
| @@ -437,12 +462,14 @@ impl Appointment { | |||
| 437 | pub async fn reject( | 462 | pub async fn reject( |
| 438 | id: &AppointmentId, | 463 | id: &AppointmentId, |
| 439 | decided_by: &UserId, | 464 | decided_by: &UserId, |
| 465 | + reason: Option<AppointmentReason>, | ||
| 440 | db: &Database, | 466 | db: &Database, |
| 441 | ) -> Result<Appointment, AppError> { | 467 | ) -> Result<Appointment, AppError> { |
| 442 | let _guard = APPOINTMENT_LOCK.lock().await; | 468 | let _guard = APPOINTMENT_LOCK.lock().await; |
| 443 | let mut appointment = Self::read_pending(id, db).await?; | 469 | let mut appointment = Self::read_pending(id, db).await?; |
| 444 | appointment.status = AppointmentStatus::Rejected; | 470 | appointment.status = AppointmentStatus::Rejected; |
| 445 | appointment.decided_by = Some(decided_by.clone()); | 471 | appointment.decided_by = Some(decided_by.clone()); |
| 472 | + appointment.reject_reason = reason; | ||
| 446 | Self::save(appointment, db).await | 473 | Self::save(appointment, db).await |
| 447 | } | 474 | } |
| 448 | 475 | ||
| @@ -453,7 +480,12 @@ impl Appointment { | |||
| 453 | /// began is history, not a plan. The guard lives here rather than in the | 480 | /// began is history, not a plan. The guard lives here rather than in the |
| 454 | /// handler so every caller inherits it: `decline_reschedule` cancels | 481 | /// handler so every caller inherits it: `decline_reschedule` cancels |
| 455 | /// through this same door and used to slip past a handler-only check. | 482 | /// through this same door and used to slip past a handler-only check. |
| 456 | - pub async fn cancel(id: &AppointmentId, db: &Database) -> Result<Appointment, AppError> { | 483 | + pub async fn cancel( |
| 484 | + id: &AppointmentId, | ||
| 485 | + cancelled_by: &UserId, | ||
| 486 | + reason: Option<AppointmentReason>, | ||
| 487 | + db: &Database, | ||
| 488 | + ) -> Result<Appointment, AppError> { | ||
| 457 | let _guard = APPOINTMENT_LOCK.lock().await; | 489 | let _guard = APPOINTMENT_LOCK.lock().await; |
| 458 | let mut appointment = Self::read(id, db).await?.ok_or(AppError::NotFound)?; | 490 | let mut appointment = Self::read(id, db).await?.ok_or(AppError::NotFound)?; |
| 459 | if !appointment.status.is_live() { | 491 | if !appointment.status.is_live() { |
| @@ -466,6 +498,8 @@ impl Appointment { | |||
| 466 | return Err(AppError::Conflict("the appointment has already started")); | 498 | return Err(AppError::Conflict("the appointment has already started")); |
| 467 | } | 499 | } |
| 468 | appointment.status = AppointmentStatus::Cancelled; | 500 | appointment.status = AppointmentStatus::Cancelled; |
| 501 | + appointment.cancelled_by = Some(cancelled_by.clone()); | ||
| 502 | + appointment.cancel_reason = reason; | ||
| 469 | Self::save(appointment, db).await | 503 | Self::save(appointment, db).await |
| 470 | } | 504 | } |
| 471 | 505 | ||
| @@ -730,7 +764,7 @@ mod tests { | |||
| 730 | Err(AppError::Conflict(_)) | 764 | Err(AppError::Conflict(_)) |
| 731 | )); | 765 | )); |
| 732 | 766 | ||
| 733 | - Appointment::reject(first.get_id(), &teacher, &db) | 767 | + Appointment::reject(first.get_id(), &teacher, None, &db) |
| 734 | .await | 768 | .await |
| 735 | .unwrap(); | 769 | .unwrap(); |
| 736 | let second = Appointment::book(slot.get_id(), &UserId::from_key("s2"), reason(), &db) | 770 | let second = Appointment::book(slot.get_id(), &UserId::from_key("s2"), reason(), &db) |
src/web/appointments.rsmodified+53 -3
| @@ -62,6 +62,22 @@ struct BookAppointment { | |||
| 62 | reason: String, | 62 | reason: String, |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | +#[derive(Deserialize, ToSchema)] | ||
| 66 | +struct CancelRequest { | ||
| 67 | + /// Optional free-text reason. Blank or absent records no reason; over-long | ||
| 68 | + /// (past `MAX_APPOINTMENT_REASON_LEN`) answers `400`. | ||
| 69 | + #[schema(example = "Rahatsızlandım, katılamayacağım")] | ||
| 70 | + reason: Option<String>, | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +#[derive(Deserialize, ToSchema)] | ||
| 74 | +struct RejectRequest { | ||
| 75 | + /// Optional free-text reason. Blank or absent records no reason; over-long | ||
| 76 | + /// (past `MAX_APPOINTMENT_REASON_LEN`) answers `400`. | ||
| 77 | + #[schema(example = "Bu saatte müsait değilim")] | ||
| 78 | + reason: Option<String>, | ||
| 79 | +} | ||
| 80 | + | ||
| 65 | #[derive(Deserialize, ToSchema)] | 81 | #[derive(Deserialize, ToSchema)] |
| 66 | struct Reschedule { | 82 | struct Reschedule { |
| 67 | /// Unix-millisecond timestamps. Must not be in the past. | 83 | /// Unix-millisecond timestamps. Must not be in the past. |
| @@ -126,6 +142,12 @@ struct AppointmentResponse { | |||
| 126 | proposed_by: Option<PersonRef>, | 142 | proposed_by: Option<PersonRef>, |
| 127 | /// Who approved or rejected it; `null` while pending. | 143 | /// Who approved or rejected it; `null` while pending. |
| 128 | decided_by: Option<PersonRef>, | 144 | decided_by: Option<PersonRef>, |
| 145 | + /// Who called it off; `null` unless `status` is `cancelled`. | ||
| 146 | + cancelled_by: Option<PersonRef>, | ||
| 147 | + /// Optional free-text reason given when cancelling; `null` when none was. | ||
| 148 | + cancel_reason: Option<String>, | ||
| 149 | + /// Optional free-text reason given when rejecting; `null` when none was. | ||
| 150 | + reject_reason: Option<String>, | ||
| 129 | created_at: i64, | 151 | created_at: i64, |
| 130 | } | 152 | } |
| 131 | 153 | ||
| @@ -153,6 +175,15 @@ impl AppointmentResponse { | |||
| 153 | decided_by: appointment | 175 | decided_by: appointment |
| 154 | .get_decided_by() | 176 | .get_decided_by() |
| 155 | .map(|id| PersonRef::resolve(people, id)), | 177 | .map(|id| PersonRef::resolve(people, id)), |
| 178 | + cancelled_by: appointment | ||
| 179 | + .get_cancelled_by() | ||
| 180 | + .map(|id| PersonRef::resolve(people, id)), | ||
| 181 | + cancel_reason: appointment | ||
| 182 | + .get_cancel_reason() | ||
| 183 | + .map(|reason| reason.as_str().to_string()), | ||
| 184 | + reject_reason: appointment | ||
| 185 | + .get_reject_reason() | ||
| 186 | + .map(|reason| reason.as_str().to_string()), | ||
| 156 | created_at: appointment.get_created_at().as_millis(), | 187 | created_at: appointment.get_created_at().as_millis(), |
| 157 | } | 188 | } |
| 158 | } | 189 | } |
| @@ -177,6 +208,7 @@ async fn appointment_responses( | |||
| 177 | ids.extend(slot.as_ref().map(|slot| slot.get_teacher().clone())); | 208 | ids.extend(slot.as_ref().map(|slot| slot.get_teacher().clone())); |
| 178 | ids.extend(row.get_proposed_by().cloned()); | 209 | ids.extend(row.get_proposed_by().cloned()); |
| 179 | ids.extend(row.get_decided_by().cloned()); | 210 | ids.extend(row.get_decided_by().cloned()); |
| 211 | + ids.extend(row.get_cancelled_by().cloned()); | ||
| 180 | } | 212 | } |
| 181 | let people = person_map(ids, db).await?; | 213 | let people = person_map(ids, db).await?; |
| 182 | Ok(rows | 214 | Ok(rows |
| @@ -551,8 +583,10 @@ async fn approve( | |||
| 551 | tag = "appointments", | 583 | tag = "appointments", |
| 552 | security(("session_cookie" = [])), | 584 | security(("session_cookie" = [])), |
| 553 | params(("id" = String, Path, description = "Appointment id")), | 585 | params(("id" = String, Path, description = "Appointment id")), |
| 586 | + request_body(content = RejectRequest, description = "Optional rejection reason; the whole body may be omitted"), | ||
| 554 | responses( | 587 | responses( |
| 555 | (status = 200, description = "Rejected", body = AppointmentResponse), | 588 | (status = 200, description = "Rejected", body = AppointmentResponse), |
| 589 | + (status = 400, description = "Reason is over-long", body = ErrorResponse), | ||
| 556 | (status = 401, description = "Not authenticated", body = ErrorResponse), | 590 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 557 | (status = 403, description = "Not the slot's teacher (and not a manager/admin)", body = ErrorResponse), | 591 | (status = 403, description = "Not the slot's teacher (and not a manager/admin)", body = ErrorResponse), |
| 558 | (status = 404, description = "Not found", body = ErrorResponse), | 592 | (status = 404, description = "Not found", body = ErrorResponse), |
| @@ -563,10 +597,17 @@ async fn reject( | |||
| 563 | State(st): State<AppState>, | 597 | State(st): State<AppState>, |
| 564 | RequireTeacher(user): RequireTeacher, | 598 | RequireTeacher(user): RequireTeacher, |
| 565 | Path(id): Path<String>, | 599 | Path(id): Path<String>, |
| 600 | + body: Option<Json<RejectRequest>>, | ||
| 566 | ) -> Result<Json<AppointmentResponse>, AppError> { | 601 | ) -> Result<Json<AppointmentResponse>, AppError> { |
| 567 | let id = AppointmentId::from_key(&id); | 602 | let id = AppointmentId::from_key(&id); |
| 568 | for_decision(&id, &user, &st.db).await?; | 603 | for_decision(&id, &user, &st.db).await?; |
| 569 | - let appointment = Appointment::reject(&id, user.get_id(), &st.db).await?; | 604 | + // A blank or absent reason records none; a present one is validated (400 if |
| 605 | + // over-long) before it reaches the row. | ||
| 606 | + let reason = match body.and_then(|Json(req)| req.reason) { | ||
| 607 | + Some(reason) if !reason.trim().is_empty() => Some(AppointmentReason::try_new(&reason)?), | ||
| 608 | + _ => None, | ||
| 609 | + }; | ||
| 610 | + let appointment = Appointment::reject(&id, user.get_id(), reason, &st.db).await?; | ||
| 570 | one_appointment(appointment, &st.db).await | 611 | one_appointment(appointment, &st.db).await |
| 571 | } | 612 | } |
| 572 | 613 | ||
| @@ -580,8 +621,10 @@ async fn reject( | |||
| 580 | tag = "appointments", | 621 | tag = "appointments", |
| 581 | security(("session_cookie" = [])), | 622 | security(("session_cookie" = [])), |
| 582 | params(("id" = String, Path, description = "Appointment id")), | 623 | params(("id" = String, Path, description = "Appointment id")), |
| 624 | + request_body(content = CancelRequest, description = "Optional cancellation reason; the whole body may be omitted"), | ||
| 583 | responses( | 625 | responses( |
| 584 | (status = 200, description = "Cancelled", body = AppointmentResponse), | 626 | (status = 200, description = "Cancelled", body = AppointmentResponse), |
| 627 | + (status = 400, description = "Reason is over-long", body = ErrorResponse), | ||
| 585 | (status = 401, description = "Not authenticated", body = ErrorResponse), | 628 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 586 | (status = 403, description = "Neither the requester nor the slot's teacher", body = ErrorResponse), | 629 | (status = 403, description = "Neither the requester nor the slot's teacher", body = ErrorResponse), |
| 587 | (status = 404, description = "Not found", body = ErrorResponse), | 630 | (status = 404, description = "Not found", body = ErrorResponse), |
| @@ -592,6 +635,7 @@ async fn cancel( | |||
| 592 | State(st): State<AppState>, | 635 | State(st): State<AppState>, |
| 593 | CurrentUser(user): CurrentUser, | 636 | CurrentUser(user): CurrentUser, |
| 594 | Path(id): Path<String>, | 637 | Path(id): Path<String>, |
| 638 | + body: Option<Json<CancelRequest>>, | ||
| 595 | ) -> Result<Json<AppointmentResponse>, AppError> { | 639 | ) -> Result<Json<AppointmentResponse>, AppError> { |
| 596 | let id = AppointmentId::from_key(&id); | 640 | let id = AppointmentId::from_key(&id); |
| 597 | let appointment = Appointment::read(&id, &st.db) | 641 | let appointment = Appointment::read(&id, &st.db) |
| @@ -605,10 +649,16 @@ async fn cancel( | |||
| 605 | "only the requester or the slot's teacher can cancel this appointment", | 649 | "only the requester or the slot's teacher can cancel this appointment", |
| 606 | )); | 650 | )); |
| 607 | } | 651 | } |
| 652 | + // A blank or absent reason records none; a present one is validated (400 if | ||
| 653 | + // over-long) before it reaches the row. | ||
| 654 | + let reason = match body.and_then(|Json(req)| req.reason) { | ||
| 655 | + Some(reason) if !reason.trim().is_empty() => Some(AppointmentReason::try_new(&reason)?), | ||
| 656 | + _ => None, | ||
| 657 | + }; | ||
| 608 | // The started-window guard lives in `Appointment::cancel`, under the lock | 658 | // The started-window guard lives in `Appointment::cancel`, under the lock |
| 609 | // and on a fresh read — a pre-lock copy of it here would only be a staler | 659 | // and on a fresh read — a pre-lock copy of it here would only be a staler |
| 610 | // second opinion, and `decline_reschedule` would still bypass it. | 660 | // second opinion, and `decline_reschedule` would still bypass it. |
| 611 | - let appointment = Appointment::cancel(&id, &st.db).await?; | 661 | + let appointment = Appointment::cancel(&id, user.get_id(), reason, &st.db).await?; |
| 612 | one_appointment(appointment, &st.db).await | 662 | one_appointment(appointment, &st.db).await |
| 613 | } | 663 | } |
| 614 | 664 | ||
| @@ -719,7 +769,7 @@ async fn decline_reschedule( | |||
| 719 | if appointment.get_proposed_starts_at().is_none() { | 769 | if appointment.get_proposed_starts_at().is_none() { |
| 720 | return Err(AppError::Conflict("no time has been proposed")); | 770 | return Err(AppError::Conflict("no time has been proposed")); |
| 721 | } | 771 | } |
| 722 | - let appointment = Appointment::cancel(&id, &st.db).await?; | 772 | + let appointment = Appointment::cancel(&id, user.get_id(), None, &st.db).await?; |
| 723 | one_appointment(appointment, &st.db).await | 773 | one_appointment(appointment, &st.db).await |
| 724 | } | 774 | } |
| 725 | 775 | ||
tests/integration.rsmodified+63 -1
| @@ -15505,15 +15505,44 @@ async fn appointment_book_and_approve_holds_the_slot() { | |||
| 15505 | async fn rejecting_a_booking_frees_the_slot() { | 15505 | async fn rejecting_a_booking_frees_the_slot() { |
| 15506 | let (app, db) = app_and_db().await; | 15506 | let (app, db) = app_and_db().await; |
| 15507 | let ali = login_as(&app, &db, "ali", "teacher").await; | 15507 | let ali = login_as(&app, &db, "ali", "teacher").await; |
| 15508 | + let ali_id = me_id(&app, &ali).await; | ||
| 15508 | let veli = login(&app, "veli").await; | 15509 | let veli = login(&app, "veli").await; |
| 15509 | let ayse = login(&app, "ayse").await; | 15510 | let ayse = login(&app, "ayse").await; |
| 15510 | let now = Timestamp::now().as_millis(); | 15511 | let now = Timestamp::now().as_millis(); |
| 15511 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | 15512 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; |
| 15512 | 15513 | ||
| 15514 | + // A bodyless reject records the decider and no reason. | ||
| 15513 | let booking = book_ok(&app, &veli, &slot).await; | 15515 | let booking = book_ok(&app, &veli, &slot).await; |
| 15514 | let res = decide(&app, &ali, &booking, "reject").await; | 15516 | let res = decide(&app, &ali, &booking, "reject").await; |
| 15515 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); | 15517 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 15516 | assert_eq!(res.body["status"], "rejected"); | 15518 | assert_eq!(res.body["status"], "rejected"); |
| 15519 | + assert_eq!(res.body["decided_by"]["id"], ali_id.as_str()); | ||
| 15520 | + assert!(res.body["reject_reason"].is_null(), "no reason was given"); | ||
| 15521 | + | ||
| 15522 | + // A reject carrying a reason records both the decider and the reason, and | ||
| 15523 | + // an over-long reason is refused before the row changes. | ||
| 15524 | + let booking = book_ok(&app, &ayse, &slot).await; | ||
| 15525 | + let res = send( | ||
| 15526 | + &app, | ||
| 15527 | + "PATCH", | ||
| 15528 | + &format!("/appointments/{booking}/reject"), | ||
| 15529 | + Some(&ali), | ||
| 15530 | + Some(json!({ "reason": "x".repeat(2000) })), | ||
| 15531 | + ) | ||
| 15532 | + .await; | ||
| 15533 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 15534 | + let res = send( | ||
| 15535 | + &app, | ||
| 15536 | + "PATCH", | ||
| 15537 | + &format!("/appointments/{booking}/reject"), | ||
| 15538 | + Some(&ali), | ||
| 15539 | + Some(json!({ "reason": "Bu saatte müsait değilim" })), | ||
| 15540 | + ) | ||
| 15541 | + .await; | ||
| 15542 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 15543 | + assert_eq!(res.body["status"], "rejected"); | ||
| 15544 | + assert_eq!(res.body["decided_by"]["id"], ali_id.as_str()); | ||
| 15545 | + assert_eq!(res.body["reject_reason"], "Bu saatte müsait değilim"); | ||
| 15517 | 15546 | ||
| 15518 | // Occupancy counts live rows only, so the next person may take it. | 15547 | // Occupancy counts live rows only, so the next person may take it. |
| 15519 | let res = book_slot(&app, &ayse, &slot).await; | 15548 | let res = book_slot(&app, &ayse, &slot).await; |
| @@ -15682,6 +15711,7 @@ async fn declining_a_reschedule_cancels_the_booking() { | |||
| 15682 | let (app, db) = app_and_db().await; | 15711 | let (app, db) = app_and_db().await; |
| 15683 | let ali = login_as(&app, &db, "ali", "teacher").await; | 15712 | let ali = login_as(&app, &db, "ali", "teacher").await; |
| 15684 | let veli = login(&app, "veli").await; | 15713 | let veli = login(&app, "veli").await; |
| 15714 | + let veli_id = me_id(&app, &veli).await; | ||
| 15685 | let ayse = login(&app, "ayse").await; | 15715 | let ayse = login(&app, "ayse").await; |
| 15686 | let now = Timestamp::now().as_millis(); | 15716 | let now = Timestamp::now().as_millis(); |
| 15687 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | 15717 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; |
| @@ -15700,6 +15730,9 @@ async fn declining_a_reschedule_cancels_the_booking() { | |||
| 15700 | let res = decide(&app, &veli, &booking, "reschedule/decline").await; | 15730 | let res = decide(&app, &veli, &booking, "reschedule/decline").await; |
| 15701 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); | 15731 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 15702 | assert_eq!(res.body["status"], "cancelled"); | 15732 | assert_eq!(res.body["status"], "cancelled"); |
| 15733 | + // The decliner is recorded as the canceller, with no reason. | ||
| 15734 | + assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str()); | ||
| 15735 | + assert!(res.body["cancel_reason"].is_null()); | ||
| 15703 | // The refused proposal stays readable on the cancelled row. | 15736 | // The refused proposal stays readable on the cancelled row. |
| 15704 | assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); | 15737 | assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); |
| 15705 | 15738 | ||
| @@ -15792,10 +15825,12 @@ async fn either_side_cancels_until_the_window_opens() { | |||
| 15792 | let (app, db) = app_and_db().await; | 15825 | let (app, db) = app_and_db().await; |
| 15793 | let ali = login_as(&app, &db, "ali", "teacher").await; | 15826 | let ali = login_as(&app, &db, "ali", "teacher").await; |
| 15794 | let veli = login(&app, "veli").await; | 15827 | let veli = login(&app, "veli").await; |
| 15828 | + let veli_id = me_id(&app, &veli).await; | ||
| 15795 | let ayse = login(&app, "ayse").await; | 15829 | let ayse = login(&app, "ayse").await; |
| 15796 | let now = Timestamp::now().as_millis(); | 15830 | let now = Timestamp::now().as_millis(); |
| 15797 | 15831 | ||
| 15798 | - // The requester's own call, on an approved meeting. | 15832 | + // The requester's own call, on an approved meeting. A bodyless cancel |
| 15833 | + // records the actor and no reason. | ||
| 15799 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | 15834 | let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; |
| 15800 | let booking = book_ok(&app, &veli, &slot).await; | 15835 | let booking = book_ok(&app, &veli, &slot).await; |
| 15801 | assert_eq!( | 15836 | assert_eq!( |
| @@ -15805,6 +15840,33 @@ async fn either_side_cancels_until_the_window_opens() { | |||
| 15805 | let res = decide(&app, &veli, &booking, "cancel").await; | 15840 | let res = decide(&app, &veli, &booking, "cancel").await; |
| 15806 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); | 15841 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 15807 | assert_eq!(res.body["status"], "cancelled"); | 15842 | assert_eq!(res.body["status"], "cancelled"); |
| 15843 | + assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str()); | ||
| 15844 | + assert!(res.body["cancel_reason"].is_null(), "no reason was given"); | ||
| 15845 | + | ||
| 15846 | + // A cancel carrying a reason records both the actor and the reason, and an | ||
| 15847 | + // over-long reason is refused before the row changes. | ||
| 15848 | + let booking = book_ok(&app, &veli, &slot).await; | ||
| 15849 | + let res = send( | ||
| 15850 | + &app, | ||
| 15851 | + "PATCH", | ||
| 15852 | + &format!("/appointments/{booking}/cancel"), | ||
| 15853 | + Some(&veli), | ||
| 15854 | + Some(json!({ "reason": "x".repeat(2000) })), | ||
| 15855 | + ) | ||
| 15856 | + .await; | ||
| 15857 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 15858 | + let res = send( | ||
| 15859 | + &app, | ||
| 15860 | + "PATCH", | ||
| 15861 | + &format!("/appointments/{booking}/cancel"), | ||
| 15862 | + Some(&veli), | ||
| 15863 | + Some(json!({ "reason": "Rahatsızlandım" })), | ||
| 15864 | + ) | ||
| 15865 | + .await; | ||
| 15866 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 15867 | + assert_eq!(res.body["status"], "cancelled"); | ||
| 15868 | + assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str()); | ||
| 15869 | + assert_eq!(res.body["cancel_reason"], "Rahatsızlandım"); | ||
| 15808 | 15870 | ||
| 15809 | // The slot's teacher may too. | 15871 | // The slot's teacher may too. |
| 15810 | let booking = book_ok(&app, &ayse, &slot).await; | 15872 | let booking = book_ok(&app, &ayse, &slot).await; |