Skip to content

Commit

Permalink
fix: update schema for db
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdixit88 committed Jun 14, 2024
1 parent 015d7e7 commit 3dc3295
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 76 deletions.
15 changes: 8 additions & 7 deletions crates/api_models/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,23 @@ pub struct RecoveryCodes {

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct CreateOrgAuthenticationMethodRequest {
pub org_id: String,
pub owner_id: String,
pub auth_method: common_enums::AuthMethod,
pub auth_config: Option<serde_json::Value>,
pub config: Option<serde_json::Value>,
pub allow_signup: bool,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct UpdateOrgAuthenticationMethodRequest {
pub org_id: String,
pub owner_id: String,
pub auth_method: common_enums::AuthMethod,
pub auth_config: Option<serde_json::Value>,
pub config: Option<serde_json::Value>,
pub allow_singup: bool,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct GetOrgAuthenticationMethodsRequest {
pub org_id: String,
pub owner_id: String,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
Expand All @@ -307,7 +309,6 @@ pub struct ListOrgAuthenticationMethods {

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct OrgAuthenticationMethodResponse {
pub org_id: String,
pub owner_id: String,
pub auth_method: common_enums::AuthMethod,
pub auth_config: Option<serde_json::Value>,
}
2 changes: 1 addition & 1 deletion crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,7 @@ pub enum TokenPurpose {
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum AuthMethod {
Okta,
OpenIdConnect,
MagicLink,
#[default]
Password,
Expand Down
22 changes: 11 additions & 11 deletions crates/diesel_models/src/org_authentication_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ use crate::{enums, schema::org_authentication_methods};
#[diesel(table_name = org_authentication_methods)]
pub struct OrgAuthenticationMethod {
pub id: i32,
pub org_id: String,
pub owner_id: String,
pub auth_method: enums::AuthMethod,
pub auth_config: Option<serde_json::Value>,
pub config: Option<serde_json::Value>,
pub allow_signup: bool,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(router_derive::Setter, Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
#[diesel(table_name = org_authentication_methods)]
pub struct OrgAuthenticationMethodNew {
pub org_id: String,
pub owner_id: String,
pub auth_method: enums::AuthMethod,
pub auth_config: Option<serde_json::Value>,
pub config: Option<serde_json::Value>,
pub allow_signup: bool,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
#[diesel(table_name = org_authentication_methods)]
pub struct OrgAuthenticationMethodUpdateInternal {
auth_config: Option<serde_json::Value>,
last_modified_at: PrimitiveDateTime,
pub config: Option<serde_json::Value>,
pub last_modified_at: PrimitiveDateTime,
}

pub enum OrgAuthenticationMethodUpdate {
UpdateAuthConfig {
auth_config: Option<serde_json::Value>,
},
UpdateConfig { config: Option<serde_json::Value> },
}

impl From<OrgAuthenticationMethodUpdate> for OrgAuthenticationMethodUpdateInternal {
fn from(value: OrgAuthenticationMethodUpdate) -> Self {
let last_modified_at = common_utils::date_time::now();
match value {
OrgAuthenticationMethodUpdate::UpdateAuthConfig { auth_config } => Self {
auth_config,
OrgAuthenticationMethodUpdate::UpdateConfig { config } => Self {
config,
last_modified_at,
},
}
Expand Down
12 changes: 6 additions & 6 deletions crates/diesel_models/src/query/org_authentication_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ impl OrgAuthenticationMethodNew {
}

impl OrgAuthenticationMethod {
pub async fn get_org_authentication_methods_details(
pub async fn list_authentication_methods_for_org_id(
conn: &PgPooledConn,
org_id: &str,
owner_id: &str,
) -> StorageResult<Vec<Self>> {
generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::org_id.eq(org_id.to_owned()),
dsl::owner_id.eq(owner_id.to_owned()),
None,
None,
Some(dsl::last_modified_at.asc()),
Expand All @@ -28,7 +28,7 @@ impl OrgAuthenticationMethod {

pub async fn update_org_authentication_method(
conn: &PgPooledConn,
org_id: &str,
owner_id: &str,
auth_method: enums::AuthMethod,
org_authentication_method_update: OrgAuthenticationMethodUpdate,
) -> StorageResult<Self> {
Expand All @@ -39,8 +39,8 @@ impl OrgAuthenticationMethod {
_,
>(
conn,
dsl::org_id
.eq(org_id.to_owned())
dsl::owner_id
.eq(owner_id.to_owned())
.and(dsl::auth_method.eq(auth_method)),
OrgAuthenticationMethodUpdateInternal::from(org_authentication_method_update),
)
Expand Down
5 changes: 3 additions & 2 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,10 @@ diesel::table! {
org_authentication_methods (id) {
id -> Int4,
#[max_length = 64]
org_id -> Varchar,
owner_id -> Varchar,
auth_method -> AuthMethod,
auth_config -> Nullable<Jsonb>,
config -> Nullable<Jsonb>,
allow_signup -> Bool,
created_at -> Timestamp,
last_modified_at -> Timestamp,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/errors/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ impl UserErrors {
Self::TwoFactorAuthRequired => "Two factor auth required",
Self::TwoFactorAuthNotSetup => "Two factor auth not setup",
Self::TotpSecretNotFound => "TOTP secret not found",
Self::OrgAuthMethodAlreadyExists => "Org authentication method already exists",
Self::InvalidOrgAuthMethodOperation => "Invalid org authentication method operation",
Self::OrgAuthMethodAlreadyExists => "Org auth method already exists",
Self::InvalidOrgAuthMethodOperation => "Invalid org auth method operation",
}
}
}
22 changes: 10 additions & 12 deletions crates/router/src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ pub async fn resend_invite(
if e.current_context().is_db_not_found() {
e.change_context(UserErrors::InvalidRoleOperation)
.attach_printable(format!(
"User role with user_id = {} and org_id = {} is not found",
"User role with user_id = {} and owner_id = {} is not found",
user.get_user_id(),
user_from_token.merchant_id
))
Expand Down Expand Up @@ -1145,7 +1145,7 @@ pub async fn switch_merchant_id(
}
})?;

let org_id = state
let owner_id = state
.store
.find_merchant_account_by_merchant_id(request.merchant_id.as_str(), &key_store)
.await
Expand All @@ -1162,7 +1162,7 @@ pub async fn switch_merchant_id(
&state,
&user,
request.merchant_id.clone(),
org_id.clone(),
owner_id.clone(),
user_from_token.role_id.clone(),
)
.await?;
Expand Down Expand Up @@ -1976,9 +1976,10 @@ pub async fn create_org_authentication_method(
state
.store
.insert_org_authentication_method(OrgAuthenticationMethodNew {
org_id: req.org_id,
owner_id: req.owner_id,
auth_method: req.auth_method,
auth_config: req.auth_config,
config: req.config,
allow_signup: req.allow_signup,
created_at: now,
last_modified_at: now,
})
Expand All @@ -1994,11 +1995,9 @@ pub async fn update_org_authentication_method(
state
.store
.update_org_authentication_method(
&req.org_id,
&req.owner_id,
req.auth_method,
OrgAuthenticationMethodUpdate::UpdateAuthConfig {
auth_config: req.auth_config,
},
OrgAuthenticationMethodUpdate::UpdateConfig { config: req.config },
)
.await
.change_context(UserErrors::InvalidOrgAuthMethodOperation)?;
Expand All @@ -2011,14 +2010,13 @@ pub async fn list_org_authentication_methods(
) -> UserResponse<user_api::ListOrgAuthenticationMethods> {
let org_authentication_methods: Vec<_> = state
.store
.get_org_authentication_methods_details(&req.org_id)
.list_authentication_methods_for_org_id(&req.owner_id)
.await
.change_context(UserErrors::InternalServerError)?
.into_iter()
.map(|user| user_api::OrgAuthenticationMethodResponse {
org_id: user.org_id,
owner_id: user.owner_id,
auth_method: user.auth_method,
auth_config: user.auth_config,
})
.collect();

Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/db/kafka_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2868,12 +2868,12 @@ impl OrgAuthenticationMethodInterface for KafkaStore {
.await
}

async fn get_org_authentication_methods_details(
async fn list_authentication_methods_for_org_id(
&self,
org_id: &str,
) -> CustomResult<Vec<storage::OrgAuthenticationMethod>, errors::StorageError> {
self.diesel_store
.get_org_authentication_methods_details(org_id)
.list_authentication_methods_for_org_id(org_id)
.await
}

Expand Down
51 changes: 26 additions & 25 deletions crates/router/src/db/org_authentication_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub trait OrgAuthenticationMethodInterface {
org_authentication_method: storage::OrgAuthenticationMethodNew,
) -> CustomResult<storage::OrgAuthenticationMethod, errors::StorageError>;

async fn get_org_authentication_methods_details(
async fn list_authentication_methods_for_org_id(
&self,
org_id: &str,
owner_id: &str,
) -> CustomResult<Vec<storage::OrgAuthenticationMethod>, errors::StorageError>;

async fn update_org_authentication_method(
&self,
org_id: &str,
owner_id: &str,
auth_method: enums::AuthMethod,
org_authentication_method_update: storage::OrgAuthenticationMethodUpdate,
) -> CustomResult<storage::OrgAuthenticationMethod, errors::StorageError>;
Expand All @@ -47,27 +47,27 @@ impl OrgAuthenticationMethodInterface for Store {
}

#[instrument(skip_all)]
async fn get_org_authentication_methods_details(
async fn list_authentication_methods_for_org_id(
&self,
org_id: &str,
owner_id: &str,
) -> CustomResult<Vec<storage::OrgAuthenticationMethod>, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::OrgAuthenticationMethod::get_org_authentication_methods_details(&conn, org_id)
storage::OrgAuthenticationMethod::list_authentication_methods_for_org_id(&conn, owner_id)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}

#[instrument(skip_all)]
async fn update_org_authentication_method(
&self,
org_id: &str,
owner_id: &str,
auth_method: enums::AuthMethod,
org_authentication_method_update: storage::OrgAuthenticationMethodUpdate,
) -> CustomResult<storage::OrgAuthenticationMethod, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::OrgAuthenticationMethod::update_org_authentication_method(
&conn,
org_id,
owner_id,
auth_method,
org_authentication_method_update,
)
Expand All @@ -83,21 +83,21 @@ impl OrgAuthenticationMethodInterface for MockDb {
org_authentication_method: storage::OrgAuthenticationMethodNew,
) -> CustomResult<storage::OrgAuthenticationMethod, errors::StorageError> {
let mut org_authentication_methods = self.org_authentication_methods.lock().await;
if org_authentication_methods
.iter()
.any(|auth_method_inner| auth_method_inner.org_id == org_authentication_method.org_id)
{
if org_authentication_methods.iter().any(|auth_method_inner| {
auth_method_inner.owner_id == org_authentication_method.owner_id
}) {
Err(errors::StorageError::DuplicateValue {
entity: "role_id",
entity: "org_id",
key: None,
})?
}
let org_authentication_method = storage::OrgAuthenticationMethod {
id: i32::try_from(org_authentication_methods.len())
.change_context(errors::StorageError::MockDbError)?,
org_id: org_authentication_method.org_id,
owner_id: org_authentication_method.owner_id,
auth_method: org_authentication_method.auth_method,
auth_config: org_authentication_method.auth_config,
config: org_authentication_method.config,
allow_signup: org_authentication_method.allow_signup,
created_at: org_authentication_method.created_at,
last_modified_at: org_authentication_method.last_modified_at,
};
Expand All @@ -106,21 +106,21 @@ impl OrgAuthenticationMethodInterface for MockDb {
Ok(org_authentication_method)
}

async fn get_org_authentication_methods_details(
async fn list_authentication_methods_for_org_id(
&self,
org_id: &str,
owner_id: &str,
) -> CustomResult<Vec<storage::OrgAuthenticationMethod>, errors::StorageError> {
let org_authentication_methods = self.org_authentication_methods.lock().await;

let org_authentication_methods_list: Vec<_> = org_authentication_methods
.iter()
.filter(|auth_method_inner| auth_method_inner.org_id == org_id)
.filter(|auth_method_inner| auth_method_inner.owner_id == owner_id)
.cloned()
.collect();
if org_authentication_methods_list.is_empty() {
return Err(errors::StorageError::ValueNotFound(format!(
"No org authentication found for org_id = {}",
org_id
"No org authentication found for owner_id = {}",
owner_id
))
.into());
}
Expand All @@ -130,21 +130,22 @@ impl OrgAuthenticationMethodInterface for MockDb {

async fn update_org_authentication_method(
&self,
org_id: &str,
owner_id: &str,
auth_method: enums::AuthMethod,
org_authentication_method_update: storage::OrgAuthenticationMethodUpdate,
) -> CustomResult<storage::OrgAuthenticationMethod, errors::StorageError> {
let mut org_authentication_methods = self.org_authentication_methods.lock().await;
org_authentication_methods
.iter_mut()
.find(|auth_method_inner| {
auth_method_inner.org_id == org_id && auth_method_inner.auth_method == auth_method
auth_method_inner.owner_id == owner_id
&& auth_method_inner.auth_method == auth_method
})
.map(|auth_method_inner| {
*auth_method_inner = match org_authentication_method_update {
storage::OrgAuthenticationMethodUpdate::UpdateAuthConfig { auth_config } => {
storage::OrgAuthenticationMethodUpdate::UpdateConfig { config } => {
storage::OrgAuthenticationMethod {
auth_config,
config,
last_modified_at: common_utils::date_time::now(),
..auth_method_inner.to_owned()
}
Expand All @@ -154,7 +155,7 @@ impl OrgAuthenticationMethodInterface for MockDb {
})
.ok_or(
errors::StorageError::ValueNotFound(format!(
"No authentication method available for the org = {org_id}"
"No authentication method available for the org = {owner_id}"
))
.into(),
)
Expand Down
Loading

0 comments on commit 3dc3295

Please sign in to comment.