diff --git a/src/core/user.rs b/src/core/user.rs index f3be97d..d880521 100644 --- a/src/core/user.rs +++ b/src/core/user.rs @@ -10,7 +10,7 @@ use crate::{ }; use bson::{doc, oid::ObjectId, uuid, DateTime}; use futures::StreamExt; -use mongodb::{Client, Collection}; +use mongodb::{options::FindOptions, Client, Collection}; use serde::{Deserialize, Serialize}; use super::{dek::Dek, session::Session}; @@ -182,58 +182,52 @@ impl User { ) -> Result> { let db = mongo_client.database("auth"); let collection: Collection = db.collection("users"); - let collection_dek: Collection = db.collection("deks"); - let mut cursor_dek = collection_dek.find(None, None).await.unwrap(); + // get recent users from the users collection till the limit provided sort by created_at + + let find_options = FindOptions::builder() + .sort(doc! { "created_at": -1 }) + .limit(limit) + .build(); + + let mut cursor = collection + .find(None, find_options) + .await + .unwrap(); + let mut users = Vec::new(); - let kek = env::var("SERVER_KEK").expect("Server Kek must be set."); // iterate over the users and decrypt the data - while let Some(dek) = cursor_dek.next().await { - let dek_data: Dek = match dek { - Ok(data) => data.decrypt(&kek), + while let Some(user) = cursor.next().await { + let user_data = match user { + Ok(data) => data, Err(_) => { return Err(Error::ServerError { - message: "Failed to get DEK".to_string(), + message: "Failed to get User".to_string(), }); } }; - let encrypted_email_dek = Encryption::encrypt_data(&dek_data.email, &dek_data.dek); - - // find the user in the users collection using the encrypted email to iterate over the users - let cursor_user = collection - .find_one( - Some(doc! { - "email": encrypted_email_dek, - }), - None, - ) - .await - .unwrap(); - - match cursor_user { - Some(user) => { - let user_data = user.decrypt(&dek_data.dek); - - users.push(UserResponse { - name: user_data.name, - email: user_data.email, - role: user_data.role, - created_at: user_data.created_at, - updated_at: user_data.updated_at, - email_verified: user_data.email_verified, - is_active: user_data.is_active, - uid: user_data.uid, - }); - } - None => { - return Err(Error::UserNotFound { - message: "No user found".to_string(), - }); + let dek_data = match Dek::get(&mongo_client, &user_data.uid).await { + Ok(dek) => dek, + Err(e) => { + return Err(e); } - } + }; + + let decrypted_user = user_data.decrypt(&dek_data.dek); + + users.push(UserResponse { + name: decrypted_user.name, + email: decrypted_user.email, + role: decrypted_user.role, + created_at: decrypted_user.created_at, + updated_at: decrypted_user.updated_at, + email_verified: decrypted_user.email_verified, + is_active: decrypted_user.is_active, + uid: decrypted_user.uid, + }); } // sort the users by created_at diff --git a/src/handlers/user_handler.rs b/src/handlers/user_handler.rs index fe11b5e..25ce5cf 100644 --- a/src/handlers/user_handler.rs +++ b/src/handlers/user_handler.rs @@ -2,9 +2,7 @@ use crate::{ core::{dek::Dek, session::Session, user::User}, errors::{Error, Result}, models::user_model::{ - ToggleUserActivationStatusPayload, ToggleUserActivationStatusResponse, UpdateUserPayload, - UpdateUserResponse, UpdateUserRolePayload, UpdateUserRoleResponse, UserEmailPayload, - UserEmailResponse, UserIdPayload, UserResponse, + RecentUserPayload, ToggleUserActivationStatusPayload, ToggleUserActivationStatusResponse, UpdateUserPayload, UpdateUserResponse, UpdateUserRolePayload, UpdateUserRoleResponse, UserEmailPayload, UserEmailResponse, UserIdPayload, UserResponse }, utils::{encryption_utils::Encryption, validation_utils::Validation}, AppState, @@ -27,10 +25,11 @@ pub async fn get_all_users_handler( pub async fn get_recent_users_handler( State(state): State, + payload: Json ) -> Result>> { println!(">> HANDLER: get_recent_users_handler called"); - match User::get_recent(&state.mongo_client, 5).await { + match User::get_recent(&state.mongo_client, payload.limit).await { Ok(users) => Ok(Json(users)), Err(e) => Err(e), } diff --git a/src/models/user_model.rs b/src/models/user_model.rs index 6fcea4a..a39b9a6 100644 --- a/src/models/user_model.rs +++ b/src/models/user_model.rs @@ -79,3 +79,8 @@ pub struct UserResponse { pub created_at: Option, pub updated_at: Option, } + +#[derive(Deserialize, Debug, Clone)] +pub struct RecentUserPayload { + pub limit: i64, +} \ No newline at end of file diff --git a/src/routes/user_routes.rs b/src/routes/user_routes.rs index b0d9de7..4316d81 100644 --- a/src/routes/user_routes.rs +++ b/src/routes/user_routes.rs @@ -14,7 +14,7 @@ use crate::{ pub fn routes(State(state): State) -> Router { let user_routes = Router::new() .route("/get-all", get(get_all_users_handler)) - .route("/get-recent", get(get_recent_users_handler)) + .route("/get-recent", post(get_recent_users_handler)) .route("/get-from-email", post(get_user_email_handler)) .route("/get-from-id", post(get_user_id_handler)) .route("/update", post(update_user_handler))