only allow guests to register for specific events; don't even show them the other trips

This commit is contained in:
2023-04-29 18:57:01 +02:00
parent 5b06eaeebc
commit bb4dba7bc9
12 changed files with 96 additions and 29 deletions

View File

@ -30,4 +30,21 @@ impl Day {
trips: Trip::get_for_day(db, day).await,
}
}
pub async fn new_guest(db: &SqlitePool, day: NaiveDate) -> Self {
let mut day = Self::new(db, day).await;
day.planned_events = day
.planned_events
.into_iter()
.filter(|e| e.planned_event.allow_guests)
.collect();
day.trips = day
.trips
.into_iter()
.filter(|t| t.trip.allow_guests)
.collect();
day
}
}

View File

@ -9,19 +9,19 @@ pub struct PlannedEvent {
pub id: i64,
name: String,
planned_amount_cox: i64,
allow_guests: bool,
trip_details_id: i64,
planned_starting_time: String,
max_people: i64,
day: String,
notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
}
#[derive(Serialize)]
pub struct PlannedEventWithUserAndTriptype {
#[serde(flatten)]
planned_event: PlannedEvent,
pub planned_event: PlannedEvent,
trip_type: Option<TripType>,
cox_needed: bool,
cox: Vec<Registration>,
@ -42,7 +42,7 @@ impl PlannedEvent {
Self,
"
SELECT
planned_event.id, planned_event.name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE planned_event.id like ?
@ -61,7 +61,7 @@ WHERE planned_event.id like ?
let day = format!("{day}");
let events = sqlx::query_as!(
PlannedEvent,
"SELECT planned_event.id, planned_event.name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE day=?",
@ -127,12 +127,13 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
db: &SqlitePool,
name: String,
planned_amount_cox: i32,
allow_guests: bool,
trip_details: TripDetails,
) {
sqlx::query!(
"INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)",
name, planned_amount_cox, allow_guests, trip_details.id
"INSERT INTO planned_event(name, planned_amount_cox, trip_details_id) VALUES(?, ?, ?)",
name,
planned_amount_cox,
trip_details.id
)
.execute(db)
.await
@ -170,7 +171,7 @@ mod test {
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
PlannedEvent::create(&pool, "new-event".into(), 2, false, trip_details).await;
PlannedEvent::create(&pool, "new-event".into(), 2, trip_details).await;
let res =
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;

View File

@ -19,13 +19,14 @@ pub struct Trip {
max_people: i64,
day: String,
notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
}
#[derive(Serialize)]
pub struct TripWithUserAndType {
#[serde(flatten)]
trip: Trip,
pub trip: Trip,
rower: Vec<Registration>,
trip_type: Option<TripType>,
}
@ -47,7 +48,7 @@ impl Trip {
sqlx::query_as!(
Self,
"
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
FROM trip
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
INNER JOIN user ON trip.cox_id = user.id
@ -100,7 +101,7 @@ WHERE trip.id=?
let trips = sqlx::query_as!(
Trip,
"
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
FROM trip
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
INNER JOIN user ON trip.cox_id = user.id

View File

@ -8,6 +8,7 @@ pub struct TripDetails {
max_people: i64,
day: String,
notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
}
@ -16,7 +17,7 @@ impl TripDetails {
sqlx::query_as!(
TripDetails,
"
SELECT id, planned_starting_time, max_people, day, notes, trip_type_id
SELECT id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
FROM trip_details
WHERE id like ?
",
@ -34,14 +35,16 @@ WHERE id like ?
max_people: i32,
day: String,
notes: Option<String>,
allow_guests: bool,
trip_type_id: Option<i64>,
) -> i64 {
let query = sqlx::query!(
"INSERT INTO trip_details(planned_starting_time, max_people, day, notes, trip_type_id) VALUES(?, ?, ?, ?, ?)" ,
"INSERT INTO trip_details(planned_starting_time, max_people, day, notes, allow_guests, trip_type_id) VALUES(?, ?, ?, ?, ?, ?)" ,
planned_starting_time,
max_people,
day,
notes,
allow_guests,
trip_type_id
)
.execute(db)
@ -97,11 +100,29 @@ mod test {
let pool = testdb!();
assert_eq!(
TripDetails::create(&pool, "10:00".into(), 2, "1970-01-01".into(), None, None).await,
TripDetails::create(
&pool,
"10:00".into(),
2,
"1970-01-01".into(),
None,
false,
None
)
.await,
3,
);
assert_eq!(
TripDetails::create(&pool, "10:00".into(), 2, "1970-01-01".into(), None, None).await,
TripDetails::create(
&pool,
"10:00".into(),
2,
"1970-01-01".into(),
None,
false,
None
)
.await,
4,
);
}

View File

@ -17,7 +17,7 @@ pub struct User {
pw: Option<String>,
pub is_cox: bool,
pub is_admin: bool,
is_guest: bool,
pub is_guest: bool,
#[serde(default = "bool::default")]
deleted: bool,
}

View File

@ -14,6 +14,10 @@ impl UserTrip {
return Err(UserTripError::EventAlreadyFull);
}
if user.is_guest && !trip_details.allow_guests {
return Err(UserTripError::GuestNotAllowedForThisEvent);
}
//check if cox if own event
let is_cox = sqlx::query!(
"SELECT count(*) as amount
@ -80,6 +84,7 @@ pub enum UserTripError {
AlreadyRegisteredAsCox,
EventAlreadyFull,
CantRegisterAtOwnEvent,
GuestNotAllowedForThisEvent,
}
#[cfg(test)]
@ -180,4 +185,19 @@ mod test {
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)
.await
.expect_err("Not allowed for guests");
assert_eq!(result, UserTripError::GuestNotAllowedForThisEvent);
}
}