From ab6aa63d3e84d7e346b4caaa0a3f87b07f58bda9 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 22 Jun 2024 23:20:03 +0200 Subject: [PATCH] Update calendar last access --- geneit_backend/src/constants.rs | 3 +++ ...mmodations_reservations_calendars_controller.rs | 8 ++++++-- geneit_backend/src/models.rs | 6 ++++++ ...ccommodations_reservations_calendars_service.rs | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/geneit_backend/src/constants.rs b/geneit_backend/src/constants.rs index 69254f1..af11c10 100644 --- a/geneit_backend/src/constants.rs +++ b/geneit_backend/src/constants.rs @@ -145,3 +145,6 @@ pub const THUMB_HEIGHT: u32 = 350; /// Accommodations reservations calendars tokens len 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); diff --git a/geneit_backend/src/controllers/accommodations_reservations_calendars_controller.rs b/geneit_backend/src/controllers/accommodations_reservations_calendars_controller.rs index 995f278..3c36396 100644 --- a/geneit_backend/src/controllers/accommodations_reservations_calendars_controller.rs +++ b/geneit_backend/src/controllers/accommodations_reservations_calendars_controller.rs @@ -12,6 +12,7 @@ use crate::services::{ accommodations_list_service, accommodations_reservations_calendars_service, accommodations_reservations_service, families_service, }; +use crate::utils::time_utils::time; #[derive(serde::Deserialize)] pub struct CreateCalendarQuery { @@ -95,7 +96,7 @@ pub struct AnonymousAccessURL { /// Get the content of the calendar pub async fn anonymous_access(req: web::Path) -> HttpResult { - let calendar = + let mut calendar = match accommodations_reservations_calendars_service::get_by_token(&req.token).await { Ok(c) => c, Err(e) => { @@ -151,7 +152,10 @@ pub async fn anonymous_access(req: web::Path) -> HttpResult 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() .content_type("text/calendar") diff --git a/geneit_backend/src/models.rs b/geneit_backend/src/models.rs index 65389d2..ab8b328 100644 --- a/geneit_backend/src/models.rs +++ b/geneit_backend/src/models.rs @@ -1,9 +1,11 @@ use crate::app_config::AppConfig; +use crate::constants; use crate::schema::{ accommodations_list, accommodations_reservations, accommodations_reservations_cals_urls, couples, families, members, memberships, photos, users, }; use crate::utils::crypt_utils::sha256; +use crate::utils::time_utils::time; use diesel::prelude::*; /// User ID holder @@ -575,6 +577,10 @@ impl AccommodationReservationCalendar { pub fn user_id(&self) -> UserID { 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)] diff --git a/geneit_backend/src/services/accommodations_reservations_calendars_service.rs b/geneit_backend/src/services/accommodations_reservations_calendars_service.rs index ff07470..c38d17a 100644 --- a/geneit_backend/src/services/accommodations_reservations_calendars_service.rs +++ b/geneit_backend/src/services/accommodations_reservations_calendars_service.rs @@ -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 pub async fn get_all_of_user( user: UserID,