backend adaptations due to cox change role
All checks were successful
CI/CD Pipeline / test (push) Successful in 10m52s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
philipp 2024-10-25 18:55:08 +02:00
parent c87baaed07
commit 779e1bbfb9
7 changed files with 46 additions and 21 deletions

View File

@ -119,7 +119,7 @@ impl Boat {
return true; return true;
} }
user.has_role(db, "cox").await user.allowed_to_steer(db).await
} }
pub async fn shipmaster_allowed_tx( pub async fn shipmaster_allowed_tx(
@ -135,7 +135,7 @@ impl Boat {
return true; 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 { pub async fn is_locked(&self, db: &SqlitePool) -> bool {
@ -260,7 +260,7 @@ ORDER BY
if user.has_role(db, "admin").await { if user.has_role(db, "admin").await {
return Self::all(db).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!( sqlx::query_as!(
Boat, Boat,
" "

View File

@ -136,8 +136,7 @@ ORDER BY created_at DESC
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
if !was_unusable_before && boat.is_locked(db).await { if !was_unusable_before && boat.is_locked(db).await {
let cox = Role::find_by_name(db, "cox").await.unwrap(); 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;
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;
} }
let technicals = let technicals =

View File

@ -89,6 +89,32 @@ impl Notification {
tx.commit().await.unwrap(); 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<Self> { pub async fn for_user(db: &SqlitePool, user: &User) -> Vec<Self> {
let rows = sqlx::query!( let rows = sqlx::query!(
" "

View File

@ -146,7 +146,7 @@ WHERE day = ? AND planned_starting_time = ?
// User is a guest, no need to bother. // User is a guest, no need to bother.
continue; continue;
}; };
if !user.has_role(db, "cox").await { if !user.allowed_to_steer(db).await {
// User is no cox, no need to bother // User is no cox, no need to bother
continue; continue;
} }

View File

@ -57,8 +57,8 @@ pub struct UserWithDetails {
impl UserWithDetails { impl UserWithDetails {
pub async fn from_user(user: User, db: &SqlitePool) -> Self { pub async fn from_user(user: User, db: &SqlitePool) -> Self {
let allowed_to_steer = let allowed_to_steer = user.allowed_to_steer(db).await;
user.has_role(db, "cox").await || user.has_role(db, "Bootsführer").await;
Self { Self {
on_water: user.on_water(db).await, on_water: user.on_water(db).await,
roles: user.roles(db).await, roles: user.roles(db).await,
@ -140,6 +140,14 @@ impl Fee {
} }
impl User { 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> { pub async fn send_welcome_email(&self, db: &SqlitePool, smtp_pw: &str) -> Result<(), String> {
let Some(mail) = &self.mail else { let Some(mail) = &self.mail else {
return Err(format!( return Err(format!(
@ -239,10 +247,8 @@ ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100),
).await?; ).await?;
// 2. Notify all coxes // 2. Notify all coxes
let coxes = Role::find_by_name(db, "cox").await.unwrap(); Notification::create_for_steering_people(
Notification::create_for_role(
db, db,
&coxes,
&format!( &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.", "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 self.name
@ -319,10 +325,8 @@ ASKÖ Ruderverein Donau Linz", self.name),
).await?; ).await?;
// 2. Notify all coxes // 2. Notify all coxes
let coxes = Role::find_by_name(db, "cox").await.unwrap(); Notification::create_for_steering_people(
Notification::create_for_role(
db, db,
&coxes,
&format!( &format!(
"Liebe Steuerberechtigte, seit {} gibt es ein neues Mitglied: {}", "Liebe Steuerberechtigte, seit {} gibt es ein neues Mitglied: {}",
self.member_since_date.clone().unwrap(), 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 { 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, let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); //Ok,
//december //december
//has 31 //has 31
@ -1031,10 +1035,8 @@ ORDER BY last_access DESC
if let Some(mail) = &self.mail { if let Some(mail) = &self.mail {
let _ = self.send_end_mail_scheckbuch(db, mail, smtp_pw).await; 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_steering_people_tx(
Notification::create_for_role_tx(
db, db,
&coxes,
&format!( &format!(
"Liebe Steuerberechtigte, {} hat alle Ausfahrten des Scheckbuchs absolviert. Hoffentlich können wir uns bald über ein neues Mitglied freuen :-)", "Liebe Steuerberechtigte, {} hat alle Ausfahrten des Scheckbuchs absolviert. Hoffentlich können wir uns bald über ein neues Mitglied freuen :-)",
self.name self.name

View File

@ -93,8 +93,6 @@ async fn steering(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage
User::all_with_role(db, &Role::find_by_name(db, "Bootsführer").await.unwrap()).await; User::all_with_role(db, &Role::find_by_name(db, "Bootsführer").await.unwrap()).await;
let mut coxes = User::all_with_role(db, &Role::find_by_name(db, "cox").await.unwrap()).await; let mut coxes = User::all_with_role(db, &Role::find_by_name(db, "cox").await.unwrap()).await;
coxes.retain(|user| !bootskundige.contains(user)); // Remove bootskundige from coxes list
coxes.retain(|user| user.name != "Externe Steuerperson"); coxes.retain(|user| user.name != "Externe Steuerperson");
context.insert("coxes", &coxes); context.insert("coxes", &coxes);

View File

@ -29,7 +29,7 @@ async fn index(
let mut context = Context::new(); let mut context = Context::new();
if user.has_role(db, "cox").await || user.has_role(db, "manage_events").await { if user.allowed_to_steer(db).await || user.has_role(db, "manage_events").await {
let triptypes = TripType::all(db).await; let triptypes = TripType::all(db).await;
context.insert("trip_types", &triptypes); context.insert("trip_types", &triptypes);
} }