mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-25 13:01:49 +00:00
Merge pull request #110 from EETagent/production_models
(backend) whoami endpoint JSON
This commit is contained in:
commit
0f2967c161
5 changed files with 41 additions and 14 deletions
|
|
@ -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, 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???
|
||||
|
|
@ -254,7 +258,7 @@ pub async fn download_portfolio(session: CandidateAuth) -> Result<Vec<u8>, Custo
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use portfolio_core::{crypto, models::candidate::ApplicationDetails, sea_orm::prelude::Uuid};
|
||||
use portfolio_core::{crypto, models::candidate::{ApplicationDetails, NewCandidateResponse}, sea_orm::prelude::Uuid};
|
||||
use rocket::{
|
||||
http::{Cookie, Status},
|
||||
local::blocking::Client,
|
||||
|
|
@ -262,7 +266,7 @@ mod tests {
|
|||
|
||||
use crate::{
|
||||
routes::admin::tests::admin_login,
|
||||
test::tests::{test_client, APPLICATION_ID, CANDIDATE_PASSWORD},
|
||||
test::tests::{test_client, APPLICATION_ID, CANDIDATE_PASSWORD, PERSONAL_ID_NUMBER},
|
||||
};
|
||||
|
||||
fn candidate_login(client: &Client) -> (Cookie, Cookie) {
|
||||
|
|
@ -294,7 +298,7 @@ mod tests {
|
|||
\"citizenship\": \"Czech Republic\",
|
||||
\"email\": \"magor@magor.cz\",
|
||||
\"sex\": \"MALE\",
|
||||
\"personalIdNumber\": \"0000000000\",
|
||||
\"personalIdNumber\": \"0101010000\",
|
||||
\"study\": \"KB\"
|
||||
},
|
||||
\"parents\": [
|
||||
|
|
@ -324,7 +328,10 @@ mod tests {
|
|||
.dispatch();
|
||||
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
assert_eq!(response.into_string().unwrap(), APPLICATION_ID.to_string());
|
||||
|
||||
let candidate = response.into_json::<NewCandidateResponse>().unwrap();
|
||||
assert_eq!(candidate.application_id, APPLICATION_ID);
|
||||
assert_eq!(candidate.personal_id_number, PERSONAL_ID_NUMBER);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use sea_orm::*;
|
||||
|
||||
use ::entity::{candidate, candidate::Entity as Candidate, parent};
|
||||
use ::entity::{candidate, candidate::Entity as Candidate};
|
||||
|
||||
use crate::Query;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use chrono::NaiveDate;
|
||||
use entity::candidate;
|
||||
use sea_orm::FromQueryResult;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
|
@ -6,6 +7,14 @@ 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, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
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 +99,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 +134,4 @@ impl BaseCandidateResponse {
|
|||
}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -335,12 +335,12 @@ pub async fn decrypt_if_exists(
|
|||
pub mod tests {
|
||||
use std::sync::Mutex;
|
||||
|
||||
use chrono::{Local};
|
||||
use chrono::Local;
|
||||
use entity::admin;
|
||||
use once_cell::sync::Lazy;
|
||||
use sea_orm::{DbConn, Set, ActiveModelTrait};
|
||||
|
||||
use crate::{crypto, models::candidate::{CandidateDetails, ParentDetails}, utils::db::get_memory_sqlite_connection, Query, services::candidate_service::tests::put_user_data};
|
||||
use crate::{crypto, models::candidate::{CandidateDetails, ParentDetails}, utils::db::get_memory_sqlite_connection, services::candidate_service::tests::put_user_data};
|
||||
|
||||
use super::{ApplicationDetails, EncryptedApplicationDetails, EncryptedString};
|
||||
|
||||
|
|
@ -353,7 +353,7 @@ pub mod tests {
|
|||
name: "name".to_string(),
|
||||
surname: "surname".to_string(),
|
||||
birthplace: "birthplace".to_string(),
|
||||
birthdate: chrono::NaiveDate::from_ymd(2000, 1, 1),
|
||||
birthdate: chrono::NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
|
||||
address: "address".to_string(),
|
||||
telephone: "telephone".to_string(),
|
||||
citizenship: "citizenship".to_string(),
|
||||
|
|
@ -375,7 +375,7 @@ pub mod tests {
|
|||
assert_eq!(details.candidate.name, "name");
|
||||
assert_eq!(details.candidate.surname, "surname");
|
||||
assert_eq!(details.candidate.birthplace, "birthplace");
|
||||
assert_eq!(details.candidate.birthdate, chrono::NaiveDate::from_ymd(2000, 1, 1));
|
||||
assert_eq!(details.candidate.birthdate, chrono::NaiveDate::from_ymd_opt(2000, 1, 1).unwrap());
|
||||
assert_eq!(details.candidate.address, "address");
|
||||
assert_eq!(details.candidate.telephone, "telephone");
|
||||
assert_eq!(details.candidate.citizenship, "citizenship");
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ mod tests {
|
|||
name: "name".to_string(),
|
||||
surname: "surname".to_string(),
|
||||
birthplace: "birthplace".to_string(),
|
||||
birthdate: chrono::NaiveDate::from_ymd(2000, 1, 1),
|
||||
birthdate: chrono::NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
|
||||
address: "address".to_string(),
|
||||
telephone: "telephone".to_string(),
|
||||
citizenship: "citizenship".to_string(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue