Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(theme): implement theme versioning API and database schema (#10593)

Muditbhatia122ebd8e192521
diff --git a/crates/api_models/src/events/user.rs b/crates/api_models/src/events/user.rs
index d46144f956..911baf8808 100644
--- a/crates/api_models/src/events/user.rs
+++ b/crates/api_models/src/events/user.rs
@@ -4,8 +4,8 @@ use common_utils::events::{ApiEventMetric, ApiEventsType};
use crate::user::sample_data::SampleDataRequest;
#[cfg(feature = "control_center_theme")]
use crate::user::theme::{
- CreateThemeRequest, CreateUserThemeRequest, GetThemeResponse, UpdateThemeRequest,
- UploadFileRequest,
+ CreateThemeRequest, CreateUserThemeRequest, GetThemeResponse, ThemeVersionResponse,
+ UpdateThemeRequest, UploadFileRequest,
};
use crate::user::{
dashboard_metadata::{
@@ -88,7 +88,8 @@ common_utils::impl_api_event_type!(
UploadFileRequest,
CreateThemeRequest,
CreateUserThemeRequest,
- UpdateThemeRequest
+ UpdateThemeRequest,
+ ThemeVersionResponse
)
);
diff --git a/crates/api_models/src/user/theme.rs b/crates/api_models/src/user/theme.rs
index acac1e7769..718d340ce0 100644
--- a/crates/api_models/src/user/theme.rs
+++ b/crates/api_models/src/user/theme.rs
@@ -20,6 +20,12 @@ pub struct GetThemeResponse {
pub theme_data: ThemeData,
}
+#[derive(Debug, Serialize)]
+pub struct ThemeVersionResponse {
+ pub theme_id: String,
+ pub theme_config_version: String,
+}
+
#[derive(Debug, MultipartForm)]
pub struct UploadFileAssetData {
pub asset_name: Text<String>,
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 280032d44f..024418e5a5 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -1705,6 +1705,8 @@ diesel::table! {
#[max_length = 64]
email_entity_name -> Varchar,
email_entity_logo_url -> Text,
+ #[max_length = 32]
+ theme_config_version -> Varchar,
}
}
diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rs
index af71e45fe1..4700e6f300 100644
--- a/crates/diesel_models/src/schema_v2.rs
+++ b/crates/diesel_models/src/schema_v2.rs
@@ -1643,6 +1643,8 @@ diesel::table! {
#[max_length = 64]
email_entity_name -> Varchar,
email_entity_logo_url -> Text,
+ #[max_length = 32]
+ theme_config_version -> Varchar,
}
}
diff --git a/crates/diesel_models/src/user/theme.rs b/crates/diesel_models/src/user/theme.rs
index b3f748fb4b..91d5a7639e 100644
--- a/crates/diesel_models/src/user/theme.rs
+++ b/crates/diesel_models/src/user/theme.rs
@@ -26,6 +26,7 @@ pub struct Theme {
pub email_background_color: String,
pub email_entity_name: String,
pub email_entity_logo_url: String,
+ pub theme_config_version: String,
}
#[derive(Clone, Debug, Insertable, DebugAsDisplay)]
@@ -45,6 +46,7 @@ pub struct ThemeNew {
pub email_background_color: String,
pub email_entity_name: String,
pub email_entity_logo_url: String,
+ pub theme_config_version: String,
}
impl ThemeNew {
@@ -55,7 +57,7 @@ impl ThemeNew {
email_config: EmailThemeConfig,
) -> Self {
let now = date_time::now();
-
+ let theme_config_version = now.assume_utc().unix_timestamp().to_string();
Self {
theme_id,
theme_name,
@@ -71,6 +73,7 @@ impl ThemeNew {
email_background_color: email_config.background_color,
email_entity_name: email_config.entity_name,
email_entity_logo_url: email_config.entity_logo_url,
+ theme_config_version,
}
}
}
@@ -87,7 +90,7 @@ impl Theme {
}
}
-#[derive(Clone, Debug, Default, AsChangeset, DebugAsDisplay)]
+#[derive(Clone, Debug, AsChangeset, DebugAsDisplay)]
#[diesel(table_name = themes)]
pub struct ThemeUpdateInternal {
pub email_primary_color: Option<String>,
@@ -95,15 +98,20 @@ pub struct ThemeUpdateInternal {
pub email_background_color: Option<String>,
pub email_entity_name: Option<String>,
pub email_entity_logo_url: Option<String>,
+ pub last_modified_at: PrimitiveDateTime,
+ pub theme_config_version: Option<String>,
}
#[derive(Clone)]
pub enum ThemeUpdate {
EmailConfig { email_config: EmailThemeConfig },
+ ThemeConfig,
}
impl From<ThemeUpdate> for ThemeUpdateInternal {
fn from(value: ThemeUpdate) -> Self {
+ let now = date_time::now();
+ let theme_config_version = now.assume_utc().unix_timestamp().to_string();
match value {
ThemeUpdate::EmailConfig { email_config } => Self {
email_primary_color: Some(email_config.primary_color),
@@ -111,6 +119,17 @@ impl From<ThemeUpdate> for ThemeUpdateInternal {
email_background_color: Some(email_config.background_color),
email_entity_name: Some(email_config.entity_name),
email_entity_logo_url: Some(email_config.entity_logo_url),
+ last_modified_at: now,
+ theme_config_version: None,
+ },
+ ThemeUpdate::ThemeConfig => Self {
+ email_primary_color: None,
+ email_foreground_color: None,
+ email_background_color: None,
+ email_entity_name: None,
+ email_entity_logo_url: None,
+ last_modified_at: now,
+ theme_config_version: Some(theme_config_version),
},
}
}
diff --git a/crates/router/src/consts/user.rs b/crates/router/src/consts/user.rs
index 21bf437f04..8833dc0f41 100644
--- a/crates/router/src/consts/user.rs
+++ b/crates/router/src/consts/user.rs
@@ -37,6 +37,8 @@ pub const REDIS_RECOVERY_CODE_ATTEMPTS_TTL_IN_SECS: i64 = 10 * 60; // 10 mins
pub const REDIS_SSO_PREFIX: &str = "SSO_";
pub const REDIS_SSO_TTL: i64 = 5 * 60; // 5 minutes
+pub const REDIS_THEME_CONFIG_VERSION_TTL_IN_SECS: i64 = 7 * 24 * 60 * 60; // 7 days
+
pub const DEFAULT_PROFILE_NAME: &str = "default";
pub const DEFAULT_PRODUCT_TYPE: common_enums::MerchantProductType =
common_enums::MerchantProductType::Orchestration;
diff --git a/crates/router/src/core/user/theme.rs b/crates/router/src/core/user/theme.rs
index e20e09c2e5..b28e520907 100644
--- a/crates/router/src/core/user/theme.rs
+++ b/crates/router/src/core/user/theme.rs
@@ -12,6 +12,7 @@ use rdkafka::message::ToBytes;
use uuid::Uuid;
use crate::{
+ consts::user::REDIS_THEME_CONFIG_VERSION_TTL_IN_SECS as redis_theme_config_ttl,
core::errors::{StorageErrorExt, UserErrors, UserResponse},
routes::SessionState,
services::authentication::UserFromToken,
@@ -333,6 +334,10 @@ pub async fn delete_user_theme(
.delete_theme_by_theme_id(theme_id.clone())
.await
.to_not_found_response(UserErrors::ThemeNotFound)?;
+
+ // Delete from Redis cache
+ theme_utils::delete_theme_version_from_redis(&state, &theme_id).await?;
+
// TODO (#6717): Delete theme folder from the theme storage.
// Currently there is no simple or easy way to delete a whole folder from S3.
// So, we are not deleting the theme folder from the theme storage.
@@ -382,6 +387,14 @@ pub async fn update_user_theme(
.attach_printable("Failed to parse ThemeData")?,
)
.await?;
+ state
+ .store
+ .update_theme_by_theme_id(theme_id.clone(), ThemeUpdate::ThemeConfig)
+ .await
+ .change_context(UserErrors::InternalServerError)?;
+
+ // Invalidate Redis cache after updating theme config
+ theme_utils::delete_theme_version_from_redis(&state, &theme_id).await?;
}
let file = theme_utils::retrieve_file_from_theme_bucket(
@@ -572,3 +585,36 @@ pub async fn get_user_theme_using_lineage(
theme_data: parsed_data,
}))
}
+
+pub async fn get_user_theme_config_version(
+ state: SessionState,
+ theme_id: String,
+) -> UserResponse<theme_api::ThemeVersionResponse> {
+ let version_from_redis = theme_utils::get_theme_version_from_redis(&state, &theme_id).await;
+
+ if let Ok(Some(cached_version)) = version_from_redis {
+ return Ok(ApplicationResponse::Json(theme_api::ThemeVersionResponse {
+ theme_id: theme_id.clone(),
+ theme_config_version: cached_version,
+ }));
+ }
+
+ let theme = state
+ .store
+ .find_theme_by_theme_id(theme_id.clone())
+ .await
+ .to_not_found_response(UserErrors::ThemeNotFound)?;
+
+ theme_utils::set_theme_version_in_redis(
+ &state,
+ &theme_id,
+ theme.theme_config_version.clone(),
+ redis_theme_config_ttl,
+ )
+ .await?;
+
+ Ok(ApplicationResponse::Json(theme_api::ThemeVersionResponse {
+ theme_id: theme.theme_id,
+ theme_config_version: theme.theme_config_version,
+ }))
+}
diff --git a/crates/router/src/db/user/theme.rs b/crates/router/src/db/user/theme.rs
index 1967880ffd..853cd97dd6 100644
--- a/crates/router/src/db/user/theme.rs
+++ b/crates/router/src/db/user/theme.rs
@@ -273,6 +273,7 @@ impl ThemeInterface for MockDb {
email_background_color: new_theme.email_background_color,
email_entity_name: new_theme.email_entity_name,
email_entity_logo_url: new_theme.email_entity_logo_url,
+ theme_config_version: new_theme.theme_config_version,
};
themes.push(theme.clone());
@@ -342,6 +343,8 @@ impl ThemeInterface for MockDb {
.iter_mut()
.find(|theme| theme.theme_id == theme_id)
.map(|theme| {
+ let now = common_utils::date_time::now();
+ let theme_config_version = now.assume_utc().unix_timestamp().to_string();
match theme_update {
ThemeUpdate::EmailConfig { email_config } => {
theme.email_primary_color = email_config.primary_color;
@@ -350,7 +353,11 @@ impl ThemeInterface for MockDb {
theme.email_entity_name = email_config.entity_name;
theme.email_entity_logo_url = email_config.entity_logo_url;
}
+ ThemeUpdate::ThemeConfig => {
+ theme.theme_config_version = theme_config_version;
+ }
}
+ theme.last_modified_at = now;
theme.clone()
})
.ok_or_else(|| {
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 48f00aa0db..ff4b6683d3 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -3055,11 +3055,20 @@ impl User {
.route(web::get().to(user::theme::list_all_themes_in_lineage)),
)
.service(
- web::resource("/{theme_id}")
- .route(web::get().to(user::theme::get_user_theme_using_theme_id))
- .route(web::put().to(user::theme::update_user_theme))
- .route(web::post().to(user::theme::upload_file_to_user_theme_storage))
- .route(web::delete().to(user::theme::delete_user_theme)),
+ web::scope("/{theme_id}")
+ .service(
+ web::resource("")
+ .route(web::get().to(user::theme::get_user_theme_using_theme_id))
+ .route(web::put().to(user::theme::update_user_theme))
+ .route(
+ web::post().to(user::theme::upload_file_to_user_theme_storage),
+ )
+ .route(web::delete().to(user::theme::delete_user_theme)),
+ )
+ .service(
+ web::resource("/version")
+ .route(web::get().to(user::theme::get_theme_version)),
+ ),
),
);
route
diff --git a/crates/router/src/routes/lock_utils.rs b/crates/router/src/routes/lock_utils.rs
index 8061c031b9..a26a0ea2f5 100644
--- a/crates/router/src/routes/lock_utils.rs
+++ b/crates/router/src/routes/lock_utils.rs
@@ -310,6 +310,7 @@ impl From<Flow> for ApiIdentifier {
| Flow::UpdateUserTheme
| Flow::DeleteUserTheme
| Flow::GetUserThemeUsingThemeId
+ | Flow::GetUserThemeConfigVersion
| Flow::UploadFileToUserThemeStorage
| Flow::GetUserThemeUsingLineage
| Flow::ListAllThemesInLineage
diff --git a/crates/router/src/routes/user/theme.rs b/crates/router/src/routes/user/theme.rs
index 29473be828..2046df29ce 100644
--- a/crates/router/src/routes/user/theme.rs
+++ b/crates/router/src/routes/user/theme.rs
@@ -311,3 +311,23 @@ pub async fn get_user_theme_using_lineage(
))
.await
}
+
+pub async fn get_theme_version(
+ state: web::Data<AppState>,
+ req: HttpRequest,
+ path: web::Path<String>,
+) -> HttpResponse {
+ let flow = Flow::GetUserThemeConfigVersion;
+ let theme_id = path.into_inner();
+
+ Box::pin(api::server_wrap(
+ flow,
+ state,
+ &req,
+ theme_id,
+ |state, (), theme_id, _| theme_core::get_user_theme_config_version(state, theme_id),
+ &auth::NoAuth,
+ api_locking::LockAction::NotApplicable,
+ ))
+ .await
+}
diff --git a/crates/router/src/utils/user/theme.rs b/crates/router/src/utils/user/theme.rs
index d82fa7d042..50f182aefd 100644
--- a/crates/router/src/utils/user/theme.rs
+++ b/crates/router/src/utils/user/theme.rs
@@ -12,6 +12,58 @@ use crate::{
services::authentication::UserFromToken,
};
+// Redis key format: {theme_id}_version
+#[inline]
+fn get_theme_version_redis_key(theme_id: &str) -> String {
+ format!("{}_version", theme_id)
+}
+
+// Get theme version from Redis
+pub async fn get_theme_version_from_redis(
+ state: &SessionState,
+ theme_id: &str,
+) -> UserResult<Option<String>> {
+ let redis_key = get_theme_version_redis_key(theme_id);
+ let redis_conn = super::get_redis_connection_for_global_tenant(state)?;
+
+ redis_conn
+ .get_key(&redis_key.into())
+ .await
+ .change_context(UserErrors::InternalServerError)
+ .attach_printable("Failed to get theme version from Redis")
+}
+// Set theme version in Redis
+pub async fn set_theme_version_in_redis(
+ state: &SessionState,
+ theme_id: &str,
+ version: String,
+ expiry: i64,
+) -> UserResult<()> {
+ let redis_key = get_theme_version_redis_key(theme_id);
+ let redis_conn = super::get_redis_connection_for_global_tenant(state)?;
+
+ redis_conn
+ .set_key_with_expiry(&redis_key.into(), version, expiry)
+ .await
+ .change_context(UserErrors::InternalServerError)
+ .attach_printable("Failed to write theme version to Redis")
+}
+
+// Delete theme version from Redis
+pub async fn delete_theme_version_from_redis(
+ state: &SessionState,
+ theme_id: &str,
+) -> UserResult<()> {
+ let redis_key = get_theme_version_redis_key(theme_id);
+ let redis_conn = super::get_redis_connection_for_global_tenant(state)?;
+
+ redis_conn
+ .delete_key(&redis_key.into())
+ .await
+ .change_context(UserErrors::InternalServerError)
+ .map(|_| ())
+}
+
fn get_theme_dir_key(theme_id: &str) -> PathBuf {
["themes", theme_id].iter().collect()
}
diff --git a/crates/router_env/src/logger/types.rs b/crates/router_env/src/logger/types.rs
index 8f6e096f7a..66a30e24dc 100644
--- a/crates/router_env/src/logger/types.rs
+++ b/crates/router_env/src/logger/types.rs
@@ -587,6 +587,8 @@ pub enum Flow {
ListAllThemesInLineage,
/// Get user theme using lineage
GetUserThemeUsingLineage,
+ /// Get user theme version
+ GetUserThemeConfigVersion,
/// List initial webhook delivery attempts
WebhookEventInitialDeliveryAttemptList,
/// List delivery attempts for a webhook event
diff --git a/migrations/2025-12-09-091856-0000_add_version_to_themes_table/down.sql b/migrations/2025-12-09-091856-0000_add_version_to_themes_table/down.sql
new file mode 100644
index 0000000000..7955f4d9d9
--- /dev/null
+++ b/migrations/2025-12-09-091856-0000_add_version_to_themes_table/down.sql
@@ -0,0 +1,3 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE themes
+DROP COLUMN IF EXISTS theme_config_version;
\ No newline at end of file
diff --git a/migrations/2025-12-09-091856-0000_add_version_to_themes_table/up.sql b/migrations/2025-12-09-091856-0000_add_version_to_themes_table/up.sql
new file mode 100644
index 0000000000..d398df7d9e
--- /dev/null
+++ b/migrations/2025-12-09-091856-0000_add_version_to_themes_table/up.sql
@@ -0,0 +1,3 @@
+-- Your SQL goes here
+ALTER TABLE themes
+ADD COLUMN IF NOT EXISTS theme_config_version VARCHAR(32) NOT NULL DEFAULT extract(epoch from now())::text;
\ No newline at end of file