feat(user): create apis for custom role (#3763)

Co-authored-by: Mani Chandra Dulam <mani.dchandra@juspay.in>
Co-authored-by: Mani Chandra <84711804+ThisIsMani@users.noreply.github.com>
This commit is contained in:
Apoorv Dixit
2024-02-22 19:55:47 +05:30
committed by GitHub
parent 2e7d30a4ad
commit 58809ab1f9
17 changed files with 410 additions and 156 deletions

View File

@ -1,6 +1,11 @@
use api_models::user_role as user_role_api;
use error_stack::ResultExt;
use crate::services::authorization::permissions::Permission;
use crate::{
core::errors::{UserErrors, UserResult},
routes::AppState,
services::authorization::permissions::Permission,
};
impl From<Permission> for user_role_api::Permission {
fn from(value: Permission) -> Self {
@ -34,3 +39,24 @@ impl From<Permission> for user_role_api::Permission {
}
}
}
pub async fn is_role_name_already_present_for_merchant(
state: &AppState,
role_name: &str,
merchant_id: &str,
org_id: &str,
) -> UserResult<()> {
let role_name_list: Vec<String> = state
.store
.list_all_roles(merchant_id, org_id)
.await
.change_context(UserErrors::InternalServerError)?
.iter()
.map(|role| role.role_name.to_owned())
.collect();
if role_name_list.contains(&role_name.to_string()) {
return Err(UserErrors::RoleNameAlreadyExists.into());
}
Ok(())
}