-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(themes): Setup themes table (#6533)
- Loading branch information
1 parent
54286c6
commit 83ec522
Showing
15 changed files
with
471 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.