notification (#282)
All checks were successful
CI/CD Pipeline / test (push) Successful in 8m58s
CI/CD Pipeline / deploy-staging (push) Successful in 4m18s
CI/CD Pipeline / deploy-main (push) Has been skipped

Reviewed-on: #282
This commit was merged in pull request #282.
This commit is contained in:
2024-03-20 16:19:12 +01:00
parent 68a1153885
commit 9d14dae4a7
12 changed files with 274 additions and 22 deletions

View File

@@ -3,21 +3,22 @@ use serde::Serialize;
use sqlx::SqlitePool;
use super::{
notification::Notification,
planned_event::{PlannedEvent, Registration},
tripdetails::TripDetails,
triptype::TripType,
user::CoxUser,
user::{CoxUser, User},
};
#[derive(Serialize, Clone, Debug)]
pub struct Trip {
id: i64,
cox_id: i64,
pub cox_id: i64,
cox_name: String,
trip_details_id: Option<i64>,
planned_starting_time: String,
pub max_people: i64,
day: String,
pub day: String,
pub notes: Option<String>,
pub allow_guests: bool,
trip_type_id: Option<i64>,
@@ -43,6 +44,51 @@ impl Trip {
)
.execute(db)
.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> {
sqlx::query_as!(
Self,
"
SELECT trip.id, cox_id, user.name as cox_name, trip_details_id, planned_starting_time, max_people, day, trip_details.notes, allow_guests, trip_type_id, always_show, is_locked
FROM trip
INNER JOIN trip_details ON trip.trip_details_id = trip_details.id
INNER JOIN user ON trip.cox_id = user.id
WHERE trip_details.id=?
",
tripdetails_id
)
.fetch_one(db)
.await
.ok()
}
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {