log important events

This commit is contained in:
philipp 2023-10-05 08:59:13 +02:00
parent c47b3bb447
commit 1386ba05c2
2 changed files with 25 additions and 4 deletions

View File

@ -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=?",

View File

@ -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)