diff --git a/api/src/lib.rs b/api/src/lib.rs index 8a8e476..1a793c7 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -59,6 +59,7 @@ async fn start() -> Result<(), rocket::Error> { routes::candidate::submit_portfolio, routes::candidate::is_portfolio_prepared, routes::candidate::is_portfolio_submitted, + routes::candidate::download_portfolio, ], ) .mount( @@ -70,6 +71,7 @@ async fn start() -> Result<(), rocket::Error> { routes::admin::create_candidate, routes::admin::get_candidate, routes::admin::reset_candidate_password, + routes::admin::get_candidate_portfolio, ], ) .mount( diff --git a/api/src/routes/admin.rs b/api/src/routes/admin.rs index 5b47fbc..e4eb796 100644 --- a/api/src/routes/admin.rs +++ b/api/src/routes/admin.rs @@ -2,7 +2,7 @@ use std::net::SocketAddr; use portfolio_core::{ crypto::random_8_char_string, - services::{admin_service::AdminService, candidate_service::CandidateService, application_service::ApplicationService}, responses::CandidateResponse, candidate_details::ApplicationDetails, + services::{admin_service::AdminService, candidate_service::CandidateService, application_service::ApplicationService, portfolio_service::PortfolioService}, responses::CandidateResponse, candidate_details::ApplicationDetails, }; use requests::{AdminLoginRequest, RegisterRequest}; use rocket::http::{Cookie, Status, CookieJar}; @@ -21,8 +21,6 @@ pub async fn login( cookies: &CookieJar<'_>, ) -> Result> { let db = conn.into_inner(); - println!("{} {}", login_form.admin_id, login_form.password); - let session_token_key = AdminService::login( db, login_form.admin_id, @@ -143,4 +141,18 @@ pub async fn reset_candidate_password( .map_err(|e| Custom(Status::from_code(e.code()).unwrap(), e.to_string()))?; Ok(new_password) +} + +#[get("/candidate//portfolio")] +pub async fn get_candidate_portfolio( + session: AdminAuth, + id: i32, +) -> Result, Custom> { + let private_key = session.get_private_key(); + + let portfolio = PortfolioService::get_portfolio(id, private_key) + .await + .map_err(|e| Custom(Status::from_code(e.code()).unwrap(), e.to_string()))?; + + Ok(portfolio) } \ No newline at end of file diff --git a/api/src/routes/candidate.rs b/api/src/routes/candidate.rs index d3aa1f3..e180352 100644 --- a/api/src/routes/candidate.rs +++ b/api/src/routes/candidate.rs @@ -3,6 +3,7 @@ use std::net::SocketAddr; use portfolio_core::candidate_details::ApplicationDetails; use portfolio_core::services::application_service::ApplicationService; use portfolio_core::services::candidate_service::CandidateService; +use portfolio_core::services::portfolio_service::PortfolioService; use requests::LoginRequest; use rocket::http::{Cookie, CookieJar, Status}; use rocket::response::status::Custom; @@ -22,8 +23,6 @@ pub async fn login( cookies: &CookieJar<'_>, ) -> Result> { let db = conn.into_inner(); - println!("{} {}", login_form.application_id, login_form.password); - let session_token_key = CandidateService::login( db, login_form.application_id, @@ -115,7 +114,7 @@ pub async fn upload_cover_letter( let candidate: entity::candidate::Model = session.into(); let candidate = - CandidateService::add_cover_letter_to_cache(candidate.application, letter.into()).await; + PortfolioService::add_cover_letter_to_cache(candidate.application, letter.into()).await; if candidate.is_err() { // TODO cleanup @@ -134,7 +133,7 @@ pub async fn upload_cover_letter( pub async fn is_cover_letter(session: CandidateAuth) -> Result> { let candidate: entity::candidate::Model = session.into(); - let exists = CandidateService::is_cover_letter(candidate.application).await; + let exists = PortfolioService::is_cover_letter(candidate.application).await; Ok(exists.to_string()) } @@ -147,7 +146,7 @@ pub async fn upload_portfolio_letter( let candidate: entity::candidate::Model = session.into(); let candidate = - CandidateService::add_portfolio_letter_to_cache(candidate.application, letter.into()).await; + PortfolioService::add_portfolio_letter_to_cache(candidate.application, letter.into()).await; if candidate.is_err() { // TODO cleanup @@ -166,7 +165,7 @@ pub async fn upload_portfolio_letter( pub async fn is_portfolio_letter(session: CandidateAuth) -> Result> { let candidate: entity::candidate::Model = session.into(); - let exists = CandidateService::is_portfolio_letter(candidate.application).await; + let exists = PortfolioService::is_portfolio_letter(candidate.application).await; Ok(exists.to_string()) } @@ -179,7 +178,7 @@ pub async fn upload_portfolio_zip( let candidate: entity::candidate::Model = session.into(); let candidate = - CandidateService::add_portfolio_zip_to_cache(candidate.application, portfolio.into()).await; + PortfolioService::add_portfolio_zip_to_cache(candidate.application, portfolio.into()).await; if candidate.is_err() { // TODO cleanup @@ -198,7 +197,7 @@ pub async fn upload_portfolio_zip( pub async fn is_portfolio_zip(session: CandidateAuth) -> Result> { let candidate: entity::candidate::Model = session.into(); - let exists = CandidateService::is_portfolio_zip(candidate.application).await; + let exists = PortfolioService::is_portfolio_zip(candidate.application).await; Ok(exists.to_string()) } @@ -212,7 +211,7 @@ pub async fn submit_portfolio( let candidate: entity::candidate::Model = session.into(); - let submit = CandidateService::add_portfolio(candidate.application, &db).await; + let submit = PortfolioService::submit(candidate.clone(), &db).await; if submit.is_err() { let e = submit.err().unwrap(); @@ -220,7 +219,7 @@ pub async fn submit_portfolio( // TODO: VĂ­ce kontrol? if e.code() == 500 { // Cleanup - CandidateService::delete_portfolio(candidate.application) + PortfolioService::delete_portfolio(candidate.application) .await .unwrap(); } @@ -237,7 +236,7 @@ pub async fn submit_portfolio( pub async fn is_portfolio_prepared(session: CandidateAuth) -> Result> { let candidate: entity::candidate::Model = session.into(); - let is_ok = CandidateService::is_portfolio_prepared(candidate.application).await; + let is_ok = PortfolioService::is_portfolio_prepared(candidate.application).await; if !is_ok { // TODO: Correct error @@ -254,7 +253,7 @@ pub async fn is_portfolio_prepared(session: CandidateAuth) -> Result Result> { let candidate: entity::candidate::Model = session.into(); - let is_ok = CandidateService::is_portfolio_submitted(candidate.application).await; + let is_ok = PortfolioService::is_portfolio_submitted(candidate.application).await; if !is_ok { // TODO: Correct error @@ -266,3 +265,21 @@ pub async fn is_portfolio_submitted(session: CandidateAuth) -> Result Result, Custom> { + let private_key = session.get_private_key(); + let candidate: entity::candidate::Model = session.into(); + + let file = PortfolioService::get_portfolio(candidate.application, private_key).await; + + if file.is_err() { + let e = file.err().unwrap(); + return Err(Custom( + Status::from_code(e.code()).unwrap_or_default(), + e.to_string(), + )); + } + + Ok(file.unwrap()) +} \ No newline at end of file diff --git a/core/src/services/application_service.rs b/core/src/services/application_service.rs index 796d8b0..eec9931 100644 --- a/core/src/services/application_service.rs +++ b/core/src/services/application_service.rs @@ -1,7 +1,7 @@ use entity::{candidate, parent}; use sea_orm::DbConn; -use crate::{error::ServiceError, candidate_details::{ApplicationDetails, EncryptedApplicationDetails}, Query, crypto}; +use crate::{error::ServiceError, candidate_details::{ApplicationDetails, EncryptedApplicationDetails}, Query}; use super::{parent_service::ParentService, candidate_service::CandidateService}; diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index 396bea1..9c12b72 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -2,7 +2,6 @@ use std::path::{Path, PathBuf}; use entity::candidate; use sea_orm::{prelude::Uuid, DbConn}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; use crate::{ candidate_details::{EncryptedApplicationDetails}, @@ -179,198 +178,6 @@ impl CandidateService { && candidate.study.is_some() } - async fn write_portfolio_file( - candidate_id: i32, - data: Vec, - filename: &str, - ) -> Result<(), ServiceError> { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - let mut file = tokio::fs::File::create(cache_path.join(filename)).await?; - - file.write_all(&data).await?; - - Ok(()) - } - - pub async fn add_cover_letter_to_cache( - candidate_id: i32, - letter: Vec, - ) -> Result<(), ServiceError> { - Self::write_portfolio_file(candidate_id, letter, "MOTIVACNI_DOPIS.pdf").await - } - - pub async fn is_cover_letter(candidate_id: i32) -> bool { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - tokio::fs::metadata(cache_path.join(cache_path.join("MOTIVACNI_DOPIS.pdf"))) - .await - .is_ok() - } - - pub async fn add_portfolio_letter_to_cache( - candidate_id: i32, - letter: Vec, - ) -> Result<(), ServiceError> { - Self::write_portfolio_file(candidate_id, letter, "PORTFOLIO.pdf").await - } - - pub async fn is_portfolio_letter(candidate_id: i32) -> bool { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - tokio::fs::metadata(cache_path.join(cache_path.join("PORTFOLIO.pdf"))) - .await - .is_ok() - } - - pub async fn add_portfolio_zip_to_cache( - candidate_id: i32, - zip: Vec, - ) -> Result<(), ServiceError> { - Self::write_portfolio_file(candidate_id, zip, "PORTFOLIO.zip").await - } - - pub async fn is_portfolio_zip(candidate_id: i32) -> bool { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - tokio::fs::metadata(cache_path.join(cache_path.join("PORTFOLIO.zip"))) - .await - .is_ok() - } - - pub async fn is_portfolio_prepared(candidate_id: i32) -> bool { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - let filenames = vec!["MOTIVACNI_DOPIS.pdf", "PORTFOLIO.pdf", "PORTFOLIO.zip"]; - - for filename in filenames { - if !tokio::fs::metadata(cache_path.join(filename)).await.is_ok() { - return false; - } - } - - true - } - - pub async fn delete_cache(candidate_id: i32) -> Result<(), ServiceError> { - let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); - - tokio::fs::remove_dir_all(&cache_path).await?; - // Recreate blank cache directory - tokio::fs::create_dir_all(&cache_path).await?; - - Ok(()) - } - - pub async fn add_portfolio(candidate_id: i32, db: &DbConn) -> Result<(), ServiceError> { - let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); - let cache_path = path.join("cache"); - - if Self::is_portfolio_prepared(candidate_id).await == false { - return Err(ServiceError::IncompletePortfolio); - } - - let mut archive = tokio::fs::File::create(path.join("PORTFOLIO.zip")).await?; - - let mut writer = async_zip::write::ZipFileWriter::new(&mut archive); - - let mut buffer = vec![vec![], vec![], vec![]]; - - let filenames = vec!["MOTIVACNI_DOPIS.pdf", "PORTFOLIO.pdf", "PORTFOLIO.zip"]; - - for (index, entry) in buffer.iter_mut().enumerate() { - let filename = filenames[index]; - let mut entry_file = tokio::fs::File::open(cache_path.join(filename)).await?; - - entry_file.read_to_end(entry).await?; - } - - Self::delete_cache(candidate_id).await?; - - for (index, entry) in buffer.iter_mut().enumerate() { - let filename = filenames[index]; - let builder = async_zip::ZipEntryBuilder::new( - filename.to_string(), - async_zip::Compression::Deflate, - ); - - writer.write_entry_whole(builder, &entry).await?; - } - - writer.close().await?; - archive.shutdown().await?; - - let admin_public_keys = Query::get_all_admin_public_keys(db).await?; - - let candidate = Query::find_candidate_by_id(db, candidate_id) - .await? - .ok_or(ServiceError::CandidateNotFound)?; - - let candidate_public_key = candidate.public_key; - - let mut admin_public_keys_refrence: Vec<&str> = - admin_public_keys.iter().map(|s| &**s).collect(); - - let mut recipients = vec![&*candidate_public_key]; - - recipients.append(&mut admin_public_keys_refrence); - - let final_path = path.join("PORTFOLIO.zip"); - - let Ok(_) = crypto::encrypt_file_with_recipients( - &final_path, - &final_path.with_extension("age"), - recipients, - ) - .await else { - return Err(ServiceError::CryptoEncryptFailed); - }; - - tokio::fs::remove_file(final_path).await?; - - Ok(()) - } - - pub async fn delete_portfolio(candidate_id: i32) -> Result<(), ServiceError> { - let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); - - let portfolio_path = path.join("PORTFOLIO.zip"); - let portfolio_age_path = portfolio_path.with_extension("age"); - - if tokio::fs::metadata(&portfolio_path).await.is_ok() { - tokio::fs::remove_file(&portfolio_path).await?; - } - - if tokio::fs::metadata(&portfolio_age_path).await.is_ok() { - tokio::fs::remove_file(&portfolio_age_path).await?; - } - - Ok(()) - } - - pub async fn is_portfolio_submitted(candidate_id: i32) -> bool { - let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); - - tokio::fs::metadata(path.join("PORTFOLIO.age")).await.is_ok() - } - - pub async fn get_portfolio(candidate_id: i32, db: &DbConn) -> Result, ServiceError> { - let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); - - let candidate = Query::find_candidate_by_id(db, candidate_id) - .await? - .ok_or(ServiceError::CandidateNotFound)?; - - let candidate_public_key = candidate.public_key; - - let path = path.join("PORTFOLIO.age"); - - let buffer = - crypto::decrypt_file_with_private_key_as_buffer(path, &candidate_public_key).await?; - - Ok(buffer) - } - async fn decrypt_private_key( candidate: candidate::Model, password: String, @@ -426,9 +233,8 @@ impl CandidateService { } #[cfg(test)] -mod tests { +pub mod tests { use sea_orm::{DbConn}; - use serial_test::serial; use crate::util::get_memory_sqlite_connection; use crate::{crypto, services::candidate_service::CandidateService, Mutation}; @@ -440,8 +246,6 @@ mod tests { use crate::candidate_details::{ApplicationDetails}; use crate::services::application_service::ApplicationService; - use std::path::{PathBuf}; - const APPLICATION_ID: i32 = 103151; #[tokio::test] @@ -552,7 +356,7 @@ mod tests { } #[cfg(test)] - async fn put_user_data(db: &DbConn) -> (candidate::Model, parent::Model) { + pub async fn put_user_data(db: &DbConn) -> (candidate::Model, parent::Model) { let plain_text_password = "test".to_string(); let (candidate, _parent) = ApplicationService::create_candidate_with_parent( &db, @@ -623,248 +427,4 @@ mod tests { assert_eq!(dec_details.parent_telephone, "parent_telephone"); assert_eq!(dec_details.parent_email, "parent_email"); } - - #[cfg(test)] - async fn create_data_store_temp_dir(application_id: i32) -> (PathBuf, PathBuf, PathBuf) { - let random_number: u32 = rand::Rng::gen(&mut rand::thread_rng()); - - let temp_dir = std::env::temp_dir().join("portfolio_test_tempdir").join(random_number.to_string()); - let application_dir = temp_dir.join(application_id.to_string()); - let application_cache_dir = application_dir.join("cache"); - - tokio::fs::create_dir_all(application_cache_dir.clone()).await.unwrap(); - - std::env::set_var("STORE_PATH", temp_dir.to_str().unwrap()); - - (temp_dir, application_dir, application_cache_dir) - } - - #[cfg(test)] - async fn clear_data_store_temp_dir(temp_dir: PathBuf) { - tokio::fs::remove_dir_all(temp_dir).await.unwrap(); - - std::env::remove_var("STORE_PATH"); - } - - #[tokio::test] - #[serial] - async fn test_folder_creation() { - let db = get_memory_sqlite_connection().await; - let plain_text_password = "test".to_string(); - - let temp_dir = std::env::temp_dir().join("portfolio_test_tempdir").join("create_folder"); - std::env::set_var("STORE_PATH", temp_dir.to_str().unwrap()); - - CandidateService::create(&db, APPLICATION_ID, &plain_text_password, "".to_string()) - .await - .ok() - .unwrap(); - - assert!(tokio::fs::metadata(temp_dir.join(APPLICATION_ID.to_string())).await.is_ok()); - assert!(tokio::fs::metadata(temp_dir.join(APPLICATION_ID.to_string()).join("cache")).await.is_ok()); - - tokio::fs::remove_dir_all(temp_dir).await.unwrap(); - } - - #[tokio::test] - #[serial] - async fn test_write_portfolio_file() { - let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::write_portfolio_file(APPLICATION_ID, vec![0], "test").await.unwrap(); - - assert!(tokio::fs::metadata(application_cache_dir.join("test")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_add_cover_letter_to_cache() { - let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(tokio::fs::metadata(application_cache_dir.join("MOTIVACNI_DOPIS.pdf")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_is_cover_letter() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(CandidateService::is_cover_letter(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_add_portfolio_letter_to_cache() { - let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(tokio::fs::metadata(application_cache_dir.join("PORTFOLIO.pdf")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_is_portfolio_letter() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(CandidateService::is_portfolio_letter(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_add_portfolio_zip_to_cache() { - let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(tokio::fs::metadata(application_cache_dir.join("PORTFOLIO.zip")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_is_portfolio_zip() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(CandidateService::is_portfolio_zip(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_is_portfolio_prepared() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(CandidateService::is_portfolio_prepared(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - //CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(!CandidateService::is_portfolio_prepared(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_delete_cache() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - assert!(CandidateService::is_portfolio_zip(APPLICATION_ID).await); - - CandidateService::delete_cache(APPLICATION_ID).await.unwrap(); - - assert!(!CandidateService::is_portfolio_zip(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_add_portfolio() { - let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - let db = get_memory_sqlite_connection().await; - put_user_data(&db).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - CandidateService::add_portfolio(APPLICATION_ID, &db).await.unwrap(); - - assert!(tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_delete_portfolio() { - let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - let db = get_memory_sqlite_connection().await; - put_user_data(&db).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - CandidateService::add_portfolio(APPLICATION_ID, &db).await.unwrap(); - - assert!(tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); - - CandidateService::delete_portfolio(APPLICATION_ID).await.unwrap(); - - assert!(!tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); - - clear_data_store_temp_dir(temp_dir).await; - } - - #[tokio::test] - #[serial] - async fn test_is_portfolio_submitted() { - let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - let db = get_memory_sqlite_connection().await; - put_user_data(&db).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - CandidateService::add_portfolio(APPLICATION_ID, &db).await.unwrap(); - - assert!(CandidateService::is_portfolio_submitted(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - - let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; - - CandidateService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - CandidateService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); - - CandidateService::add_portfolio(APPLICATION_ID, &db).await.unwrap(); - - tokio::fs::remove_file(application_dir.join("PORTFOLIO.age")).await.unwrap(); - - assert!(!CandidateService::is_portfolio_submitted(APPLICATION_ID).await); - - clear_data_store_temp_dir(temp_dir).await; - - - } - } diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 28fe987..9cea043 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -2,4 +2,5 @@ pub mod session_service; pub mod candidate_service; pub mod admin_service; pub mod parent_service; -pub mod application_service; \ No newline at end of file +pub mod application_service; +pub mod portfolio_service; \ No newline at end of file diff --git a/core/src/services/portfolio_service.rs b/core/src/services/portfolio_service.rs new file mode 100644 index 0000000..fe75805 --- /dev/null +++ b/core/src/services/portfolio_service.rs @@ -0,0 +1,515 @@ +use std::{path::{PathBuf, Path}}; + +use entity::candidate; +use sea_orm::DbConn; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +use crate::{error::ServiceError, Query, crypto}; + +#[derive(Copy, Clone)] +enum FileType { + CoverLetterPdf, + PortfolioLetterPdf, + PortfolioZip, + Age, +} + +impl FileType { + pub fn as_str(&self) -> &'static str { + match self { + FileType::CoverLetterPdf => "MOTIVACNI_DOPIS.pdf", + FileType::PortfolioLetterPdf => "PORTFOLIO.pdf", + FileType::PortfolioZip => "PORTFOLIO.zip", + FileType::Age => "PORTFOLIO.age", + } + } +} + +impl ToString for FileType { + fn to_string(&self) -> String { + self.as_str().to_string() + } +} + + +pub struct PortfolioService; +impl PortfolioService { + // Get root path or local directory + fn get_file_store_path() -> PathBuf { + dotenv::dotenv().ok(); + Path::new(&std::env::var("STORE_PATH").unwrap_or_else(|_| "".to_string())).to_path_buf() + } + + /// Writes file to desired location + async fn write_portfolio_file( + candidate_id: i32, + data: Vec, + filename: FileType, + ) -> Result<(), ServiceError> { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + + let mut file = tokio::fs::File::create(cache_path.join(filename.as_str())).await?; + + file.write_all(&data).await?; + + Ok(()) + } + + + pub async fn add_cover_letter_to_cache( + candidate_id: i32, + letter: Vec, + ) -> Result<(), ServiceError> { + Self::write_portfolio_file(candidate_id, letter, FileType::CoverLetterPdf).await + } + + pub async fn add_portfolio_letter_to_cache( + candidate_id: i32, + letter: Vec, + ) -> Result<(), ServiceError> { + Self::write_portfolio_file(candidate_id, letter, FileType::PortfolioLetterPdf).await + } + + pub async fn add_portfolio_zip_to_cache( + candidate_id: i32, + zip: Vec, + ) -> Result<(), ServiceError> { + Self::write_portfolio_file(candidate_id, zip, FileType::PortfolioZip).await + } + + + pub async fn is_cover_letter(candidate_id: i32) -> bool { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + + tokio::fs::metadata(cache_path.join(cache_path.join(FileType::CoverLetterPdf.as_str()))) + .await + .is_ok() + } + + pub async fn is_portfolio_letter(candidate_id: i32) -> bool { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + + tokio::fs::metadata( + cache_path.join( + cache_path.join(FileType::PortfolioLetterPdf.as_str()) + ) + ) + .await + .is_ok() + } + + pub async fn is_portfolio_zip(candidate_id: i32) -> bool { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + + tokio::fs::metadata( + cache_path.join( + cache_path.join(FileType::PortfolioZip.as_str()) + ) + ) + .await + .is_ok() + } + + + /// Returns true if portfolio is ready to be moved to the final directory + pub async fn is_portfolio_prepared(candidate_id: i32) -> bool { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + + let filenames = vec![FileType::CoverLetterPdf, FileType::PortfolioLetterPdf, FileType::PortfolioZip]; + for filename in filenames { + if !tokio::fs::metadata( + cache_path.join(filename.as_str()) + ).await.is_ok() { + return false; + } + } + true + } + + /// Removes all files from cache + pub async fn delete_cache(candidate_id: i32) -> Result<(), ServiceError> { + let cache_path = Self::get_file_store_path().join(&candidate_id.to_string()).join("cache"); + tokio::fs::remove_dir_all(&cache_path).await?; + // Recreate blank cache directory + tokio::fs::create_dir_all(&cache_path).await?; + + Ok(()) + } + + + /// Move files from cache to final directory and delete cache afterwards + pub async fn submit(candidate: candidate::Model, db: &DbConn) -> Result<(), ServiceError> { + let candidate_id = candidate.application; + let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); + let cache_path = path.join("cache"); + + if Self::is_portfolio_prepared(candidate_id).await == false { + return Err(ServiceError::IncompletePortfolio); + } + + let mut archive = tokio::fs::File::create(path.join(FileType::PortfolioZip.as_str())).await?; + let mut writer = async_zip::write::ZipFileWriter::new(&mut archive); + let mut buffer = vec![vec![], vec![], vec![]]; + + let filenames = vec![FileType::CoverLetterPdf, FileType::PortfolioLetterPdf, FileType::PortfolioZip]; + for (index, entry) in buffer.iter_mut().enumerate() { + let filename = filenames[index]; + let mut entry_file = tokio::fs::File::open(cache_path.join(filename.as_str())).await?; + + entry_file.read_to_end(entry).await?; + } + + Self::delete_cache(candidate_id).await?; + + for (index, entry) in buffer.iter_mut().enumerate() { + let filename = filenames[index]; + let builder = async_zip::ZipEntryBuilder::new( + filename.to_string(), + async_zip::Compression::Deflate, + ); + + writer.write_entry_whole(builder, &entry).await?; + } + + writer.close().await?; + archive.shutdown().await?; + + let admin_public_keys = Query::get_all_admin_public_keys(db).await?; + let candidate_public_key = candidate.public_key; + let mut admin_public_keys_refrence: Vec<&str> = admin_public_keys.iter().map(|s| &**s).collect(); + let mut recipients = vec![&*candidate_public_key]; + recipients.append(&mut admin_public_keys_refrence); + + let final_path = path.join(FileType::PortfolioZip.as_str()); + + crypto::encrypt_file_with_recipients( + &final_path, + &final_path.with_extension("age"), + recipients, + ).await?; + tokio::fs::remove_file(final_path).await?; + + Ok(()) + } + + /// Delete PORTFOLIO.age file + pub async fn delete_portfolio(candidate_id: i32) -> Result<(), ServiceError> { + let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); + + let portfolio_path = path.join(FileType::PortfolioZip.as_str()); + let portfolio_age_path = portfolio_path.with_extension("age"); + + if tokio::fs::metadata(&portfolio_path).await.is_ok() { + tokio::fs::remove_file(&portfolio_path).await?; + } + + if tokio::fs::metadata(&portfolio_age_path).await.is_ok() { + tokio::fs::remove_file(&portfolio_age_path).await?; + } + + Ok(()) + } + + /// Returns true if portfolio is submitted + pub async fn is_portfolio_submitted(candidate_id: i32) -> bool { + let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); + + tokio::fs::metadata(path.join(FileType::Age.as_str())).await.is_ok() + } + + /// Returns decrypted portfolio zip as Vec of bytes + pub async fn get_portfolio(candidate_id: i32, private_key: String) -> Result, ServiceError> { + let path = Self::get_file_store_path().join(&candidate_id.to_string()).to_path_buf(); + + let path = path.join(FileType::Age.as_str()); + + let buffer = + crypto::decrypt_file_with_private_key_as_buffer(path, &private_key).await?; + + Ok(buffer) + } +} + +#[cfg(test)] +mod tests { + use serial_test::serial; + + use crate::{services::{portfolio_service::{PortfolioService, FileType}, candidate_service::{CandidateService, tests::put_user_data}}, util::get_memory_sqlite_connection, crypto}; + use std::path::PathBuf; + + const APPLICATION_ID: i32 = 103151; + + #[cfg(test)] + async fn create_data_store_temp_dir(application_id: i32) -> (PathBuf, PathBuf, PathBuf) { + let random_number: u32 = rand::Rng::gen(&mut rand::thread_rng()); + + let temp_dir = std::env::temp_dir().join("portfolio_test_tempdir").join(random_number.to_string()); + let application_dir = temp_dir.join(application_id.to_string()); + let application_cache_dir = application_dir.join("cache"); + + tokio::fs::create_dir_all(application_cache_dir.clone()).await.unwrap(); + + std::env::set_var("STORE_PATH", temp_dir.to_str().unwrap()); + + (temp_dir, application_dir, application_cache_dir) + } + + #[cfg(test)] + async fn clear_data_store_temp_dir(temp_dir: PathBuf) { + tokio::fs::remove_dir_all(temp_dir).await.unwrap(); + + std::env::remove_var("STORE_PATH"); + } + + #[tokio::test] + #[serial] + async fn test_folder_creation() { + let db = get_memory_sqlite_connection().await; + let plain_text_password = "test".to_string(); + + let temp_dir = std::env::temp_dir().join("portfolio_test_tempdir").join("create_folder"); + std::env::set_var("STORE_PATH", temp_dir.to_str().unwrap()); + + CandidateService::create(&db, APPLICATION_ID, &plain_text_password, "".to_string()) + .await + .ok() + .unwrap(); + + assert!(tokio::fs::metadata(temp_dir.join(APPLICATION_ID.to_string())).await.is_ok()); + assert!(tokio::fs::metadata(temp_dir.join(APPLICATION_ID.to_string()).join("cache")).await.is_ok()); + + tokio::fs::remove_dir_all(temp_dir).await.unwrap(); + } + + #[tokio::test] + #[serial] + async fn test_write_portfolio_file() { + let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::write_portfolio_file(APPLICATION_ID, vec![0], crate::services::portfolio_service::FileType::PortfolioLetterPdf).await.unwrap(); + + assert!(tokio::fs::metadata(application_cache_dir.join(FileType::PortfolioLetterPdf.as_str())).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_add_cover_letter_to_cache() { + let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(tokio::fs::metadata(application_cache_dir.join("MOTIVACNI_DOPIS.pdf")).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_is_cover_letter() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(PortfolioService::is_cover_letter(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_add_portfolio_letter_to_cache() { + let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(tokio::fs::metadata(application_cache_dir.join("PORTFOLIO.pdf")).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_is_portfolio_letter() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(PortfolioService::is_portfolio_letter(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_add_portfolio_zip_to_cache() { + let (temp_dir, _, application_cache_dir) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(tokio::fs::metadata(application_cache_dir.join("PORTFOLIO.zip")).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_is_portfolio_zip() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(PortfolioService::is_portfolio_zip(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_is_portfolio_prepared() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(PortfolioService::is_portfolio_prepared(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + //PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(!PortfolioService::is_portfolio_prepared(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_delete_cache() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + assert!(PortfolioService::is_portfolio_zip(APPLICATION_ID).await); + + PortfolioService::delete_cache(APPLICATION_ID).await.unwrap(); + + assert!(!PortfolioService::is_portfolio_zip(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_add_portfolio() { + let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + let db = get_memory_sqlite_connection().await; + let (candidate, _) = put_user_data(&db).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + PortfolioService::submit(candidate, &db).await.unwrap(); + + assert!(tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_delete_portfolio() { + let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + let db = get_memory_sqlite_connection().await; + let (candidate, _) = put_user_data(&db).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + PortfolioService::submit(candidate, &db).await.unwrap(); + + assert!(tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); + + PortfolioService::delete_portfolio(APPLICATION_ID).await.unwrap(); + + assert!(!tokio::fs::metadata(application_dir.join("PORTFOLIO.age")).await.is_ok()); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_is_portfolio_submitted() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + let db = get_memory_sqlite_connection().await; + let (candidate, _) = put_user_data(&db).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + PortfolioService::submit(candidate.clone(), &db).await.unwrap(); + + assert!(PortfolioService::is_portfolio_submitted(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + + let (temp_dir, application_dir, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]).await.unwrap(); + + PortfolioService::submit(candidate.clone(), &db).await.unwrap(); + + tokio::fs::remove_file(application_dir.join("PORTFOLIO.age")).await.unwrap(); + + assert!(!PortfolioService::is_portfolio_submitted(APPLICATION_ID).await); + + clear_data_store_temp_dir(temp_dir).await; + } + + #[tokio::test] + #[serial] + async fn test_get_portfolio() { + let (temp_dir, _, _) = create_data_store_temp_dir(APPLICATION_ID).await; + + let db = get_memory_sqlite_connection().await; + let (candidate, _parent) = put_user_data(&db).await; + + let private_key = crypto::decrypt_password(candidate.private_key.clone(), "test".to_string()) + .await + .unwrap(); + + PortfolioService::add_cover_letter_to_cache(APPLICATION_ID, vec![0]) + .await + .unwrap(); + PortfolioService::add_portfolio_letter_to_cache(APPLICATION_ID, vec![0]) + .await + .unwrap(); + PortfolioService::add_portfolio_zip_to_cache(APPLICATION_ID, vec![0]) + .await + .unwrap(); + + PortfolioService::submit(candidate, &db) + .await + .unwrap(); + + PortfolioService::get_portfolio(APPLICATION_ID, private_key) + .await + .unwrap(); + + clear_data_store_temp_dir(temp_dir).await; + } +} \ No newline at end of file