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:
@@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||
use super::util::{clamp_limit_offset, ensure_affected, role_level};
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::workspaces::WorkspaceMember;
|
||||
use crate::models::workspaces::{Workspace, WorkspaceMember};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
@@ -23,12 +23,11 @@ impl WorkspaceService {
|
||||
pub async fn workspace_members(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<WorkspaceMember>, 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 (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
sqlx::query_as::<_, WorkspaceMember>(
|
||||
@@ -36,7 +35,7 @@ impl WorkspaceService {
|
||||
last_active_at, created_at, updated_at FROM workspace_member \
|
||||
WHERE workspace_id = $1 AND status = 'active' ORDER BY created_at ASC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
@@ -47,11 +46,10 @@ impl WorkspaceService {
|
||||
pub async fn workspace_add_member(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
params: AddMemberParams,
|
||||
) -> Result<WorkspaceMember, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let ws = self.find_workspace_by_id(workspace_id).await?;
|
||||
let actor_role = self
|
||||
.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
||||
.await?;
|
||||
@@ -59,7 +57,7 @@ impl WorkspaceService {
|
||||
let settings_allow = sqlx::query_scalar::<_, bool>(
|
||||
"SELECT allow_member_invites FROM workspace_settings WHERE workspace_id = $1",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -73,7 +71,7 @@ impl WorkspaceService {
|
||||
let existing = sqlx::query_scalar::<_, bool>(
|
||||
"SELECT EXISTS(SELECT 1 FROM workspace_member WHERE workspace_id = $1 AND user_id = $2)",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.bind(params.user_id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
@@ -96,7 +94,6 @@ impl WorkspaceService {
|
||||
return Err(AppError::BadRequest("invalid role".into()));
|
||||
}
|
||||
|
||||
// Non-owner admins cannot grant roles equal to or higher than their own
|
||||
if actor_role != Role::Owner && role_level(role) >= role_level(actor_role) {
|
||||
return Err(AppError::BadRequest(
|
||||
"cannot grant role equal to or higher than your own".into(),
|
||||
@@ -123,7 +120,7 @@ impl WorkspaceService {
|
||||
RETURNING id, workspace_id, user_id, role, status, invited_by, joined_at, last_active_at, created_at, updated_at",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.bind(params.user_id)
|
||||
.bind(role.to_string())
|
||||
.bind(user_uid)
|
||||
@@ -136,7 +133,7 @@ impl WorkspaceService {
|
||||
"UPDATE workspace_stats SET members_count = members_count + 1, updated_at = $1 WHERE workspace_id = $2",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -148,12 +145,11 @@ impl WorkspaceService {
|
||||
pub async fn workspace_update_member_role(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
member_id: Uuid,
|
||||
params: UpdateMemberRoleParams,
|
||||
) -> Result<WorkspaceMember, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let ws = self.find_workspace_by_id(workspace_id).await?;
|
||||
let actor_role = self
|
||||
.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
||||
.await?;
|
||||
@@ -171,7 +167,6 @@ impl WorkspaceService {
|
||||
return Err(AppError::BadRequest("invalid role".into()));
|
||||
}
|
||||
|
||||
// Non-owner admins cannot grant roles equal to or higher than their own
|
||||
if actor_role != Role::Owner && role_level(new_role) >= role_level(actor_role) {
|
||||
return Err(AppError::BadRequest(
|
||||
"cannot grant role equal to or higher than your own".into(),
|
||||
@@ -184,7 +179,7 @@ impl WorkspaceService {
|
||||
WHERE id = $1 AND workspace_id = $2",
|
||||
)
|
||||
.bind(member_id)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
@@ -222,7 +217,7 @@ impl WorkspaceService {
|
||||
.bind(new_role.to_string())
|
||||
.bind(now)
|
||||
.bind(member_id)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_one(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -234,11 +229,10 @@ impl WorkspaceService {
|
||||
pub async fn workspace_remove_member(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_id: Uuid,
|
||||
ws: &Workspace,
|
||||
member_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let ws = self.find_workspace_by_id(workspace_id).await?;
|
||||
let actor_role = self
|
||||
.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
||||
.await?;
|
||||
@@ -249,7 +243,7 @@ impl WorkspaceService {
|
||||
WHERE id = $1 AND workspace_id = $2",
|
||||
)
|
||||
.bind(member_id)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
@@ -283,7 +277,7 @@ impl WorkspaceService {
|
||||
let result =
|
||||
sqlx::query("DELETE FROM workspace_member WHERE id = $1 AND workspace_id = $2")
|
||||
.bind(member_id)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -293,7 +287,7 @@ impl WorkspaceService {
|
||||
"UPDATE workspace_stats SET members_count = GREATEST(members_count - 1, 0), updated_at = $1 WHERE workspace_id = $2",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -302,9 +296,8 @@ impl WorkspaceService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn workspace_leave(&self, ctx: &Session, workspace_id: Uuid) -> Result<(), AppError> {
|
||||
pub async fn workspace_leave(&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?;
|
||||
|
||||
if ws.owner_id == user_uid {
|
||||
return Err(AppError::BadRequest(
|
||||
@@ -328,7 +321,7 @@ impl WorkspaceService {
|
||||
|
||||
let result =
|
||||
sqlx::query("DELETE FROM workspace_member WHERE workspace_id = $1 AND user_id = $2")
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.bind(user_uid)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
@@ -339,7 +332,7 @@ impl WorkspaceService {
|
||||
"UPDATE workspace_stats SET members_count = GREATEST(members_count - 1, 0), updated_at = $1 WHERE workspace_id = $2",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(workspace_id)
|
||||
.bind(ws.id)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
Reference in New Issue
Block a user