From 779e1bbfb9b103ff03e2aa18c63791f15f11eec6 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 25 Oct 2024 18:55:08 +0200 Subject: [PATCH] backend adaptations due to cox change role --- src/model/boat.rs | 6 +++--- src/model/boatdamage.rs | 3 +-- src/model/notification.rs | 26 ++++++++++++++++++++++++++ src/model/tripdetails.rs | 2 +- src/model/user.rs | 26 ++++++++++++++------------ src/tera/mod.rs | 2 -- src/tera/planned.rs | 2 +- 7 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index e25af7a..4776cf9 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -119,7 +119,7 @@ impl Boat { return true; } - user.has_role(db, "cox").await + user.allowed_to_steer(db).await } pub async fn shipmaster_allowed_tx( @@ -135,7 +135,7 @@ impl Boat { return true; } - user.has_role_tx(db, "cox").await + user.allowed_to_steer_tx(db).await } pub async fn is_locked(&self, db: &SqlitePool) -> bool { @@ -260,7 +260,7 @@ ORDER BY if user.has_role(db, "admin").await { return Self::all(db).await; } - let mut boats = if user.has_role(db, "cox").await { + let mut boats = if user.allowed_to_steer(db).await { sqlx::query_as!( Boat, " diff --git a/src/model/boatdamage.rs b/src/model/boatdamage.rs index e7dc017..bcdb377 100644 --- a/src/model/boatdamage.rs +++ b/src/model/boatdamage.rs @@ -136,8 +136,7 @@ ORDER BY created_at DESC .map_err(|e| e.to_string())?; if !was_unusable_before && boat.is_locked(db).await { - let cox = Role::find_by_name(db, "cox").await.unwrap(); - Notification::create_for_role(db, &cox, &format!("Liebe Steuerberechtigte, bitte beachten, dass {} bis auf weiteres aufgrund von Reparaturarbeiten gesperrt ist.", boat.name), "Boot gesperrt", None, None).await; + Notification::create_for_steering_people(db, &format!("Liebe Steuerberechtigte, bitte beachten, dass {} bis auf weiteres aufgrund von Reparaturarbeiten gesperrt ist.", boat.name), "Boot gesperrt", None, None).await; } let technicals = diff --git a/src/model/notification.rs b/src/model/notification.rs index 0b193ae..ae73ad3 100644 --- a/src/model/notification.rs +++ b/src/model/notification.rs @@ -89,6 +89,32 @@ impl Notification { tx.commit().await.unwrap(); } + pub async fn create_for_steering_people_tx( + db: &mut Transaction<'_, Sqlite>, + message: &str, + category: &str, + link: Option<&str>, + action_after_reading: Option<&str>, + ) { + let cox = Role::find_by_name_tx(db, "cox").await.unwrap(); + Self::create_for_role_tx(db, &cox, message, category, link, action_after_reading).await; + let bootsf = Role::find_by_name_tx(db, "Bootsführer").await.unwrap(); + Self::create_for_role_tx(db, &bootsf, message, category, link, action_after_reading).await; + } + + pub async fn create_for_steering_people( + db: &SqlitePool, + message: &str, + category: &str, + link: Option<&str>, + action_after_reading: Option<&str>, + ) { + let cox = Role::find_by_name(db, "cox").await.unwrap(); + Self::create_for_role(db, &cox, message, category, link, action_after_reading).await; + let bootsf = Role::find_by_name(db, "Bootsführer").await.unwrap(); + Self::create_for_role(db, &bootsf, message, category, link, action_after_reading).await; + } + pub async fn for_user(db: &SqlitePool, user: &User) -> Vec { let rows = sqlx::query!( " diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index 1dd59a5..df5204e 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -146,7 +146,7 @@ WHERE day = ? AND planned_starting_time = ? // User is a guest, no need to bother. continue; }; - if !user.has_role(db, "cox").await { + if !user.allowed_to_steer(db).await { // User is no cox, no need to bother continue; } diff --git a/src/model/user.rs b/src/model/user.rs index a50287d..abe3487 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -57,8 +57,8 @@ pub struct UserWithDetails { impl UserWithDetails { pub async fn from_user(user: User, db: &SqlitePool) -> Self { - let allowed_to_steer = - user.has_role(db, "cox").await || user.has_role(db, "Bootsführer").await; + let allowed_to_steer = user.allowed_to_steer(db).await; + Self { on_water: user.on_water(db).await, roles: user.roles(db).await, @@ -140,6 +140,14 @@ impl Fee { } impl User { + pub async fn allowed_to_steer(&self, db: &SqlitePool) -> bool { + self.has_role(db, "cox").await || self.has_role(db, "Bootsführer").await + } + + pub async fn allowed_to_steer_tx(&self, db: &mut Transaction<'_, Sqlite>) -> bool { + self.has_role_tx(db, "cox").await || self.has_role_tx(db, "Bootsführer").await + } + pub async fn send_welcome_email(&self, db: &SqlitePool, smtp_pw: &str) -> Result<(), String> { let Some(mail) = &self.mail else { return Err(format!( @@ -239,10 +247,8 @@ ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100), ).await?; // 2. Notify all coxes - let coxes = Role::find_by_name(db, "cox").await.unwrap(); - Notification::create_for_role( + Notification::create_for_steering_people( db, - &coxes, &format!( "Liebe Steuerberechtigte, {} hat nun ein Scheckbuch. Wie immer, freuen wir uns wenn du uns beim A+F Rudern unterstützt oder selber Ausfahrten ausschreibst. Bitte beachte, dass Scheckbuch-Personen nur Ausfahrten sehen, bei denen 'Scheckbuch-Anmeldungen erlauben' ausgewählt wurde.", self.name @@ -319,10 +325,8 @@ ASKÖ Ruderverein Donau Linz", self.name), ).await?; // 2. Notify all coxes - let coxes = Role::find_by_name(db, "cox").await.unwrap(); - Notification::create_for_role( + Notification::create_for_steering_people( db, - &coxes, &format!( "Liebe Steuerberechtigte, seit {} gibt es ein neues Mitglied: {}", self.member_since_date.clone().unwrap(), @@ -979,7 +983,7 @@ ORDER BY last_access DESC } pub(crate) async fn amount_days_to_show(&self, db: &SqlitePool) -> i64 { - if self.has_role(db, "cox").await { + if self.allowed_to_steer(db).await { let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); //Ok, //december //has 31 @@ -1031,10 +1035,8 @@ ORDER BY last_access DESC if let Some(mail) = &self.mail { let _ = self.send_end_mail_scheckbuch(db, mail, smtp_pw).await; } - let coxes = Role::find_by_name_tx(db, "cox").await.unwrap(); - Notification::create_for_role_tx( + Notification::create_for_steering_people_tx( db, - &coxes, &format!( "Liebe Steuerberechtigte, {} hat alle Ausfahrten des Scheckbuchs absolviert. Hoffentlich können wir uns bald über ein neues Mitglied freuen :-)", self.name diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 314483b..363b6ac 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -93,8 +93,6 @@ async fn steering(db: &State, user: User, flash: Option