deletion-event-only-ok-noone-registered #513

Merged
philipp merged 5 commits from deletion-event-only-ok-noone-registered into staging 2024-05-21 18:44:49 +02:00
3 changed files with 57 additions and 21 deletions

View File

@ -119,6 +119,10 @@ ORDER BY created_at DESC
pub async fn create(db: &SqlitePool, boatdamage: BoatDamageToAdd<'_>) -> Result<(), String> {
Log::create(db, format!("New boat damage: {boatdamage:?}")).await;
let Some(boat) = Boat::find_by_id(db, boatdamage.boat_id as i32).await else {
return Err("Boot gibt's ned".into());
};
let was_unusable_before = boat.is_locked(db).await;
sqlx::query!(
"INSERT INTO boat_damage(boat_id, desc, user_id_created, lock_boat) VALUES (?,?,?, ?)",
@ -131,6 +135,11 @@ ORDER BY created_at DESC
.await
.map_err(|e| e.to_string())?;
if !was_unusable_before && boat.is_locked(db).await {
let cox = Role::find_by_name(db, "cox").await.unwrap();
Notification::create_for_role(db, &cox, &format!("Liebe Steuerberechtigte, bitte beachten, dass {} bis auf weiteres aufgrund von Reparaturarbeiten gesperrt ist.", boat.name), "Boot gesperrt", None).await;
}
let technicals =
User::all_with_role(db, &Role::find_by_name(db, "tech").await.unwrap()).await;
for technical in technicals {
@ -144,10 +153,7 @@ ORDER BY created_at DESC
.await
.unwrap()
.name,
Boat::find_by_id(db, boatdamage.boat_id as i32)
.await
.unwrap()
.name,
boat.name,
boatdamage.desc
),
"Neuer Bootsschaden angelegt",
@ -267,35 +273,41 @@ ORDER BY created_at DESC
pub async fn verified(
&self,
db: &SqlitePool,
boat: BoatDamageVerified<'_>,
boat_form: BoatDamageVerified<'_>,
) -> Result<(), String> {
if let Some(verifier) = User::find_by_id(db, boat.user_id_verified).await {
if let Some(verifier) = User::find_by_id(db, boat_form.user_id_verified).await {
if !verifier.has_role(db, "tech").await {
Log::create(db, format!("User {verifier:?} tried to verify boat {boat:?}. The user is no tech. Manually craftted request?")).await;
Log::create(db, format!("User {verifier:?} tried to verify boat {boat_form:?}. The user is no tech. Manually craftted request?")).await;
return Err("You are not allowed to verify the boat!".into());
}
} else {
Log::create(db, format!("Someone tried to verify the boat {boat:?} with user_id={} which does not exist. Manually craftted request?", boat.user_id_verified)).await;
Log::create(db, format!("Someone tried to verify the boat {boat_form:?} with user_id={} which does not exist. Manually craftted request?", boat_form.user_id_verified)).await;
return Err("Could not find user".into());
}
Log::create(db, format!("Verified boat damage: {boat:?}")).await;
let Some(boat) = Boat::find_by_id(db, self.boat_id as i32).await else {
return Err("Boot gibt's ned".into());
};
let was_unusable_before = boat.is_locked(db).await;
Log::create(db, format!("Verified boat damage: {boat_form:?}")).await;
sqlx::query!(
"UPDATE boat_damage SET desc=?, user_id_verified=?, verified_at=CURRENT_TIMESTAMP WHERE id=?",
boat.desc,
boat.user_id_verified,
boat_form.desc,
boat_form.user_id_verified,
self.id
)
.execute(db)
.await.map_err(|e| e.to_string())?;
if boat.user_id_verified != self.user_id_created as i32 {
let user_verified = User::find_by_id(db, boat.user_id_verified).await.unwrap();
if boat_form.user_id_verified != self.user_id_created as i32 {
let user_verified = User::find_by_id(db, boat_form.user_id_verified)
.await
.unwrap();
let user_created = User::find_by_id(db, self.user_id_created as i32)
.await
.unwrap();
let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap();
if user_verified.id == self.user_id_fixed.unwrap() {
Notification::create(
@ -325,6 +337,11 @@ ORDER BY created_at DESC
}
}
if was_unusable_before && !boat.is_locked(db).await {
let cox = Role::find_by_name(db, "cox").await.unwrap();
Notification::create_for_role(db, &cox, &format!("Liebe Steuerberechtigte, {} wurde repariert und freut sich ab sofort wieder gerudert zu werden :-)", boat.name), "Boot repariert", None).await;
}
Ok(())
}
}

View File

@ -240,11 +240,29 @@ INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
}
pub async fn delete(&self, db: &SqlitePool) {
pub async fn delete(&self, db: &SqlitePool) -> Result<(), String> {
if Registration::all_rower(db, self.trip_details_id)
.await
.len()
> 0
{
return Err(
"Event kann nicht gelöscht werden, weil mind. 1 Ruderer angemeldet ist.".into(),
);
}
if Registration::all_cox(db, self.trip_details_id).await.len() > 0 {
return Err(
"Event kann nicht gelöscht werden, weil mind. 1 Steuerperson angemeldet ist."
.into(),
);
}
sqlx::query!("DELETE FROM planned_event WHERE id = ?", self.id)
.execute(db)
.await
.unwrap(); //Okay, as PlannedEvent can only be created with proper DB backing
Ok(())
}
pub async fn get_ics_feed(db: &SqlitePool) -> String {

View File

@ -78,12 +78,13 @@ async fn update(
#[get("/planned-event/<id>/delete")]
async fn delete(db: &State<SqlitePool>, id: i64, _admin: PlannedEventUser) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, id).await {
Some(planned_event) => {
planned_event.delete(db).await;
Flash::success(Redirect::to("/planned"), "Event gelöscht")
}
None => Flash::error(Redirect::to("/planned"), "PlannedEvent does not exist"),
let Some(event) = PlannedEvent::find_by_id(db, id).await else {
return Flash::error(Redirect::to("/planned"), "PlannedEvent does not exist");
};
match event.delete(db).await {
Ok(()) => Flash::success(Redirect::to("/planned"), "Event gelöscht"),
Err(e) => Flash::error(Redirect::to("/planned"), e),
}
}