Skip to content

Commit

Permalink
Merge pull request #62 from Rajdip019/dashboard-setup
Browse files Browse the repository at this point in the history
Recent User endpoint fixed
  • Loading branch information
Rajdip019 authored Jul 24, 2024
2 parents b11163e + bdf36b3 commit 9efeb75
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
76 changes: 35 additions & 41 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -182,58 +182,52 @@ impl User {
) -> Result<Vec<UserResponse>> {
let db = mongo_client.database("auth");
let collection: Collection<User> = db.collection("users");
let collection_dek: Collection<Dek> = 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
Expand Down
7 changes: 3 additions & 4 deletions src/handlers/user_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,10 +25,11 @@ pub async fn get_all_users_handler(

pub async fn get_recent_users_handler(
State(state): State<AppState>,
payload: Json<RecentUserPayload>
) -> Result<Json<Vec<UserResponse>>> {
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),
}
Expand Down
5 changes: 5 additions & 0 deletions src/models/user_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ pub struct UserResponse {
pub created_at: Option<DateTime>,
pub updated_at: Option<DateTime>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct RecentUserPayload {
pub limit: i64,
}
2 changes: 1 addition & 1 deletion src/routes/user_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
pub fn routes(State(state): State<AppState>) -> 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))
Expand Down

0 comments on commit 9efeb75

Please sign in to comment.