From a36fc300f081d87f13b89eb10e7e83cf5e0c3c16 Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 28 Mar 2024 08:34:02 +0100 Subject: [PATCH] show new notifications for boatdamages; Fixes #310 --- src/model/boatdamage.rs | 104 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/src/model/boatdamage.rs b/src/model/boatdamage.rs index 37207f2..1f9802e 100644 --- a/src/model/boatdamage.rs +++ b/src/model/boatdamage.rs @@ -5,6 +5,8 @@ use rocket::FromForm; use sqlx::{FromRow, SqlitePool}; use super::log::Log; +use super::notification::Notification; +use super::role::Role; #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct BoatDamage { @@ -124,35 +126,125 @@ ORDER BY created_at DESC .execute(db) .await .map_err(|e| e.to_string())?; + + let technicals = + User::all_with_role(db, &Role::find_by_name(db, "tech").await.unwrap()).await; + for technical in technicals { + if technical.id as i32 != boatdamage.user_id_created { + Notification::create( + db, + &technical, + &format!( + "{} hat einen neuen Bootschaden für Boot '{}' angelegt: {}", + User::find_by_id(db, boatdamage.user_id_created) + .await + .unwrap() + .name, + Boat::find_by_id(db, boatdamage.boat_id as i32) + .await + .unwrap() + .name, + boatdamage.desc + ), + "Neuer Bootsschaden angelegt", + None, + ) + .await; + } + } + + Notification::create( + db, + &User::find_by_id(db, boatdamage.user_id_created) + .await + .unwrap(), + &format!( + "Du hat einen neuen Bootschaden für Boot '{}' angelegt: {}", + Boat::find_by_id(db, boatdamage.boat_id as i32) + .await + .unwrap() + .name, + boatdamage.desc + ), + "Neuer Bootsschaden angelegt", + None, + ) + .await; + Ok(()) } - pub async fn fixed(&self, db: &SqlitePool, boat: BoatDamageFixed<'_>) -> Result<(), String> { - Log::create(db, format!("Fixed boat damage: {boat:?}")).await; + pub async fn fixed( + &self, + db: &SqlitePool, + boat_damage: BoatDamageFixed<'_>, + ) -> Result<(), String> { + Log::create(db, format!("Fixed boat damage: {boat_damage:?}")).await; + + let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap(); sqlx::query!( "UPDATE boat_damage SET desc=?, user_id_fixed=?, fixed_at=CURRENT_TIMESTAMP WHERE id=?", - boat.desc, - boat.user_id_fixed, + boat_damage.desc, + boat_damage.user_id_fixed, self.id ) .execute(db) .await .map_err(|e| e.to_string())?; - let user = User::find_by_id(db, boat.user_id_fixed).await.unwrap(); + let user = User::find_by_id(db, boat_damage.user_id_fixed) + .await + .unwrap(); if user.has_role(db, "tech").await { return self .verified( db, BoatDamageVerified { - desc: boat.desc, + desc: boat_damage.desc, user_id_verified: user.id as i32, }, ) .await; } + let technicals = + User::all_with_role(db, &Role::find_by_name(db, "tech").await.unwrap()).await; + for technical in technicals { + if technical.id as i32 != boat_damage.user_id_fixed { + Notification::create( + db, + &technical, + &format!( + "{} hat den Bootschaden '{}' beim Boot '{}' repariert. Könntest du das bei Gelegenheit verifizieren?", + User::find_by_id(db, boat_damage.user_id_fixed) + .await + .unwrap() + .name, + boat_damage.desc, + boat.name, + ), + "Bootsschaden repariert", + None, + ) + .await; + } + } + + Notification::create( + db, + &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; + Ok(()) }