mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-22 05:48:56 +00:00
Merge pull request #103 from EETagent/admin_list_candidates
Change Candidate List data format
This commit is contained in:
commit
9f35f6fa89
5 changed files with 44 additions and 46 deletions
|
|
@ -129,7 +129,7 @@ pub async fn list_candidates(
|
|||
|
||||
}
|
||||
|
||||
let candidates = CandidateService::list_candidates(private_key, db, field, page)
|
||||
let candidates = CandidateService::list_candidates(&private_key, db, field, page)
|
||||
.await
|
||||
.map_err(to_custom_error)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,16 +17,15 @@ impl ApplicationId {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(FromQueryResult)]
|
||||
pub struct CandidateParentResult {
|
||||
#[derive(FromQueryResult, Clone)]
|
||||
pub struct CandidateResult {
|
||||
pub application: i32,
|
||||
pub name: Option<String>,
|
||||
pub surname: Option<String>,
|
||||
pub email: Option<String>,
|
||||
pub telephone: Option<String>,
|
||||
pub study: Option<String>,
|
||||
pub citizenship: Option<String>,
|
||||
|
||||
pub parent_name: Option<String>,
|
||||
pub parent_surname: Option<String>,
|
||||
}
|
||||
|
||||
impl Query {
|
||||
|
|
@ -43,7 +42,7 @@ impl Query {
|
|||
db: &DbConn,
|
||||
field_of_study_opt: Option<String>,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<CandidateParentResult>, DbErr> {
|
||||
) -> Result<Vec<CandidateResult>, DbErr> {
|
||||
let select = Candidate::find();
|
||||
let query = if let Some(study) = field_of_study_opt {
|
||||
select.filter(candidate::Column::Study.eq(study))
|
||||
|
|
@ -51,16 +50,15 @@ impl Query {
|
|||
select
|
||||
}
|
||||
.order_by(candidate::Column::Application, Order::Asc)
|
||||
.join(JoinType::InnerJoin, candidate::Relation::Parent.def())
|
||||
.column_as(parent::Column::Name, "parent_name")
|
||||
.column_as(parent::Column::Surname, "parent_surname")
|
||||
.into_model::<CandidateParentResult>()
|
||||
.paginate(db, PAGE_SIZE);
|
||||
.into_model::<CandidateResult>();
|
||||
|
||||
if let Some(page) = page {
|
||||
query.fetch_page(page).await
|
||||
query
|
||||
.paginate(db, PAGE_SIZE)
|
||||
.fetch_page(page).await
|
||||
} else {
|
||||
query.fetch().await
|
||||
query
|
||||
.all(db).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use chrono::NaiveDate;
|
|||
use sea_orm::FromQueryResult;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use crate::{error::ServiceError};
|
||||
use crate::{error::ServiceError, database::query::candidate::CandidateResult, services::portfolio_service::SubmissionProgress};
|
||||
|
||||
use super::candidate_details::decrypt_if_exists;
|
||||
|
||||
|
|
@ -23,8 +23,10 @@ pub struct BaseCandidateResponse {
|
|||
pub application_id: i32,
|
||||
pub name: String,
|
||||
pub surname: String,
|
||||
pub email: String,
|
||||
pub telephone: String,
|
||||
pub study: String,
|
||||
pub submitted: bool,
|
||||
pub progress: SubmissionProgress,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||
|
|
@ -91,21 +93,23 @@ pub struct Row {
|
|||
impl BaseCandidateResponse {
|
||||
pub async fn from_encrypted(
|
||||
private_key: &String,
|
||||
application_id: i32,
|
||||
name_opt: Option<String>,
|
||||
surname_opt: Option<String>,
|
||||
study_opt: Option<String>,
|
||||
submitted: bool,
|
||||
c: CandidateResult,
|
||||
progress: Option<SubmissionProgress>,
|
||||
) -> Result<Self, ServiceError> {
|
||||
let name = decrypt_if_exists(private_key, name_opt).await?;
|
||||
let surname = decrypt_if_exists(private_key, surname_opt).await?;
|
||||
let name = decrypt_if_exists(private_key, c.name).await?;
|
||||
let surname = decrypt_if_exists(private_key, c.surname).await?;
|
||||
let email = decrypt_if_exists(private_key, c.email).await?;
|
||||
let telephone = decrypt_if_exists(private_key, c.telephone).await?;
|
||||
let progress = progress.unwrap_or(SubmissionProgress::NoneInCache);
|
||||
Ok(
|
||||
Self {
|
||||
application_id: c.application,
|
||||
name,
|
||||
application_id,
|
||||
surname,
|
||||
study: study_opt.unwrap_or("".to_string()),
|
||||
submitted,
|
||||
email,
|
||||
telephone,
|
||||
study: c.study.unwrap_or("".to_string()),
|
||||
progress,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ impl CandidateService {
|
|||
}
|
||||
|
||||
pub async fn list_candidates(
|
||||
private_key: String,
|
||||
private_key: &String,
|
||||
db: &DbConn,
|
||||
field_of_study: Option<String>,
|
||||
page: Option<u64>,
|
||||
|
|
@ -173,22 +173,17 @@ impl CandidateService {
|
|||
page
|
||||
).await?;
|
||||
|
||||
let mut result: Vec<BaseCandidateResponse> = vec![];
|
||||
|
||||
for candidate in candidates {
|
||||
result.push(
|
||||
BaseCandidateResponse::from_encrypted(
|
||||
&private_key,
|
||||
candidate.application,
|
||||
candidate.name,
|
||||
candidate.surname,
|
||||
candidate.study,
|
||||
true
|
||||
).await?
|
||||
)
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
futures::future::try_join_all(
|
||||
candidates
|
||||
.iter()
|
||||
.map(|c| async move {
|
||||
BaseCandidateResponse::from_encrypted(
|
||||
private_key,
|
||||
c.clone(),
|
||||
PortfolioService::get_submission_progress(c.application).await.ok()
|
||||
).await
|
||||
})
|
||||
).await
|
||||
}
|
||||
|
||||
pub fn is_candidate_info(candidate: &candidate::Model) -> bool {
|
||||
|
|
@ -372,12 +367,12 @@ pub mod tests {
|
|||
let db = get_memory_sqlite_connection().await;
|
||||
let admin = create_admin(&db).await;
|
||||
let private_key = crypto::decrypt_password(admin.private_key, "admin".to_string()).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None, None).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(&private_key, &db, None, None).await.unwrap();
|
||||
assert_eq!(candidates.len(), 0);
|
||||
|
||||
put_user_data(&db).await;
|
||||
|
||||
let candidates = CandidateService::list_candidates(private_key.clone(), &db, None, None).await.unwrap();
|
||||
let candidates = CandidateService::list_candidates(&private_key, &db, None, None).await.unwrap();
|
||||
assert_eq!(candidates.len(), 1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||
|
||||
use crate::{error::ServiceError, Query, crypto};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SubmissionProgress {
|
||||
NoneInCache,
|
||||
SomeInCache(Vec<FileType>),
|
||||
|
|
@ -49,7 +50,7 @@ impl Serialize for SubmissionProgress {
|
|||
}
|
||||
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum FileType {
|
||||
CoverLetterPdf = 1,
|
||||
PortfolioLetterPdf = 2,
|
||||
|
|
|
|||
Loading…
Reference in a new issue