From 2a8c339dcdf7fe1c1277ac24a9ab3ff429754f7a Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 30 Jul 2024 23:24:46 +0200 Subject: [PATCH] push --- src/model/logbook.rs | 77 ++++++++++++++++++++++++++++---------------- src/tera/log.rs | 4 +-- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index c99b107..5c68214 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -1,6 +1,6 @@ use std::ops::DerefMut; -use chrono::{Datelike, Local, NaiveDateTime}; +use chrono::{Datelike, Duration, Local, NaiveDateTime}; use rocket::FromForm; use serde::Serialize; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; @@ -110,6 +110,20 @@ pub struct LogbookWithBoatAndRowers { pub rowers: Vec, } +impl LogbookWithBoatAndRowers { + pub(crate) async fn from(db: &SqlitePool, log: Logbook) -> Self { + Self { + rowers: Rower::for_log(db, &log).await, + boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), + shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), + steering_user: User::find_by_id(db, log.steering_person as i32) + .await + .unwrap(), + logbook: log, + } + } +} + #[derive(Debug, PartialEq)] pub enum LogbookAdminUpdateError { NotAllowed, @@ -248,15 +262,7 @@ ORDER BY departure DESC let mut ret = Vec::new(); for log in logs { - ret.push(LogbookWithBoatAndRowers { - rowers: Rower::for_log(db, &log).await, - boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), - shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), - steering_user: User::find_by_id(db, log.steering_person as i32) - .await - .unwrap(), - logbook: log, - }); + ret.push(LogbookWithBoatAndRowers::from(db, log).await); } ret } @@ -280,15 +286,7 @@ ORDER BY departure DESC let mut ret = Vec::new(); for log in logs { - ret.push(LogbookWithBoatAndRowers { - rowers: Rower::for_log(db, &log).await, - boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), - shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), - steering_user: User::find_by_id(db, log.steering_person as i32) - .await - .unwrap(), - logbook: log, - }); + ret.push(LogbookWithBoatAndRowers::from(db, log).await); } ret } @@ -313,15 +311,7 @@ ORDER BY departure DESC let mut ret = Vec::new(); for log in logs { - ret.push(LogbookWithBoatAndRowers { - rowers: Rower::for_log(db, &log).await, - boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), - shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), - steering_user: User::find_by_id(db, log.steering_person as i32) - .await - .unwrap(), - logbook: log, - }); + ret.push(LogbookWithBoatAndRowers::from(db, log).await); } ret } @@ -713,6 +703,37 @@ ORDER BY departure DESC || user.has_role(db, "Vorstand").await || user.id == self.shipmaster { + let now = Local::now().naive_local(); + let difference = now - self.departure; + if difference > Duration::hours(1) { + let vorstand = Role::find_by_name(db, "Vorstand").await.unwrap(); + let logbook = LogbookWithBoatAndRowers::from(db, self.clone()).await; + let mut msg = format!("{} hat folgenden Logbuch-Eintrag jetzt gelöscht, welcher bereits vor über einer Stunde begonnen wurde: Schiffsführer: {}, Steuerperson: {}, Abfahrt: {}", user.name, logbook.steering_user.name, logbook.steering_user.name, logbook.logbook.departure.format("%Y-%m-%d %H:%M")); + if let Some(destination) = logbook.logbook.destination { + msg.push_str(&format!(", Ziel: {}", destination)); + } else { + msg.push_str(", kein Ziel eingegeben"); + } + msg.push_str(", Ruderer: "); + let mut it = logbook.rowers.clone().into_iter().peekable(); + while let Some(rower) = it.next() { + msg.push_str(&rower.name); + if it.peek().is_some() { + msg.push_str(" + "); + } + } + + Notification::create_for_role( + db, + &vorstand, + &msg, + "Ungewöhnliches Verhalten", + None, + None, + ) + .await; + } + sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) .execute(db) .await diff --git a/src/tera/log.rs b/src/tera/log.rs index fa02887..91c342f 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -311,12 +311,12 @@ async fn update( Flash::success( Redirect::to("/log/show"), - format!("Logbucheintrag erfolgreich bearbeitet"), + "Logbucheintrag erfolgreich bearbeitet".to_string(), ) } Err(LogbookAdminUpdateError::NotAllowed) => Flash::error( Redirect::to("/log/show"), - format!("Du hast keine Erlaubnis, diesen Logbucheintrag zu bearbeiten!"), + "Du hast keine Erlaubnis, diesen Logbucheintrag zu bearbeiten!".to_string(), ), } } -- 2.45.2