Add an accommodations reservations module #188

Merged
pierre merged 81 commits from accomodation_module into master 2024-06-22 21:30:26 +00:00
4 changed files with 29 additions and 2 deletions
Showing only changes of commit ab6aa63d3e - Show all commits

View File

@ -145,3 +145,6 @@ pub const THUMB_HEIGHT: u32 = 350;
/// Accommodations reservations calendars tokens len /// Accommodations reservations calendars tokens len
pub const ACCOMMODATIONS_RESERVATIONS_CALENDARS_TOKENS_LEN: usize = 50; pub const ACCOMMODATIONS_RESERVATIONS_CALENDARS_TOKENS_LEN: usize = 50;
/// Minimum interval before calendar used time update
pub const TIME_USED_UPDATE_MIN_INTERVAL: Duration = Duration::from_secs(60);

View File

@ -12,6 +12,7 @@ use crate::services::{
accommodations_list_service, accommodations_reservations_calendars_service, accommodations_list_service, accommodations_reservations_calendars_service,
accommodations_reservations_service, families_service, accommodations_reservations_service, families_service,
}; };
use crate::utils::time_utils::time;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct CreateCalendarQuery { pub struct CreateCalendarQuery {
@ -95,7 +96,7 @@ pub struct AnonymousAccessURL {
/// Get the content of the calendar /// Get the content of the calendar
pub async fn anonymous_access(req: web::Path<AnonymousAccessURL>) -> HttpResult { pub async fn anonymous_access(req: web::Path<AnonymousAccessURL>) -> HttpResult {
let calendar = let mut calendar =
match accommodations_reservations_calendars_service::get_by_token(&req.token).await { match accommodations_reservations_calendars_service::get_by_token(&req.token).await {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
@ -151,7 +152,10 @@ pub async fn anonymous_access(req: web::Path<AnonymousAccessURL>) -> HttpResult
cal.events.push(event); cal.events.push(event);
} }
// TODO : update last used of calendar, if required if calendar.should_update_last_used() {
calendar.time_used = time() as i64;
accommodations_reservations_calendars_service::update(&calendar).await?;
}
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/calendar") .content_type("text/calendar")

View File

@ -1,9 +1,11 @@
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use crate::constants;
use crate::schema::{ use crate::schema::{
accommodations_list, accommodations_reservations, accommodations_reservations_cals_urls, accommodations_list, accommodations_reservations, accommodations_reservations_cals_urls,
couples, families, members, memberships, photos, users, couples, families, members, memberships, photos, users,
}; };
use crate::utils::crypt_utils::sha256; use crate::utils::crypt_utils::sha256;
use crate::utils::time_utils::time;
use diesel::prelude::*; use diesel::prelude::*;
/// User ID holder /// User ID holder
@ -575,6 +577,10 @@ impl AccommodationReservationCalendar {
pub fn user_id(&self) -> UserID { pub fn user_id(&self) -> UserID {
UserID(self.user_id) UserID(self.user_id)
} }
pub fn should_update_last_used(&self) -> bool {
(self.time_used + constants::TIME_USED_UPDATE_MIN_INTERVAL.as_secs() as i64) < time() as i64
}
} }
#[derive(Insertable)] #[derive(Insertable)]

View File

@ -34,6 +34,20 @@ pub async fn create(
}) })
} }
/// Update the information of a reservations calendar
pub async fn update(cal: &AccommodationReservationCalendar) -> anyhow::Result<()> {
db_connection::execute(|conn| {
diesel::update(
accommodations_reservations_cals_urls::dsl::accommodations_reservations_cals_urls
.filter(accommodations_reservations_cals_urls::dsl::id.eq(cal.id().0)),
)
.set((accommodations_reservations_cals_urls::dsl::time_used.eq(cal.time_used),))
.execute(conn)
})?;
Ok(())
}
/// Get all the calendars of a user /// Get all the calendars of a user
pub async fn get_all_of_user( pub async fn get_all_of_user(
user: UserID, user: UserID,