From 28940e46afdbe10a24fbbff3f0326ae46820ae9f Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sun, 25 Dec 2022 22:22:13 +0100 Subject: [PATCH] feat: NewCandidateResponse --- api/src/routes/candidate.rs | 10 +++++++--- core/src/models/candidate.rs | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/api/src/routes/candidate.rs b/api/src/routes/candidate.rs index 88a63d4..c69dd1a 100644 --- a/api/src/routes/candidate.rs +++ b/api/src/routes/candidate.rs @@ -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> { +pub async fn whoami(session: CandidateAuth) -> Result, Custom> { + 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??? diff --git a/core/src/models/candidate.rs b/core/src/models/candidate.rs index c875763..d185ee9 100644 --- a/core/src/models/candidate.rs +++ b/core/src/models/candidate.rs @@ -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, } +impl NewCandidateResponse { + pub async fn from_encrypted(private_key: &String, c: candidate::Model) -> Result { + 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 { } ) } - } \ No newline at end of file