Skip to content

Commit 1758e55

Browse files
committed
add helper function
1 parent 3dee527 commit 1758e55

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

crates/router/src/connector/cybersource/transformers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,9 +1986,9 @@ impl<F>
19861986
resource_id: types::ResponseId::NoResponseId,
19871987
redirection_data,
19881988
mandate_reference: None,
1989-
connector_metadata: Some(serde_json::json!({
1990-
"three_ds_data": three_ds_data
1991-
})),
1989+
connector_metadata: Some(
1990+
serde_json::json!({"three_ds_data":three_ds_data}),
1991+
),
19921992
network_txn_id: None,
19931993
connector_response_reference_id,
19941994
incremental_authorization_allowed: None,

crates/router/src/core/errors/user.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub enum UserErrors {
5454
MerchantIdParsingError,
5555
#[error("ChangePasswordError")]
5656
ChangePasswordError,
57+
#[error("UserNotExist")]
58+
UserNotExist,
5759
#[error("InvalidDeleteOperation")]
5860
InvalidDeleteOperation,
5961
}
@@ -159,9 +161,15 @@ impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorRespon
159161
"Old and new password cannot be same",
160162
None,
161163
)),
162-
Self::InvalidDeleteOperation => AER::BadRequest(ApiError::new(
164+
Self::UserNotExist => AER::BadRequest(ApiError::new(
163165
sub_code,
164166
30,
167+
"User does not exist in records",
168+
None,
169+
)),
170+
Self::InvalidDeleteOperation => AER::BadRequest(ApiError::new(
171+
sub_code,
172+
31,
165173
"Delete Operation Not Supported",
166174
None,
167175
)),

crates/router/src/core/user.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
services::{authentication as auth, ApplicationResponse},
1919
types::domain,
2020
utils,
21+
utils::user::can_delete_user_role,
2122
};
2223
pub mod dashboard_metadata;
2324
#[cfg(feature = "dummy_connector")]
@@ -478,7 +479,7 @@ pub async fn delete_user(
478479
.await
479480
.map_err(|e| {
480481
if e.current_context().is_db_not_found() {
481-
e.change_context(UserErrors::UserNotFound)
482+
e.change_context(UserErrors::UserNotExist)
482483
} else {
483484
e.change_context(UserErrors::InternalServerError)
484485
}
@@ -501,17 +502,11 @@ pub async fn delete_user(
501502
.find(|&role| role.merchant_id == user_from_token.merchant_id.as_str())
502503
{
503504
Some(user_role) => {
504-
if user_role.role_id == consts::user_role::ROLE_ID_INTERNAL_ADMIN
505-
|| user_role.role_id == consts::user_role::ROLE_ID_MERCHANT_ADMIN
506-
|| user_role.role_id == consts::user_role::ROLE_ID_ORGANIZATION_ADMIN
507-
{
508-
return Err(UserErrors::InvalidDeleteOperation.into())
509-
.attach_printable("Cannot delete");
510-
}
505+
let _ = can_delete_user_role(&user_role.role_id);
511506
}
512507
None => {
513508
return Err(UserErrors::InvalidDeleteOperation.into())
514-
.attach_printable("User not found");
509+
.attach_printable("User role not found");
515510
}
516511
};
517512

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

529-
return Ok(ApplicationResponse::StatusOk);
524+
Ok(ApplicationResponse::StatusOk)
530525
} else {
531526
let _ = state
532527
.store
@@ -545,7 +540,7 @@ pub async fn delete_user(
545540
.change_context(UserErrors::InternalServerError)
546541
.attach_printable("Error while deleting user role");
547542

548-
return Ok(ApplicationResponse::StatusOk);
543+
Ok(ApplicationResponse::StatusOk)
549544
}
550545
}
551546

crates/router/src/utils/user.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use error_stack::ResultExt;
44
use masking::Secret;
55

66
use crate::{
7+
consts,
78
core::errors::{UserErrors, UserResult},
89
routes::AppState,
910
services::authentication::{AuthToken, UserFromToken},
@@ -111,3 +112,15 @@ pub fn get_dashboard_entry_response(
111112
user_role: user_role.role_id,
112113
})
113114
}
115+
116+
pub fn can_delete_user_role(role_id: &str) -> UserResult<()> {
117+
match role_id {
118+
consts::user_role::ROLE_ID_ORGANIZATION_ADMIN
119+
| consts::user_role::ROLE_ID_INTERNAL_ADMIN
120+
| consts::user_role::ROLE_ID_INTERNAL_VIEW_ONLY_USER
121+
| consts::user_role::INTERNAL_USER_MERCHANT_ID => {
122+
Err(UserErrors::InvalidDeleteOperation.into()).attach_printable("Cannot delete")
123+
}
124+
_ => Ok(()),
125+
}
126+
}

0 commit comments

Comments
 (0)