diff --git a/src/model/trip.rs b/src/model/trip.rs index 393d119..47d65d9 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -77,23 +77,30 @@ impl Trip { .await; if same_starting_datetime.len() > 1 { for notify in same_starting_datetime { - if notify.id != trip_details.id { - // notify everyone except oneself - if let Some(trip) = Trip::find_by_trip_details(db, notify.id).await { - let user = User::find_by_id(db, trip.cox_id as i32).await.unwrap(); - Notification::create( - db, - &user, - &format!( - "{} hat eine Ausfahrt zur selben Zeit ({} um {}) wie du erstellt", - cox.user.name, trip.day, trip.planned_starting_time - ), - "Neue Ausfahrt zur selben Zeit", - None, - None, - ) - .await; - } + // don't notify oneself + if notify.id == trip_details.id { + continue; + } + + // don't notify people who have cancelled their trip + if notify.cancelled(db) { + continue; + } + + if let Some(trip) = Trip::find_by_trip_details(db, notify.id).await { + let user = User::find_by_id(db, trip.cox_id as i32).await.unwrap(); + Notification::create( + db, + &user, + &format!( + "{} hat eine Ausfahrt zur selben Zeit ({} um {}) wie du erstellt", + cox.user.name, trip.day, trip.planned_starting_time + ), + "Neue Ausfahrt zur selben Zeit", + None, + None, + ) + .await; } } } diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index 1afda2d..5e420ad 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -77,6 +77,10 @@ WHERE day = ? AND planned_starting_time = ? .await.unwrap() } + pub fn cancelled(&self, db: &SqlitePool) -> bool { + self.max_people == 0 + } + /// This function is called when a person registers to a trip or when the cox changes the /// amount of free places. pub async fn check_free_spaces(&self, db: &SqlitePool) { @@ -85,7 +89,7 @@ WHERE day = ? AND planned_starting_time = ? return; } - if self.max_people == 0 { + if self.cancelled(db) { // Cox cancelled event, thus it's probably bad weather. Don't bother with sending // notifications return;