Skip to content

Commit

Permalink
feat(themes): Setup themes table (#6533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsMani authored and Sayak Bhattacharya committed Nov 26, 2024
1 parent 54286c6 commit 83ec522
Show file tree
Hide file tree
Showing 15 changed files with 471 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod keymanager;

/// Enum for Authentication Level
pub mod authentication;
/// Enum for Theme Lineage
pub mod theme;

use std::{
borrow::Cow,
Expand Down
39 changes: 39 additions & 0 deletions crates/common_utils/src/types/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::id_type;

/// Enum for having all the required lineage for every level.
/// Currently being used for theme related APIs and queries.
#[derive(Debug)]
pub enum ThemeLineage {
/// Tenant lineage variant
Tenant {
/// tenant_id: String
tenant_id: String,
},
/// Org lineage variant
Organization {
/// tenant_id: String
tenant_id: String,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
},
/// Merchant lineage variant
Merchant {
/// tenant_id: String
tenant_id: String,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
/// merchant_id: MerchantId
merchant_id: id_type::MerchantId,
},
/// Profile lineage variant
Profile {
/// tenant_id: String
tenant_id: String,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
/// merchant_id: MerchantId
merchant_id: id_type::MerchantId,
/// profile_id: ProfileId
profile_id: id_type::ProfileId,
},
}
2 changes: 2 additions & 0 deletions crates/diesel_models/src/query/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use common_utils::pii;
use diesel::{associations::HasTable, ExpressionMethods};

pub mod sample_data;
pub mod theme;

use crate::{
query::generics, schema::users::dsl as users_dsl, user::*, PgPooledConn, StorageResult,
Expand Down
95 changes: 95 additions & 0 deletions crates/diesel_models/src/query/user/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use common_utils::types::theme::ThemeLineage;
use diesel::{
associations::HasTable,
pg::Pg,
sql_types::{Bool, Nullable},
BoolExpressionMethods, ExpressionMethods, NullableExpressionMethods,
};

use crate::{
query::generics,
schema::themes::dsl,
user::theme::{Theme, ThemeNew},
PgPooledConn, StorageResult,
};

impl ThemeNew {
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<Theme> {
generics::generic_insert(conn, self).await
}
}

impl Theme {
fn lineage_filter(
lineage: ThemeLineage,
) -> Box<
dyn diesel::BoxableExpression<<Self as HasTable>::Table, Pg, SqlType = Nullable<Bool>>
+ 'static,
> {
match lineage {
ThemeLineage::Tenant { tenant_id } => Box::new(
dsl::tenant_id
.eq(tenant_id)
.and(dsl::org_id.is_null())
.and(dsl::merchant_id.is_null())
.and(dsl::profile_id.is_null())
.nullable(),
),
ThemeLineage::Organization { tenant_id, org_id } => Box::new(
dsl::tenant_id
.eq(tenant_id)
.and(dsl::org_id.eq(org_id))
.and(dsl::merchant_id.is_null())
.and(dsl::profile_id.is_null()),
),
ThemeLineage::Merchant {
tenant_id,
org_id,
merchant_id,
} => Box::new(
dsl::tenant_id
.eq(tenant_id)
.and(dsl::org_id.eq(org_id))
.and(dsl::merchant_id.eq(merchant_id))
.and(dsl::profile_id.is_null()),
),
ThemeLineage::Profile {
tenant_id,
org_id,
merchant_id,
profile_id,
} => Box::new(
dsl::tenant_id
.eq(tenant_id)
.and(dsl::org_id.eq(org_id))
.and(dsl::merchant_id.eq(merchant_id))
.and(dsl::profile_id.eq(profile_id)),
),
}
}

pub async fn find_by_lineage(
conn: &PgPooledConn,
lineage: ThemeLineage,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
Self::lineage_filter(lineage),
)
.await
}

pub async fn delete_by_theme_id_and_lineage(
conn: &PgPooledConn,
theme_id: String,
lineage: ThemeLineage,
) -> StorageResult<Self> {
generics::generic_delete_one_with_result::<<Self as HasTable>::Table, _, _>(
conn,
dsl::theme_id
.eq(theme_id)
.and(Self::lineage_filter(lineage)),
)
.await
}
}
21 changes: 21 additions & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,26 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;

themes (theme_id) {
#[max_length = 64]
theme_id -> Varchar,
#[max_length = 64]
tenant_id -> Varchar,
#[max_length = 64]
org_id -> Nullable<Varchar>,
#[max_length = 64]
merchant_id -> Nullable<Varchar>,
#[max_length = 64]
profile_id -> Nullable<Varchar>,
created_at -> Timestamp,
last_modified_at -> Timestamp,
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
Expand Down Expand Up @@ -1408,6 +1428,7 @@ diesel::allow_tables_to_appear_in_same_query!(
reverse_lookup,
roles,
routing_algorithm,
themes,
unified_translations,
user_authentication_methods,
user_key_store,
Expand Down
21 changes: 21 additions & 0 deletions crates/diesel_models/src/schema_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,26 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;

themes (theme_id) {
#[max_length = 64]
theme_id -> Varchar,
#[max_length = 64]
tenant_id -> Varchar,
#[max_length = 64]
org_id -> Nullable<Varchar>,
#[max_length = 64]
merchant_id -> Nullable<Varchar>,
#[max_length = 64]
profile_id -> Nullable<Varchar>,
created_at -> Timestamp,
last_modified_at -> Timestamp,
}
}

diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
Expand Down Expand Up @@ -1355,6 +1375,7 @@ diesel::allow_tables_to_appear_in_same_query!(
reverse_lookup,
roles,
routing_algorithm,
themes,
unified_translations,
user_authentication_methods,
user_key_store,
Expand Down
3 changes: 2 additions & 1 deletion crates/diesel_models/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use time::PrimitiveDateTime;
use crate::{diesel_impl::OptionalDieselArray, enums::TotpStatus, schema::users};

pub mod dashboard_metadata;

pub mod sample_data;
pub mod theme;

#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
#[diesel(table_name = users, primary_key(user_id), check_for_backend(diesel::pg::Pg))]
pub struct User {
Expand Down
29 changes: 29 additions & 0 deletions crates/diesel_models/src/user/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use common_utils::id_type;
use diesel::{Identifiable, Insertable, Queryable, Selectable};
use time::PrimitiveDateTime;

use crate::schema::themes;

#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
#[diesel(table_name = themes, primary_key(theme_id), check_for_backend(diesel::pg::Pg))]
pub struct Theme {
pub theme_id: String,
pub tenant_id: String,
pub org_id: Option<id_type::OrganizationId>,
pub merchant_id: Option<id_type::MerchantId>,
pub profile_id: Option<id_type::ProfileId>,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}

#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
#[diesel(table_name = themes)]
pub struct ThemeNew {
pub theme_id: String,
pub tenant_id: String,
pub org_id: Option<id_type::OrganizationId>,
pub merchant_id: Option<id_type::MerchantId>,
pub profile_id: Option<id_type::ProfileId>,
pub created_at: PrimitiveDateTime,
pub last_modified_at: PrimitiveDateTime,
}
1 change: 1 addition & 0 deletions crates/router/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub trait GlobalStorageInterface:
+ user::UserInterface
+ user_role::UserRoleInterface
+ user_key_store::UserKeyStoreInterface
+ user::theme::ThemeInterface
+ 'static
{
}
Expand Down
35 changes: 33 additions & 2 deletions crates/router/src/db/kafka_store.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions crates/router/src/db/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
services::Store,
};
pub mod sample_data;
pub mod theme;

#[async_trait::async_trait]
pub trait UserInterface {
Expand Down
Loading

0 comments on commit 83ec522

Please sign in to comment.