add planned mod
This commit is contained in:
392
src/model/planned/usertrip.rs
Normal file
392
src/model/planned/usertrip.rs
Normal file
@ -0,0 +1,392 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, SqlitePool};
|
||||
|
||||
use super::{
|
||||
trip::{Trip, TripWithDetails},
|
||||
tripdetails::TripDetails,
|
||||
};
|
||||
use crate::model::{
|
||||
notification::Notification,
|
||||
planned::tripdetails::{Action, CoxAtTrip::Yes},
|
||||
user::{SteeringUser, User},
|
||||
};
|
||||
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct UserTrip {
|
||||
pub user_id: Option<i64>,
|
||||
pub user_note: Option<String>,
|
||||
pub trip_details_id: i64,
|
||||
pub created_at: String, // TODO: switch to NaiveDateTime
|
||||
}
|
||||
|
||||
impl UserTrip {
|
||||
pub async fn create(
|
||||
db: &SqlitePool,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
user_note: Option<String>,
|
||||
) -> Result<String, UserTripError> {
|
||||
if trip_details.is_full(db).await {
|
||||
return Err(UserTripError::EventAlreadyFull);
|
||||
}
|
||||
|
||||
if trip_details.is_locked {
|
||||
return Err(UserTripError::DetailsLocked);
|
||||
}
|
||||
|
||||
if user.has_role(db, "scheckbuch").await && !trip_details.allow_guests {
|
||||
return Err(UserTripError::GuestNotAllowedForThisEvent);
|
||||
}
|
||||
|
||||
if !trip_details.user_sees_trip(db, user).await {
|
||||
return Err(UserTripError::NotVisibleToUser);
|
||||
}
|
||||
|
||||
//TODO: Check if user sees the event (otherwise she could forge trip_details_id)
|
||||
|
||||
let is_cox = trip_details.user_is_cox(db, user).await;
|
||||
let name_newly_registered_person = if user_note.is_none() {
|
||||
if let Yes(action) = is_cox {
|
||||
match action {
|
||||
Action::Helping => return Err(UserTripError::AlreadyRegisteredAsCox),
|
||||
Action::Own => return Err(UserTripError::CantRegisterAtOwnEvent),
|
||||
};
|
||||
}
|
||||
|
||||
if trip_details.user_is_rower(db, user).await {
|
||||
return Err(UserTripError::AlreadyRegistered);
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
||||
user.id,
|
||||
trip_details.id,
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
user.name.clone()
|
||||
} else {
|
||||
if !trip_details.user_allowed_to_change(db, user).await {
|
||||
return Err(UserTripError::NotAllowedToAddGuest);
|
||||
}
|
||||
sqlx::query!(
|
||||
"INSERT INTO user_trip (user_note, trip_details_id) VALUES(?, ?)",
|
||||
user_note,
|
||||
trip_details.id,
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
user_note.clone().unwrap()
|
||||
};
|
||||
|
||||
if let Some(trip) = Trip::find_by_trip_details(db, trip_details.id).await {
|
||||
if user_note.is_none() {
|
||||
// Don't show notification if we add guest (as only we are
|
||||
// allowed to do so)
|
||||
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
|
||||
Notification::create(
|
||||
db,
|
||||
&cox,
|
||||
&format!(
|
||||
"{} hat sich für deine Ausfahrt am {} registriert",
|
||||
name_newly_registered_person, trip.day
|
||||
),
|
||||
"Registrierung bei deiner Ausfahrt",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
trip_details.check_free_spaces(db).await;
|
||||
}
|
||||
|
||||
Ok(name_newly_registered_person)
|
||||
}
|
||||
|
||||
pub async fn tripdetails(&self, db: &SqlitePool) -> TripDetails {
|
||||
TripDetails::find_by_id(db, self.trip_details_id)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn find_by_userid_and_trip_detail_id(
|
||||
db: &SqlitePool,
|
||||
user_id: i64,
|
||||
trip_detail_id: i64,
|
||||
) -> Option<Self> {
|
||||
sqlx::query_as!(Self, "SELECT user_id, user_note, trip_details_id, created_at FROM user_trip WHERE user_id= ? AND trip_details_id = ?", user_id, trip_detail_id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn self_delete(&self, db: &SqlitePool) -> Result<(), UserTripDeleteError> {
|
||||
let trip_details = self.tripdetails(db).await;
|
||||
if let Some(id) = self.user_id {
|
||||
let user = User::find_by_id(db, id as i32).await.unwrap();
|
||||
return Self::delete(db, &user, &trip_details, self.user_note.clone()).await;
|
||||
}
|
||||
|
||||
Ok(()) // TODO: fixme
|
||||
}
|
||||
|
||||
//TODO: cleaner code
|
||||
pub async fn delete(
|
||||
db: &SqlitePool,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
name: Option<String>,
|
||||
) -> Result<(), UserTripDeleteError> {
|
||||
if trip_details.is_locked {
|
||||
return Err(UserTripDeleteError::DetailsLocked);
|
||||
}
|
||||
|
||||
if !trip_details.user_sees_trip(db, user).await {
|
||||
return Err(UserTripDeleteError::NotVisibleToUser);
|
||||
}
|
||||
|
||||
let mut trip_to_delete = None;
|
||||
let mut some_trip = None;
|
||||
if let Some(trip) = Trip::find_by_trip_details(db, trip_details.id).await {
|
||||
some_trip = Some(trip.clone());
|
||||
// If trip is cancelled, and lost rower just unregistered, delete the trip
|
||||
if TripDetails::find_by_id(db, trip_details.id)
|
||||
.await
|
||||
.unwrap()
|
||||
.cancelled()
|
||||
{
|
||||
let trip = TripWithDetails::from(db, trip.clone()).await;
|
||||
if trip.rower.len() == 1 {
|
||||
trip_to_delete = Some(trip.trip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(name) = name.clone() {
|
||||
if !trip_details.user_allowed_to_change(db, user).await {
|
||||
return Err(UserTripDeleteError::NotAllowedToDeleteGuest);
|
||||
}
|
||||
|
||||
if sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_note = ? AND trip_details_id = ?",
|
||||
name,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.rows_affected()
|
||||
== 0
|
||||
{
|
||||
return Err(UserTripDeleteError::GuestNotParticipating);
|
||||
}
|
||||
} else {
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let mut add_info = "";
|
||||
if let Some(trip) = &trip_to_delete {
|
||||
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
|
||||
trip.delete(db, &SteeringUser::new(db, &cox).await.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
add_info = " Das war die letzte angemeldete Person. Nachdem nun alle Bescheid wissen, wird die Ausfahrt ab sofort nicht mehr angezeigt.";
|
||||
}
|
||||
|
||||
if let Some(trip) = some_trip {
|
||||
let opt_cancelled = if trip_to_delete.is_some() {
|
||||
"abgesagten "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
if let Some(name) = name {
|
||||
if !add_info.is_empty() {
|
||||
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
|
||||
Notification::create(
|
||||
db,
|
||||
&cox,
|
||||
&format!(
|
||||
"Du hast {} von deiner {}Ausfahrt am {} abgemeldet.{}",
|
||||
name, opt_cancelled, trip.day, add_info
|
||||
),
|
||||
"Abmeldung von deiner Ausfahrt",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
|
||||
Notification::create(
|
||||
db,
|
||||
&cox,
|
||||
&format!(
|
||||
"{} hat sich von deiner {}Ausfahrt am {} abgemeldet.{}",
|
||||
user.name, opt_cancelled, trip.day, add_info
|
||||
),
|
||||
"Abmeldung von deiner Ausfahrt",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripError {
|
||||
AlreadyRegistered,
|
||||
AlreadyRegisteredAsCox,
|
||||
EventAlreadyFull,
|
||||
DetailsLocked,
|
||||
CantRegisterAtOwnEvent,
|
||||
GuestNotAllowedForThisEvent,
|
||||
NotAllowedToAddGuest,
|
||||
NotVisibleToUser,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripDeleteError {
|
||||
DetailsLocked,
|
||||
GuestNotParticipating,
|
||||
NotAllowedToDeleteGuest,
|
||||
NotVisibleToUser,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
model::{
|
||||
planned::{
|
||||
event::Event, trip::Trip, tripdetails::TripDetails, usertrip::UserTripError,
|
||||
},
|
||||
user::SteeringUser,
|
||||
},
|
||||
testdb,
|
||||
};
|
||||
|
||||
use super::{User, UserTrip};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_succ_create() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_full() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
let user2 = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let user3 = User::find_by_name(&pool, "rower2".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
UserTrip::create(&pool, &user2, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user3, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because trip is already full");
|
||||
|
||||
assert_eq!(result, UserTripError::EventAlreadyFull);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_already_registered() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegistered);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_own_trip() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 2).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::CantRegisterAtOwnEvent);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_planned_event() {
|
||||
let pool = testdb!();
|
||||
|
||||
let cox = SteeringUser::new(
|
||||
&pool,
|
||||
&User::find_by_name(&pool, "cox".into()).await.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let event = Event::find_by_id(&pool, 1).await.unwrap();
|
||||
Trip::new_join(&pool, &cox, &event).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
let result = UserTrip::create(&pool, &cox, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is already registered as cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegisteredAsCox);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_guest() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "guest".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Not allowed for guests");
|
||||
|
||||
assert_eq!(result, UserTripError::GuestNotAllowedForThisEvent);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user