feat: code cleanup

This commit is contained in:
Sebastian Pravda 2022-12-22 11:37:01 +01:00
parent da538a50b3
commit a96c0ce9f7
3 changed files with 19 additions and 24 deletions

View file

@ -22,11 +22,17 @@ impl Query {
}
pub async fn find_related_candidate_sessions(db: &DbConn, candidate: candidate::Model) -> Result<Vec<session::Model>, DbErr> {
candidate.find_related(Session).all(db).await
candidate.find_related(Session)
.order_by_asc(session::Column::UpdatedAt)
.all(db)
.await
}
pub async fn find_related_admin_sessions(db: &DbConn, admin: admin::Model) -> Result<Vec<admin_session::Model>, DbErr> {
admin.find_related(admin_session::Entity).all(db).await
admin.find_related(admin_session::Entity)
.order_by_asc(admin_session::Column::UpdatedAt)
.all(db)
.await
}
// find session by user id
@ -50,7 +56,6 @@ impl Query {
#[cfg(test)]
mod tests {
use entity::{session, admin, candidate, admin_session};
use sea_orm::ActiveValue::NotSet;
use sea_orm::{prelude::Uuid, ActiveModelTrait, Set};
use crate::utils::db::get_memory_sqlite_connection;

View file

@ -82,8 +82,7 @@ impl AuthenticableTrait for AdminService {
let session = Mutation::insert_admin_session(db, admin.id, random_uuid, ip_addr).await?;
Self::delete_old_sessions(db, admin, 1)
.await?;
Self::delete_old_sessions(db, admin, 1).await?;
Ok(session.id.to_string())
}
@ -92,14 +91,11 @@ impl AuthenticableTrait for AdminService {
admin: admin::Model,
keep_n_recent: usize,
) -> Result<(), ServiceError> {
let mut sessions = Query::find_related_admin_sessions(db, admin)
.await?;
sessions.sort_by_key(|s| s.created_at);
let sessions = sessions.iter()
let sessions = Query::find_related_admin_sessions(db, admin)
.await?
.iter()
.map(|s| s.clone().into_active_model())
.collect::<Vec<admin_session::ActiveModel>>();
.collect();
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
Ok(())

View file

@ -250,8 +250,7 @@ impl AuthenticableTrait for CandidateService {
.await?
.ok_or(ServiceError::CandidateNotFound)?;
let session_id = Self::new_session(db, candidate.clone(), password.clone(), ip_addr)
.await?;
let session_id = Self::new_session(db, candidate.clone(), password.clone(), ip_addr).await?;
let private_key = Self::decrypt_private_key(candidate, password).await?;
Ok((session_id, private_key))
@ -296,9 +295,7 @@ impl AuthenticableTrait for CandidateService {
let session = Mutation::insert_candidate_session(db, random_uuid, candidate.application, ip_addr).await?;
Self::delete_old_sessions(db, candidate, 3)
.await
.ok();
Self::delete_old_sessions(db, candidate, 3).await?;
Ok(session.id.to_string())
}
@ -307,14 +304,11 @@ impl AuthenticableTrait for CandidateService {
candidate: candidate::Model,
keep_n_recent: usize,
) -> Result<(), ServiceError> {
let mut sessions = Query::find_related_candidate_sessions(db, candidate)
.await?;
sessions.sort_by_key(|s| s.created_at);
let sessions = sessions.iter()
let sessions = Query::find_related_candidate_sessions(db, candidate)
.await?
.iter()
.map(|s| s.clone().into_active_model())
.collect::<Vec<session::ActiveModel>>();
.collect();
SessionService::delete_sessions(db, sessions, keep_n_recent).await?;
Ok(())