Merge pull request 'allow for sending notifications to single users' (#516) from allow-notifications-for-single-user into main #517

Merged
philipp merged 3 commits from remove-users-from-canceled-trips-after-reading-notification into staging 2024-05-21 20:01:19 +02:00
12 changed files with 62 additions and 20 deletions
Showing only changes of commit b4967b54e9 - Show all commits

1
Cargo.lock generated
View File

@ -2272,6 +2272,7 @@ dependencies = [
"lettre", "lettre",
"log", "log",
"openssl", "openssl",
"regex",
"rocket", "rocket",
"rocket_dyn_templates", "rocket_dyn_templates",
"serde", "serde",

View File

@ -27,6 +27,7 @@ csv = "1.3"
itertools = "0.12" itertools = "0.12"
job_scheduler_ng = "2.0" job_scheduler_ng = "2.0"
ureq = { version = "2.9", features = ["json"] } ureq = { version = "2.9", features = ["json"] }
regex = "1.10"
[target.'cfg(not(windows))'.dependencies] [target.'cfg(not(windows))'.dependencies]
openssl = { version = "0.10", features = [ "vendored" ] } openssl = { version = "0.10", features = [ "vendored" ] }

View File

@ -160,6 +160,7 @@ CREATE TABLE IF NOT EXISTS "notification" (
"read_at" DATETIME, "read_at" DATETIME,
"created_at" DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
"category" TEXT NOT NULL, "category" TEXT NOT NULL,
"action_after_reading" TEXT,
"link" TEXT "link" TEXT
); );

View File

@ -137,7 +137,7 @@ ORDER BY created_at DESC
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(); 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).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 =
@ -158,6 +158,7 @@ ORDER BY created_at DESC
), ),
"Neuer Bootsschaden angelegt", "Neuer Bootsschaden angelegt",
None, None,
None,
) )
.await; .await;
} }
@ -178,6 +179,7 @@ ORDER BY created_at DESC
), ),
"Neuer Bootsschaden angelegt", "Neuer Bootsschaden angelegt",
None, None,
None,
) )
.await; .await;
@ -235,7 +237,7 @@ ORDER BY created_at DESC
boat.name, boat.name,
), ),
"Bootsschaden repariert", "Bootsschaden repariert",
None, None,None
) )
.await; .await;
} }
@ -261,7 +263,7 @@ ORDER BY created_at DESC
boat_damage.desc, boat.name, boat_damage.desc, boat.name,
), ),
"Bootsschaden repariert", "Bootsschaden repariert",
None, None,None
) )
.await; .await;
} }
@ -320,6 +322,7 @@ ORDER BY created_at DESC
), ),
"Bootsschaden repariert & verifiziert", "Bootsschaden repariert & verifiziert",
None, None,
None
) )
.await; .await;
} else { } else {
@ -333,13 +336,14 @@ ORDER BY created_at DESC
), ),
"Bootsschaden verifiziert", "Bootsschaden verifiziert",
None, None,
None
).await; ).await;
} }
} }
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(); let cox = Role::find_by_name(db, "cox").await.unwrap();
Notification::create_for_role(db, &cox, &format!("Liebe Steuerberechtigte, {} wurde repariert und freut sich ab sofort wieder gerudert zu werden :-)", boat.name), "Boot repariert", None).await; Notification::create_for_role(db, &cox, &format!("Liebe Steuerberechtigte, {} wurde repariert und freut sich ab sofort wieder gerudert zu werden :-)", boat.name), "Boot repariert", None, None).await;
} }
Ok(()) Ok(())

View File

@ -175,7 +175,7 @@ WHERE end_date >= CURRENT_DATE ORDER BY end_date
boatreservation.usage boatreservation.usage
), ),
"Neue Bootsreservierung", "Neue Bootsreservierung",
None, None,None
) )
.await; .await;
} }

View File

@ -549,6 +549,7 @@ ORDER BY departure DESC
), ),
"Neuer Logbucheintrag", "Neuer Logbucheintrag",
None, None,
None,
) )
.await; .await;
} }
@ -578,7 +579,7 @@ ORDER BY departure DESC
&vorstand, &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())), &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", "Mehrtägige Ausfahrt eingetragen",
None, None,None
).await; ).await;
} }
@ -590,7 +591,7 @@ ORDER BY departure DESC
&vorstand, &vorstand,
&format!("'{}' hat eine Ausfahrt mit externem Boot '{}' am {} eingetragen ({} km; Ziel: {}; Anmerkungen: {}). Falls das nicht stimmen sollte, bitte nachhaken.",user.name,boat.name,log.departure,log.distance_in_km, log.destination, log.comments.unwrap_or("".into())), &format!("'{}' hat eine Ausfahrt mit externem Boot '{}' am {} eingetragen ({} km; Ziel: {}; Anmerkungen: {}). Falls das nicht stimmen sollte, bitte nachhaken.",user.name,boat.name,log.departure,log.distance_in_km, log.destination, log.comments.unwrap_or("".into())),
"Ausfahrt mit externem Boot eingetragen", "Ausfahrt mit externem Boot eingetragen",
None, None,None,
).await; ).await;
} }

View File

@ -1,6 +1,7 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -15,6 +16,7 @@ pub struct Notification {
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
pub category: String, pub category: String,
pub link: Option<String>, pub link: Option<String>,
pub action_after_reading: Option<String>,
} }
impl Notification { impl Notification {
@ -30,13 +32,15 @@ impl Notification {
message: &str, message: &str,
category: &str, category: &str,
link: Option<&str>, link: Option<&str>,
action_after_reading: Option<&str>,
) { ) {
sqlx::query!( sqlx::query!(
"INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)", "INSERT INTO notification(user_id, message, category, link, action_after_reading) VALUES (?, ?, ?, ?, ?)",
user.id, user.id,
message, message,
category, category,
link link,
action_after_reading
) )
.execute(db.deref_mut()) .execute(db.deref_mut())
.await .await
@ -49,9 +53,10 @@ impl Notification {
message: &str, message: &str,
category: &str, category: &str,
link: Option<&str>, link: Option<&str>,
action_after_reading: Option<&str>,
) { ) {
let mut tx = db.begin().await.unwrap(); let mut tx = db.begin().await.unwrap();
Self::create_with_tx(&mut tx, user, message, category, link).await; Self::create_with_tx(&mut tx, user, message, category, link, action_after_reading).await;
tx.commit().await.unwrap(); tx.commit().await.unwrap();
} }
@ -61,11 +66,12 @@ impl Notification {
message: &str, message: &str,
category: &str, category: &str,
link: Option<&str>, link: Option<&str>,
action_after_reading: Option<&str>,
) { ) {
let users = User::all_with_role_tx(db, role).await; let users = User::all_with_role_tx(db, role).await;
for user in users { for user in users {
Self::create_with_tx(db, &user, message, category, link).await; Self::create_with_tx(db, &user, message, category, link, action_after_reading).await;
} }
} }
@ -75,16 +81,18 @@ impl Notification {
message: &str, message: &str,
category: &str, category: &str,
link: Option<&str>, link: Option<&str>,
action_after_reading: Option<&str>,
) { ) {
let mut tx = db.begin().await.unwrap(); let mut tx = db.begin().await.unwrap();
Self::create_for_role_tx(&mut tx, role, message, category, link).await; Self::create_for_role_tx(&mut tx, role, message, category, link, action_after_reading)
.await;
tx.commit().await.unwrap(); tx.commit().await.unwrap();
} }
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!(
" "
SELECT id, user_id, message, read_at, datetime(created_at, 'localtime') as created_at, category, link FROM notification SELECT id, user_id, message, read_at, datetime(created_at, 'localtime') as created_at, category, link, action_after_reading FROM notification
WHERE WHERE
user_id = ? user_id = ?
AND ( AND (
@ -113,6 +121,7 @@ ORDER BY read_at DESC, created_at DESC;
.unwrap(), .unwrap(),
category: rec.category, category: rec.category,
link: rec.link, link: rec.link,
action_after_reading: rec.action_after_reading,
}) })
.collect() .collect()
} }
@ -125,5 +134,24 @@ ORDER BY read_at DESC, created_at DESC;
.execute(db) .execute(db)
.await .await
.unwrap(); .unwrap();
if let Some(action) = self.action_after_reading.as_ref() {
// User read notification about cancelled trip/event
let re = Regex::new(r"^remove_user_trip_with_trip_details_id:(\d+)$").unwrap();
if let Some(caps) = re.captures(action) {
if let Some(matched) = caps.get(1) {
if let Ok(number) = matched.as_str().parse::<i32>() {
let _ = sqlx::query!(
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
self.user_id,
number
)
.execute(db)
.await
.unwrap();
}
}
}
}
} }
} }

View File

@ -80,6 +80,7 @@ impl Trip {
), ),
"Neue Ausfahrt zur selben Zeit", "Neue Ausfahrt zur selben Zeit",
None, None,
None,
) )
.await; .await;
} }
@ -209,10 +210,9 @@ WHERE day=?
let rowers = TripWithUserAndType::from(db, trip.clone()).await.rower; let rowers = TripWithUserAndType::from(db, trip.clone()).await.rower;
for user in rowers { for user in rowers {
if let Some(user) = User::find_by_name(db, &user.name).await { if let Some(user) = User::find_by_name(db, &user.name).await {
let notes = if let Some(notes) = notes { let notes = match notes {
format!(": {notes}") Some(n) if !n.is_empty() => n,
} else { _ => ".",
String::from(".")
}; };
Notification::create( Notification::create(
@ -224,6 +224,10 @@ WHERE day=?
), ),
"Absage Ausfahrt", "Absage Ausfahrt",
None, None,
Some(&format!(
"remove_user_trip_with_trip_details_id:{}",
trip_details_id
)),
) )
.await; .await;
} }

View File

@ -120,7 +120,7 @@ WHERE day = ? AND planned_starting_time = ?
continue; continue;
} }
Notification::create(db, &user, &format!("Du hast dich als Ruderer bei der Ausfahrt von {} am {} um {} angemeldet. Bei allen Ausfahrten zu dieser Zeit sind nun alle Plätze ausgebucht. Damit noch mehr (Nicht-Steuerleute) mitfahren können, wäre es super, wenn du eine eigene Ausfahrt zur selben Zeit ausschreiben könntest.", cox.name, self.day, self.planned_starting_time), "Volle Ausfahrt", None).await; Notification::create(db, &user, &format!("Du hast dich als Ruderer bei der Ausfahrt von {} am {} um {} angemeldet. Bei allen Ausfahrten zu dieser Zeit sind nun alle Plätze ausgebucht. Damit noch mehr (Nicht-Steuerleute) mitfahren können, wäre es super, wenn du eine eigene Ausfahrt zur selben Zeit ausschreiben könntest.", cox.name, self.day, self.planned_starting_time), "Volle Ausfahrt", None, None).await;
} }
} }
} }

View File

@ -202,7 +202,7 @@ ASKÖ Ruderverein Donau Linz", self.name, SCHECKBUCH/100),
self.name self.name
), ),
"Neues Scheckbuch", "Neues Scheckbuch",
None, None,None
) )
.await; .await;
} }
@ -246,6 +246,7 @@ ASKÖ Ruderverein Donau Linz", self.name),
), ),
"Neues Vereinsmitglied", "Neues Vereinsmitglied",
None, None,
None,
) )
.await; .await;
} }

View File

@ -76,6 +76,7 @@ impl UserTrip {
), ),
"Registrierung bei deiner Ausfahrt", "Registrierung bei deiner Ausfahrt",
None, None,
None,
) )
.await; .await;

View File

@ -76,7 +76,7 @@ async fn send_group(
}; };
for user in User::all_with_role(db, &role).await { for user in User::all_with_role(db, &role).await {
Notification::create(db, &user, &d.message, &d.category, None).await; Notification::create(db, &user, &d.message, &d.category, None, None).await;
} }
Log::create(db, "Notification successfully sent".into()).await; Log::create(db, "Notification successfully sent".into()).await;
Flash::success( Flash::success(