From 53be6cb72dd1c8cf7a3250b1cfb41cf5f5a6f1d8 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Fri, 16 Dec 2022 11:44:32 +0100 Subject: [PATCH] feat: multiple parent csv export --- core/src/database/query/candidate.rs | 17 +++++------------ core/src/database/query/parent.rs | 2 +- core/src/models/candidate.rs | 7 ++++++- core/src/models/candidate_details.rs | 6 +++--- core/src/services/application_service.rs | 2 +- core/src/services/candidate_service.rs | 6 +++--- core/src/services/parent_service.rs | 6 +++--- core/src/utils/csv.rs | 24 +++++++++++++----------- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/core/src/database/query/candidate.rs b/core/src/database/query/candidate.rs index f41cd0b..3077dfb 100644 --- a/core/src/database/query/candidate.rs +++ b/core/src/database/query/candidate.rs @@ -2,7 +2,7 @@ use sea_orm::*; use ::entity::{candidate, candidate::Entity as Candidate, parent}; -use crate::{Query, models::candidate::CandidateWithParent}; +use crate::Query; pub const PAGE_SIZE: u64 = 20; @@ -39,7 +39,7 @@ impl Query { .await } - pub async fn list_candidates( + pub async fn list_candidates_preview( db: &DbConn, field_of_study_opt: Option, page: Option, @@ -64,18 +64,11 @@ impl Query { } } - - pub async fn list_all_candidates_with_parents( - db: &DbConn, - ) -> Result, DbErr> { + pub async fn list_candidates_full( + db: &DbConn + ) -> Result, DbErr> { Candidate::find() .order_by(candidate::Column::Application, Order::Asc) - .join(JoinType::InnerJoin, candidate::Relation::Parent.def()) - .column_as(parent::Column::Name, "parent_name") - .column_as(parent::Column::Surname, "parent_surname") - .column_as(parent::Column::Telephone, "parent_telephone") - .column_as(parent::Column::Email, "parent_email") - .into_model::() .all(db) .await } diff --git a/core/src/database/query/parent.rs b/core/src/database/query/parent.rs index 52fd6e7..a325458 100644 --- a/core/src/database/query/parent.rs +++ b/core/src/database/query/parent.rs @@ -21,7 +21,7 @@ impl Query { pub async fn find_candidate_parents( db: &DbConn, - candidate: candidate::Model, + candidate: &candidate::Model, ) -> Result, DbErr> { candidate.find_related(parent::Entity) diff --git a/core/src/models/candidate.rs b/core/src/models/candidate.rs index a93e44a..3df8967 100644 --- a/core/src/models/candidate.rs +++ b/core/src/models/candidate.rs @@ -63,7 +63,7 @@ pub struct ApplicationDetails { /// CSV export (admin endpoint) #[derive(FromQueryResult, Serialize, Default)] #[serde(rename_all = "camelCase")] -pub struct CandidateWithParent { +pub struct Row { pub application: i32, pub name: Option, pub surname: Option, @@ -81,6 +81,11 @@ pub struct CandidateWithParent { pub parent_surname: Option, pub parent_telephone: Option, pub parent_email: Option, + + pub second_parent_name: Option, + pub second_parent_surname: Option, + pub second_parent_telephone: Option, + pub second_parent_email: Option, } impl BaseCandidateResponse { diff --git a/core/src/models/candidate_details.rs b/core/src/models/candidate_details.rs index 912e6c9..96fe25f 100644 --- a/core/src/models/candidate_details.rs +++ b/core/src/models/candidate_details.rs @@ -3,7 +3,7 @@ use chrono::NaiveDate; use entity::{candidate, parent}; use futures::future; -use crate::{crypto, models::candidate::{CandidateWithParent, ApplicationDetails}, error::ServiceError}; +use crate::{crypto, models::candidate::{Row, ApplicationDetails}, error::ServiceError}; use super::candidate::{CandidateDetails, ParentDetails}; @@ -290,11 +290,11 @@ impl TryFrom<(candidate::Model, Vec)> for EncryptedApplicationDet } } -impl TryFrom for EncryptedApplicationDetails { +impl TryFrom for EncryptedApplicationDetails { type Error = ServiceError; fn try_from( - cp: CandidateWithParent, + cp: Row, ) -> Result { Ok(EncryptedApplicationDetails { candidate: EncryptedCandidateDetails { diff --git a/core/src/services/application_service.rs b/core/src/services/application_service.rs index 6c225e3..933d223 100644 --- a/core/src/services/application_service.rs +++ b/core/src/services/application_service.rs @@ -43,7 +43,7 @@ impl ApplicationService { candidate: candidate::Model, // parents: Vec, ) -> Result { - let parents = Query::find_candidate_parents(db, candidate.clone()).await?; + let parents = Query::find_candidate_parents(db, &candidate).await?; let enc_details = EncryptedApplicationDetails::try_from((candidate, parents))?; enc_details.decrypt(private_key).await diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index a22ea53..c7f52cd 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -102,7 +102,7 @@ impl CandidateService { ) -> Result { let candidate = Query::find_candidate_by_id(db, id).await? .ok_or(ServiceError::CandidateNotFound)?; - let parents = Query::find_candidate_parents(db, candidate.clone()).await?; + let parents = Query::find_candidate_parents(db, &candidate).await?; let new_password_plain = crypto::random_8_char_string(); @@ -163,7 +163,7 @@ impl CandidateService { page: Option, ) -> Result, ServiceError> { - let candidates = Query::list_candidates( + let candidates = Query::list_candidates_preview( db, field_of_study, page @@ -374,7 +374,7 @@ pub mod tests { #[cfg(test)] pub async fn put_user_data(db: &DbConn) -> (candidate::Model, Vec) { - use crate::{models::candidate_details::tests::APPLICATION_DETAILS, Query}; + use crate::{models::candidate_details::tests::APPLICATION_DETAILS}; let plain_text_password = "test".to_string(); let (candidate, _parent) = ApplicationService::create_candidate_with_parent( diff --git a/core/src/services/parent_service.rs b/core/src/services/parent_service.rs index c3aee6c..0ba5779 100644 --- a/core/src/services/parent_service.rs +++ b/core/src/services/parent_service.rs @@ -1,7 +1,7 @@ use entity::{parent, candidate}; use sea_orm::DbConn; -use crate::{error::ServiceError, Mutation, models::{candidate_details::{EncryptedParentDetails}, candidate::ParentDetails}, Query, utils::db::get_recipients}; +use crate::{error::ServiceError, Mutation, models::{candidate_details::{EncryptedParentDetails}, candidate::ParentDetails}, Query}; pub struct ParentService; @@ -22,7 +22,7 @@ impl ParentService { parents_details: &Vec, recipients: &Vec, ) -> Result, ServiceError> { - let found_parents = Query::find_candidate_parents(db, ref_candidate.clone()).await?; + let found_parents = Query::find_candidate_parents(db, &ref_candidate).await?; if found_parents.len() > 2 { return Err(ServiceError::ParentOverflow); } @@ -48,7 +48,7 @@ mod tests { use once_cell::sync::Lazy; - use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::{tests::APPLICATION_DETAILS, EncryptedParentDetails, EncryptedApplicationDetails}}, services::{candidate_service::CandidateService, application_service::ApplicationService}, crypto}; + use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::EncryptedApplicationDetails}, services::{candidate_service::CandidateService, application_service::ApplicationService}, crypto}; use super::ParentService; diff --git a/core/src/utils/csv.rs b/core/src/utils/csv.rs index c741628..be3467f 100644 --- a/core/src/utils/csv.rs +++ b/core/src/utils/csv.rs @@ -1,13 +1,9 @@ use sea_orm::{DbConn}; -use crate::{error::ServiceError, models::candidate_details::{EncryptedApplicationDetails}, Query, models::candidate::{CandidateWithParent, ApplicationDetails}}; - - -type Row = CandidateWithParent; +use crate::{error::ServiceError, models::candidate_details::{EncryptedApplicationDetails}, Query, models::candidate::{Row, ApplicationDetails}}; impl From<(i32, ApplicationDetails)> for Row { fn from((application, d): (i32, ApplicationDetails)) -> Self { let c = d.candidate; - let p = d.parents[0].clone(); Self { application, name: Some(c.name), @@ -22,10 +18,15 @@ impl From<(i32, ApplicationDetails)> for Row { study: Some(c.study), personal_identification_number: Some(c.personal_id_number), - parent_name: Some(p.name), - parent_surname: Some(p.surname), - parent_telephone: Some(p.telephone), - parent_email: Some(p.email), + parent_name: d.parents.get(0).map(|p| p.name.clone()), + parent_surname: d.parents.get(0).map(|p| p.surname.clone()), + parent_telephone: d.parents.get(0).map(|p| p.telephone.clone()), + parent_email: d.parents.get(0).map(|p| p.email.clone()), + + second_parent_name: d.parents.get(1).map(|p| p.name.clone()), + second_parent_surname: d.parents.get(1).map(|p| p.surname.clone()), + second_parent_telephone: d.parents.get(1).map(|p| p.telephone.clone()), + second_parent_email: d.parents.get(1).map(|p| p.email.clone()), } } } @@ -36,11 +37,12 @@ pub async fn export( ) -> Result, ServiceError> { let mut wtr = csv::Writer::from_writer(vec![]); - let candidates_with_parents = Query::list_all_candidates_with_parents(&db).await?; + let candidates_with_parents = Query::list_candidates_full(&db).await?; for candidate in candidates_with_parents { let application = candidate.application; + let parents = Query::find_candidate_parents(db, &candidate).await?; - let row: Row = match EncryptedApplicationDetails::try_from(candidate) { + let row: Row = match EncryptedApplicationDetails::try_from((candidate, parents)) { Ok(d) => Row::from( d .decrypt(private_key.to_string())