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
+21 -47
View File
@@ -52,12 +52,11 @@ impl WorkspaceService {
pub async fn workspace_get(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
) -> Result<Workspace, 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?;
Ok(ws)
Ok(ws.clone())
}
pub async fn workspace_create(
@@ -177,17 +176,16 @@ impl WorkspaceService {
pub async fn workspace_update(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
params: UpdateWorkspaceParams,
) -> Result<Workspace, 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 name =
merge_optional_text(params.name, Some(ws.name.clone())).unwrap_or(ws.name.clone());
let description = merge_optional_text(params.description, ws.description);
let description = merge_optional_text(params.description, ws.description.clone());
let visibility = parse_enum(
params.visibility,
ws.visibility,
@@ -204,7 +202,6 @@ impl WorkspaceService {
None => ws.default_role.parse().unwrap_or(Role::Member),
};
// Restrict default_role to safe roles only
match default_role {
Role::Member | Role::Contributor | Role::Viewer | Role::Guest => {}
_ => {
@@ -239,7 +236,7 @@ impl WorkspaceService {
.bind(visibility)
.bind(default_role.to_string())
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.fetch_optional(&mut *txn)
.await
.map_err(AppError::Database)?
@@ -249,13 +246,8 @@ impl WorkspaceService {
Ok(result)
}
pub async fn workspace_archive(
&self,
ctx: &Session,
workspace_id: Uuid,
) -> Result<(), AppError> {
pub async fn workspace_archive(&self, ctx: &Session, ws: &Workspace) -> 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?;
@@ -278,7 +270,7 @@ impl WorkspaceService {
WHERE id = $2 AND deleted_at IS NULL AND status <> 'archived'",
)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.execute(&mut *txn)
.await
.map_err(AppError::Database)?;
@@ -291,13 +283,8 @@ impl WorkspaceService {
Ok(())
}
pub async fn workspace_unarchive(
&self,
ctx: &Session,
workspace_id: Uuid,
) -> Result<(), AppError> {
pub async fn workspace_unarchive(&self, ctx: &Session, ws: &Workspace) -> 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?;
@@ -320,7 +307,7 @@ impl WorkspaceService {
WHERE id = $2 AND deleted_at IS NULL AND status = 'archived'",
)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.execute(&mut *txn)
.await
.map_err(AppError::Database)?;
@@ -333,13 +320,8 @@ impl WorkspaceService {
Ok(())
}
pub async fn workspace_delete(
&self,
ctx: &Session,
workspace_id: Uuid,
) -> Result<(), AppError> {
pub async fn workspace_delete(&self, ctx: &Session, ws: &Workspace) -> 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?;
@@ -362,7 +344,7 @@ impl WorkspaceService {
WHERE id = $2 AND deleted_at IS NULL",
)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.execute(&mut *txn)
.await
.map_err(AppError::Database)?;
@@ -375,11 +357,10 @@ impl WorkspaceService {
pub async fn workspace_transfer_owner(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
new_owner_id: Uuid,
) -> Result<Workspace, 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?;
@@ -391,7 +372,7 @@ impl WorkspaceService {
let is_member = sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM workspace_member WHERE workspace_id = $1 AND user_id = $2 AND status = 'active')",
)
.bind(workspace_id)
.bind(ws.id)
.bind(new_owner_id)
.fetch_one(self.ctx.db.reader())
.await
@@ -422,7 +403,7 @@ impl WorkspaceService {
WHERE workspace_id = $2 AND user_id = $3",
)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.bind(new_owner_id)
.execute(&mut *txn)
.await
@@ -433,7 +414,7 @@ impl WorkspaceService {
WHERE workspace_id = $2 AND user_id = $3",
)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.bind(user_uid)
.execute(&mut *txn)
.await
@@ -446,7 +427,7 @@ impl WorkspaceService {
)
.bind(new_owner_id)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.fetch_one(&mut *txn)
.await
.map_err(AppError::Database)?;
@@ -458,13 +439,12 @@ impl WorkspaceService {
pub async fn workspace_upload_avatar(
&self,
ctx: &Session,
workspace_id: Uuid,
ws: &Workspace,
data: Vec<u8>,
content_type: Option<String>,
file_name: Option<String>,
) -> Result<Workspace, 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?;
@@ -473,12 +453,7 @@ impl WorkspaceService {
crate::service::util::validate_avatar_size(data.len(), 5 * 1024 * 1024)?;
let old_avatar_url = ws.avatar_url.clone();
let storage_key = format!(
"workspaces/{}/avatar/{}.{}",
workspace_id,
Uuid::now_v7(),
ext
);
let storage_key = format!("workspaces/{}/avatar/{}.{}", ws.id, Uuid::now_v7(), ext);
self.ctx.storage.put(&storage_key, data).await?;
let avatar_url = self.ctx.storage.public_url(&storage_key).ok_or_else(|| {
AppError::Config("APP_S3_PUBLIC_URL is required for avatar upload".into())
@@ -506,7 +481,7 @@ impl WorkspaceService {
)
.bind(&avatar_url)
.bind(now)
.bind(workspace_id)
.bind(ws.id)
.fetch_optional(&mut *txn)
.await
.map_err(AppError::Database)?;
@@ -557,12 +532,12 @@ impl WorkspaceService {
pub async fn workspace_user_role(
&self,
user_uid: Uuid,
workspace_id: Uuid,
ws: &Workspace,
) -> Result<Option<Role>, AppError> {
let role_str: Option<String> = sqlx::query_scalar(
"SELECT role FROM workspace_member WHERE workspace_id = $1 AND user_id = $2 AND status = 'active'",
)
.bind(workspace_id)
.bind(ws.id)
.bind(user_uid)
.fetch_optional(self.ctx.db.reader())
.await
@@ -571,7 +546,6 @@ impl WorkspaceService {
match role_str {
Some(r) => Ok(Some(r.parse().unwrap_or(Role::Unknown))),
None => {
let ws = self.find_workspace_by_id(workspace_id).await?;
if ws.owner_id == user_uid {
return Ok(Some(Role::Owner));
}