Skip to content

Commit

Permalink
add helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdixit88 committed Jan 18, 2024
1 parent 3dee527 commit 1758e55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
6 changes: 3 additions & 3 deletions crates/router/src/connector/cybersource/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,9 +1986,9 @@ impl<F>
resource_id: types::ResponseId::NoResponseId,
redirection_data,
mandate_reference: None,
connector_metadata: Some(serde_json::json!({
"three_ds_data": three_ds_data
})),
connector_metadata: Some(
serde_json::json!({"three_ds_data":three_ds_data}),
),
network_txn_id: None,
connector_response_reference_id,
incremental_authorization_allowed: None,
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/errors/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum UserErrors {
MerchantIdParsingError,
#[error("ChangePasswordError")]
ChangePasswordError,
#[error("UserNotExist")]
UserNotExist,
#[error("InvalidDeleteOperation")]
InvalidDeleteOperation,
}
Expand Down Expand Up @@ -159,9 +161,15 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon
"Old and new password cannot be same",
None,
)),
Self::InvalidDeleteOperation => AER::BadRequest(ApiError::new(
Self::UserNotExist => AER::BadRequest(ApiError::new(
sub_code,
30,
"User does not exist in records",
None,
)),
Self::InvalidDeleteOperation => AER::BadRequest(ApiError::new(
sub_code,
31,
"Delete Operation Not Supported",
None,
)),
Expand Down
17 changes: 6 additions & 11 deletions crates/router/src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -478,7 +479,7 @@ pub async fn delete_user(
.await
.map_err(|e| {
if e.current_context().is_db_not_found() {
e.change_context(UserErrors::UserNotFound)
e.change_context(UserErrors::UserNotExist)
} else {
e.change_context(UserErrors::InternalServerError)
}
Expand All @@ -501,17 +502,11 @@ pub async fn delete_user(
.find(|&role| role.merchant_id == user_from_token.merchant_id.as_str())
{
Some(user_role) => {
if user_role.role_id == consts::user_role::ROLE_ID_INTERNAL_ADMIN
|| user_role.role_id == consts::user_role::ROLE_ID_MERCHANT_ADMIN
|| user_role.role_id == consts::user_role::ROLE_ID_ORGANIZATION_ADMIN
{
return Err(UserErrors::InvalidDeleteOperation.into())
.attach_printable("Cannot delete");
}
let _ = can_delete_user_role(&user_role.role_id);
}
None => {
return Err(UserErrors::InvalidDeleteOperation.into())
.attach_printable("User not found");
.attach_printable("User role not found");
}
};

Expand All @@ -526,7 +521,7 @@ pub async fn delete_user(
.change_context(UserErrors::InternalServerError)
.attach_printable("Error while deleting user role");

return Ok(ApplicationResponse::StatusOk);
Ok(ApplicationResponse::StatusOk)
} else {
let _ = state
.store
Expand All @@ -545,7 +540,7 @@ pub async fn delete_user(
.change_context(UserErrors::InternalServerError)
.attach_printable("Error while deleting user role");

return Ok(ApplicationResponse::StatusOk);
Ok(ApplicationResponse::StatusOk)
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/router/src/utils/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use error_stack::ResultExt;
use masking::Secret;

use crate::{
consts,
core::errors::{UserErrors, UserResult},
routes::AppState,
services::authentication::{AuthToken, UserFromToken},
Expand Down Expand Up @@ -111,3 +112,15 @@ pub fn get_dashboard_entry_response(
user_role: user_role.role_id,
})
}

pub fn can_delete_user_role(role_id: &str) -> UserResult<()> {
match role_id {
consts::user_role::ROLE_ID_ORGANIZATION_ADMIN
| consts::user_role::ROLE_ID_INTERNAL_ADMIN
| consts::user_role::ROLE_ID_INTERNAL_VIEW_ONLY_USER
| consts::user_role::INTERNAL_USER_MERCHANT_ID => {
Err(UserErrors::InvalidDeleteOperation.into()).attach_printable("Cannot delete")
}
_ => Ok(()),
}
}

0 comments on commit 1758e55

Please sign in to comment.