feat: NewCandidateResponse

This commit is contained in:
Sebastian Pravda 2022-12-25 22:22:13 +01:00
parent a59b06d18c
commit 28940e46af
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
2 changed files with 27 additions and 4 deletions

View file

@ -2,7 +2,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use portfolio_core::Query;
use portfolio_core::models::auth::AuthenticableTrait;
use portfolio_core::models::candidate::ApplicationDetails;
use portfolio_core::models::candidate::{ApplicationDetails, BaseCandidateResponse, NewCandidateResponse};
use portfolio_core::sea_orm::prelude::Uuid;
use portfolio_core::services::application_service::ApplicationService;
use portfolio_core::services::candidate_service::CandidateService;
@ -72,9 +72,13 @@ pub async fn logout(
}
#[get("/whoami")]
pub async fn whoami(session: CandidateAuth) -> Result<String, Custom<String>> {
pub async fn whoami(session: CandidateAuth) -> Result<Json<NewCandidateResponse>, Custom<String>> {
let private_key = session.get_private_key();
let candidate: entity::candidate::Model = session.into();
Ok(candidate.application.to_string())
let response = NewCandidateResponse::from_encrypted(&private_key, candidate).await
.map_err(to_custom_error)?;
Ok(Json(response))
}
// TODO: use put instead of post???

View file

@ -1,4 +1,5 @@
use chrono::NaiveDate;
use entity::candidate;
use sea_orm::FromQueryResult;
use serde::{Serialize, Deserialize};
@ -6,6 +7,13 @@ use crate::{error::ServiceError, database::query::candidate::CandidateResult, se
use super::candidate_details::decrypt_if_exists;
/// Minimal candidate response containing database only not null fields
#[derive(Debug, Serialize)]
pub struct NewCandidateResponse {
pub application_id: i32,
pub personal_id_number: String,
}
/// Create candidate (admin endpoint)
/// Password change (admin endpoint)
#[derive(Debug, Serialize, Deserialize)]
@ -90,6 +98,18 @@ pub struct Row {
pub second_parent_email: Option<String>,
}
impl NewCandidateResponse {
pub async fn from_encrypted(private_key: &String, c: candidate::Model) -> Result<Self, ServiceError> {
let id_number = decrypt_if_exists(private_key, Some(c.personal_identification_number)).await?;
Ok(
Self {
application_id: c.application,
personal_id_number: id_number,
}
)
}
}
impl BaseCandidateResponse {
pub async fn from_encrypted(
private_key: &String,
@ -113,5 +133,4 @@ impl BaseCandidateResponse {
}
)
}
}