Merge branch 'main' of ssh://git.hofer.link:2222/Ruderverein-Donau-Linz/rowt

This commit is contained in:
2024-11-27 08:10:55 +01:00
14 changed files with 173 additions and 55 deletions

View File

@ -9,8 +9,12 @@ use serde::Serialize;
use sqlx::{FromRow, Row, SqlitePool};
use super::{
notification::Notification, role::Role, tripdetails::TripDetails, triptype::TripType,
user::User,
log::Log,
notification::Notification,
role::Role,
tripdetails::TripDetails,
triptype::TripType,
user::{EventUser, User},
};
#[derive(Serialize, Clone, FromRow, Debug, PartialEq)]
@ -242,6 +246,7 @@ WHERE trip_details.id=?
pub async fn create(
db: &SqlitePool,
user: &EventUser,
name: &str,
planned_amount_cox: i32,
always_show: bool,
@ -270,6 +275,15 @@ WHERE trip_details.id=?
.execute(db)
.await
.unwrap(); //Okay, as TripDetails can only be created with proper DB backing
Log::create(
db,
format!(
"{} created event {} on {} at {}.",
user.user.name, name, trip_details.day, trip_details.planned_starting_time
),
)
.await;
}
//TODO: create unit test
@ -449,7 +463,13 @@ WHERE trip_details.id=?
#[cfg(test)]
mod test {
use crate::{model::tripdetails::TripDetails, testdb};
use crate::{
model::{
tripdetails::TripDetails,
user::{EventUser, User},
},
testdb,
};
use super::Event;
use chrono::Local;
@ -469,7 +489,10 @@ mod test {
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
Event::create(&pool, "new-event".into(), 2, false, &trip_details).await;
let admin = EventUser::new(&pool, User::find_by_id(&pool, 1).await.unwrap())
.await
.unwrap();
Event::create(&pool, &admin, "new-event".into(), 2, false, &trip_details).await;
let res = Event::get_for_day(&pool, Local::now().date_naive()).await;
assert_eq!(res.len(), 2);

View File

@ -221,7 +221,7 @@ mod test {
notification::Notification,
trip::Trip,
tripdetails::{TripDetails, TripDetailsToAdd},
user::{SteeringUser, User},
user::{EventUser, SteeringUser, User},
usertrip::UserTrip,
},
testdb,
@ -247,7 +247,10 @@ mod test {
let trip_details = TripDetails::find_by_id(&pool, tripdetails_id)
.await
.unwrap();
Event::create(&pool, "new-event".into(), 2, false, &trip_details).await;
let user = EventUser::new(&pool, User::find_by_id(&pool, 1).await.unwrap())
.await
.unwrap();
Event::create(&pool, &user, "new-event".into(), 2, false, &trip_details).await;
let event = Event::find_by_trip_details(&pool, trip_details.id)
.await
.unwrap();

View File

@ -9,7 +9,7 @@ use super::{
notification::Notification,
tripdetails::TripDetails,
triptype::TripType,
user::{SteeringUser, User},
user::{ErgoUser, SteeringUser, User},
usertrip::UserTrip,
};
@ -38,7 +38,7 @@ pub struct TripWithUserAndType {
}
pub struct TripUpdate<'a> {
pub cox: &'a SteeringUser,
pub cox: &'a User,
pub trip: &'a Trip,
pub max_people: i32,
pub notes: Option<&'a str>,
@ -63,9 +63,23 @@ impl TripWithUserAndType {
impl Trip {
/// Cox decides to create own trip.
pub async fn new_own(db: &SqlitePool, cox: &SteeringUser, trip_details: TripDetails) {
Self::perform_new(db, &cox.user, trip_details).await
}
pub async fn new_own_ergo(db: &SqlitePool, ergo: &ErgoUser, trip_details: TripDetails) {
let typ = trip_details.triptype(db).await;
if let Some(typ) = typ {
let allowed_type = TripType::find_by_id(db, 4).await.unwrap();
if typ == allowed_type {
Self::perform_new(db, &ergo.user, trip_details).await;
}
}
}
async fn perform_new(db: &SqlitePool, user: &User, trip_details: TripDetails) {
let _ = sqlx::query!(
"INSERT INTO trip (cox_id, trip_details_id) VALUES(?, ?)",
cox.id,
user.id,
trip_details.id
)
.execute(db)
@ -96,7 +110,7 @@ impl Trip {
&user,
&format!(
"{} hat eine Ausfahrt zur selben Zeit ({} um {}) wie du erstellt",
cox.user.name, trip.day, trip.planned_starting_time
user.name, trip.day, trip.planned_starting_time
),
"Neue Ausfahrt zur selben Zeit",
None,
@ -273,6 +287,12 @@ WHERE day=?
return Err(TripUpdateError::NotYourTrip);
}
if update.trip_type != Some(4) {
if !update.cox.allowed_to_steer(db).await {
return Err(TripUpdateError::TripTypeNotAllowed);
}
}
let Some(trip_details_id) = update.trip.trip_details_id else {
return Err(TripUpdateError::TripDetailsDoesNotExist); //TODO: Remove?
};
@ -314,7 +334,7 @@ WHERE day=?
&user,
&format!(
"Die Ausfahrt von {} am {} um {} wurde abgesagt. {} Bitte gib Bescheid, dass du die Info erhalten hast indem du auf ✓ klickst.",
update.cox.user.name,
update.cox.name,
update.trip.day,
update.trip.planned_starting_time,
notes
@ -384,11 +404,7 @@ WHERE day=?
Ok(())
}
pub(crate) async fn delete(
&self,
db: &SqlitePool,
user: &SteeringUser,
) -> Result<(), TripDeleteError> {
pub(crate) async fn delete(&self, db: &SqlitePool, user: &User) -> Result<(), TripDeleteError> {
let registered_rower = Registration::all_rower(db, self.trip_details_id.unwrap()).await;
if !registered_rower.is_empty() {
return Err(TripDeleteError::SomebodyAlreadyRegistered);
@ -398,7 +414,7 @@ WHERE day=?
return Err(TripDeleteError::NotYourTrip);
}
Log::create(db, format!("{} deleted trip: {:#?}", user.user.name, self)).await;
Log::create(db, format!("{} deleted trip: {:#?}", user.name, self)).await;
sqlx::query!("DELETE FROM trip WHERE id = ?", self.id)
.execute(db)
@ -464,6 +480,7 @@ pub enum TripDeleteError {
pub enum TripUpdateError {
NotYourTrip,
TripDetailsDoesNotExist,
TripTypeNotAllowed,
}
#[cfg(test)]

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
#[derive(FromRow, Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct TripType {
pub id: i64,
pub name: String,

View File

@ -1166,6 +1166,7 @@ macro_rules! special_user {
}
special_user!(TechUser, +"tech");
special_user!(ErgoUser, +"ergo");
special_user!(SteeringUser, +"cox", +"Bootsführer");
special_user!(AdminUser, +"admin");
special_user!(AllowedForPlannedTripsUser, +"Donau Linz", +"scheckbuch");