refactor: code cleanup

This commit is contained in:
Sebastian Pravda 2022-11-27 23:07:01 +01:00
parent 9957ea232f
commit 7d5fa12465
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
2 changed files with 16 additions and 18 deletions

View file

@ -1,5 +1,3 @@
use std::path::{Path, PathBuf};
use entity::candidate; use entity::candidate;
use sea_orm::{prelude::Uuid, DbConn}; use sea_orm::{prelude::Uuid, DbConn};
@ -10,7 +8,7 @@ use crate::{
Mutation, Query, responses::{BaseCandidateResponse, CreateCandidateResponse}, util::get_recipients, Mutation, Query, responses::{BaseCandidateResponse, CreateCandidateResponse}, util::get_recipients,
}; };
use super::{session_service::{AdminUser, SessionService}, application_service::ApplicationService}; use super::{session_service::{AdminUser, SessionService}, application_service::ApplicationService, portfolio_service::PortfolioService};
// TODO // TODO
@ -45,12 +43,6 @@ const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"];
pub struct CandidateService; pub struct CandidateService;
impl CandidateService { impl CandidateService {
// 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()
}
/// Creates a new candidate with: /// Creates a new candidate with:
/// Encrypted personal identification number /// Encrypted personal identification number
/// Hashed password /// Hashed password
@ -74,25 +66,22 @@ impl CandidateService {
{ {
return Err(ServiceError::UserAlreadyExists); return Err(ServiceError::UserAlreadyExists);
} }
PortfolioService::create_user_dir(application_id).await?;
let hashed_password = hash_password(plain_text_password.to_string()).await?; let hashed_password = hash_password(plain_text_password.to_string()).await?;
let (pubkey, priv_key_plain_text) = crypto::create_identity(); let (pubkey, priv_key_plain_text) = crypto::create_identity();
let encrypted_priv_key = crypto::encrypt_password(
let encrypted_priv_key = priv_key_plain_text,
crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await?; plain_text_password.to_string()
).await?;
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?;
tokio::fs::create_dir_all(Self::get_file_store_path().join(&application_id.to_string()).join("cache")).await?;
let candidate = Mutation::create_candidate( let candidate = Mutation::create_candidate(
db, db,
application_id, application_id,
@ -101,7 +90,8 @@ impl CandidateService {
pubkey, pubkey,
encrypted_priv_key, encrypted_priv_key,
) )
.await?; .await?;
Ok(candidate) Ok(candidate)
} }

View file

@ -145,6 +145,14 @@ impl PortfolioService {
Ok(()) Ok(())
} }
pub async fn create_user_dir(application_id: i32) -> tokio::io::Result<()> {
tokio::fs::create_dir_all(
Self::get_file_store_path()
.join(&application_id.to_string())
.join("cache"))
.await
}
pub async fn add_cover_letter_to_cache( pub async fn add_cover_letter_to_cache(
candidate_id: i32, candidate_id: i32,