From 22d499d38b3e639c80c1d128710eefc90f63b6ca Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 17 Apr 2024 12:44:31 +0200 Subject: [PATCH] send creator of boatdamage notification when it has been repaired --- src/model/boatdamage.rs | 72 ++++++++++++++++++++++++++++++++++------- src/tera/mod.rs | 1 - 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/model/boatdamage.rs b/src/model/boatdamage.rs index ce4988b..a14e655 100644 --- a/src/model/boatdamage.rs +++ b/src/model/boatdamage.rs @@ -235,19 +235,31 @@ ORDER BY created_at DESC } } - Notification::create( - db, - &User::find_by_id(db, boat_damage.user_id_fixed) + if boat_damage.user_id_fixed != self.user_id_created as i32 { + let user_fixed = User::find_by_id(db, boat_damage.user_id_fixed) .await - .unwrap(), - &format!( - "Du hat den Bootschaden '{}' beim Boot '{}' repariert. Danke für deine Hilfe!", - boat_damage.desc, boat.name, - ), - "Bootsschaden repariert", - None, - ) - .await; + .unwrap(); + let user_created = User::find_by_id(db, self.user_id_created as i32) + .await + .unwrap(); + + // Boatdamage is also directly verified, if a tech has repaired it. We don't want to + // send 2 notifications. + if !user_fixed.has_role(db, "tech").await { + Notification::create( + db, + &user_created, + &format!( + "{} hat den von dir eingetragenen Bootschaden '{}' beim Boot '{}' repariert. Dieser muss nun noch von unseren Bootswarten bestätigt werden.", + user_fixed.name, + boat_damage.desc, boat.name, + ), + "Bootsschaden repariert", + None, + ) + .await; + } + } Ok(()) } @@ -277,6 +289,42 @@ ORDER BY created_at DESC ) .execute(db) .await.map_err(|e| e.to_string())?; + + if boat.user_id_verified != self.user_id_created as i32 { + let user_verified = User::find_by_id(db, boat.user_id_verified).await.unwrap(); + let user_created = User::find_by_id(db, self.user_id_created as i32) + .await + .unwrap(); + let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap(); + + if user_verified.id == self.user_id_fixed.unwrap() { + Notification::create( + db, + &user_created, + &format!( + "{} hat den von dir eingetragenen Bootschaden '{}' beim Boot '{}' repariert und verifiziert.", + user_verified.name, + self.desc, boat.name, + ), + "Bootsschaden repariert & verifiziert", + None, + ) + .await; + } else { + Notification::create( + db, + &user_created, + &format!( + "{} hat verifiziert, dass der von dir eingetragenen Bootschaden '{}' beim Boot '{}' korrekt repariert wurde.", + user_verified.name, + self.desc, boat.name, + ), + "Bootsschaden verifiziert", + None, + ).await; + } + } + Ok(()) } } diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 02b5905..5f89c2b 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -145,7 +145,6 @@ impl Fairing for Usage { { let config = req.rocket().state::().unwrap(); let Ok(mut file) = OpenOptions::new() - .write(true) .append(true) .open(config.usage_log_path.clone()) else {