Add an accommodations reservations module #188
@ -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);
|
||||
|
@ -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<AnonymousAccessURL>) -> 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<AnonymousAccessURL>) -> 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")
|
||||
|
@ -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)]
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user