rowt/src/model/usertrip.rs

269 lines
7.9 KiB
Rust
Raw Normal View History

2023-04-04 15:16:21 +02:00
use sqlx::SqlitePool;
2024-04-15 18:24:18 +02:00
use super::{notification::Notification, trip::Trip, tripdetails::TripDetails, user::User};
2023-08-09 22:30:37 +02:00
use crate::model::tripdetails::{Action, CoxAtTrip::Yes};
2023-04-04 15:16:21 +02:00
pub struct UserTrip {}
impl UserTrip {
pub async fn create(
db: &SqlitePool,
2023-04-28 19:08:17 +02:00
user: &User,
trip_details: &TripDetails,
2023-08-09 22:30:37 +02:00
user_note: Option<String>,
2024-03-29 21:46:15 +01:00
) -> Result<String, UserTripError> {
2023-04-26 12:21:30 +02:00
if trip_details.is_full(db).await {
return Err(UserTripError::EventAlreadyFull);
}
2023-08-09 11:54:18 +02:00
if trip_details.is_locked {
return Err(UserTripError::DetailsLocked);
}
if user.has_role(db, "scheckbuch").await && !trip_details.allow_guests {
return Err(UserTripError::GuestNotAllowedForThisEvent);
}
2023-08-09 22:30:37 +02:00
//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;
2024-04-15 23:26:52 +02:00
let name_newly_registered_person = if user_note.is_none() {
2023-08-09 22:30:37 +02:00
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();
2024-04-15 23:26:52 +02:00
user.name.clone()
2023-08-09 22:30:37 +02:00
} 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();
2024-04-15 23:26:52 +02:00
user_note.unwrap()
};
if let Some(trip) = Trip::find_by_trip_details(db, trip_details.id).await {
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
),
2024-03-21 19:38:47 +01:00
"Registrierung bei deiner Ausfahrt",
None,
None,
)
.await;
trip_details.check_free_spaces(db).await;
2023-04-06 18:48:18 +02:00
}
2024-03-29 21:46:15 +01:00
Ok(name_newly_registered_person)
2023-04-04 15:16:21 +02:00
}
2023-08-09 11:54:18 +02:00
pub async fn delete(
db: &SqlitePool,
user: &User,
trip_details: &TripDetails,
2023-08-09 22:30:37 +02:00
name: Option<String>,
2023-08-09 11:54:18 +02:00
) -> Result<(), UserTripDeleteError> {
if trip_details.is_locked {
return Err(UserTripDeleteError::DetailsLocked);
}
2023-08-09 22:30:37 +02:00
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();
}
2023-08-09 11:54:18 +02:00
Ok(())
2023-04-04 15:16:21 +02:00
}
}
2023-04-28 19:08:17 +02:00
#[derive(Debug, PartialEq)]
pub enum UserTripError {
AlreadyRegistered,
AlreadyRegisteredAsCox,
EventAlreadyFull,
2023-08-09 11:54:18 +02:00
DetailsLocked,
2023-04-28 19:08:17 +02:00
CantRegisterAtOwnEvent,
GuestNotAllowedForThisEvent,
2023-08-09 22:30:37 +02:00
NotAllowedToAddGuest,
}
2023-04-26 16:54:53 +02:00
2023-08-09 11:54:18 +02:00
#[derive(Debug, PartialEq)]
pub enum UserTripDeleteError {
DetailsLocked,
2023-08-09 22:30:37 +02:00
GuestNotParticipating,
NotAllowedToDeleteGuest,
2023-08-09 11:54:18 +02:00
}
2023-04-26 16:54:53 +02:00
#[cfg(test)]
mod test {
2023-04-28 19:08:17 +02:00
use crate::{
model::{
2024-05-28 09:08:48 +02:00
event::Event, trip::Trip, tripdetails::TripDetails, user::CoxUser,
2023-04-28 19:08:17 +02:00
usertrip::UserTripError,
},
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();
2023-08-09 22:30:37 +02:00
UserTrip::create(&pool, &user, &trip_details, None)
.await
.unwrap();
2023-04-28 19:08:17 +02:00
}
#[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();
2023-08-09 22:30:37 +02:00
UserTrip::create(&pool, &user, &trip_details, None)
.await
.unwrap();
UserTrip::create(&pool, &user2, &trip_details, None)
2023-04-28 19:08:17 +02:00
.await
.unwrap();
2023-08-09 22:30:37 +02:00
let result = UserTrip::create(&pool, &user3, &trip_details, None)
2023-04-28 19:08:17 +02:00
.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();
2023-04-26 16:54:53 +02:00
2023-08-09 22:30:37 +02:00
UserTrip::create(&pool, &user, &trip_details, None)
.await
.unwrap();
2023-04-26 16:54:53 +02:00
2023-08-09 22:30:37 +02:00
let result = UserTrip::create(&pool, &user, &trip_details, None)
2023-04-28 19:08:17 +02:00
.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();
2023-08-09 22:30:37 +02:00
let result = UserTrip::create(&pool, &user, &trip_details, None)
2023-04-28 19:08:17 +02:00
.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 = CoxUser::new(
&pool,
User::find_by_name(&pool, "cox".into()).await.unwrap(),
)
.await
.unwrap();
2023-04-28 19:08:17 +02:00
2024-05-28 09:08:48 +02:00
let event = Event::find_by_id(&pool, 1).await.unwrap();
Trip::new_join(&pool, &cox, &event).await.unwrap();
2023-04-28 19:08:17 +02:00
2023-08-09 22:30:37 +02:00
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
let result = UserTrip::create(&pool, &cox, &trip_details, None)
2023-04-28 19:08:17 +02:00
.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();
2023-08-09 22:30:37 +02:00
let result = UserTrip::create(&pool, &user, &trip_details, None)
.await
.expect_err("Not allowed for guests");
assert_eq!(result, UserTripError::GuestNotAllowedForThisEvent);
}
2023-04-26 16:54:53 +02:00
}