Fill acitivites from various activities; Fixes #972
Some checks failed
CI/CD Pipeline / test (push) Failing after 11s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
2025-05-04 18:38:14 +02:00
parent 8777ccb341
commit e853381bd7
12 changed files with 349 additions and 258 deletions

View File

@ -2,7 +2,7 @@
use super::{AllowedToEditPaymentStatusUser, ManageUserUser, User};
use crate::model::{
activity::Activity, family::Family, log::Log, mail::valid_mails, notification::Notification,
activity::ActivityBuilder, family::Family, mail::valid_mails, notification::Notification,
role::Role,
};
use chrono::NaiveDate;
@ -19,13 +19,10 @@ impl User {
) -> Result<(), String> {
let note = note.trim();
Activity::create(
db,
&format!("({updated_by}) {note}"),
&format!("user-{};", user.id),
None,
)
.await;
ActivityBuilder::new(&format!("({updated_by}) {note}"))
.relevant_for_user(&user)
.save(db)
.await;
Ok(())
}
@ -50,12 +47,18 @@ impl User {
.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.mail {
Some(old_mail) => format!(
"{updated_by} has changed the mail address of {self} from {old_mail} to {new_mail}"
),
None => format!("{updated_by} has added a mail address for {self}: {new_mail}"),
Some(old_mail) => {
format!("{updated_by} hat die Mail-Adresse von {self} von {old_mail} auf {new_mail} geändert.")
}
None => {
format!("{updated_by} eine neue Mail-Adresse für {self} hinzugefügt: {new_mail}")
}
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
Ok(())
}
@ -84,11 +87,15 @@ impl User {
query.execute(db).await.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.phone {
Some(old_phone) if new_phone.is_empty() => format!("{updated_by} has removed the phone number of {self} (old number: {old_phone})"),
Some(old_phone) => format!("{updated_by} has changed the phone number of {self} from {old_phone} to {new_phone}"),
None => format!("{updated_by} has added a phone number for {self}: {new_phone}")
Some(old_phone) if new_phone.is_empty() => format!("{updated_by} hat die Telefonnummer von {self} entfernt (alte Nummer: {old_phone})"),
Some(old_phone) => format!("{updated_by} hat die Telefonnummer von {self} von {old_phone} auf {new_phone} geändert."),
None => format!("{updated_by} hat eine neue Telefonnummer für {self} hinzugefügt: {new_phone}")
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
}
pub(crate) async fn update_address(
@ -119,11 +126,15 @@ impl User {
query.execute(db).await.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.address {
Some(old_address) if new_address.is_empty() => format!("{updated_by} has removed the address of {self} (old address: {old_address})"),
Some(old_address) => format!("{updated_by} has changed the address of {self} from {old_address} to {new_address}"),
None => format!("{updated_by} has added an address for {self}: {new_address}")
Some(old_address) if new_address.is_empty() => format!("{updated_by} hat die Adresse von {self} entfernt (alte Adresse: {old_address})"),
Some(old_address) => format!("{updated_by} hat die Adresse von {self} von {old_address} auf {new_address} geändert."),
None => format!("{updated_by} hat eine Adresse für {self} hinzugefügt: {new_address}")
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
}
pub(crate) async fn update_nickname(
@ -146,11 +157,14 @@ impl User {
query.execute(db).await.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.nickname {
Some(old_nickname) if new_nickname.is_empty() => format!("{updated_by} has removed the nickname of {self} (old nickname: {old_nickname})"),
Some(old_nickname) => format!("{updated_by} has changed the nickname of {self} from {old_nickname} to {new_nickname}"),
None => format!("{updated_by} has added a nickname for {self}: {new_nickname}")
Some(old_nickname) if new_nickname.is_empty() => format!("{updated_by} hat den Sitznamen von {self} entfernt (alter Spitzname: {old_nickname})"),
Some(old_nickname) => format!("{updated_by} hat den Spitznamen von {self} von {old_nickname} auf {new_nickname} geändert."),
None => format!("{updated_by} hat einen neuen Spitznamen für {self} hinzugefügt: {new_nickname}")
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
Ok(())
}
@ -171,10 +185,14 @@ impl User {
.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.member_since_date {
Some(old_member_since_date) => format!("{updated_by} has changed the member_since date of {self} from {old_member_since_date} to {new_member_since_date}"),
None => format!("{updated_by} has added a member_since_date for {self}: {new_member_since_date}")
Some(old_member_since_date) => format!("{updated_by} hat das Beitrittsdatum von {self} von {old_member_since_date} auf {new_member_since_date} geändert."),
None => format!("{updated_by} hat ein neues Beitrittsdatum für {self} hinzugefügt: {new_member_since_date}")
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
}
pub(crate) async fn update_birthdate(
@ -193,10 +211,14 @@ impl User {
.unwrap(); //Okay, because we can only create a User of a valid id
let msg = match &self.birthdate{
Some(old_birthdate) => format!("{updated_by} has changed the birthdate of {self} from {old_birthdate} to {new_birthdate}"),
None => format!("{updated_by} has added a birthdate for {self}: {new_birthdate}")
Some(old_birthdate) => format!("{updated_by} hat das Geburtsdatum von {self} von {old_birthdate} auf {new_birthdate} geändert."),
None => format!("{updated_by} hat ein Geburtsdatum für {self} hinzugefügt: {new_birthdate}")
};
Log::create(db, msg).await;
ActivityBuilder::new(&msg)
.relevant_for_user(self)
.save(db)
.await;
}
pub(crate) async fn update_family(
@ -215,20 +237,26 @@ impl User {
.execute(db)
.await
.unwrap();
ActivityBuilder::new(&format!(
"{updated_by} hat {self} zu einer Familie hinzugefügt."
))
.relevant_for_user(self)
.save(db)
.await;
} else {
sqlx::query!("UPDATE user SET family_id = NULL where id = ?", self.id)
.execute(db)
.await
.unwrap();
ActivityBuilder::new(&format!(
"{updated_by} hat die Familienzugehörigkeit von {self} gelöscht."
))
.relevant_for_user(self)
.save(db)
.await;
};
Family::clean_families_without_members(db).await;
Log::create(
db,
format!("{updated_by} hat die Familie von {self} aktualisiert."),
)
.await;
}
pub(crate) async fn change_skill(
@ -257,6 +285,10 @@ impl User {
None,
)
.await;
ActivityBuilder::new(&format!("{updated_by} hat {self} zur Steuerperson gemacht"))
.relevant_for_user(self)
.save(db)
.await;
}
(old, new) if old == Some(cox.clone()) && new == Some(bootsfuehrer.clone()) => {
self.remove_role(db, updated_by, &cox).await?;
@ -272,6 +304,10 @@ impl User {
None,
)
.await;
ActivityBuilder::new(&format!("{updated_by} hat {self} zum Bootsführer gemacht"))
.relevant_for_user(self)
.save(db)
.await;
}
(old, new) if new == None => {
if let Some(old) = old {
@ -286,6 +322,10 @@ impl User {
None,
)
.await;
ActivityBuilder::new(&format!("{updated_by} hat {self} zum normalen Mitlgied gemacht (keine Steuerperson/Schiffsführer mehr)"))
.relevant_for_user(self)
.save(db)
.await;
}
}
(old, new) => return Err(format!("Not allowed to change from {old:?} to {new:?}")),
@ -317,12 +357,11 @@ impl User {
new.push_str("Keine Ermäßigung");
}
Activity::create(
db,
&format!("({updated_by}) Ermäßigung von {self} von {old} auf {new} geändert"),
&format!("user-{};", self.id),
None,
)
ActivityBuilder::new(&format!(
"{updated_by} hat die Ermäßigung von {self} von {old} auf {new} geändert"
))
.relevant_for_user(&self)
.save(db)
.await;
Ok(())
@ -347,10 +386,11 @@ impl User {
.await
.unwrap();
Log::create(
db,
format!("{updated_by} has removed role {role} from user {self}"),
)
ActivityBuilder::new(&format!(
"{updated_by} hat die Rolle {role} von {self} entfernt."
))
.relevant_for_user(&self)
.save(db)
.await;
Ok(())
@ -372,10 +412,11 @@ impl User {
.await
.unwrap();
Log::create(
db,
format!("{updated_by} has set that user {self} has NOT paid the fee (yet)"),
)
ActivityBuilder::new(&format!(
"{updated_by} hat den Bezahlstatus von {self} auf 'nicht bezahlt' gesetzt."
))
.relevant_for_user(&self)
.save(db)
.await;
}
pub(crate) async fn has_paid(
@ -394,10 +435,11 @@ impl User {
.await
.expect("paid role has no group");
Log::create(
db,
format!("{updated_by} has set that user {self} has paid the fee (yet)"),
)
ActivityBuilder::new(&format!(
"{updated_by} hat den Bezahlstatus von {self} auf 'bezahlt' gesetzt."
))
.relevant_for_user(&self)
.save(db)
.await;
}
@ -427,10 +469,11 @@ impl User {
)
})?;
Log::create(
db,
format!("{updated_by} has added role {role} to user {self}"),
)
ActivityBuilder::new(&format!(
"{updated_by} hat die Rolle '{role}' dem Benutzer {self} hinzugefügt."
))
.relevant_for_user(&self)
.save(db)
.await;
Ok(())
@ -461,10 +504,11 @@ impl User {
.await
.unwrap(); //Okay, because we can only create a User of a valid id
Log::create(
db,
format!("{updated_by} has added the membership pdf for user {self}"),
)
ActivityBuilder::new(&format!(
"{updated_by} hat die Mitgliedserklärung (PDF) für user {self} hinzugefügt."
))
.relevant_for_user(&self)
.save(db)
.await;
Ok(())