ernst + board got rowingbadge notification on every trip of ernst, bc last 'wanderfahrt' was removed
Some checks failed
CI/CD Pipeline / test (push) Has been cancelled
CI/CD Pipeline / deploy-staging (push) Has been cancelled
CI/CD Pipeline / deploy-main (push) Has been cancelled

This commit is contained in:
2025-05-17 16:23:24 +02:00
parent 129c90f1aa
commit 5202060e2f
3 changed files with 34 additions and 42 deletions

View File

@ -2,7 +2,7 @@ use std::cmp;
use chrono::{Datelike, Local, NaiveDate};
use serde::Serialize;
use sqlx::{Sqlite, SqlitePool, Transaction};
use sqlx::{Acquire, Sqlite, SqlitePool, Transaction};
use crate::model::{
logbook::{Filter, Logbook, LogbookWithBoatAndRowers},
@ -141,11 +141,7 @@ impl Status {
}
}
pub(crate) async fn for_user_tx(
db: &mut Transaction<'_, Sqlite>,
user: &User,
exclude_last_log: bool,
) -> Option<Self> {
pub(crate) async fn for_user_tx(db: &mut Transaction<'_, Sqlite>, user: &User) -> Option<Self> {
let Ok(agebracket) = AgeBracket::try_from(user) else {
return None;
};
@ -164,7 +160,6 @@ impl Status {
agebracket.required_dist_single_day_in_km(),
year,
Filter::SingleDayOnly,
exclude_last_log,
)
.await;
let multi_day_trips_over_required_distance =
@ -174,7 +169,6 @@ impl Status {
agebracket.required_dist_multi_day_in_km(),
year,
Filter::MultiDayOnly,
exclude_last_log,
)
.await;
@ -195,7 +189,7 @@ impl Status {
pub(crate) async fn for_user(db: &SqlitePool, user: &User) -> Option<Self> {
let mut tx = db.begin().await.unwrap();
let ret = Self::for_user_tx(&mut tx, user, false).await;
let ret = Self::for_user_tx(&mut tx, user).await;
tx.commit().await.unwrap();
ret
}
@ -204,11 +198,19 @@ impl Status {
db: &mut Transaction<'_, Sqlite>,
user: &User,
) -> bool {
if let Some(status) = Self::for_user_tx(db, user, false).await {
if let Some(status) = Self::for_user_tx(db, user).await {
// if user has agebracket...
if status.achieved {
// ... and has achieved the 'Fahrtenabzeichen'
let without_last_entry = Self::for_user_tx(db, user, true).await.unwrap();
let mut without_last = db.begin().await.unwrap();
let last = Logbook::completed_with_user_tx(&mut without_last, user).await;
let last = last.last().unwrap();
sqlx::query!("DELETE FROM logbook WHERE id=?", last.logbook.id)
.execute(&mut *without_last)
.await
.unwrap(); //Okay, because we can only create a Logbook of a valid id
let without_last_entry = Self::for_user_tx(&mut without_last, user).await.unwrap();
if !without_last_entry.achieved {
// ... and this wasn't the case before the last logentry
return true;