refactor(workspace): pass workspace object instead of id to service methods

- Replace workspace_id parameter with Workspace object reference in all workspace service methods
- Remove redundant find_workspace_by_id calls that were duplicated in each method
- Update all method signatures across approval, audit, billing, branding, core, settings and stats modules
- Modify SQL queries to bind ws.id instead of separate workspace_id parameter
- Add Workspace import to all affected modules
- Adjust method calls in API handlers to pass workspace object instead of id
- Consolidate workspace retrieval logic to single location per operation flow
This commit is contained in:
zhenyi
2026-06-07 18:44:01 +08:00
parent 297a54f312
commit dca717be10
75 changed files with 2306 additions and 212 deletions
+6 -8
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::error::AppError;
use crate::models::common::Role;
use crate::models::workspaces::WorkspaceCustomBranding;
use crate::models::workspaces::{Workspace, WorkspaceCustomBranding};
use crate::service::WorkspaceService;
use crate::session::Session;
@@ -24,27 +24,25 @@ impl WorkspaceService {
pub async fn workspace_branding(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
) -> Result<WorkspaceCustomBranding, AppError> {
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
self.ensure_workspace_branding(ws.id).await
}
pub async fn workspace_update_branding(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
params: UpdateBrandingParams,
) -> Result<WorkspaceCustomBranding, AppError> {
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 current = self.ensure_workspace_branding(ws.id).await?;
let now = chrono::Utc::now();
let mut txn = self
@@ -75,7 +73,7 @@ impl WorkspaceService {
.bind(merge_optional_text(params.support_url, current.support_url))
.bind(params.enabled.unwrap_or(current.enabled))
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.fetch_one(&mut *txn)
.await
.map_err(AppError::Database)?;