mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-06 19:40:49 +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();
|
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
|
.await
|
||||||
.map_err(to_custom_error)?;
|
.map_err(to_custom_error)?;
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
Json(
|
Json(
|
||||||
CreateCandidateResponse {
|
CreateCandidateResponse {
|
||||||
application_id: form.application_id,
|
application_id: application.id,
|
||||||
personal_id_number: form.personal_id_number,
|
field_of_study: application.field_of_study,
|
||||||
|
applications: applications.iter()
|
||||||
|
.map(|a| a.id)
|
||||||
|
.collect(),
|
||||||
|
personal_id_number,
|
||||||
password: plain_text_password,
|
password: plain_text_password,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ pub struct NewCandidateResponse {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct CreateCandidateResponse {
|
pub struct CreateCandidateResponse {
|
||||||
pub application_id: i32,
|
pub application_id: i32,
|
||||||
|
pub field_of_study: String,
|
||||||
|
pub applications: Vec<i32>,
|
||||||
pub personal_id_number: String,
|
pub personal_id_number: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ impl ApplicationService {
|
||||||
application_id: i32,
|
application_id: i32,
|
||||||
plain_text_password: &String,
|
plain_text_password: &String,
|
||||||
personal_id_number: 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
|
// Check if application id starts with 101, 102 or 103
|
||||||
if !Self::is_application_id_valid(application_id) {
|
if !Self::is_application_id_valid(application_id) {
|
||||||
return Err(ServiceError::InvalidApplicationId);
|
return Err(ServiceError::InvalidApplicationId);
|
||||||
|
|
@ -50,7 +50,7 @@ impl ApplicationService {
|
||||||
application_id,
|
application_id,
|
||||||
admin_private_key,
|
admin_private_key,
|
||||||
db,
|
db,
|
||||||
personal_id_number,
|
&personal_id_number,
|
||||||
&pubkey,
|
&pubkey,
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
|
|
@ -72,15 +72,31 @@ impl ApplicationService {
|
||||||
}
|
}
|
||||||
return Err(ServiceError::InternalServerError);
|
return Err(ServiceError::InternalServerError);
|
||||||
}
|
}
|
||||||
|
Ok(
|
||||||
Ok(application)
|
/* 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(
|
async fn find_or_create_candidate_with_personal_id(
|
||||||
application_id: i32,
|
application_id: i32,
|
||||||
admin_private_key: &String,
|
admin_private_key: &String,
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
personal_id_number: String,
|
personal_id_number: &String,
|
||||||
pubkey: &String,
|
pubkey: &String,
|
||||||
// enc_personal_id_number: &EncryptedString,
|
// enc_personal_id_number: &EncryptedString,
|
||||||
) -> Result<(candidate::Model, String), ServiceError> {
|
) -> Result<(candidate::Model, String), ServiceError> {
|
||||||
|
|
@ -98,7 +114,7 @@ impl ApplicationService {
|
||||||
|
|
||||||
let found_ids: Vec<&(i32, String)> = ids_decrypted
|
let found_ids: Vec<&(i32, String)> = ids_decrypted
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_, id)| id == &personal_id_number)
|
.filter(|(_, id)| id == personal_id_number)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if let Some((candidate_id, _)) = found_ids.first() {
|
if let Some((candidate_id, _)) = found_ids.first() {
|
||||||
|
|
@ -107,14 +123,14 @@ impl ApplicationService {
|
||||||
application_id,
|
application_id,
|
||||||
*candidate_id,
|
*candidate_id,
|
||||||
pubkey,
|
pubkey,
|
||||||
personal_id_number
|
personal_id_number.to_owned()
|
||||||
).await?
|
).await?
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let recipients = get_recipients(db, pubkey).await?;
|
let recipients = get_recipients(db, pubkey).await?;
|
||||||
|
|
||||||
let enc_personal_id_number = EncryptedString::new(
|
let enc_personal_id_number = EncryptedString::new(
|
||||||
&personal_id_number,
|
personal_id_number,
|
||||||
&recipients,
|
&recipients,
|
||||||
).await?;
|
).await?;
|
||||||
Ok(
|
Ok(
|
||||||
|
|
@ -354,6 +370,10 @@ impl ApplicationService {
|
||||||
Ok(
|
Ok(
|
||||||
CreateCandidateResponse {
|
CreateCandidateResponse {
|
||||||
application_id: id,
|
application_id: id,
|
||||||
|
field_of_study: application.field_of_study,
|
||||||
|
applications: applications.iter()
|
||||||
|
.map(|a| a.id)
|
||||||
|
.collect(),
|
||||||
personal_id_number,
|
personal_id_number,
|
||||||
password: new_password_plain,
|
password: new_password_plain,
|
||||||
}
|
}
|
||||||
|
|
@ -489,7 +509,7 @@ mod application_tests {
|
||||||
|
|
||||||
let secret_message = "trnka".to_string();
|
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 =
|
let encrypted_message =
|
||||||
crypto::encrypt_password_with_recipients(&secret_message, &vec![&application.public_key])
|
crypto::encrypt_password_with_recipients(&secret_message, &vec![&application.public_key])
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ pub mod tests {
|
||||||
APPLICATION_ID,
|
APPLICATION_ID,
|
||||||
&plain_text_password,
|
&plain_text_password,
|
||||||
"0000001111".to_string()
|
"0000001111".to_string()
|
||||||
).await.unwrap();
|
).await.unwrap().0;
|
||||||
|
|
||||||
let candidate= ApplicationService::find_related_candidate(db, &application).await.unwrap();
|
let candidate= ApplicationService::find_related_candidate(db, &application).await.unwrap();
|
||||||
ParentService::create(db, candidate.id).await.unwrap();
|
ParentService::create(db, candidate.id).await.unwrap();
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ mod tests {
|
||||||
|
|
||||||
let db = get_memory_sqlite_connection().await;
|
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_eq!(application.id.to_owned(), 103151);
|
||||||
assert_ne!(application.password.to_owned(), SECRET.to_string());
|
assert_ne!(application.password.to_owned(), SECRET.to_string());
|
||||||
|
|
@ -66,7 +66,7 @@ mod tests {
|
||||||
async fn test_candidate_session_correct_password() {
|
async fn test_candidate_session_correct_password() {
|
||||||
let db = &get_memory_sqlite_connection().await;
|
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
|
// correct password
|
||||||
let session = ApplicationService::new_session(
|
let session = ApplicationService::new_session(
|
||||||
|
|
@ -88,7 +88,7 @@ mod tests {
|
||||||
async fn test_candidate_session_incorrect_password() {
|
async fn test_candidate_session_incorrect_password() {
|
||||||
let db = &get_memory_sqlite_connection().await;
|
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
|
// incorrect password
|
||||||
assert!(ApplicationService::new_session(
|
assert!(ApplicationService::new_session(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue