From 1386ba05c272401bcc724904f4003c6c3dec7ccf Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 5 Oct 2023 08:59:13 +0200 Subject: [PATCH] log important events --- src/model/boatdamage.rs | 23 ++++++++++++++++++++--- src/model/logbook.rs | 6 +++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/model/boatdamage.rs b/src/model/boatdamage.rs index 7dbabfb..b50b754 100644 --- a/src/model/boatdamage.rs +++ b/src/model/boatdamage.rs @@ -4,6 +4,8 @@ use rocket::serde::{Deserialize, Serialize}; use rocket::FromForm; use sqlx::{FromRow, SqlitePool}; +use super::log::Log; + #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct BoatDamage { pub id: i64, @@ -28,6 +30,7 @@ pub struct BoatDamageWithDetails { boat: Boat, } +#[derive(Debug)] pub struct BoatDamageToAdd<'r> { pub boat_id: i64, pub desc: &'r str, @@ -35,13 +38,13 @@ pub struct BoatDamageToAdd<'r> { pub lock_boat: bool, } -#[derive(FromForm)] +#[derive(FromForm, Debug)] pub struct BoatDamageFixed<'r> { pub desc: &'r str, pub user_id_fixed: i32, } -#[derive(FromForm)] +#[derive(FromForm, Debug)] pub struct BoatDamageVerified<'r> { pub desc: &'r str, pub user_id_verified: i32, @@ -107,6 +110,8 @@ ORDER BY created_at DESC } pub async fn create(db: &SqlitePool, boatdamage: BoatDamageToAdd<'_>) -> Result<(), String> { + Log::create(db, format!("New boat damage: {boatdamage:?}")).await; + sqlx::query!( "INSERT INTO boat_damage(boat_id, desc, user_id_created, lock_boat) VALUES (?,?,?, ?)", boatdamage.boat_id, @@ -121,6 +126,8 @@ ORDER BY created_at DESC } pub async fn fixed(&self, db: &SqlitePool, boat: BoatDamageFixed<'_>) -> Result<(), String> { + Log::create(db, format!("Fixed boat damage: {boat:?}")).await; + sqlx::query!( "UPDATE boat_damage SET desc=?, user_id_fixed=?, fixed_at=CURRENT_TIMESTAMP WHERE id=?", boat.desc, @@ -152,7 +159,17 @@ ORDER BY created_at DESC db: &SqlitePool, boat: BoatDamageVerified<'_>, ) -> Result<(), String> { - //TODO: Check if user is allowed to verify + if let Some(verifier) = User::find_by_id(db, boat.user_id_verified).await { + if !verifier.is_tech { + Log::create(db, format!("User {verifier:?} tried to verify boat {boat:?}. The user is no tech. Manually craftted request?")).await; + return Err("You are not allowed to verify the boat!".into()); + } + } 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; + return Err("Could not find user".into()); + } + + Log::create(db, format!("Verified boat damage: {boat:?}")).await; sqlx::query!( "UPDATE boat_damage SET desc=?, user_id_verified=?, verified_at=CURRENT_TIMESTAMP WHERE id=?", diff --git a/src/model/logbook.rs b/src/model/logbook.rs index adca6f0..4289231 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -4,7 +4,7 @@ use rocket::FromForm; use serde::Serialize; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; -use super::{boat::Boat, rower::Rower, user::User}; +use super::{boat::Boat, log::Log, rower::Rower, user::User}; #[derive(FromRow, Serialize, Clone, Debug)] pub struct Logbook { @@ -232,6 +232,7 @@ ORDER BY departure DESC } //let departure = format!("{}+02:00", &log.departure); + Log::create(db, format!("New trip started: {log:?}")).await; let mut tx = db.begin().await.unwrap(); @@ -339,6 +340,7 @@ ORDER BY departure DESC return Err(LogbookUpdateError::ArrivalNotAfterDeparture); } + Log::create(db, format!("New trip: {log:?}")).await; let mut tx = db.begin().await.unwrap(); sqlx::query!( @@ -367,6 +369,8 @@ ORDER BY departure DESC } pub async fn delete(&self, db: &SqlitePool, user: &User) -> Result<(), LogbookDeleteError> { + Log::create(db, format!("{user:?} deleted trip: {self:?}")).await; + if user.is_admin || user.id == self.shipmaster { sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) .execute(db)