Allow to add real guests
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::{tripdetails::TripDetails, user::User};
|
||||
use crate::model::tripdetails::{Action, CoxAtTrip::Yes};
|
||||
|
||||
pub struct UserTrip {}
|
||||
|
||||
@ -9,6 +10,7 @@ impl UserTrip {
|
||||
db: &SqlitePool,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
user_note: Option<String>,
|
||||
) -> Result<(), UserTripError> {
|
||||
if trip_details.is_full(db).await {
|
||||
return Err(UserTripError::EventAlreadyFull);
|
||||
@ -22,74 +24,84 @@ impl UserTrip {
|
||||
return Err(UserTripError::GuestNotAllowedForThisEvent);
|
||||
}
|
||||
|
||||
//TODO: Check if user sees the event (otherwise she could forge trip_details_id
|
||||
//TODO: Check if user sees the event (otherwise she could forge trip_details_id)
|
||||
|
||||
//check if cox if own event
|
||||
let is_cox = sqlx::query!(
|
||||
"SELECT count(*) as amount
|
||||
FROM trip
|
||||
WHERE trip_details_id = ?
|
||||
AND cox_id = ?",
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
if is_cox.amount > 0 {
|
||||
return Err(UserTripError::CantRegisterAtOwnEvent);
|
||||
let is_cox = trip_details.user_is_cox(db, user).await;
|
||||
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();
|
||||
} 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();
|
||||
}
|
||||
|
||||
//TODO: can probably move to trip.rs?
|
||||
//check if cox if planned_event
|
||||
let is_cox = sqlx::query!(
|
||||
"SELECT count(*) as amount
|
||||
FROM trip
|
||||
WHERE planned_event_id = (
|
||||
SELECT id FROM planned_event WHERE trip_details_id = ?
|
||||
)
|
||||
AND cox_id = ?",
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
if is_cox.amount > 0 {
|
||||
return Err(UserTripError::AlreadyRegisteredAsCox);
|
||||
}
|
||||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(UserTripError::AlreadyRegistered),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
if let Some(name) = name {
|
||||
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();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -102,11 +114,14 @@ pub enum UserTripError {
|
||||
DetailsLocked,
|
||||
CantRegisterAtOwnEvent,
|
||||
GuestNotAllowedForThisEvent,
|
||||
NotAllowedToAddGuest,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripDeleteError {
|
||||
DetailsLocked,
|
||||
GuestNotParticipating,
|
||||
NotAllowedToDeleteGuest,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -130,7 +145,9 @@ mod test {
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
@ -143,12 +160,14 @@ mod test {
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
UserTrip::create(&pool, &user2, &trip_details)
|
||||
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)
|
||||
let result = UserTrip::create(&pool, &user3, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because trip is already full");
|
||||
|
||||
@ -162,9 +181,11 @@ mod test {
|
||||
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).await.unwrap();
|
||||
UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
@ -179,7 +200,7 @@ mod test {
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 2).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
@ -196,12 +217,11 @@ mod test {
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
Trip::new_join(&pool, &cox, &planned_event).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &cox, &trip_details)
|
||||
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");
|
||||
|
||||
@ -216,7 +236,7 @@ mod test {
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
let result = UserTrip::create(&pool, &user, &trip_details, None)
|
||||
.await
|
||||
.expect_err("Not allowed for guests");
|
||||
|
||||
|
Reference in New Issue
Block a user