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:
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::workspaces::WorkspaceStats;
|
||||
use crate::models::workspaces::{Workspace, WorkspaceStats};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
@@ -10,28 +10,26 @@ impl WorkspaceService {
|
||||
pub async fn workspace_stats(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
) -> Result<WorkspaceStats, 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?;
|
||||
self.ensure_workspace_stats(workspace_id).await
|
||||
self.ensure_workspace_stats(ws.id).await
|
||||
}
|
||||
|
||||
pub async fn workspace_refresh_stats(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
) -> Result<WorkspaceStats, 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 members_count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM workspace_member WHERE workspace_id = $1 AND status = 'active'",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -39,7 +37,7 @@ impl WorkspaceService {
|
||||
let repos_count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM repo WHERE workspace_id = $1 AND deleted_at IS NULL",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -47,7 +45,7 @@ impl WorkspaceService {
|
||||
let issues_count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM issue WHERE repo_id IN (SELECT id FROM repo WHERE workspace_id = $1 AND deleted_at IS NULL) AND deleted_at IS NULL",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -55,7 +53,7 @@ impl WorkspaceService {
|
||||
let prs_count = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM pull_request WHERE repo_id IN (SELECT id FROM repo WHERE workspace_id = $1 AND deleted_at IS NULL) AND deleted_at IS NULL",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -72,7 +70,7 @@ impl WorkspaceService {
|
||||
.bind(issues_count)
|
||||
.bind(prs_count)
|
||||
.bind(now)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
Reference in New Issue
Block a user