From 8fd8ee66240319061a0b3d971f63ab8683531414 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Thu, 19 Jan 2023 18:35:56 +0100 Subject: [PATCH] feat!: school order selection *database* - firstSchool - secondSchool *CandidateDetails* School struct with 'name' & 'field' attributes --- api/src/routes/candidate.rs | 2 ++ core/src/database/mutation/candidate.rs | 2 ++ core/src/models/candidate.rs | 4 ++- core/src/models/candidate_details.rs | 24 ++++++++++++-- core/src/models/mod.rs | 3 +- core/src/models/school.rs | 31 +++++++++++++++++++ core/src/services/parent_service.rs | 4 ++- entity/src/candidate.rs | 2 ++ .../src/m20221024_121621_create_candidate.rs | 4 +++ 9 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 core/src/models/school.rs diff --git a/api/src/routes/candidate.rs b/api/src/routes/candidate.rs index 49bfd34..0fab9db 100644 --- a/api/src/routes/candidate.rs +++ b/api/src/routes/candidate.rs @@ -319,6 +319,8 @@ mod tests { \"schoolName\": \"29988383\", \"healthInsurance\": \"000\", \"grades\": [], + \"firstSchool\": {\"name\": \"SSPÅ \", \"field\": \"KB\"}, + \"secondSchool\": {\"name\": \"SSPÅ \", \"field\": \"IT\"}, \"testLanguage\": \"CZ\" }, \"parents\": [ diff --git a/core/src/database/mutation/candidate.rs b/core/src/database/mutation/candidate.rs index ad7a961..aef582b 100644 --- a/core/src/database/mutation/candidate.rs +++ b/core/src/database/mutation/candidate.rs @@ -54,6 +54,8 @@ impl Mutation { candidate.school_name = Set(enc_candidate.school_name.map(|e| e.into())); candidate.health_insurance = Set(enc_candidate.health_insurance.map(|e| e.into())); candidate.grades_json = Set(enc_candidate.grades_json.map(|e| e.into())); + candidate.first_school = Set(enc_candidate.first_school.map(|e| e.into())); + candidate.second_school = Set(enc_candidate.second_school.map(|e| e.into())); candidate.test_language = Set(enc_candidate.test_language.map(|s| s)); candidate.encrypted_by_id = Set(Some(encrypted_by_id)); diff --git a/core/src/models/candidate.rs b/core/src/models/candidate.rs index b3b9ae4..920e387 100644 --- a/core/src/models/candidate.rs +++ b/core/src/models/candidate.rs @@ -6,7 +6,7 @@ use crate::{ error::ServiceError, }; -use super::{candidate_details::{EncryptedString, EncryptedCandidateDetails}, grade::GradeList}; +use super::{candidate_details::{EncryptedString, EncryptedCandidateDetails}, grade::GradeList, school::School}; pub enum FieldOfStudy { G, @@ -73,6 +73,8 @@ pub struct CandidateDetails { pub school_name: String, pub health_insurance: String, pub grades: GradeList, + pub first_school: School, + pub second_school: School, pub test_language: String, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] diff --git a/core/src/models/candidate_details.rs b/core/src/models/candidate_details.rs index 5400e46..9d243b6 100644 --- a/core/src/models/candidate_details.rs +++ b/core/src/models/candidate_details.rs @@ -5,7 +5,7 @@ use futures::future; use crate::{crypto, models::candidate::{ApplicationDetails}, error::ServiceError, utils::date::parse_naive_date_from_opt_str}; -use super::{candidate::{CandidateDetails, ParentDetails}, application::ApplicationRow, grade::GradeList}; +use super::{candidate::{CandidateDetails, ParentDetails}, application::ApplicationRow, grade::GradeList, school::School}; pub const NAIVE_DATE_FMT: &str = "%Y-%m-%d"; @@ -27,6 +27,8 @@ pub struct EncryptedCandidateDetails { pub school_name: Option, pub health_insurance: Option, pub grades_json: Option, + pub first_school: Option, + pub second_school: Option, pub test_language: Option, } @@ -121,6 +123,8 @@ impl EncryptedCandidateDetails { ) -> Result { let birthdate_str = form.birthdate.format(NAIVE_DATE_FMT).to_string(); let grades_str = form.grades.to_string(); + let (first_school_str, second_school_str) = + (form.first_school.to_string(), form.second_school.to_string()); let d = tokio::try_join!( EncryptedString::new_option(&form.name, recipients), EncryptedString::new_option(&form.surname, recipients), @@ -135,6 +139,8 @@ impl EncryptedCandidateDetails { EncryptedString::new_option(&form.school_name, recipients), EncryptedString::new_option(&form.health_insurance, recipients), EncryptedString::new_option(&grades_str, recipients), + EncryptedString::new_option(&first_school_str, recipients), + EncryptedString::new_option(&second_school_str, recipients), )?; Ok( @@ -152,6 +158,8 @@ impl EncryptedCandidateDetails { school_name: d.10, health_insurance: d.11, grades_json: d.12, + first_school: d.13, + second_school: d.14, test_language: Some(form.test_language.to_owned()), } ) @@ -172,8 +180,12 @@ impl EncryptedCandidateDetails { EncryptedString::decrypt_option(&self.school_name, priv_key), // 10 EncryptedString::decrypt_option(&self.health_insurance, priv_key), // 11 EncryptedString::decrypt_option(&self.grades_json, priv_key), // 12 + EncryptedString::decrypt_option(&self.first_school, priv_key), // 13 + EncryptedString::decrypt_option(&self.second_school, priv_key), // 14 )?; + println!("d: {:?}", d.12); + Ok(CandidateDetails { name: d.0.unwrap_or_default(), surname: d.1.unwrap_or_default(), @@ -188,6 +200,8 @@ impl EncryptedCandidateDetails { school_name: d.10.unwrap_or_default(), health_insurance: d.11.unwrap_or_default(), grades: GradeList::from_opt_str(d.12).unwrap_or_default(), + first_school: School::from_opt_str(d.13).unwrap_or_default(), + second_school: School::from_opt_str(d.14).unwrap_or_default(), test_language: self.test_language.to_owned().unwrap_or_default().to_string(), } ) @@ -224,6 +238,8 @@ impl From<&candidate::Model> for EncryptedCandidateDetails { school_name: EncryptedString::try_from(&candidate.school_name).ok(), health_insurance: EncryptedString::try_from(&candidate.health_insurance).ok(), grades_json: EncryptedString::try_from(&candidate.grades_json).ok(), + first_school: EncryptedString::try_from(&candidate.first_school).ok(), + second_school: EncryptedString::try_from(&candidate.second_school).ok(), test_language: candidate.test_language.to_owned(), } } @@ -362,6 +378,8 @@ impl TryFrom for EncryptedApplicationDetails { personal_id_number: EncryptedString::try_from(&cp.personal_identification_number).ok(), school_name: EncryptedString::try_from(&cp.school_name).ok(), health_insurance: EncryptedString::try_from(&cp.health_insurance).ok(), + first_school: None, // TODO + second_school: None, // TODO grades_json: None, // TODO test_language: None // TODO }, @@ -395,7 +413,7 @@ pub mod tests { use once_cell::sync::Lazy; use sea_orm::{DbConn, Set, ActiveModelTrait}; - use crate::{crypto, models::{candidate::{CandidateDetails, ParentDetails}, grade::GradeList}, utils::db::get_memory_sqlite_connection, services::candidate_service::tests::put_user_data}; + use crate::{crypto, models::{candidate::{CandidateDetails, ParentDetails}, grade::GradeList, school::School}, utils::db::get_memory_sqlite_connection, services::candidate_service::tests::put_user_data}; use super::{ApplicationDetails, EncryptedApplicationDetails, EncryptedString}; @@ -418,6 +436,8 @@ pub mod tests { school_name: "school_name".to_string(), health_insurance: "health_insurance".to_string(), grades: GradeList::from(vec![]), + first_school: School::from_opt_str(Some("{\"name\": \"SSPS\", \"field\": \"KB\"}".to_string())).unwrap(), + second_school: School::from_opt_str(Some("{\"name\": \"SSPS\", \"field\": \"IT\"}".to_string())).unwrap(), test_language: "test_language".to_string(), }, parents: vec![ParentDetails { diff --git a/core/src/models/mod.rs b/core/src/models/mod.rs index 08cc4f2..2f790c0 100644 --- a/core/src/models/mod.rs +++ b/core/src/models/mod.rs @@ -2,4 +2,5 @@ pub mod candidate_details; pub mod candidate; pub mod auth; pub mod application; -pub mod grade; \ No newline at end of file +pub mod grade; +pub mod school; \ No newline at end of file diff --git a/core/src/models/school.rs b/core/src/models/school.rs new file mode 100644 index 0000000..3a3ceed --- /dev/null +++ b/core/src/models/school.rs @@ -0,0 +1,31 @@ +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct School { + name: String, + field: String, +} + +impl School { + pub fn from_opt_str(school: Option) -> Option { + println!("School: {:?}", school); + school.map( + |school| serde_json::from_str(&school).unwrap() // TODO: handle error + ) + } +} + +impl ToString for School { + fn to_string(&self) -> String { + serde_json::to_string(&self).unwrap() + } +} + +impl Default for School { + fn default() -> Self { + Self { + name: String::default(), + field: String::default(), + } + } +} \ No newline at end of file diff --git a/core/src/services/parent_service.rs b/core/src/services/parent_service.rs index 13d0299..a24636f 100644 --- a/core/src/services/parent_service.rs +++ b/core/src/services/parent_service.rs @@ -54,7 +54,7 @@ mod tests { use once_cell::sync::Lazy; - use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::EncryptedApplicationDetails, grade::GradeList}, services::{candidate_service::{CandidateService, tests::put_user_data}, application_service::ApplicationService, parent_service::ParentService}, crypto}; + use crate::{utils::db::get_memory_sqlite_connection, models::{candidate::{ParentDetails, ApplicationDetails, CandidateDetails}, candidate_details::EncryptedApplicationDetails, grade::GradeList, school::School}, services::{candidate_service::{CandidateService, tests::put_user_data}, application_service::ApplicationService, parent_service::ParentService}, crypto}; pub static APPLICATION_DETAILS_TWO_PARENTS: Lazy> = Lazy::new(|| Mutex::new(ApplicationDetails { @@ -72,6 +72,8 @@ mod tests { school_name: "school_name".to_string(), health_insurance: "health_insurance".to_string(), grades: GradeList::from(vec![]), + first_school: School::from_opt_str(Some("{\"name\": \"SSPS\", \"field\": \"KB\"}".to_string())).unwrap(), + second_school: School::from_opt_str(Some("{\"name\": \"SSPS\", \"field\": \"IT\"}".to_string())).unwrap(), test_language: "test_language".to_string(), }, parents: vec![ParentDetails { diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index 7084efd..a48d4db 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -21,6 +21,8 @@ pub struct Model { pub school_name: Option, pub health_insurance: Option, pub grades_json: Option, + pub first_school: Option, + pub second_school: Option, pub test_language: Option, pub encrypted_by_id: Option, pub created_at: DateTime, diff --git a/migration/src/m20221024_121621_create_candidate.rs b/migration/src/m20221024_121621_create_candidate.rs index da7f7ce..4e46469 100644 --- a/migration/src/m20221024_121621_create_candidate.rs +++ b/migration/src/m20221024_121621_create_candidate.rs @@ -32,6 +32,8 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Candidate::SchoolName).string()) .col(ColumnDef::new(Candidate::HealthInsurance).string()) .col(ColumnDef::new(Candidate::GradesJson).string()) + .col(ColumnDef::new(Candidate::FirstSchool).string()) + .col(ColumnDef::new(Candidate::SecondSchool).string()) .col(ColumnDef::new(Candidate::TestLanguage).string()) .col(ColumnDef::new(Candidate::EncryptedById).integer()) .col(ColumnDef::new(Candidate::CreatedAt).date_time().not_null()) @@ -66,6 +68,8 @@ pub enum Candidate { SchoolName, HealthInsurance, GradesJson, + FirstSchool, + SecondSchool, TestLanguage, EncryptedById, CreatedAt,