notification #292
| @@ -3,10 +3,11 @@ use serde::Serialize; | |||||||
| use sqlx::SqlitePool; | use sqlx::SqlitePool; | ||||||
|  |  | ||||||
| use super::{ | use super::{ | ||||||
|  |     notification::Notification, | ||||||
|     planned_event::{PlannedEvent, Registration}, |     planned_event::{PlannedEvent, Registration}, | ||||||
|     tripdetails::TripDetails, |     tripdetails::TripDetails, | ||||||
|     triptype::TripType, |     triptype::TripType, | ||||||
|     user::CoxUser, |     user::{CoxUser, User}, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #[derive(Serialize, Clone, Debug)] | #[derive(Serialize, Clone, Debug)] | ||||||
| @@ -43,6 +44,34 @@ impl Trip { | |||||||
|         ) |         ) | ||||||
|         .execute(db) |         .execute(db) | ||||||
|         .await; |         .await; | ||||||
|  |  | ||||||
|  |         let same_starting_datetime = TripDetails::find_by_startingdatetime( | ||||||
|  |             db, | ||||||
|  |             trip_details.day, | ||||||
|  |             trip_details.planned_starting_time, | ||||||
|  |         ) | ||||||
|  |         .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", | ||||||
|  |                                 user.name, trip.day, trip.planned_starting_time | ||||||
|  |                             ), | ||||||
|  |                             "Neue Ausfahrt zur selben Zeit".into(), | ||||||
|  |                             None, | ||||||
|  |                         ) | ||||||
|  |                         .await; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub async fn find_by_trip_details(db: &SqlitePool, tripdetails_id: i64) -> Option<Self> { |     pub async fn find_by_trip_details(db: &SqlitePool, tripdetails_id: i64) -> Option<Self> { | ||||||
|   | |||||||
| @@ -46,6 +46,24 @@ WHERE id like ? | |||||||
|         .ok() |         .ok() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     pub async fn find_by_startingdatetime( | ||||||
|  |         db: &SqlitePool, | ||||||
|  |         day: String, | ||||||
|  |         planned_starting_time: String, | ||||||
|  |     ) -> Vec<Self> { | ||||||
|  |         sqlx::query_as!( | ||||||
|  |             Self, | ||||||
|  |             " | ||||||
|  | SELECT id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked | ||||||
|  | FROM trip_details  | ||||||
|  | WHERE day = ? AND planned_starting_time = ? | ||||||
|  |         " | ||||||
|  |         , day, planned_starting_time | ||||||
|  |         ) | ||||||
|  |         .fetch_all(db) | ||||||
|  |         .await.unwrap() | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /// Creates a new entry in `trip_details` and returns its id. |     /// Creates a new entry in `trip_details` and returns its id. | ||||||
|     pub async fn create(db: &SqlitePool, tripdetails: TripDetailsToAdd<'_>) -> i64 { |     pub async fn create(db: &SqlitePool, tripdetails: TripDetailsToAdd<'_>) -> i64 { | ||||||
|         let query = sqlx::query!( |         let query = sqlx::query!( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user