-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(user): add support to delete user #3374
Changes from 4 commits
cf45556
3dee527
1758e55
d4403be
1147107
8505300
6d64e92
81088f7
3744821
54e3678
a4a600a
1474c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,10 @@ pub enum UserErrors { | |
MerchantIdParsingError, | ||
#[error("ChangePasswordError")] | ||
ChangePasswordError, | ||
#[error("UserNotExist")] | ||
UserNotExist, | ||
#[error("InvalidDeleteOperation")] | ||
InvalidDeleteOperation, | ||
} | ||
|
||
impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorResponse> for UserErrors { | ||
|
@@ -157,6 +161,18 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon | |
"Old and new password cannot be same", | ||
None, | ||
)), | ||
Self::UserNotExist => AER::BadRequest(ApiError::new( | ||
sub_code, | ||
30, | ||
"User does not exist in records", | ||
None, | ||
)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
Self::InvalidDeleteOperation => AER::BadRequest(ApiError::new( | ||
sub_code, | ||
31, | ||
"Delete Operation Not Supported", | ||
None, | ||
)), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ use crate::{ | |
services::{authentication as auth, ApplicationResponse}, | ||
types::domain, | ||
utils, | ||
utils::user::can_delete_user_role, | ||
}; | ||
pub mod dashboard_metadata; | ||
#[cfg(feature = "dummy_connector")] | ||
|
@@ -467,6 +468,82 @@ pub async fn invite_user( | |
} | ||
} | ||
|
||
pub async fn delete_user( | ||
state: AppState, | ||
request: user_api::DeleteUserRequest, | ||
user_from_token: auth::UserFromToken, | ||
) -> UserResponse<()> { | ||
let user_from_db: domain::UserFromStorage = state | ||
.store | ||
.find_user_by_email(request.email.to_owned().expose().expose().as_str()) | ||
.await | ||
.map_err(|e| { | ||
if e.current_context().is_db_not_found() { | ||
e.change_context(UserErrors::UserNotExist) | ||
} else { | ||
e.change_context(UserErrors::InternalServerError) | ||
} | ||
})? | ||
.into(); | ||
|
||
if user_from_db.get_user_id() == user_from_token.user_id { | ||
return Err(UserErrors::InvalidDeleteOperation.into()) | ||
.attach_printable("User deleting himself"); | ||
} | ||
|
||
let user_roles = state | ||
.store | ||
.list_user_roles_by_user_id(user_from_db.get_user_id()) | ||
.await | ||
.change_context(UserErrors::InternalServerError)?; | ||
|
||
match user_roles | ||
.iter() | ||
.find(|&role| role.merchant_id == user_from_token.merchant_id.as_str()) | ||
{ | ||
Some(user_role) => { | ||
let _ = can_delete_user_role(&user_role.role_id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling ? |
||
} | ||
None => { | ||
return Err(UserErrors::InvalidDeleteOperation.into()) | ||
.attach_printable("User role not found"); | ||
} | ||
}; | ||
|
||
if user_roles.len() > 1 { | ||
let _ = state | ||
.store | ||
.delete_user_role_by_user_id_merchant_id( | ||
user_from_db.get_user_id(), | ||
user_from_token.merchant_id.as_str(), | ||
) | ||
.await | ||
.change_context(UserErrors::InternalServerError) | ||
.attach_printable("Error while deleting user role"); | ||
|
||
Ok(ApplicationResponse::StatusOk) | ||
} else { | ||
let _ = state | ||
.store | ||
.delete_user_by_user_id(user_from_db.get_user_id()) | ||
.await | ||
.change_context(UserErrors::InternalServerError) | ||
.attach_printable("Error while deleting user entry"); | ||
|
||
let _ = state | ||
.store | ||
.delete_user_role_by_user_id_merchant_id( | ||
user_from_db.get_user_id(), | ||
user_from_token.merchant_id.as_str(), | ||
) | ||
.await | ||
.change_context(UserErrors::InternalServerError) | ||
.attach_printable("Error while deleting user role"); | ||
|
||
Ok(ApplicationResponse::StatusOk) | ||
} | ||
} | ||
|
||
pub async fn create_internal_user( | ||
state: AppState, | ||
request: user_api::CreateInternalUserRequest, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,7 @@ impl From<Flow> for ApiIdentifier { | |
| Flow::ForgotPassword | ||
| Flow::ResetPassword | ||
| Flow::InviteUser | ||
| Flow::DeleteUser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this in |
||
| Flow::UserSignUpWithMerchantId | ||
| Flow::VerifyEmail | ||
| Flow::VerifyEmailRequest => Self::User, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to
delete_user_scoped_dashboard_metadata_by_merchant_id