reduce amount of magic values, goal is to have specific states -> e.g. cancelled
Some checks failed
CI/CD Pipeline / test (push) Successful in 16m32s
CI/CD Pipeline / deploy-staging (push) Has been cancelled
CI/CD Pipeline / deploy-main (push) Has been cancelled

This commit is contained in:
2025-04-18 23:01:17 +02:00
parent f98963a28a
commit 10740f988d
6 changed files with 44 additions and 29 deletions

View File

@@ -46,6 +46,12 @@ pub struct TripUpdate<'a> {
pub is_locked: bool,
}
impl<'a> TripUpdate<'a> {
fn cancelled(&self) -> bool {
self.max_people == -1
}
}
impl TripWithUserAndType {
pub async fn from(db: &SqlitePool, trip: Trip) -> Self {
let mut trip_type = None;
@@ -245,7 +251,7 @@ WHERE trip.id=?
return Err(CoxHelpError::DetailsLocked);
}
if event.max_people == 0 {
if event.is_cancelled() {
return Err(CoxHelpError::CanceledEvent);
}
@@ -309,9 +315,9 @@ WHERE day=?
};
let tripdetails = TripDetails::find_by_id(db, trip_details_id).await.unwrap();
let was_already_cancelled = tripdetails.max_people == 0;
let was_already_cancelled = tripdetails.cancelled();
let is_locked = if update.max_people == 0 {
let is_locked = if update.cancelled() {
false
} else {
update.is_locked
@@ -329,7 +335,7 @@ WHERE day=?
.await
.unwrap(); //Okay, as trip_details can only be created with proper DB backing
if update.max_people == 0 && !was_already_cancelled {
if update.cancelled() && !was_already_cancelled {
let rowers = TripWithUserAndType::from(db, update.trip.clone())
.await
.rower;
@@ -368,7 +374,7 @@ WHERE day=?
.await;
}
if update.max_people > 0 && was_already_cancelled {
if !update.cancelled() && was_already_cancelled {
Notification::delete_by_action(
db,
&format!("remove_user_trip_with_trip_details_id:{}", trip_details_id),
@@ -463,7 +469,7 @@ WHERE day=?
}
fn is_cancelled(&self) -> bool {
self.max_people == 0
self.max_people == -1
}
}