Merge branch 'no-data-mangling' into 'staging'

don't allow to add entry which is not completed today

See merge request PhilippHofer/rot!116
This commit is contained in:
PhilippHofer 2023-11-16 14:50:39 +00:00
commit f28a66b11e
2 changed files with 38 additions and 21 deletions

View File

@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::{NaiveDateTime, Utc};
use rocket::FromForm;
use serde::Serialize;
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -99,6 +99,7 @@ pub enum LogbookUpdateError {
ShipmasterNotInRowers,
SteeringPersonNotInRowers,
UserNotAllowedToUseBoat,
OnlyAllowedToEndTripsEndingToday,
}
#[derive(Debug, PartialEq)]
@ -120,6 +121,7 @@ pub enum LogbookCreateError {
ShipmasterNotInRowers,
NotYourEntry,
ArrivalSetButNotRemainingTwo,
OnlyAllowedToEndTripsEndingToday,
}
impl From<LogbookUpdateError> for LogbookCreateError {
@ -140,6 +142,9 @@ impl From<LogbookUpdateError> for LogbookCreateError {
LogbookUpdateError::UserNotAllowedToUseBoat => {
LogbookCreateError::UserNotAllowedToUseBoat
}
LogbookUpdateError::OnlyAllowedToEndTripsEndingToday => {
LogbookCreateError::OnlyAllowedToEndTripsEndingToday
}
}
}
}
@ -446,13 +451,8 @@ ORDER BY departure DESC
}
if !boat.shipmaster_allowed(user).await && self.shipmaster != user.id {
//second part:
//shipmaster has
//entered a
//different user,
//then the user
//should be able
//to `home` it
//second part: shipmaster has entered a different user, then the user should be able to
//`home` it
return Err(LogbookUpdateError::UserNotAllowedToUseBoat);
}
@ -468,6 +468,10 @@ ORDER BY departure DESC
if arr.timestamp() <= dep.timestamp() {
return Err(LogbookUpdateError::ArrivalNotAfterDeparture);
}
let today = Utc::now().date_naive();
if arr.date() != today && !user.is_admin {
return Err(LogbookUpdateError::OnlyAllowedToEndTripsEndingToday);
}
Log::create_with_tx(db, format!("New trip: {log:?}")).await;
@ -793,6 +797,8 @@ mod test {
let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
let user = User::find_by_id(&pool, 2).await.unwrap();
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
logbook
.home(
&pool,
@ -806,8 +812,8 @@ mod test {
shipmaster: Some(2),
steering_person: Some(2),
shipmaster_only_steering: false,
departure: "1990-01-01T10:00".into(),
arrival: "1990-01-01T12:00".into(),
departure: format!("{}T10:00", current_date),
arrival: format!("{}T12:00", current_date),
},
)
.await

View File

@ -182,6 +182,7 @@ async fn create_logbook(
Err(LogbookCreateError::ShipmasterNotInRowers) => Flash::error(Redirect::to("/log"), "Schiffsführer nicht in Liste der Ruderer!"),
Err(LogbookCreateError::NotYourEntry) => Flash::error(Redirect::to("/log"), "Nicht deine Ausfahrt!"),
Err(LogbookCreateError::ArrivalSetButNotRemainingTwo) => Flash::error(Redirect::to("/log"), "Ankunftszeit gesetzt aber nicht Distanz + Strecke"),
Err(LogbookCreateError::OnlyAllowedToEndTripsEndingToday) => Flash::error(Redirect::to("/log"), format!("Nur Ausfahrten, die heute enden dürfen eingetragen werden. Für einen Nachtrag schreibe alle Daten Philipp (Tel. nr. siehe Signal oder it@rudernlinz.at).")),
}
}
@ -251,6 +252,7 @@ async fn home_logbook(
match logbook.home(db, &user.user, data.into_inner()).await {
Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt korrekt eingetragen"),
Err(LogbookUpdateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
Err(LogbookUpdateError::OnlyAllowedToEndTripsEndingToday) => Flash::error(Redirect::to("/log"), format!("Nur Ausfahrten, die heute enden dürfen eingetragen werden. Für einen Nachtrag schreibe alle Daten Philipp (Tel. nr. siehe Signal oder it@rudernlinz.at).")),
Err(e) => Flash::error(
Redirect::to("/log"),
format!("Eintrag {logbook_id} konnte nicht abgesendet werden (Fehler: {e:?})!"),
@ -522,11 +524,12 @@ mod test {
.header(ContentType::Form) // Set the content type to form
.body("name=admin&password=admin"); // Add the form data to the request body;
login.dispatch().await;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client
.post("/log")
.header(ContentType::Form)
.body("boat_id=1&shipmaster=4&departure=2199-12-31T10:00&steering_person=4&rowers[]=4");
let req = client.post("/log").header(ContentType::Form).body(format!(
"boat_id=1&shipmaster=4&departure={0}T10:00&steering_person=4&rowers[]=4",
current_date
));
let response = req.dispatch().await;
assert_eq!(response.status(), Status::SeeOther);
@ -554,10 +557,12 @@ mod test {
let req = client.get("/log/kiosk/ekrv2019/Linz");
let _ = req.dispatch().await;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client
.post("/log/1")
.header(ContentType::Form)
.body("destination=Ottensheim&distance_in_km=25&shipmaster=2&steering_person=2&departure=1990-01-01T10:00&arrival=1990-01-01T12:00&rowers[]=2");
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster=2&steering_person=2&departure={0}T10:00&arrival={0}T12:00&rowers[]=2", current_date));
let response = req.dispatch().await;
assert_eq!(response.status(), Status::SeeOther);
@ -664,9 +669,10 @@ mod test {
.unwrap()
.id;
let shipmaster_id = User::find_by_name(&db, "rower2".into()).await.unwrap().id;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client.post("/log").header(ContentType::Form).body(format!(
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}"
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure={0}T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}", current_date
));
let response = req.dispatch().await;
@ -695,7 +701,7 @@ mod test {
let req = client
.post(format!("/log/{log_id}"))
.header(ContentType::Form)
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure=1990-01-01T10:00&arrival=1990-01-01T12:00&rowers[]={shipmaster_id}"));
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure={0}T10:00&arrival={0}T12:00&rowers[]={shipmaster_id}", current_date));
let response = req.dispatch().await;
assert_eq!(response.status(), Status::SeeOther);
@ -882,11 +888,12 @@ mod test {
.header(ContentType::Form) // Set the content type to form
.body("name=rower2&password=rower"); // Add the form data to the request body;
login.dispatch().await;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client
.post("/log/1")
.header(ContentType::Form)
.body("destination=Ottensheim&distance_in_km=25&shipmaster=1&steering_person=1&departure=1199-12-12T10:00&arrival=1199-12-12T12:00&rowers[]=1");
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster=1&steering_person=1&departure={0}T10:00&arrival={0}T12:00&rowers[]=1", current_date));
let response = req.dispatch().await;
assert_eq!(response.status(), Status::SeeOther);
@ -912,8 +919,10 @@ mod test {
let boat_id = Boat::find_by_name(db, boat_name).await.unwrap().id;
let shipmaster_id = User::find_by_name(db, &shipmaster_name).await.unwrap().id;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client.post("/log").header(ContentType::Form).body(format!(
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}"
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure={current_date}T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}"
));
let response = req.dispatch().await;
@ -935,7 +944,7 @@ mod test {
let req = client
.post(format!("/log/{log_id}"))
.header(ContentType::Form)
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure=1199-12-31T10:00&arrival=1199-12-31T12:00&rowers[]={shipmaster_id}"));
.body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure={current_date}T10:00&arrival={current_date}T12:00&rowers[]={shipmaster_id}"));
let response = req.dispatch().await;
assert_eq!(response.status(), Status::SeeOther);
@ -962,8 +971,10 @@ mod test {
let boat_id = Boat::find_by_name(db, boat_name).await.unwrap().id;
let shipmaster_id = User::find_by_name(db, &shipmaster_name).await.unwrap().id;
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let req = client.post("/log").header(ContentType::Form).body(format!(
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure=2199-12-31T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}"
"boat_id={boat_id}&shipmaster={shipmaster_id}&departure={current_date}T10:00&steering_person={shipmaster_id}&rowers[]={shipmaster_id}"
));
let response = req.dispatch().await;