new newbie flag

This commit is contained in:
2026-06-04 19:49:16 +02:00
parent 0e1973fbac
commit c940ce0fdc
17 changed files with 114 additions and 28 deletions
+23 -8
View File
@@ -48,6 +48,7 @@ pub struct Registration {
pub name: String,
pub registered_at: String,
pub is_guest: bool,
pub is_newbie: bool,
pub is_real_guest: bool,
}
@@ -57,12 +58,13 @@ impl Registration {
&format!(
r#"
SELECT
(SELECT name FROM user WHERE user_trip.user_id = user.id) as "name?",
(SELECT name FROM user WHERE user_trip.user_id = user.id) as "name?",
user_note,
user_id,
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
(SELECT EXISTS (SELECT 1 FROM user_role WHERE user_role.user_id = user_trip.user_id AND user_role.role_id = (SELECT id FROM role WHERE name = 'scheckbuch'))) as is_guest
FROM user_trip WHERE trip_details_id = {}
(SELECT EXISTS (SELECT 1 FROM user_role WHERE user_role.user_id = user_trip.user_id AND user_role.role_id = (SELECT id FROM role WHERE name = 'scheckbuch'))) as is_guest,
(SELECT EXISTS (SELECT 1 FROM user_role WHERE user_role.user_id = user_trip.user_id AND user_role.role_id = (SELECT id FROM role WHERE name = 'Vereinsneuling'))) as is_newbie
FROM user_trip WHERE trip_details_id = {}
"#,trip_details_id),
)
.fetch_all(db)
@@ -74,6 +76,7 @@ FROM user_trip WHERE trip_details_id = {}
name: r.get::<Option<String>, usize>(0).or(r.get::<Option<String>, usize>(1)).unwrap(), //Ok, either name or user_note needs to be set
registered_at: r.get::<String,usize>(3),
is_guest: r.get::<bool, usize>(4),
is_newbie: r.get::<bool, usize>(5),
is_real_guest: r.get::<Option<i64>, usize>(2).is_none(),
})
.collect()
@@ -98,6 +101,7 @@ FROM trip WHERE planned_event_id = ?
name: r.name.unwrap(),
registered_at: r.registered_at.unwrap(),
is_guest: false,
is_newbie: false,
is_real_guest: false,
})
.collect() //Okay, as Event can only be created with proper DB backing
@@ -113,6 +117,7 @@ pub struct EventUpdate<'a> {
pub always_show: bool,
pub is_locked: bool,
pub trip_type_id: Option<i64>,
pub allow_guests: bool,
}
impl EventUpdate<'_> {
@@ -318,7 +323,17 @@ WHERE trip_details.id=?
}
//TODO: create unit test
pub async fn update(&self, db: &SqlitePool, user: &EventUser, update: &EventUpdate<'_>) {
pub async fn update(&self, db: &SqlitePool, user: &EventUser, update: &EventUpdate<'_>) -> Result<(), String> {
let tripdetails = self.trip_details(db).await;
let was_already_cancelled = tripdetails.cancelled();
if tripdetails.allow_guests && !update.allow_guests {
let rowers = Registration::all_rower(db, self.trip_details_id).await;
if rowers.iter().any(|r| r.is_newbie) {
return Err("Es sind bereits Neulinge angemeldet — 'Neulinge willkommen' kann nicht deaktiviert werden.".into());
}
}
sqlx::query!(
"UPDATE planned_event SET name = ?, planned_amount_cox = ? WHERE id = ?",
update.name,
@@ -329,16 +344,14 @@ WHERE trip_details.id=?
.await
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
let tripdetails = self.trip_details(db).await;
let was_already_cancelled = tripdetails.cancelled();
sqlx::query!(
"UPDATE trip_details SET max_people = ?, notes = ?, always_show = ?, is_locked = ?, trip_type_id = ? WHERE id = ?",
"UPDATE trip_details SET max_people = ?, notes = ?, always_show = ?, is_locked = ?, trip_type_id = ?, allow_guests = ? WHERE id = ?",
update.max_people,
update.notes,
update.always_show,
update.is_locked,
update.trip_type_id,
update.allow_guests,
self.trip_details_id
)
.execute(db)
@@ -426,6 +439,8 @@ WHERE trip_details.id=?
.await;
Notification::delete_by_action(db, &format!("remove_trip_by_event:{}", self.id)).await;
}
Ok(())
}
pub async fn delete(&self, db: &SqlitePool) -> Result<(), String> {
+14 -1
View File
@@ -48,6 +48,7 @@ pub struct TripUpdate<'a> {
pub notes: Option<&'a str>,
pub trip_type: Option<i64>, //TODO: Move to `TripType`
pub is_locked: bool,
pub allow_guests: bool,
}
impl TripUpdate<'_> {
@@ -228,6 +229,13 @@ WHERE day=?
let tripdetails = TripDetails::find_by_id(db, trip_details_id).await.unwrap();
let was_already_cancelled = tripdetails.cancelled();
if tripdetails.allow_guests && !update.allow_guests {
let rowers = Registration::all_rower(db, trip_details_id).await;
if rowers.iter().any(|r| r.is_newbie) {
return Err(TripUpdateError::NeulingAlreadyRegistered);
}
}
let is_locked = if update.cancelled() {
false
} else {
@@ -235,11 +243,12 @@ WHERE day=?
};
sqlx::query!(
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, is_locked = ? WHERE id = ?",
"UPDATE trip_details SET max_people = ?, notes = ?, trip_type_id = ?, is_locked = ?, allow_guests = ? WHERE id = ?",
update.max_people,
update.notes,
update.trip_type,
is_locked,
update.allow_guests,
trip_details_id
)
.execute(db)
@@ -407,6 +416,7 @@ pub enum TripUpdateError {
NotYourTrip,
TripDetailsDoesNotExist,
TripTypeNotAllowed,
NeulingAlreadyRegistered,
}
#[cfg(test)]
@@ -540,6 +550,7 @@ mod test {
notes: None,
trip_type: None,
is_locked: false,
allow_guests: false,
};
assert!(Trip::update_own(&pool, &update).await.is_ok());
@@ -568,6 +579,7 @@ mod test {
notes: None,
trip_type: Some(1),
is_locked: false,
allow_guests: false,
};
assert!(Trip::update_own(&pool, &update).await.is_ok());
@@ -596,6 +608,7 @@ mod test {
notes: None,
trip_type: None,
is_locked: false,
allow_guests: false,
};
assert!(Trip::update_own(&pool, &update).await.is_err());
assert_eq!(trip.max_people, 1);