mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-19 14:31:05 +00:00
feat: application create - more detailed response
This commit is contained in:
parent
76411aff97
commit
d3ff293224
5 changed files with 48 additions and 16 deletions
|
|
@ -94,15 +94,25 @@ pub async fn create_candidate(
|
|||
|
||||
let plain_text_password = random_12_char_string();
|
||||
|
||||
ApplicationService::create(&private_key, &db, form.application_id, &plain_text_password, form.personal_id_number.clone())
|
||||
let (application, applications, personal_id_number) = ApplicationService::create(
|
||||
&private_key,
|
||||
&db,
|
||||
form.application_id,
|
||||
&plain_text_password,
|
||||
form.personal_id_number.clone()
|
||||
)
|
||||
.await
|
||||
.map_err(to_custom_error)?;
|
||||
|
||||
Ok(
|
||||
Json(
|
||||
CreateCandidateResponse {
|
||||
application_id: form.application_id,
|
||||
personal_id_number: form.personal_id_number,
|
||||
application_id: application.id,
|
||||
field_of_study: application.field_of_study,
|
||||
applications: applications.iter()
|
||||
.map(|a| a.id)
|
||||
.collect(),
|
||||
personal_id_number,
|
||||
password: plain_text_password,
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -54,6 +54,8 @@ pub struct NewCandidateResponse {
|
|||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CreateCandidateResponse {
|
||||
pub application_id: i32,
|
||||
pub field_of_study: String,
|
||||
pub applications: Vec<i32>,
|
||||
pub personal_id_number: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ impl ApplicationService {
|
|||
application_id: i32,
|
||||
plain_text_password: &String,
|
||||
personal_id_number: String,
|
||||
) -> Result<application::Model, ServiceError> {
|
||||
) -> Result<(application::Model, Vec<application::Model>, String), ServiceError> {
|
||||
// Check if application id starts with 101, 102 or 103
|
||||
if !Self::is_application_id_valid(application_id) {
|
||||
return Err(ServiceError::InvalidApplicationId);
|
||||
|
|
@ -50,7 +50,7 @@ impl ApplicationService {
|
|||
application_id,
|
||||
admin_private_key,
|
||||
db,
|
||||
personal_id_number,
|
||||
&personal_id_number,
|
||||
&pubkey,
|
||||
).await?;
|
||||
|
||||
|
|
@ -72,15 +72,31 @@ impl ApplicationService {
|
|||
}
|
||||
return Err(ServiceError::InternalServerError);
|
||||
}
|
||||
|
||||
Ok(application)
|
||||
Ok(
|
||||
/* NewCandidateResponse {
|
||||
current_application: application.id,
|
||||
applications: applications
|
||||
.iter()
|
||||
.map(|a| a.id)
|
||||
.collect::<Vec<i32>>(),
|
||||
details_filled: false,
|
||||
encrypted_by: Some(application.id),
|
||||
field_of_study: application.field_of_study,
|
||||
personal_id_number: personal_id_number,
|
||||
} */
|
||||
(
|
||||
application,
|
||||
applications,
|
||||
personal_id_number,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
async fn find_or_create_candidate_with_personal_id(
|
||||
application_id: i32,
|
||||
admin_private_key: &String,
|
||||
db: &DbConn,
|
||||
personal_id_number: String,
|
||||
personal_id_number: &String,
|
||||
pubkey: &String,
|
||||
// enc_personal_id_number: &EncryptedString,
|
||||
) -> Result<(candidate::Model, String), ServiceError> {
|
||||
|
|
@ -98,7 +114,7 @@ impl ApplicationService {
|
|||
|
||||
let found_ids: Vec<&(i32, String)> = ids_decrypted
|
||||
.iter()
|
||||
.filter(|(_, id)| id == &personal_id_number)
|
||||
.filter(|(_, id)| id == personal_id_number)
|
||||
.collect();
|
||||
|
||||
if let Some((candidate_id, _)) = found_ids.first() {
|
||||
|
|
@ -107,14 +123,14 @@ impl ApplicationService {
|
|||
application_id,
|
||||
*candidate_id,
|
||||
pubkey,
|
||||
personal_id_number
|
||||
personal_id_number.to_owned()
|
||||
).await?
|
||||
)
|
||||
} else {
|
||||
let recipients = get_recipients(db, pubkey).await?;
|
||||
|
||||
let enc_personal_id_number = EncryptedString::new(
|
||||
&personal_id_number,
|
||||
personal_id_number,
|
||||
&recipients,
|
||||
).await?;
|
||||
Ok(
|
||||
|
|
@ -354,6 +370,10 @@ impl ApplicationService {
|
|||
Ok(
|
||||
CreateCandidateResponse {
|
||||
application_id: id,
|
||||
field_of_study: application.field_of_study,
|
||||
applications: applications.iter()
|
||||
.map(|a| a.id)
|
||||
.collect(),
|
||||
personal_id_number,
|
||||
password: new_password_plain,
|
||||
}
|
||||
|
|
@ -489,7 +509,7 @@ mod application_tests {
|
|||
|
||||
let secret_message = "trnka".to_string();
|
||||
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103100, &plain_text_password, "".to_string()).await.unwrap();
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103100, &plain_text_password, "".to_string()).await.unwrap().0;
|
||||
|
||||
let encrypted_message =
|
||||
crypto::encrypt_password_with_recipients(&secret_message, &vec![&application.public_key])
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ pub mod tests {
|
|||
APPLICATION_ID,
|
||||
&plain_text_password,
|
||||
"0000001111".to_string()
|
||||
).await.unwrap();
|
||||
).await.unwrap().0;
|
||||
|
||||
let candidate= ApplicationService::find_related_candidate(db, &application).await.unwrap();
|
||||
ParentService::create(db, candidate.id).await.unwrap();
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ mod tests {
|
|||
|
||||
let db = get_memory_sqlite_connection().await;
|
||||
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap();
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap().0;
|
||||
|
||||
assert_eq!(application.id.to_owned(), 103151);
|
||||
assert_ne!(application.password.to_owned(), SECRET.to_string());
|
||||
|
|
@ -66,7 +66,7 @@ mod tests {
|
|||
async fn test_candidate_session_correct_password() {
|
||||
let db = &get_memory_sqlite_connection().await;
|
||||
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap();
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap().0;
|
||||
|
||||
// correct password
|
||||
let session = ApplicationService::new_session(
|
||||
|
|
@ -88,7 +88,7 @@ mod tests {
|
|||
async fn test_candidate_session_incorrect_password() {
|
||||
let db = &get_memory_sqlite_connection().await;
|
||||
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap();
|
||||
let application = ApplicationService::create(&"".to_string(), &db, 103151, &SECRET.to_string(), "".to_string()).await.unwrap().0;
|
||||
|
||||
// incorrect password
|
||||
assert!(ApplicationService::new_session(
|
||||
|
|
|
|||
Loading…
Reference in a new issue