show notification to vorstand if boat entry with 'externes boot' or on multiple days is entered
This commit is contained in:
parent
8315a27ea8
commit
219b80377d
@ -5,7 +5,9 @@ use rocket::FromForm;
|
||||
use serde::Serialize;
|
||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||
|
||||
use super::{boat::Boat, log::Log, notification::Notification, rower::Rower, user::User};
|
||||
use super::{
|
||||
boat::Boat, log::Log, notification::Notification, role::Role, rower::Rower, user::User,
|
||||
};
|
||||
|
||||
#[derive(FromRow, Serialize, Clone, Debug)]
|
||||
pub struct Logbook {
|
||||
@ -560,6 +562,31 @@ ORDER BY departure DESC
|
||||
.execute(db.deref_mut())
|
||||
.await.unwrap(); //TODO: fixme
|
||||
|
||||
let duration = arr - dep;
|
||||
if duration.num_days() > 0 {
|
||||
let vorstand = Role::find_by_name_tx(db, "Vorstand").await.unwrap();
|
||||
|
||||
Notification::create_for_role_tx(
|
||||
db,
|
||||
&vorstand,
|
||||
&format!("'{}' hat eine mehrtägige Ausfahrt vom {} bis {} eingetragen ({} km; Ziel: {}; Anmerkungen: {}). Falls das nicht stimmen sollte, bitte nachhaken.",user.name,log.departure, log.arrival, log.distance_in_km, log.destination, log.comments.clone().unwrap_or("".into())),
|
||||
"Mehrtägige Ausfahrt eingetragen",
|
||||
None,
|
||||
).await;
|
||||
}
|
||||
|
||||
if boat.name == "Externes Boot" {
|
||||
let vorstand = Role::find_by_name_tx(db, "Vorstand").await.unwrap();
|
||||
|
||||
Notification::create_for_role_tx(
|
||||
db,
|
||||
&vorstand,
|
||||
&format!("'{}' hat eine Ausfahrt mit *Externem Boot* am {} eingetragen ({} km; Ziel: {}; Anmerkungen: {}). Falls das nicht stimmen sollte, bitte nachhaken.",user.name,log.departure,log.distance_in_km, log.destination, log.comments.unwrap_or("".into())),
|
||||
"Ausfahrt mit externem Boot eingetragen",
|
||||
None,
|
||||
).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||
|
||||
use super::user::User;
|
||||
use super::{role::Role, user::User};
|
||||
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||
pub struct Notification {
|
||||
@ -55,6 +55,20 @@ impl Notification {
|
||||
tx.commit().await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn create_for_role_tx(
|
||||
db: &mut Transaction<'_, Sqlite>,
|
||||
role: &Role,
|
||||
message: &str,
|
||||
category: &str,
|
||||
link: Option<&str>,
|
||||
) {
|
||||
let users = User::all_with_role_tx(db, role).await;
|
||||
|
||||
for user in users {
|
||||
Self::create_with_tx(db, &user, message, category, link).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn for_user(db: &SqlitePool, user: &User) -> Vec<Self> {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
|
@ -1,5 +1,7 @@
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use serde::Serialize;
|
||||
use sqlx::{FromRow, SqlitePool};
|
||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||
|
||||
#[derive(FromRow, Serialize, Clone)]
|
||||
pub struct Role {
|
||||
@ -45,6 +47,21 @@ WHERE name like ?
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn find_by_name_tx(db: &mut Transaction<'_, Sqlite>, name: &str) -> Option<Self> {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"
|
||||
SELECT id, name
|
||||
FROM role
|
||||
WHERE name like ?
|
||||
",
|
||||
name
|
||||
)
|
||||
.fetch_one(db.deref_mut())
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn names_from_role(&self, db: &SqlitePool) -> Vec<String> {
|
||||
let query = format!(
|
||||
"SELECT u.name
|
||||
|
@ -370,6 +370,13 @@ ORDER BY last_access DESC
|
||||
}
|
||||
|
||||
pub async fn all_with_role(db: &SqlitePool, role: &Role) -> Vec<Self> {
|
||||
let mut tx = db.begin().await.unwrap();
|
||||
let ret = Self::all_with_role_tx(&mut tx, role).await;
|
||||
tx.commit().await.unwrap();
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn all_with_role_tx(db: &mut Transaction<'_, Sqlite>, role: &Role) -> Vec<Self> {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"
|
||||
@ -380,7 +387,7 @@ WHERE ur.role_id = ? AND deleted = 0
|
||||
ORDER BY name;
|
||||
", role.id
|
||||
)
|
||||
.fetch_all(db)
|
||||
.fetch_all(db.deref_mut())
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user