delete cancelled trips if all rowers have seen the notification
All checks were successful
CI/CD Pipeline / test (push) Successful in 11m21s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
2024-09-03 17:39:08 +03:00
parent 76022a1f0e
commit a441d99b5e
4 changed files with 145 additions and 31 deletions

View File

@@ -1,9 +1,21 @@
use sqlx::SqlitePool;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
use super::{notification::Notification, trip::Trip, tripdetails::TripDetails, user::User};
use super::{
notification::Notification,
trip::{Trip, TripWithUserAndType},
tripdetails::TripDetails,
user::{CoxUser, User},
};
use crate::model::tripdetails::{Action, CoxAtTrip::Yes};
pub struct UserTrip {}
#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
pub struct UserTrip {
pub user_id: Option<i64>,
pub user_note: Option<String>,
pub trip_details_id: i64,
pub created_at: String, // TODO: switch to NaiveDateTime
}
impl UserTrip {
pub async fn create(
@@ -66,23 +78,27 @@ impl UserTrip {
.await
.unwrap();
user_note.unwrap()
user_note.clone().unwrap()
};
if let Some(trip) = Trip::find_by_trip_details(db, trip_details.id).await {
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
Notification::create(
db,
&cox,
&format!(
"{} hat sich für deine Ausfahrt am {} registriert",
name_newly_registered_person, trip.day
),
"Registrierung bei deiner Ausfahrt",
None,
None,
)
.await;
if user_note.is_none() {
// Don't show notification if we add guest (as only we are
// allowed to do so)
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
Notification::create(
db,
&cox,
&format!(
"{} hat sich für deine Ausfahrt am {} registriert",
name_newly_registered_person, trip.day
),
"Registrierung bei deiner Ausfahrt",
None,
None,
)
.await;
}
trip_details.check_free_spaces(db).await;
}
@@ -90,6 +106,34 @@ impl UserTrip {
Ok(name_newly_registered_person)
}
pub async fn tripdetails(&self, db: &SqlitePool) -> TripDetails {
TripDetails::find_by_id(db, self.trip_details_id)
.await
.unwrap()
}
pub async fn find_by_userid_and_trip_detail_id(
db: &SqlitePool,
user_id: i64,
trip_detail_id: i64,
) -> Option<Self> {
sqlx::query_as!(Self, "SELECT user_id, user_note, trip_details_id, created_at FROM user_trip WHERE user_id= ? AND trip_details_id = ?", user_id, trip_detail_id)
.fetch_one(db)
.await
.ok()
}
pub async fn self_delete(&self, db: &SqlitePool) -> Result<(), UserTripDeleteError> {
let trip_details = self.tripdetails(db).await;
if let Some(id) = self.user_id {
let user = User::find_by_id(db, id as i32).await.unwrap();
return Self::delete(db, &user, &trip_details, self.user_note.clone()).await;
}
Ok(()) // TODO: fixme
}
//TODO: cleaner code
pub async fn delete(
db: &SqlitePool,
user: &User,
@@ -104,7 +148,24 @@ impl UserTrip {
return Err(UserTripDeleteError::NotVisibleToUser);
}
if let Some(name) = name {
let mut trip_to_delete = None;
let mut some_trip = None;
if let Some(trip) = Trip::find_by_trip_details(db, trip_details.id).await {
some_trip = Some(trip.clone());
// If trip is cancelled, and lost rower just unregistered, delete the trip
if TripDetails::find_by_id(db, trip_details.id)
.await
.unwrap()
.cancelled()
{
let trip = TripWithUserAndType::from(db, trip.clone()).await;
if trip.rower.len() == 1 {
trip_to_delete = Some(trip.trip);
}
}
}
if let Some(name) = name.clone() {
if !trip_details.user_allowed_to_change(db, user).await {
return Err(UserTripDeleteError::NotAllowedToDeleteGuest);
}
@@ -132,6 +193,55 @@ impl UserTrip {
.await
.unwrap();
}
let mut add_info = "";
if let Some(trip) = &trip_to_delete {
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
trip.delete(db, &CoxUser::new(db, cox).await.unwrap())
.await
.unwrap();
add_info = " Das war die letzte angemeldete Person. Nachdem nun alle Bescheid wissen, wird die Ausfahrt ab sofort nicht mehr angezeigt.";
}
if let Some(trip) = some_trip {
let opt_cancelled = if trip_to_delete.is_some() {
"abgesagten "
} else {
""
};
if let Some(name) = name {
if !add_info.is_empty() {
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
Notification::create(
db,
&cox,
&format!(
"Du hast {} von deiner {}Ausfahrt am {} abgemeldet.{}",
name, opt_cancelled, trip.day, add_info
),
"Abmeldung von deiner Ausfahrt",
None,
None,
)
.await;
}
} else {
let cox = User::find_by_id(db, trip.cox_id as i32).await.unwrap();
Notification::create(
db,
&cox,
&format!(
"{} hat sich von deiner {}Ausfahrt am {} abgemeldet.{}",
user.name, opt_cancelled, trip.day, add_info
),
"Abmeldung von deiner Ausfahrt",
None,
None,
)
.await;
}
}
Ok(())
}
}