notification-on-boatdamages #509

Merged
philipp merged 2 commits from notification-on-boatdamages into staging 2024-05-20 21:21:43 +02:00

View File

@ -119,6 +119,10 @@ ORDER BY created_at DESC
pub async fn create(db: &SqlitePool, boatdamage: BoatDamageToAdd<'_>) -> Result<(), String> { pub async fn create(db: &SqlitePool, boatdamage: BoatDamageToAdd<'_>) -> Result<(), String> {
Log::create(db, format!("New boat damage: {boatdamage:?}")).await; Log::create(db, format!("New boat damage: {boatdamage:?}")).await;
let Some(boat) = Boat::find_by_id(db, boatdamage.boat_id as i32).await else {
return Err("Boot gibt's ned".into());
};
let was_unusable_before = boat.is_locked(db).await;
sqlx::query!( sqlx::query!(
"INSERT INTO boat_damage(boat_id, desc, user_id_created, lock_boat) VALUES (?,?,?, ?)", "INSERT INTO boat_damage(boat_id, desc, user_id_created, lock_boat) VALUES (?,?,?, ?)",
@ -131,6 +135,11 @@ ORDER BY created_at DESC
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
if !was_unusable_before && boat.is_locked(db).await {
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;
}
let technicals = let technicals =
User::all_with_role(db, &Role::find_by_name(db, "tech").await.unwrap()).await; User::all_with_role(db, &Role::find_by_name(db, "tech").await.unwrap()).await;
for technical in technicals { for technical in technicals {
@ -144,10 +153,7 @@ ORDER BY created_at DESC
.await .await
.unwrap() .unwrap()
.name, .name,
Boat::find_by_id(db, boatdamage.boat_id as i32) boat.name,
.await
.unwrap()
.name,
boatdamage.desc boatdamage.desc
), ),
"Neuer Bootsschaden angelegt", "Neuer Bootsschaden angelegt",
@ -267,35 +273,41 @@ ORDER BY created_at DESC
pub async fn verified( pub async fn verified(
&self, &self,
db: &SqlitePool, db: &SqlitePool,
boat: BoatDamageVerified<'_>, boat_form: BoatDamageVerified<'_>,
) -> Result<(), String> { ) -> Result<(), String> {
if let Some(verifier) = User::find_by_id(db, boat.user_id_verified).await { if let Some(verifier) = User::find_by_id(db, boat_form.user_id_verified).await {
if !verifier.has_role(db, "tech").await { if !verifier.has_role(db, "tech").await {
Log::create(db, format!("User {verifier:?} tried to verify boat {boat:?}. The user is no tech. Manually craftted request?")).await; Log::create(db, format!("User {verifier:?} tried to verify boat {boat_form:?}. The user is no tech. Manually craftted request?")).await;
return Err("You are not allowed to verify the boat!".into()); return Err("You are not allowed to verify the boat!".into());
} }
} else { } else {
Log::create(db, format!("Someone tried to verify the boat {boat:?} with user_id={} which does not exist. Manually craftted request?", boat.user_id_verified)).await; Log::create(db, format!("Someone tried to verify the boat {boat_form:?} with user_id={} which does not exist. Manually craftted request?", boat_form.user_id_verified)).await;
return Err("Could not find user".into()); return Err("Could not find user".into());
} }
Log::create(db, format!("Verified boat damage: {boat:?}")).await; let Some(boat) = Boat::find_by_id(db, self.boat_id as i32).await else {
return Err("Boot gibt's ned".into());
};
let was_unusable_before = boat.is_locked(db).await;
Log::create(db, format!("Verified boat damage: {boat_form:?}")).await;
sqlx::query!( sqlx::query!(
"UPDATE boat_damage SET desc=?, user_id_verified=?, verified_at=CURRENT_TIMESTAMP WHERE id=?", "UPDATE boat_damage SET desc=?, user_id_verified=?, verified_at=CURRENT_TIMESTAMP WHERE id=?",
boat.desc, boat_form.desc,
boat.user_id_verified, boat_form.user_id_verified,
self.id self.id
) )
.execute(db) .execute(db)
.await.map_err(|e| e.to_string())?; .await.map_err(|e| e.to_string())?;
if boat.user_id_verified != self.user_id_created as i32 { if boat_form.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_verified = User::find_by_id(db, boat_form.user_id_verified)
.await
.unwrap();
let user_created = User::find_by_id(db, self.user_id_created as i32) let user_created = User::find_by_id(db, self.user_id_created as i32)
.await .await
.unwrap(); .unwrap();
let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap();
if user_verified.id == self.user_id_fixed.unwrap() { if user_verified.id == self.user_id_fixed.unwrap() {
Notification::create( Notification::create(
@ -325,6 +337,11 @@ ORDER BY created_at DESC
} }
} }
if was_unusable_before && !boat.is_locked(db).await {
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;
}
Ok(()) Ok(())
} }
} }