diff --git a/core/src/candidate_details.rs b/core/src/candidate_details.rs index 020abb0..027cf5f 100644 --- a/core/src/candidate_details.rs +++ b/core/src/candidate_details.rs @@ -1,8 +1,11 @@ +use chrono::{NaiveDate}; use entity::candidate; use serde::{Serialize, Deserialize}; use crate::{error::ServiceError, crypto}; +pub const NAIVE_DATE_FMT: &str = "%Y-%m-%d"; + pub struct EncryptedString(String); impl EncryptedString { @@ -20,7 +23,7 @@ impl EncryptedString { } } - pub async fn to_string(self) -> String { + pub fn to_string(self) -> String { self.0 } } @@ -42,11 +45,22 @@ impl TryFrom> for EncryptedString { } } +impl TryFrom> for EncryptedString { // TODO: take a look at this + type Error = ServiceError; + + fn try_from(d: Option) -> Result { + match d { + Some(d) => Ok(Self(d.to_string())), + None => Err(ServiceError::CandidateDetailsNotSet), + } + } +} + pub(crate) struct EncryptedCandidateDetails { pub name: EncryptedString, pub surname: EncryptedString, pub birthplace: EncryptedString, - // pub birthdate: NaiveDate, + pub birthdate: EncryptedString, pub address: EncryptedString, pub telephone: EncryptedString, pub citizenship: EncryptedString, @@ -57,11 +71,12 @@ pub(crate) struct EncryptedCandidateDetails { impl EncryptedCandidateDetails { pub async fn new(form: CandidateDetails, recipients: Vec<&str>) -> Result { + let birthdate_str = form.birthdate.format(NAIVE_DATE_FMT).to_string(); let d = tokio::try_join!( EncryptedString::new(&form.name, &recipients), EncryptedString::new(&form.surname, &recipients), EncryptedString::new(&form.birthplace, &recipients), - EncryptedString::new("&form.birthdate", &recipients), // TODO + EncryptedString::new(&birthdate_str, &recipients), // TODO EncryptedString::new(&form.address, &recipients), EncryptedString::new(&form.telephone, &recipients), EncryptedString::new(&form.citizenship, &recipients), @@ -74,7 +89,7 @@ impl EncryptedCandidateDetails { name: d.0, surname: d.1, birthplace: d.2, - // birthdate: NaiveDate::from_ymd(2000, 1, 1), + birthdate: d.3, address: d.4, telephone: d.5, citizenship: d.6, @@ -89,7 +104,7 @@ impl EncryptedCandidateDetails { self.name.decrypt(&priv_key), self.surname.decrypt(&priv_key), self.birthplace.decrypt(&priv_key), - // self.birthdate.decrypt(&priv_key), + self.birthdate.decrypt(&priv_key), self.address.decrypt(&priv_key), self.telephone.decrypt(&priv_key), self.citizenship.decrypt(&priv_key), @@ -102,13 +117,13 @@ impl EncryptedCandidateDetails { name: d.0, surname: d.1, birthplace: d.2, - // birthdate: NaiveDate::from_ymd(2000, 1, 1), // TODO - address: d.3, - telephone: d.4, - citizenship: d.5, - email: d.6, - sex: d.7, - study: d.8, + birthdate: NaiveDate::parse_from_str(&d.3, NAIVE_DATE_FMT).unwrap(), // TODO + address: d.4, + telephone: d.5, + citizenship: d.6, + email: d.7, + sex: d.8, + study: d.9, }) } } @@ -121,7 +136,7 @@ impl TryFrom for EncryptedCandidateDetails { name: EncryptedString::try_from(candidate.name)?, surname: EncryptedString::try_from(candidate.surname)?, birthplace: EncryptedString::try_from(candidate.birthplace)?, - // birthdate?, + birthdate: EncryptedString::try_from(candidate.birthdate)?, address: EncryptedString::try_from(candidate.address)?, telephone: EncryptedString::try_from(candidate.telephone)?, citizenship: EncryptedString::try_from(candidate.citizenship)?, @@ -137,7 +152,7 @@ pub struct CandidateDetails { pub name: String, pub surname: String, pub birthplace: String, - // pub birthdate: NaiveDate, + pub birthdate: NaiveDate, // TODO: User NaiveDate or String? pub address: String, pub telephone: String, pub citizenship: String, diff --git a/core/src/database/mutation/candidate.rs b/core/src/database/mutation/candidate.rs index f1fe53e..9b191b0 100644 --- a/core/src/database/mutation/candidate.rs +++ b/core/src/database/mutation/candidate.rs @@ -1,4 +1,4 @@ -use crate::{Mutation, candidate_details::EncryptedCandidateDetails}; +use crate::{Mutation, candidate_details::{EncryptedCandidateDetails}}; use ::entity::candidate::{self}; use sea_orm::{*}; @@ -35,6 +35,7 @@ impl Mutation { user.name = Set(Some(enc_details.name.into())); user.surname = Set(Some(enc_details.surname.into())); user.birthplace = Set(Some(enc_details.birthplace.into())); + user.birthdate = Set(Some(enc_details.birthdate.into())); user.address = Set(Some(enc_details.address.into())); user.telephone = Set(Some(enc_details.telephone.into())); user.citizenship = Set(Some(enc_details.citizenship.into())); diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index 188e6fb..3cd7651 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -293,6 +293,8 @@ mod tests { #[cfg(test)] async fn put_user_data(db: &DbConn) -> Model { + use chrono::NaiveDate; + let plain_text_password = "test".to_string(); let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string()) .await @@ -303,7 +305,7 @@ mod tests { name: "test".to_string(), surname: "a".to_string(), birthplace: "b".to_string(), - // birthdate: NaiveDate::from_ymd(1999, 1, 1), + birthdate: NaiveDate::from_ymd(1999, 1, 1), address: "test".to_string(), telephone: "test".to_string(), citizenship: "test".to_string(), diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index bf738f9..002093d 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -10,7 +10,7 @@ pub struct Model { pub surname: Option, pub birth_surname: Option, pub birthplace: Option, - pub birthdate: Option, + pub birthdate: Option, pub address: Option, pub telephone: Option, pub citizenship: Option, diff --git a/migration/src/m20221024_121621_create_candidate.rs b/migration/src/m20221024_121621_create_candidate.rs index 2b7bc26..66d6466 100644 --- a/migration/src/m20221024_121621_create_candidate.rs +++ b/migration/src/m20221024_121621_create_candidate.rs @@ -23,7 +23,7 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Candidate::Surname).string()) .col(ColumnDef::new(Candidate::BirthSurname).string()) .col(ColumnDef::new(Candidate::Birthplace).string()) - .col(ColumnDef::new(Candidate::Birthdate).date()) + .col(ColumnDef::new(Candidate::Birthdate).string()) .col(ColumnDef::new(Candidate::Address).string()) .col(ColumnDef::new(Candidate::Telephone).string()) .col(ColumnDef::new(Candidate::Citizenship).string())