feat: return more data in candidate list

This commit is contained in:
Sebastian Pravda 2022-12-22 16:40:24 +01:00
parent 0a610fb0ee
commit 41dd84b22d
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
4 changed files with 32 additions and 39 deletions

View file

@ -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)?;

View file

@ -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,10 +50,7 @@ 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>()
.into_model::<CandidateResult>()
.paginate(db, PAGE_SIZE);
if let Some(page) = page {

View file

@ -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};
use super::candidate_details::decrypt_if_exists;
@ -23,6 +23,8 @@ 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,
}
@ -91,20 +93,21 @@ 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>,
c: CandidateResult,
submitted: bool,
) -> 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?;
Ok(
Self {
application_id: c.application,
name,
application_id,
surname,
study: study_opt.unwrap_or("".to_string()),
email,
telephone,
study: c.study.unwrap_or("".to_string()),
submitted,
}
)

View file

@ -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,16 @@ 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(),
true).await
})
).await
}
pub fn is_candidate_info(candidate: &candidate::Model) -> bool {
@ -372,12 +366,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);
}