feat: parent details

This commit is contained in:
Sebastian Pravda 2022-11-11 23:29:09 +01:00
parent 3dff252a0a
commit 9e1ba16ef6
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
16 changed files with 184 additions and 127 deletions

View file

@ -75,22 +75,14 @@ pub async fn create_candidate(
let plain_text_password = random_8_char_string(); let plain_text_password = random_8_char_string();
let candidate = CandidateService::create( CandidateService::create(
db, db,
form.application_id, form.application_id,
&plain_text_password, &plain_text_password,
form.personal_id_number, form.personal_id_number,
) )
.await; .await
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?;
if candidate.is_err() {
// TODO cleanup
let e = candidate.err().unwrap();
return Err(Custom(
Status::from_code(e.code()).unwrap_or_default(),
e.message(),
));
}
Ok(plain_text_password) Ok(plain_text_password)
} }

View file

@ -1,11 +1,12 @@
use chrono::{NaiveDate}; use chrono::{NaiveDate};
use entity::candidate; use entity::{candidate, parent};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::{error::ServiceError, crypto}; use crate::{error::ServiceError, crypto};
pub const NAIVE_DATE_FMT: &str = "%Y-%m-%d"; pub const NAIVE_DATE_FMT: &str = "%Y-%m-%d";
#[derive(Clone)]
pub struct EncryptedString(String); pub struct EncryptedString(String);
impl EncryptedString { impl EncryptedString {
@ -56,7 +57,9 @@ impl TryFrom<Option<NaiveDate>> for EncryptedString { // TODO: take a look at th
} }
} }
pub(crate) struct EncryptedCandidateDetails { #[derive(Clone)]
pub struct EncryptedCandidateDetails {
// Candidate
pub name: EncryptedString, pub name: EncryptedString,
pub surname: EncryptedString, pub surname: EncryptedString,
pub birthplace: EncryptedString, pub birthplace: EncryptedString,
@ -67,6 +70,12 @@ pub(crate) struct EncryptedCandidateDetails {
pub email: EncryptedString, pub email: EncryptedString,
pub sex: EncryptedString, pub sex: EncryptedString,
pub study: EncryptedString, pub study: EncryptedString,
// Parent
pub parent_name: EncryptedString,
pub parent_surname: EncryptedString,
pub parent_telephone: EncryptedString,
pub parent_email: EncryptedString,
} }
impl EncryptedCandidateDetails { impl EncryptedCandidateDetails {
@ -76,13 +85,18 @@ impl EncryptedCandidateDetails {
EncryptedString::new(&form.name, &recipients), EncryptedString::new(&form.name, &recipients),
EncryptedString::new(&form.surname, &recipients), EncryptedString::new(&form.surname, &recipients),
EncryptedString::new(&form.birthplace, &recipients), EncryptedString::new(&form.birthplace, &recipients),
EncryptedString::new(&birthdate_str, &recipients), // TODO EncryptedString::new(&birthdate_str, &recipients),
EncryptedString::new(&form.address, &recipients), EncryptedString::new(&form.address, &recipients),
EncryptedString::new(&form.telephone, &recipients), EncryptedString::new(&form.telephone, &recipients),
EncryptedString::new(&form.citizenship, &recipients), EncryptedString::new(&form.citizenship, &recipients),
EncryptedString::new(&form.email, &recipients), EncryptedString::new(&form.email, &recipients),
EncryptedString::new(&form.sex, &recipients), EncryptedString::new(&form.sex, &recipients),
EncryptedString::new(&form.study, &recipients), EncryptedString::new(&form.study, &recipients),
EncryptedString::new(&form.parent_name, &recipients),
EncryptedString::new(&form.parent_surname, &recipients),
EncryptedString::new(&form.parent_telephone, &recipients),
EncryptedString::new(&form.parent_email, &recipients),
)?; )?;
Ok(EncryptedCandidateDetails { Ok(EncryptedCandidateDetails {
@ -96,21 +110,31 @@ impl EncryptedCandidateDetails {
email: d.7, email: d.7,
sex: d.8, sex: d.8,
study: d.9, study: d.9,
parent_name: d.10,
parent_surname: d.11,
parent_telephone: d.12,
parent_email: d.13,
}) })
} }
pub async fn decrypt(self, priv_key: String) -> Result<CandidateDetails, ServiceError> { pub async fn decrypt(self, priv_key: String) -> Result<CandidateDetails, ServiceError> {
let d = tokio::try_join!( let d = tokio::try_join!(
self.name.decrypt(&priv_key), self.name.decrypt(&priv_key), // 0
self.surname.decrypt(&priv_key), self.surname.decrypt(&priv_key), // 1
self.birthplace.decrypt(&priv_key), self.birthplace.decrypt(&priv_key), // 2
self.birthdate.decrypt(&priv_key), self.birthdate.decrypt(&priv_key), // 3
self.address.decrypt(&priv_key), self.address.decrypt(&priv_key), // 4
self.telephone.decrypt(&priv_key), self.telephone.decrypt(&priv_key), // 5
self.citizenship.decrypt(&priv_key), self.citizenship.decrypt(&priv_key), // 6
self.email.decrypt(&priv_key), self.email.decrypt(&priv_key), // 7
self.sex.decrypt(&priv_key), self.sex.decrypt(&priv_key), // 8
self.study.decrypt(&priv_key), self.study.decrypt(&priv_key), // 9
self.parent_name.decrypt(&priv_key),
self.parent_surname.decrypt(&priv_key),
self.parent_telephone.decrypt(&priv_key),
self.parent_email.decrypt(&priv_key),
)?; )?;
Ok(CandidateDetails { Ok(CandidateDetails {
@ -124,14 +148,19 @@ impl EncryptedCandidateDetails {
email: d.7, email: d.7,
sex: d.8, sex: d.8,
study: d.9, study: d.9,
parent_name: d.10,
parent_surname: d.11,
parent_telephone: d.12,
parent_email: d.13,
}) })
} }
} }
impl TryFrom<candidate::Model> for EncryptedCandidateDetails { impl TryFrom<(candidate::Model, parent::Model)> for EncryptedCandidateDetails {
type Error = ServiceError; type Error = ServiceError;
fn try_from(candidate: candidate::Model) -> Result<Self, Self::Error> { fn try_from((candidate, parent): (candidate::Model, parent::Model)) -> Result<Self, Self::Error> {
Ok(EncryptedCandidateDetails { Ok(EncryptedCandidateDetails {
name: EncryptedString::try_from(candidate.name)?, name: EncryptedString::try_from(candidate.name)?,
surname: EncryptedString::try_from(candidate.surname)?, surname: EncryptedString::try_from(candidate.surname)?,
@ -143,12 +172,18 @@ impl TryFrom<candidate::Model> for EncryptedCandidateDetails {
email: EncryptedString::try_from(candidate.email)?, email: EncryptedString::try_from(candidate.email)?,
sex: EncryptedString::try_from(candidate.sex)?, sex: EncryptedString::try_from(candidate.sex)?,
study: EncryptedString::try_from(candidate.study)?, study: EncryptedString::try_from(candidate.study)?,
parent_name: EncryptedString::try_from(parent.name)?,
parent_surname: EncryptedString::try_from(parent.surname)?,
parent_telephone: EncryptedString::try_from(parent.telephone)?,
parent_email: EncryptedString::try_from(parent.email)?,
}) })
} }
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct CandidateDetails { pub struct CandidateDetails {
// Candidate
pub name: String, pub name: String,
pub surname: String, pub surname: String,
pub birthplace: String, pub birthplace: String,
@ -159,4 +194,10 @@ pub struct CandidateDetails {
pub email: String, pub email: String,
pub sex: String, pub sex: String,
pub study: String, pub study: String,
// Parent
pub parent_name: String,
pub parent_surname: String,
pub parent_telephone: String,
pub parent_email: String,
} }

View file

@ -1,4 +1,4 @@
use crate::Mutation; use crate::{Mutation, candidate_details::EncryptedCandidateDetails};
use ::entity::parent::{self, Model}; use ::entity::parent::{self, Model};
use sea_orm::*; use sea_orm::*;
@ -17,17 +17,14 @@ impl Mutation {
pub async fn add_parent_details( pub async fn add_parent_details(
db: &DbConn, db: &DbConn,
user: Model, parent: Model,
name: String, enc_details: EncryptedCandidateDetails, // TODO: use seperate struct??
surname: String,
telephone: String,
email: String,
) -> Result<Model, sea_orm::DbErr> { ) -> Result<Model, sea_orm::DbErr> {
let mut user: parent::ActiveModel = user.into(); let mut user: parent::ActiveModel = parent.into();
user.name = Set(Some(name)); user.name = Set(Some(enc_details.parent_name.into()));
user.surname = Set(Some(surname)); user.surname = Set(Some(enc_details.parent_surname.into()));
user.telephone = Set(Some(telephone)); user.telephone = Set(Some(enc_details.parent_telephone.into()));
user.email = Set(Some(email)); user.email = Set(Some(enc_details.parent_email.into()));
user.updated_at = Set(chrono::offset::Local::now().naive_local()); user.updated_at = Set(chrono::offset::Local::now().naive_local());

View file

@ -3,3 +3,4 @@ pub struct Query;
pub mod candidate; pub mod candidate;
pub mod admin; pub mod admin;
pub mod session; pub mod session;
pub mod parent;

View file

@ -0,0 +1,17 @@
use entity::parent::Model;
use entity::parent::Entity;
use sea_orm::{DbConn, DbErr};
use sea_orm::EntityTrait;
use crate::Query;
impl Query {
pub async fn find_parent_by_id(
db: &DbConn,
application_id: i32,
) -> Result<Option<Model>, DbErr> {
Entity::find_by_id(application_id).one(db).await
}
}

View file

@ -5,7 +5,8 @@ pub enum ServiceError {
ExpiredSession, ExpiredSession,
JwtError, JwtError,
UserAlreadyExists, UserAlreadyExists,
UserNotFound, CandidateNotFound,
ParentNotFound,
DbError, DbError,
UserNotFoundByJwtId, UserNotFoundByJwtId,
UserNotFoundBySessionId, UserNotFoundBySessionId,
@ -24,7 +25,8 @@ impl ServiceError {
ServiceError::ExpiredSession => (401, "Session expired, please login again".to_string()), ServiceError::ExpiredSession => (401, "Session expired, please login again".to_string()),
ServiceError::JwtError => (500, "Error while encoding JWT".to_string()), ServiceError::JwtError => (500, "Error while encoding JWT".to_string()),
ServiceError::UserAlreadyExists => (409, "User already exists".to_string()), ServiceError::UserAlreadyExists => (409, "User already exists".to_string()),
ServiceError::UserNotFound => (404, "User not found".to_string()), ServiceError::CandidateNotFound => (404, "User not found".to_string()),
ServiceError::ParentNotFound => (500, "Parent not found".to_string()),
ServiceError::DbError => (500, "Database error".to_string()), ServiceError::DbError => (500, "Database error".to_string()),
ServiceError::UserNotFoundByJwtId => (500, "User not found, please contact technical support".to_string()), ServiceError::UserNotFoundByJwtId => (500, "User not found, please contact technical support".to_string()),
ServiceError::UserNotFoundBySessionId => (500, "User not found, please contact technical support".to_string()), ServiceError::UserNotFoundBySessionId => (500, "User not found, please contact technical support".to_string()),

View file

@ -20,7 +20,7 @@ impl AdminService {
}; };
let Some(admin) = admin else { let Some(admin) = admin else {
return Err(ServiceError::UserNotFound); return Err(ServiceError::CandidateNotFound);
}; };
let private_key_encrypted = admin.private_key; let private_key_encrypted = admin.private_key;

View file

@ -0,0 +1,5 @@
/* pub struct ApplicationService;
impl ApplicationService {
pub fn create
} */

View file

@ -1,4 +1,4 @@
use entity::candidate; use entity::{candidate};
use sea_orm::{prelude::Uuid, DbConn}; use sea_orm::{prelude::Uuid, DbConn};
use crate::{ use crate::{
@ -7,7 +7,7 @@ use crate::{
Mutation, Query, candidate_details::{CandidateDetails, EncryptedCandidateDetails}, Mutation, Query, candidate_details::{CandidateDetails, EncryptedCandidateDetails},
}; };
use super::session_service::{AdminUser, SessionService}; use super::{session_service::{AdminUser, SessionService}, parent_service::ParentService};
const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"]; const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"];
@ -52,9 +52,9 @@ impl CandidateService {
let Ok(hashed_personal_id_number) = hash_password(personal_id_number).await else { let Ok(hashed_personal_id_number) = hash_password(personal_id_number).await else {
return Err(ServiceError::CryptoHashFailed); return Err(ServiceError::CryptoHashFailed);
}; };
/* let encrypted_personal_id_number = crypto::encrypt_password_with_recipients(
&personal_id_number, &vec![&pubkey] ParentService::create_parent(db, application_id)
).await.unwrap(); */ .await?;
Mutation::create_candidate( Mutation::create_candidate(
db, db,
@ -64,8 +64,8 @@ impl CandidateService {
pubkey, pubkey,
encrypted_priv_key, encrypted_priv_key,
) )
.await .await
.map_err(|_| ServiceError::DbError) .map_err(|_| ServiceError::DbError)
} }
pub async fn add_candidate_details( pub async fn add_candidate_details(
@ -86,20 +86,22 @@ impl CandidateService {
let enc_details = EncryptedCandidateDetails::new(form, recipients).await?; let enc_details = EncryptedCandidateDetails::new(form, recipients).await?;
Mutation::add_candidate_details(db, candidate, enc_details) ParentService::add_parent_details(db, candidate.application, enc_details.clone()).await?;
Mutation::add_candidate_details(db, candidate, enc_details.clone())
.await .await
.map_err(|_| ServiceError::DbError) .map_err(|_| ServiceError::DbError)
} }
pub async fn decrypt_details( pub async fn decrypt_details(
db: &DbConn, db: &DbConn,
candidate_id: i32, application_id: i32,
password: String, password: String,
) -> Result<CandidateDetails, ServiceError> { ) -> Result<CandidateDetails, ServiceError> {
let candidate = match Query::find_candidate_by_id(db, candidate_id).await { let candidate = match Query::find_candidate_by_id(db, application_id).await {
Ok(candidate) => candidate.unwrap(), Ok(candidate) => candidate.unwrap(),
Err(_) => return Err(ServiceError::DbError), // TODO: logging Err(_) => return Err(ServiceError::DbError), // TODO: logging
}; };
let parent = Query::find_parent_by_id(db, application_id).await.unwrap().unwrap();
match crypto::verify_password((&password).to_string(), candidate.code.clone()).await { match crypto::verify_password((&password).to_string(), candidate.code.clone()).await {
Ok(valid) => { Ok(valid) => {
@ -114,7 +116,7 @@ impl CandidateService {
.await .await
.ok() .ok()
.unwrap(); .unwrap();
let enc_details = EncryptedCandidateDetails::try_from(candidate)?; let enc_details = EncryptedCandidateDetails::try_from((candidate, parent))?;
enc_details.decrypt(dec_priv_key).await enc_details.decrypt(dec_priv_key).await
} }
@ -162,7 +164,7 @@ impl CandidateService {
}; };
let Some(candidate) = candidate else { let Some(candidate) = candidate else {
return Err(ServiceError::UserNotFound); return Err(ServiceError::CandidateNotFound);
}; };
let private_key_encrypted = candidate.private_key; let private_key_encrypted = candidate.private_key;
@ -221,7 +223,7 @@ mod tests {
use crate::{ use crate::{
crypto, crypto,
services::candidate_service::{CandidateService, CandidateDetails}, services::candidate_service::{CandidateService, CandidateDetails}, Query, Mutation,
}; };
use super::EncryptedCandidateDetails; use super::EncryptedCandidateDetails;
@ -239,7 +241,7 @@ mod tests {
#[cfg(test)] #[cfg(test)]
async fn get_memory_sqlite_connection() -> DbConn { async fn get_memory_sqlite_connection() -> DbConn {
use entity::{admin, candidate}; use entity::{admin, candidate, parent};
use sea_orm::Schema; use sea_orm::Schema;
use sea_orm::{sea_query::TableCreateStatement, ConnectionTrait, DbBackend}; use sea_orm::{sea_query::TableCreateStatement, ConnectionTrait, DbBackend};
@ -248,8 +250,8 @@ mod tests {
let schema = Schema::new(DbBackend::Sqlite); let schema = Schema::new(DbBackend::Sqlite);
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity); let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
let stmt2: TableCreateStatement = schema.create_table_from_entity(admin::Entity); let stmt2: TableCreateStatement = schema.create_table_from_entity(admin::Entity);
let stmt3: TableCreateStatement = schema.create_table_from_entity(parent::Entity);
db.execute(db.get_database_backend().build(&stmt)) db.execute(db.get_database_backend().build(&stmt))
.await .await
@ -257,6 +259,9 @@ mod tests {
db.execute(db.get_database_backend().build(&stmt2)) db.execute(db.get_database_backend().build(&stmt2))
.await .await
.unwrap(); .unwrap();
db.execute(db.get_database_backend().build(&stmt3))
.await
.unwrap();
db db
} }
@ -268,6 +273,9 @@ mod tests {
let secret_message = "trnka".to_string(); let secret_message = "trnka".to_string();
Mutation::create_parent(&db, 1)
.await.unwrap();
let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string()) let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string())
.await .await
.ok() .ok()
@ -303,7 +311,7 @@ mod tests {
let form = CandidateDetails { let form = CandidateDetails {
name: "test".to_string(), name: "test".to_string(),
surname: "a".to_string(), surname: "aaa".to_string(),
birthplace: "b".to_string(), birthplace: "b".to_string(),
birthdate: NaiveDate::from_ymd(1999, 1, 1), birthdate: NaiveDate::from_ymd(1999, 1, 1),
address: "test".to_string(), address: "test".to_string(),
@ -312,10 +320,15 @@ mod tests {
email: "test".to_string(), email: "test".to_string(),
sex: "test".to_string(), sex: "test".to_string(),
study: "test".to_string(), study: "test".to_string(),
parent_name: "test".to_string(),
parent_surname: "test".to_string(),
parent_telephone: "test".to_string(),
parent_email: "test".to_string(),
}; };
CandidateService::add_candidate_details(&db, candidate, form) CandidateService::add_candidate_details(&db, candidate, form)
.await .await
.ok()
.unwrap() .unwrap()
} }
@ -331,16 +344,15 @@ mod tests {
let password = "test".to_string(); let password = "test".to_string();
let db = get_memory_sqlite_connection().await; let db = get_memory_sqlite_connection().await;
let enc_candidate = put_user_data(&db).await; let enc_candidate = put_user_data(&db).await;
let enc_parent = Query::find_parent_by_id(&db, enc_candidate.application).await.unwrap().unwrap();
let dec_priv_key = crypto::decrypt_password(enc_candidate.private_key.clone(), password) let dec_priv_key = crypto::decrypt_password(enc_candidate.private_key.clone(), password)
.await .await
.unwrap(); .unwrap();
let dec_candidate = EncryptedCandidateDetails::try_from(enc_candidate) let enc_details = EncryptedCandidateDetails::try_from((enc_candidate, enc_parent)).ok().unwrap();
.unwrap() let dec_details = enc_details.decrypt(dec_priv_key).await.ok().unwrap();
.decrypt(dec_priv_key)
.await
.unwrap();
assert_eq!(dec_candidate.name, "test"); assert_eq!(dec_details.name, "test"); // TODO: test every element
assert_eq!(dec_details.parent_surname, "test");
} }
} }

View file

@ -1,3 +1,5 @@
pub mod session_service; pub mod session_service;
pub mod candidate_service; pub mod candidate_service;
pub mod admin_service; pub mod admin_service;
pub mod parent_service;
pub mod application_service;

View file

@ -0,0 +1,36 @@
use entity::{parent};
use sea_orm::DbConn;
use crate::{error::ServiceError, Mutation, candidate_details::EncryptedCandidateDetails, Query};
pub struct ParentService;
impl ParentService {
pub async fn create_parent(
db: &DbConn,
application_id: i32,
) -> Result<parent::Model, ServiceError> {
let parent = Mutation::create_parent(db, application_id)
.await
.map_err(|_| ServiceError::DbError)?;
Ok(parent)
}
pub async fn add_parent_details(
db: &DbConn,
application_id: i32,
enc_details: EncryptedCandidateDetails,
) -> Result<parent::Model, ServiceError> {
let parent = Query::find_parent_by_id(db, application_id)
.await
.map_err(|_| ServiceError::DbError)?
.ok_or(ServiceError::ParentNotFound)?;
let parent = Mutation::add_parent_details(db, parent, enc_details)
.await
.map_err(|_| ServiceError::DbError)?;
Ok(parent)
}
}

View file

@ -57,7 +57,7 @@ impl SessionService {
let candidate = match Query::find_candidate_by_id(db, user_id.unwrap()).await { let candidate = match Query::find_candidate_by_id(db, user_id.unwrap()).await {
Ok(candidate) => match candidate { Ok(candidate) => match candidate {
Some(candidate) => candidate, Some(candidate) => candidate,
None => return Err(ServiceError::UserNotFound), None => return Err(ServiceError::CandidateNotFound),
}, },
Err(_) => return Err(ServiceError::DbError), Err(_) => return Err(ServiceError::DbError),
}; };
@ -78,7 +78,7 @@ impl SessionService {
let admin = match Query::find_admin_by_id(db, admin_id.unwrap()).await { let admin = match Query::find_admin_by_id(db, admin_id.unwrap()).await {
Ok(admin) => match admin { Ok(admin) => match admin {
Some(admin) => admin, Some(admin) => admin,
None => return Err(ServiceError::UserNotFound), None => return Err(ServiceError::CandidateNotFound),
}, },
Err(_) => return Err(ServiceError::DbError), Err(_) => return Err(ServiceError::DbError),
}; };
@ -162,7 +162,7 @@ impl SessionService {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use entity::{admin, candidate, session}; use entity::{admin, candidate, session, parent};
use sea_orm::{ use sea_orm::{
prelude::Uuid, sea_query::TableCreateStatement, ConnectionTrait, Database, DbBackend, prelude::Uuid, sea_query::TableCreateStatement, ConnectionTrait, Database, DbBackend,
@ -183,6 +183,7 @@ mod tests {
let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity); let stmt: TableCreateStatement = schema.create_table_from_entity(candidate::Entity);
let stmt2: TableCreateStatement = schema.create_table_from_entity(admin::Entity); let stmt2: TableCreateStatement = schema.create_table_from_entity(admin::Entity);
let stmt3: TableCreateStatement = schema.create_table_from_entity(session::Entity); let stmt3: TableCreateStatement = schema.create_table_from_entity(session::Entity);
let stmt4: TableCreateStatement = schema.create_table_from_entity(parent::Entity);
db.execute(db.get_database_backend().build(&stmt)) db.execute(db.get_database_backend().build(&stmt))
.await .await
.unwrap(); .unwrap();
@ -192,6 +193,9 @@ mod tests {
db.execute(db.get_database_backend().build(&stmt3)) db.execute(db.get_database_backend().build(&stmt3))
.await .await
.unwrap(); .unwrap();
db.execute(db.get_database_backend().build(&stmt4))
.await
.unwrap();
db db
} }
@ -218,9 +222,8 @@ mod tests {
async fn test_candidate_session_correct_password() { async fn test_candidate_session_correct_password() {
let db = &get_memory_sqlite_connection().await; let db = &get_memory_sqlite_connection().await;
CandidateService::create(&db, 103151, &"Tajny_kod".to_string(), "".to_string()) CandidateService::create(db, 103151, &"Tajny_kod".to_string(), "".to_string())
.await .await
.ok()
.unwrap(); .unwrap();
// correct password // correct password
@ -231,9 +234,8 @@ mod tests {
"Tajny_kod".to_string(), "Tajny_kod".to_string(),
"127.0.0.1".to_string(), "127.0.0.1".to_string(),
) )
.await .await
.ok() .unwrap();
.unwrap();
// println!("{}", session.err().unwrap().1); // println!("{}", session.err().unwrap().1);
assert!( assert!(
SessionService::auth_user_session(db, Uuid::parse_str(&session).unwrap()) SessionService::auth_user_session(db, Uuid::parse_str(&session).unwrap())
@ -249,7 +251,6 @@ mod tests {
let candidate_form = let candidate_form =
CandidateService::create(&db, 103151, &"Tajny_kod".to_string(), "".to_string()) CandidateService::create(&db, 103151, &"Tajny_kod".to_string(), "".to_string())
.await .await
.ok()
.unwrap(); .unwrap();
// incorrect password // incorrect password

View file

@ -3,7 +3,7 @@ use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "candidate")] #[sea_orm(table_name = "candidate")]
pub struct Model { pub struct Model {
#[sea_orm(column_type = "Integer", primary_key, auto_increment = false)] #[sea_orm(primary_key, auto_increment = false)]
pub application: i32, pub application: i32,
pub code: String, pub code: String,
pub name: Option<String>, pub name: Option<String>,
@ -17,8 +17,8 @@ pub struct Model {
pub email: Option<String>, pub email: Option<String>,
pub sex: Option<String>, pub sex: Option<String>,
pub study: Option<String>, pub study: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub personal_identification_number: Option<String>, pub personal_identification_number: Option<String>,
#[sea_orm(column_type = "Text")]
pub personal_identification_number_hash: String, pub personal_identification_number_hash: String,
pub public_key: String, pub public_key: String,
pub private_key: String, pub private_key: String,
@ -28,18 +28,10 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { pub enum Relation {
#[sea_orm(has_one = "super::parent::Entity")]
Parent,
#[sea_orm(has_many = "super::session::Entity")] #[sea_orm(has_many = "super::session::Entity")]
Session, Session,
} }
impl Related<super::parent::Entity> for Entity {
fn to() -> RelationDef {
Relation::Parent.def()
}
}
impl Related<super::session::Entity> for Entity { impl Related<super::session::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::Session.def() Relation::Session.def()

View file

@ -14,19 +14,6 @@ pub struct Model {
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { pub enum Relation {}
#[sea_orm(
belongs_to = "super::candidate::Entity",
from = "Column::Application",
to = "super::candidate::Column::Application"
)]
Candidate,
}
impl Related<super::candidate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Candidate.def()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View file

@ -7,7 +7,6 @@ mod m20221024_134454_insert_sample_admin;
mod m20221025_154422_create_session; mod m20221025_154422_create_session;
mod m20221027_194728_session_create_user_fk; mod m20221027_194728_session_create_user_fk;
mod m20221028_194728_session_create_admin_fk; mod m20221028_194728_session_create_admin_fk;
mod m20221030_133428_parent_create_candidate_fk;
pub struct Migrator; pub struct Migrator;
#[async_trait::async_trait] #[async_trait::async_trait]
@ -21,7 +20,6 @@ impl MigratorTrait for Migrator {
Box::new(m20221025_154422_create_session::Migration), Box::new(m20221025_154422_create_session::Migration),
Box::new(m20221027_194728_session_create_user_fk::Migration), Box::new(m20221027_194728_session_create_user_fk::Migration),
Box::new(m20221028_194728_session_create_admin_fk::Migration), Box::new(m20221028_194728_session_create_admin_fk::Migration),
Box::new(m20221030_133428_parent_create_candidate_fk::Migration),
] ]
} }
} }

View file

@ -1,26 +0,0 @@
use sea_orm_migration::prelude::*;
use crate::{m20221024_124701_create_parent::Parent, m20221024_121621_create_candidate::Candidate};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.create_foreign_key(ForeignKey::create()
.name("candidate_fk")
.from(Parent::Table, Parent::Application)
.to(Candidate::Table, Candidate::Application)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.to_owned()).await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_foreign_key(ForeignKey::drop()
.name("candidate_fk")
.table(Parent::Table)
.to_owned()).await
}
}