feat(themes): Setup themes table (#6533)

This commit is contained in:
Mani Chandra
2024-11-14 14:10:20 +05:30
committed by GitHub
parent afd7f7d209
commit 29be1d4fad
15 changed files with 471 additions and 3 deletions

View File

@ -145,6 +145,7 @@ pub trait GlobalStorageInterface:
+ user::UserInterface
+ user_role::UserRoleInterface
+ user_key_store::UserKeyStoreInterface
+ user::theme::ThemeInterface
+ 'static
{
}

View File

@ -1,7 +1,11 @@
use std::sync::Arc;
use common_enums::enums::MerchantStorageScheme;
use common_utils::{errors::CustomResult, id_type, pii, types::keymanager::KeyManagerState};
use common_utils::{
errors::CustomResult,
id_type, pii,
types::{keymanager::KeyManagerState, theme::ThemeLineage},
};
use diesel_models::{
enums,
enums::ProcessTrackerStatus,
@ -34,7 +38,7 @@ use time::PrimitiveDateTime;
use super::{
dashboard_metadata::DashboardMetadataInterface,
role::RoleInterface,
user::{sample_data::BatchSampleDataInterface, UserInterface},
user::{sample_data::BatchSampleDataInterface, theme::ThemeInterface, UserInterface},
user_authentication_method::UserAuthenticationMethodInterface,
user_key_store::UserKeyStoreInterface,
user_role::{ListUserRolesByOrgIdPayload, ListUserRolesByUserIdPayload, UserRoleInterface},
@ -3683,3 +3687,30 @@ impl UserAuthenticationMethodInterface for KafkaStore {
.await
}
}
#[async_trait::async_trait]
impl ThemeInterface for KafkaStore {
async fn insert_theme(
&self,
theme: storage::theme::ThemeNew,
) -> CustomResult<storage::theme::Theme, errors::StorageError> {
self.diesel_store.insert_theme(theme).await
}
async fn find_theme_by_lineage(
&self,
lineage: ThemeLineage,
) -> CustomResult<storage::theme::Theme, errors::StorageError> {
self.diesel_store.find_theme_by_lineage(lineage).await
}
async fn delete_theme_by_lineage_and_theme_id(
&self,
theme_id: String,
lineage: ThemeLineage,
) -> CustomResult<storage::theme::Theme, errors::StorageError> {
self.diesel_store
.delete_theme_by_lineage_and_theme_id(theme_id, lineage)
.await
}
}

View File

@ -11,6 +11,7 @@ use crate::{
services::Store,
};
pub mod sample_data;
pub mod theme;
#[async_trait::async_trait]
pub trait UserInterface {

View File

@ -0,0 +1,203 @@
use common_utils::types::theme::ThemeLineage;
use diesel_models::user::theme as storage;
use error_stack::report;
use super::MockDb;
use crate::{
connection,
core::errors::{self, CustomResult},
services::Store,
};
#[async_trait::async_trait]
pub trait ThemeInterface {
async fn insert_theme(
&self,
theme: storage::ThemeNew,
) -> CustomResult<storage::Theme, errors::StorageError>;
async fn find_theme_by_lineage(
&self,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError>;
async fn delete_theme_by_lineage_and_theme_id(
&self,
theme_id: String,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError>;
}
#[async_trait::async_trait]
impl ThemeInterface for Store {
async fn insert_theme(
&self,
theme: storage::ThemeNew,
) -> CustomResult<storage::Theme, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
theme
.insert(&conn)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn find_theme_by_lineage(
&self,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError> {
let conn = connection::pg_connection_read(self).await?;
storage::Theme::find_by_lineage(&conn, lineage)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn delete_theme_by_lineage_and_theme_id(
&self,
theme_id: String,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::Theme::delete_by_theme_id_and_lineage(&conn, theme_id, lineage)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
}
fn check_theme_with_lineage(theme: &storage::Theme, lineage: &ThemeLineage) -> bool {
match lineage {
ThemeLineage::Tenant { tenant_id } => {
&theme.tenant_id == tenant_id
&& theme.org_id.is_none()
&& theme.merchant_id.is_none()
&& theme.profile_id.is_none()
}
ThemeLineage::Organization { tenant_id, org_id } => {
&theme.tenant_id == tenant_id
&& theme
.org_id
.as_ref()
.is_some_and(|org_id_inner| org_id_inner == org_id)
&& theme.merchant_id.is_none()
&& theme.profile_id.is_none()
}
ThemeLineage::Merchant {
tenant_id,
org_id,
merchant_id,
} => {
&theme.tenant_id == tenant_id
&& theme
.org_id
.as_ref()
.is_some_and(|org_id_inner| org_id_inner == org_id)
&& theme
.merchant_id
.as_ref()
.is_some_and(|merchant_id_inner| merchant_id_inner == merchant_id)
&& theme.profile_id.is_none()
}
ThemeLineage::Profile {
tenant_id,
org_id,
merchant_id,
profile_id,
} => {
&theme.tenant_id == tenant_id
&& theme
.org_id
.as_ref()
.is_some_and(|org_id_inner| org_id_inner == org_id)
&& theme
.merchant_id
.as_ref()
.is_some_and(|merchant_id_inner| merchant_id_inner == merchant_id)
&& theme
.profile_id
.as_ref()
.is_some_and(|profile_id_inner| profile_id_inner == profile_id)
}
}
}
#[async_trait::async_trait]
impl ThemeInterface for MockDb {
async fn insert_theme(
&self,
new_theme: storage::ThemeNew,
) -> CustomResult<storage::Theme, errors::StorageError> {
let mut themes = self.themes.lock().await;
for theme in themes.iter() {
if new_theme.theme_id == theme.theme_id {
return Err(errors::StorageError::DuplicateValue {
entity: "theme_id",
key: None,
}
.into());
}
if new_theme.tenant_id == theme.tenant_id
&& new_theme.org_id == theme.org_id
&& new_theme.merchant_id == theme.merchant_id
&& new_theme.profile_id == theme.profile_id
{
return Err(errors::StorageError::DuplicateValue {
entity: "lineage",
key: None,
}
.into());
}
}
let theme = storage::Theme {
theme_id: new_theme.theme_id,
tenant_id: new_theme.tenant_id,
org_id: new_theme.org_id,
merchant_id: new_theme.merchant_id,
profile_id: new_theme.profile_id,
created_at: new_theme.created_at,
last_modified_at: new_theme.last_modified_at,
};
themes.push(theme.clone());
Ok(theme)
}
async fn find_theme_by_lineage(
&self,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError> {
let themes = self.themes.lock().await;
themes
.iter()
.find(|theme| check_theme_with_lineage(theme, &lineage))
.cloned()
.ok_or(
errors::StorageError::ValueNotFound(format!(
"Theme with lineage {:?} not found",
lineage
))
.into(),
)
}
async fn delete_theme_by_lineage_and_theme_id(
&self,
theme_id: String,
lineage: ThemeLineage,
) -> CustomResult<storage::Theme, errors::StorageError> {
let mut themes = self.themes.lock().await;
let index = themes
.iter()
.position(|theme| {
theme.theme_id == theme_id && check_theme_with_lineage(theme, &lineage)
})
.ok_or(errors::StorageError::ValueNotFound(format!(
"Theme with id {} and lineage {:?} not found",
theme_id, lineage
)))?;
let theme = themes.remove(index);
Ok(theme)
}
}