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 sea_orm::{prelude::Uuid, DbConn};
@ -10,7 +8,7 @@ use crate::{
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
@ -45,12 +43,6 @@ const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"];
pub struct 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:
/// Encrypted personal identification number
/// Hashed password
@ -74,25 +66,22 @@ impl CandidateService {
{
return Err(ServiceError::UserAlreadyExists);
}
PortfolioService::create_user_dir(application_id).await?;
let hashed_password = hash_password(plain_text_password.to_string()).await?;
let (pubkey, priv_key_plain_text) = crypto::create_identity();
let encrypted_priv_key =
crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await?;
let encrypted_priv_key = crypto::encrypt_password(
priv_key_plain_text,
plain_text_password.to_string()
).await?;
let recipients = get_recipients(db, &pubkey).await?;
let enc_personal_id_number = EncryptedString::new(
&personal_id_number,
&recipients,
).await?;
tokio::fs::create_dir_all(Self::get_file_store_path().join(&application_id.to_string()).join("cache")).await?;
let candidate = Mutation::create_candidate(
db,
application_id,
@ -101,7 +90,8 @@ impl CandidateService {
pubkey,
encrypted_priv_key,
)
.await?;
.await?;
Ok(candidate)
}

View file

@ -145,6 +145,14 @@ impl PortfolioService {
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(
candidate_id: i32,