72 lines
2.1 KiB
Rust
72 lines
2.1 KiB
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use chrono::Days;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::{
|
|
endpoints::{availability::NewOrEditAvailabilityTemplate, IdPath},
|
|
utils::{ApplicationError, TemplateResponse},
|
|
};
|
|
use brass_db::models::{find_free_date_time_slots, Availability, Role, User};
|
|
|
|
#[actix_web::get("/availability/edit/{id}")]
|
|
pub async fn get(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let Some(availability) = Availability::read(pool.get_ref(), path.id).await? else {
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
};
|
|
|
|
if user.role != Role::Admin && user.role != Role::AreaManager && availability.user_id != user.id
|
|
{
|
|
return Err(ApplicationError::Unauthorized);
|
|
}
|
|
|
|
let availabilities = Availability::read_all_by_user_and_date(
|
|
pool.get_ref(),
|
|
availability.user_id,
|
|
&availability.start.date(),
|
|
)
|
|
.await?;
|
|
|
|
let slot_suggestions = find_free_date_time_slots(&availabilities)
|
|
.into_iter()
|
|
.filter(|(a, b)| *b == availability.start || *a == availability.end)
|
|
.collect();
|
|
|
|
let other_user = if availability.user_id != user.id {
|
|
Some(availability.user_id)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let other_users = if availability.user_id != user.id {
|
|
vec![User::read_by_id(pool.get_ref(), availability.user_id)
|
|
.await?
|
|
.unwrap()]
|
|
} else {
|
|
Vec::default()
|
|
};
|
|
|
|
let template = NewOrEditAvailabilityTemplate {
|
|
user: user.into_inner(),
|
|
date: availability.start.date(),
|
|
enddate: Some(availability.end.date()),
|
|
id: Some(path.id),
|
|
start: Some(availability.start.time()),
|
|
end: Some(availability.end.time()),
|
|
comment: availability.comment.as_deref(),
|
|
slot_suggestions,
|
|
datetomorrow: availability
|
|
.start
|
|
.date()
|
|
.checked_add_days(Days::new(1))
|
|
.unwrap(),
|
|
other_user,
|
|
other_users,
|
|
};
|
|
|
|
Ok(template.to_response()?)
|
|
}
|