From bf04ff780fa614b09306541df90c304e242713d9 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 26 Mar 2024 12:34:19 +0100 Subject: [PATCH] only use membership_pdf when necessary --- src/model/family.rs | 2 +- src/model/rower.rs | 2 +- src/model/user.rs | 71 +++++++++++++++++++++--------------------- src/tera/admin/user.rs | 3 +- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/model/family.rs b/src/model/family.rs index 6bff530..f648cfd 100644 --- a/src/model/family.rs +++ b/src/model/family.rs @@ -75,7 +75,7 @@ GROUP BY family.id;" } pub async fn members(&self, db: &SqlitePool) -> Vec { - sqlx::query_as!(User, "SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf FROM user WHERE family_id = ?", self.id) + sqlx::query_as!(User, "SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE family_id = ?", self.id) .fetch_all(db) .await .unwrap() diff --git a/src/model/rower.rs b/src/model/rower.rs index a953738..7099cff 100644 --- a/src/model/rower.rs +++ b/src/model/rower.rs @@ -16,7 +16,7 @@ impl Rower { sqlx::query_as!( User, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE id in (SELECT rower_id FROM rower WHERE logbook_id=?) ", diff --git a/src/model/user.rs b/src/model/user.rs index 1e0a0da..b6fef48 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -26,7 +26,7 @@ const REGULAR: i32 = 22000; const UNTERSTUETZEND: i32 = 2500; const FOERDERND: i32 = 8500; -#[derive(FromRow, Serialize, Deserialize, Clone)] +#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] pub struct User { pub id: i64, pub name: String, @@ -44,30 +44,6 @@ pub struct User { pub phone: Option, pub address: Option, pub family_id: Option, - pub membership_pdf: Option>, -} - -impl std::fmt::Debug for User { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("User") - .field("id", &self.id) - .field("name", &self.name) - .field("dob", &self.dob) - .field("weight", &self.weight) - .field("sex", &self.sex) - .field("deleted", &self.deleted) - .field("last_access", &self.last_access) - .field("member_since_date", &self.member_since_date) - .field("birthdate", &self.birthdate) - .field("mail", &self.mail) - .field("nickname", &self.nickname) - .field("notes", &self.notes) - .field("phone", &self.phone) - .field("address", &self.address) - .field("family_id", &self.family_id) - // Note that membership_pdf is intentionally omitted here - .finish() - } } #[derive(Debug, Serialize, Deserialize)] @@ -310,7 +286,7 @@ impl User { sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE id like ? ", @@ -325,7 +301,7 @@ WHERE id like ? sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE id like ? ", @@ -340,7 +316,7 @@ WHERE id like ? sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE name like ? ", @@ -382,7 +358,7 @@ WHERE name like ? sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE deleted = 0 ORDER BY last_access DESC @@ -397,7 +373,7 @@ ORDER BY last_access DESC sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user u JOIN user_role ur ON u.id = ur.user_id WHERE ur.role_id = ? AND deleted = 0 @@ -413,14 +389,14 @@ ORDER BY name; sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf FROM user +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE family_id IS NOT NULL GROUP BY family_id UNION -- Select users with a null family_id, without grouping -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf FROM user +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE family_id IS NULL; " ) @@ -433,7 +409,7 @@ WHERE family_id IS NULL; sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE deleted = 0 AND dob != '' and weight != '' and sex != '' ORDER BY name @@ -448,7 +424,7 @@ ORDER BY name sqlx::query_as!( Self, " -SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, membership_pdf +SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id FROM user WHERE deleted = 0 AND (SELECT COUNT(*) FROM user_role WHERE user_id=user.id AND role_id = (SELECT id FROM role WHERE name = 'cox')) > 0 ORDER BY last_access DESC @@ -473,7 +449,9 @@ ORDER BY last_access DESC family_id = Some(Family::insert(db).await) } - if self.membership_pdf.is_none() { + let user_with_membershippdf = UserWithMembershipPdf::from(db, self.clone()).await; + + if user_with_membershippdf.membership_pdf.is_none() { if let Some(membership_pdf) = data.membership_pdf { let mut stream = membership_pdf.open().await.unwrap(); let mut buffer = Vec::new(); @@ -982,6 +960,29 @@ impl Deref for PlannedEventUser { } } +#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] +pub struct UserWithMembershipPdf { + #[serde(flatten)] + pub user: User, + pub membership_pdf: Option>, +} + +impl UserWithMembershipPdf { + pub(crate) async fn from(db: &SqlitePool, user: User) -> Self { + let membership_pdf: Option> = + sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = $1", user.id) + .fetch_optional(db) + .await + .unwrap() + .unwrap(); + + Self { + user, + membership_pdf, + } + } +} + #[async_trait] impl<'r> FromRequest<'r> for PlannedEventUser { type Error = LoginError; diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index aca09e9..332c74a 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -5,7 +5,7 @@ use crate::model::{ log::Log, logbook::Logbook, role::Role, - user::{AdminUser, User, UserWithRoles, VorstandUser}, + user::{AdminUser, User, UserWithMembershipPdf, UserWithRoles, VorstandUser}, }; use futures::future::join_all; use rocket::{ @@ -280,6 +280,7 @@ async fn download_membership_pdf( user: i32, ) -> (ContentType, Vec) { let user = User::find_by_id(db, user).await.unwrap(); + let user = UserWithMembershipPdf::from(db, user).await; Log::create( db, format!(