diff --git a/api/src/lib.rs b/api/src/lib.rs index 14f5ebb..ebd5374 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -4,7 +4,7 @@ extern crate rocket; use std::net::SocketAddr; use guards::request::auth::{CandidateAuth, AdminAuth}; -use portfolio_core::services::candidate_service::CandidateService; +use portfolio_core::services::candidate_service::{CandidateService, AddUserDetailsForm}; use requests::{LoginRequest, RegisterRequest}; use rocket::http::Status; use rocket::{Rocket, Build}; @@ -57,6 +57,23 @@ async fn admin(session: AdminAuth) -> Result> { Ok("Hello admin".to_string()) } +#[put("/details", data = "
")] +async fn fill_details(conn: Connection<'_, Db>, details: Json, session: CandidateAuth) -> Result> { + let db = conn.into_inner(); + let form = details.into_inner(); + let candidate: entity::candidate::Model = session.into(); + + let candidate = CandidateService::add_user_details(db, candidate, form) + .await; + + if candidate.is_err() { // TODO cleanup + let e = candidate.err().unwrap(); + return Err(Custom(Status::from_code(e.code()).unwrap_or_default(), e.message())); + } + + Ok("Details added".to_string()) +} + #[post("/login", data = "")] async fn login(conn: Connection<'_, Db>, login_form: Json, ip_addr: SocketAddr) -> Result> { let db = conn.into_inner(); @@ -90,7 +107,7 @@ async fn start() -> Result<(), rocket::Error> { .attach(Db::init()) .attach(AdHoc::try_on_ignite("Migrations", run_migrations)) //.mount("/", FileServer::from(relative!("/static"))) - .mount("/", routes![create, login, hello, validate, admin]) + .mount("/", routes![create, login, hello, validate, fill_details, admin]) .register("/", catchers![]) .launch() .await diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index dbf7301..c8d0d7b 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -17,7 +17,7 @@ pub(crate) struct EncryptedAddUserData { pub name: String, pub surname: String, pub birthplace: String, - pub birthdate: NaiveDate, + // pub birthdate: NaiveDate, pub address: String, pub telephone: String, pub citizenship: String, @@ -58,7 +58,7 @@ impl EncryptedAddUserData { name, surname, birthplace, - birthdate: NaiveDate::from_ymd(2000, 1, 1), + // birthdate: NaiveDate::from_ymd(2000, 1, 1), address, telephone, citizenship, @@ -74,7 +74,7 @@ pub struct AddUserDetailsForm { pub name: String, pub surname: String, pub birthplace: String, - pub birthdate: NaiveDate, + // pub birthdate: NaiveDate, pub address: String, pub telephone: String, pub citizenship: String, @@ -143,16 +143,16 @@ impl CandidateService { pub async fn add_user_details( db: &DbConn, - application_id: i32, + user: candidate::Model, form: AddUserDetailsForm, ) -> Result { - let Ok(user) = Query::find_candidate_by_id(db, application_id).await else { + /* let Ok(user) = Query::find_candidate_by_id(db, application_id).await else { return Err(ServiceError::DbError); }; let Some(user_unwrapped) = user else { return Err(ServiceError::UserNotFound); - }; + }; */ let Ok(admin_public_keys) = Query::get_all_admin_public_keys(db).await else { return Err(ServiceError::DbError); @@ -161,7 +161,7 @@ impl CandidateService { let mut admin_public_keys_refrence: Vec<&str> = admin_public_keys.iter().map(|s| &**s).collect(); - let mut recipients = vec![&*user_unwrapped.public_key]; + let mut recipients = vec![&*user.public_key]; recipients.append(&mut admin_public_keys_refrence); @@ -169,7 +169,7 @@ impl CandidateService { Mutation::add_candidate_details( db, - user_unwrapped, + user, enc_details, ) .await @@ -291,7 +291,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(), @@ -299,7 +299,7 @@ mod tests { sex: "test".to_string(), study: "test".to_string(), }; - let candidate = CandidateService::add_user_details(&db, candidate.application, form).await.ok().unwrap(); + let candidate = CandidateService::add_user_details(&db, candidate, form).await.ok().unwrap();