forked from Ruderverein-Donau-Linz/rowt
be able to update data individually; Fixes #952
This commit is contained in:
@ -7,7 +7,7 @@ use super::user::User;
|
||||
|
||||
#[derive(FromRow, Serialize, Clone)]
|
||||
pub struct Family {
|
||||
id: i64,
|
||||
pub(crate) id: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
@ -91,4 +91,18 @@ GROUP BY family.id;"
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn clean_families_without_members(db: &SqlitePool) {
|
||||
sqlx::query(
|
||||
"DELETE FROM family
|
||||
WHERE id NOT IN (
|
||||
SELECT DISTINCT family_id
|
||||
FROM user
|
||||
WHERE family_id IS NOT NULL
|
||||
);",
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ pub struct TripUpdate<'a> {
|
||||
pub is_locked: bool,
|
||||
}
|
||||
|
||||
impl<'a> TripUpdate<'a> {
|
||||
impl TripUpdate<'_> {
|
||||
fn cancelled(&self) -> bool {
|
||||
self.max_people == -1
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
// TODO: put back in `src/model/user/mod.rs` once that is cleaned up
|
||||
|
||||
use super::{AllowedToEditPaymentStatusUser, ManageUserUser, User};
|
||||
use crate::model::{log::Log, mail::valid_mails, role::Role};
|
||||
use crate::model::{family::Family, log::Log, mail::valid_mails, role::Role};
|
||||
use chrono::NaiveDate;
|
||||
use rocket::{fs::TempFile, tokio::io::AsyncReadExt};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
impl User {
|
||||
@ -60,6 +62,35 @@ impl User {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn update_address(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
updated_by: &ManageUserUser,
|
||||
new_address: &str,
|
||||
) -> Result<(), String> {
|
||||
let new_address = new_address.trim();
|
||||
|
||||
let query = if new_address.is_empty() {
|
||||
sqlx::query!("UPDATE user SET address = NULL where id = ?", self.id)
|
||||
} else {
|
||||
sqlx::query!(
|
||||
"UPDATE user SET address = ? where id = ?",
|
||||
new_address,
|
||||
self.id
|
||||
)
|
||||
};
|
||||
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}")
|
||||
};
|
||||
Log::create(db, msg).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn update_nickname(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
@ -89,6 +120,82 @@ impl User {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn update_member_since(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
updated_by: &ManageUserUser,
|
||||
new_member_since_date: &NaiveDate,
|
||||
) {
|
||||
sqlx::query!(
|
||||
"UPDATE user SET member_since_date = ? where id = ?",
|
||||
new_member_since_date,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.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}")
|
||||
};
|
||||
Log::create(db, msg).await;
|
||||
}
|
||||
|
||||
pub(crate) async fn update_birthdate(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
updated_by: &ManageUserUser,
|
||||
new_birthdate: &NaiveDate,
|
||||
) {
|
||||
sqlx::query!(
|
||||
"UPDATE user SET birthdate = ? where id = ?",
|
||||
new_birthdate,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.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}")
|
||||
};
|
||||
Log::create(db, msg).await;
|
||||
}
|
||||
|
||||
pub(crate) async fn update_family(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
updated_by: &ManageUserUser,
|
||||
family: Option<Family>,
|
||||
) {
|
||||
if let Some(family) = family {
|
||||
let family_id = family.id;
|
||||
sqlx::query!(
|
||||
"UPDATE user SET family_id = ? where id = ?",
|
||||
family_id,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
} else {
|
||||
sqlx::query!("UPDATE user SET family_id = NULL where id = ?", self.id)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
};
|
||||
|
||||
Family::clean_families_without_members(db).await;
|
||||
|
||||
Log::create(
|
||||
db,
|
||||
format!("{updated_by} hat die Familie von {self} aktualisiert."),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(crate) async fn remove_role(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
@ -196,4 +303,35 @@ impl User {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn add_membership_pdf(
|
||||
&self,
|
||||
db: &SqlitePool,
|
||||
updated_by: &ManageUserUser,
|
||||
membership_pdf: &TempFile<'_>,
|
||||
) -> Result<(), String> {
|
||||
if self.has_membership_pdf(db).await {
|
||||
return Err(format!("User {self} hat bereits eine Beitrittserklärung."));
|
||||
}
|
||||
|
||||
let mut stream = membership_pdf.open().await.unwrap();
|
||||
let mut buffer = Vec::new();
|
||||
stream.read_to_end(&mut buffer).await.unwrap();
|
||||
sqlx::query!(
|
||||
"UPDATE user SET membership_pdf = ? where id = ?",
|
||||
buffer,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.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}"),
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ impl User {
|
||||
.await?;
|
||||
} else if self.has_role(db, "schnupperant").await {
|
||||
self.send_welcome_mail_schnupper(db, mail, smtp_pw).await?;
|
||||
} else if let Some(scheckbuch) = ScheckbuchUser::new(db, &self).await {
|
||||
} else if let Some(scheckbuch) = ScheckbuchUser::new(db, self).await {
|
||||
scheckbuch.notify(db, mail, smtp_pw).await?;
|
||||
} else {
|
||||
return Err(format!(
|
||||
@ -272,7 +272,7 @@ ASKÖ Ruderverein Donau Linz", self.name),
|
||||
}
|
||||
|
||||
pub async fn allowed_to_update_always_show_trip(&self, db: &SqlitePool) -> bool {
|
||||
AllowedToUpdateTripToAlwaysBeShownUser::new(db, &self)
|
||||
AllowedToUpdateTripToAlwaysBeShownUser::new(db, self)
|
||||
.await
|
||||
.is_some()
|
||||
}
|
||||
|
Reference in New Issue
Block a user