use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::error::AppError; use crate::models::common::Role; use crate::models::workspaces::WorkspaceCustomBranding; use crate::service::WorkspaceService; use crate::session::Session; use super::util::merge_optional_text; #[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)] pub struct UpdateBrandingParams { pub logo_url: Option, pub favicon_url: Option, pub primary_color: Option, pub accent_color: Option, pub custom_css: Option, pub support_url: Option, pub enabled: Option, } impl WorkspaceService { pub async fn workspace_branding( &self, ctx: &Session, workspace_id: Uuid, ) -> Result { let user_uid = ctx.user().ok_or(AppError::Unauthorized)?; let ws = self.find_workspace_by_id(workspace_id).await?; self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin) .await?; self.ensure_workspace_branding(workspace_id).await } pub async fn workspace_update_branding( &self, ctx: &Session, workspace_id: Uuid, params: UpdateBrandingParams, ) -> Result { let user_uid = ctx.user().ok_or(AppError::Unauthorized)?; let ws = self.find_workspace_by_id(workspace_id).await?; self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin) .await?; let current = self.ensure_workspace_branding(workspace_id).await?; let now = chrono::Utc::now(); let mut txn = self .ctx .db .writer() .begin() .await .map_err(|_| AppError::TxnError)?; sqlx::query("SET LOCAL app.current_user_id = $1") .bind(user_uid) .execute(&mut *txn) .await .map_err(AppError::Database)?; let result = sqlx::query_as::<_, WorkspaceCustomBranding>( "UPDATE workspace_custom_branding SET logo_url = $1, favicon_url = $2, primary_color = $3, \ accent_color = $4, custom_css = $5, support_url = $6, enabled = $7, updated_at = $8 \ WHERE workspace_id = $9 \ RETURNING workspace_id, logo_url, favicon_url, primary_color, accent_color, \ custom_css, support_url, enabled, created_at, updated_at", ) .bind(merge_optional_text(params.logo_url, current.logo_url)) .bind(merge_optional_text(params.favicon_url, current.favicon_url)) .bind(merge_optional_text(params.primary_color, current.primary_color)) .bind(merge_optional_text(params.accent_color, current.accent_color)) .bind(merge_optional_text(params.custom_css, current.custom_css)) .bind(merge_optional_text(params.support_url, current.support_url)) .bind(params.enabled.unwrap_or(current.enabled)) .bind(now) .bind(workspace_id) .fetch_one(&mut *txn) .await .map_err(AppError::Database)?; txn.commit().await.map_err(|_| AppError::TxnError)?; Ok(result) } async fn ensure_workspace_branding( &self, workspace_id: Uuid, ) -> Result { if let Some(branding) = self.find_workspace_branding(workspace_id).await? { return Ok(branding); } let now = chrono::Utc::now(); sqlx::query( "INSERT INTO workspace_custom_branding (workspace_id, enabled, created_at, updated_at) \ VALUES ($1, false, $2, $2) ON CONFLICT (workspace_id) DO NOTHING", ) .bind(workspace_id) .bind(now) .execute(self.ctx.db.writer()) .await .map_err(AppError::Database)?; self.find_workspace_branding(workspace_id) .await? .ok_or(AppError::NotFound("workspace branding not found".into())) } async fn find_workspace_branding( &self, workspace_id: Uuid, ) -> Result, AppError> { sqlx::query_as::<_, WorkspaceCustomBranding>( "SELECT workspace_id, logo_url, favicon_url, primary_color, accent_color, \ custom_css, support_url, enabled, created_at, updated_at \ FROM workspace_custom_branding WHERE workspace_id = $1", ) .bind(workspace_id) .fetch_optional(self.ctx.db.reader()) .await .map_err(AppError::Database) } }