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
+7 -10
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::error::AppError;
use crate::models::common::{RequestType, Role, Status};
use crate::models::workspaces::WorkspacePendingApproval;
use crate::models::workspaces::{Workspace, WorkspacePendingApproval};
use crate::service::WorkspaceService;
use crate::session::Session;
@@ -19,12 +19,11 @@ impl WorkspaceService {
pub async fn workspace_pending_approvals(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
limit: i64,
offset: i64,
) -> Result<Vec<WorkspacePendingApproval>, 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 (limit, offset) = clamp_limit_offset(limit, offset);
@@ -33,7 +32,7 @@ impl WorkspaceService {
reviewed_by, reviewed_at, expires_at, created_at, updated_at \
FROM workspace_pending_approval WHERE workspace_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
)
.bind(workspace_id)
.bind(ws.id)
.bind(limit)
.bind(offset)
.fetch_all(self.ctx.db.reader())
@@ -44,11 +43,10 @@ impl WorkspaceService {
pub async fn workspace_request_approval(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
params: RequestApprovalParams,
) -> Result<WorkspacePendingApproval, AppError> {
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
let ws = self.find_workspace_by_id(workspace_id).await?;
self.ensure_workspace_readable(user_uid, &ws).await?;
let request_type = parse_enum(
@@ -79,7 +77,7 @@ impl WorkspaceService {
reviewed_by, reviewed_at, expires_at, created_at, updated_at",
)
.bind(Uuid::now_v7())
.bind(workspace_id)
.bind(ws.id)
.bind(user_uid)
.bind(request_type)
.bind(params.reason)
@@ -96,12 +94,11 @@ impl WorkspaceService {
pub async fn workspace_review_approval(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
approval_id: Uuid,
approved: bool,
) -> Result<(), 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::Owner)
.await?;
@@ -133,7 +130,7 @@ impl WorkspaceService {
.bind(user_uid)
.bind(now)
.bind(approval_id)
.bind(workspace_id)
.bind(ws.id)
.execute(&mut *txn)
.await
.map_err(AppError::Database)?;