use serde::{Serialize, Deserialize}; use crate::{candidate_details::EncryptedString, error::ServiceError}; #[derive(Debug, Serialize, Deserialize)] pub struct CreateCandidateResponse { pub application_id: i32, pub personal_id_number: String, pub password: String, } #[derive(Debug, Serialize)] pub struct BaseCandidateResponse { pub application_id: i32, pub name: String, pub surname: String, pub study: String, pub submitted: bool, } impl BaseCandidateResponse { pub async fn from_encrypted( private_key: &String, application_id: i32, name_opt: Option, surname_opt: Option, study_opt: Option, submitted: bool, ) -> Result { let name = decrypt_if_exists(private_key, name_opt).await?; let surname = decrypt_if_exists(private_key, surname_opt).await?; Ok( Self { name, application_id, surname, study: study_opt.unwrap_or("".to_string()), submitted, } ) } } async fn decrypt_if_exists( private_key: &String, encrypted_string: Option, ) -> Result { match EncryptedString::try_from(encrypted_string) { Ok(encrypted_string) => Ok(encrypted_string.decrypt(private_key).await?), Err(_) => Ok(String::from("")), } }