diff --git a/geneit_backend/src/controllers/accommodations_reservations_controller.rs b/geneit_backend/src/controllers/accommodations_reservations_controller.rs index 67469fb..afee380 100644 --- a/geneit_backend/src/controllers/accommodations_reservations_controller.rs +++ b/geneit_backend/src/controllers/accommodations_reservations_controller.rs @@ -146,3 +146,51 @@ pub async fn delete(m: FamilyAndAccommodationReservationInPath) -> HttpResult { Ok(HttpResponse::Accepted().finish()) } + +#[derive(serde::Deserialize)] +pub struct ValidateQuery { + validate: bool, +} + +/// Validate or reject a reservation +pub async fn validate_or_reject( + m: FamilyAndAccommodationReservationInPath, + q: web::Json, +) -> HttpResult { + if !m.membership().is_admin { + return Ok(HttpResponse::BadRequest().json("Only an admin can validate a reservation!")); + } + + if m.validated == Some(q.validate) { + return Ok( + HttpResponse::AlreadyReported().json("This reservation has already been processed!") + ); + } + + // In case of re-validation, check that the time is still available + if m.validated == Some(false) && q.validate { + let potential_conflicts = + accommodations_reservations_service::get_reservations_for_time_interval( + m.accommodation_id(), + m.reservation_start as usize, + m.reservation_end as usize, + ) + .await?; + + if potential_conflicts + .iter() + .any(|a| a.validated != Some(false)) + { + return Ok(HttpResponse::Conflict().json( + "This cannot be accepted as it would create a conflict with another reservation!", + )); + } + } + + // Update reservation validation status + let mut reservation = m.to_reservation(); + reservation.validated = Some(q.validate); + accommodations_reservations_service::update(&mut reservation).await?; + + Ok(HttpResponse::Accepted().finish()) +} diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index 5799638..0de9626 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -252,7 +252,10 @@ async fn main() -> std::io::Result<()> { "/family/{id}/accommodations/reservation/{reservation_id}", web::delete().to(accommodations_reservations_controller::delete), ) - // TODO : validate or reject + .route( + "/family/{id}/accommodations/reservation/{reservation_id}/validate", + web::post().to(accommodations_reservations_controller::validate_or_reject), + ) // [ACCOMMODATIONS] Calendars controller // TODO : create // TODO : list