feat(service): expand service layer with new domain operations
- Add IM service modules: audit, channel roles, custom emojis, forum tags, integrations, invitations, repo links, slash commands, stages, voice, webhooks - Add PR service modules: review requests, templates - Add repo service modules: contributors, release assets, git extras (archive, branch rename, commit extras, diff/merge, tag, tree) - Add user service: social (follow/block) - Add internal auth service - Update existing service modules with expanded functionality - Remove deleted IM modules: articles, delivery trace, drafts, follows, messages, polls, presence, reactions, threads
This commit is contained in:
@@ -7,7 +7,7 @@ use crate::models::workspaces::{Workspace, WorkspacePendingApproval};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, parse_enum};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, parse_enum, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct RequestApprovalParams {
|
||||
@@ -64,8 +64,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -116,8 +115,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::models::workspaces::{Workspace, WorkspaceBilling};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::merge_optional_text;
|
||||
use super::util::{merge_optional_text, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateBillingParams {
|
||||
@@ -51,8 +51,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::models::workspaces::{Workspace, WorkspaceCustomBranding};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::merge_optional_text;
|
||||
use super::util::{merge_optional_text, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateBrandingParams {
|
||||
@@ -52,8 +52,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
+109
-31
@@ -7,7 +7,9 @@ use crate::models::workspaces::Workspace;
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum};
|
||||
use super::util::{
|
||||
clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum, set_local_user_id,
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreateWorkspaceParams {
|
||||
@@ -71,6 +73,20 @@ impl WorkspaceService {
|
||||
return Err(AppError::BadRequest("name is required".into()));
|
||||
}
|
||||
|
||||
let exists: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM workspace WHERE lower(name) = lower($1) AND deleted_at IS NULL)",
|
||||
)
|
||||
.bind(&name)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
if exists {
|
||||
return Err(AppError::Conflict(format!(
|
||||
"workspace name '{}' is already taken",
|
||||
name
|
||||
)));
|
||||
}
|
||||
|
||||
let visibility = match params.visibility {
|
||||
Some(ref v) => parse_enum(
|
||||
Some(v.clone()),
|
||||
@@ -91,8 +107,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -219,8 +234,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -259,8 +273,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -296,8 +309,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -333,8 +345,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -354,6 +365,59 @@ impl WorkspaceService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn workspace_restore(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
workspace_name: &str,
|
||||
) -> Result<Workspace, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
.ctx
|
||||
.db
|
||||
.writer()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
let ws = sqlx::query_as::<_, Workspace>(
|
||||
"SELECT id, owner_id, name, description, avatar_url, visibility, plan, status, \
|
||||
default_role, is_personal, archived_at, created_at, updated_at, deleted_at \
|
||||
FROM workspace WHERE lower(name) = lower($1) AND deleted_at IS NOT NULL FOR UPDATE",
|
||||
)
|
||||
.bind(workspace_name)
|
||||
.fetch_optional(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("workspace not found".into()))?;
|
||||
|
||||
if ws.owner_id != user_uid {
|
||||
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Owner)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let result = sqlx::query_as::<_, Workspace>(
|
||||
"UPDATE workspace SET deleted_at = NULL, status = 'active', updated_at = $1 \
|
||||
WHERE id = $2 AND deleted_at IS NOT NULL \
|
||||
RETURNING id, owner_id, name, description, avatar_url, visibility, plan, status, \
|
||||
default_role, is_personal, archived_at, created_at, updated_at, deleted_at",
|
||||
)
|
||||
.bind(now)
|
||||
.bind(ws.id)
|
||||
.fetch_optional(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("workspace not found".into()))?;
|
||||
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn workspace_transfer_owner(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
@@ -361,28 +425,12 @@ impl WorkspaceService {
|
||||
new_owner_id: Uuid,
|
||||
) -> Result<Workspace, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Owner)
|
||||
.await?;
|
||||
|
||||
if new_owner_id == ws.owner_id {
|
||||
return Err(AppError::BadRequest(
|
||||
"new owner must be different from current owner".into(),
|
||||
));
|
||||
}
|
||||
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(ws.id)
|
||||
.bind(new_owner_id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
if !is_member {
|
||||
return Err(AppError::BadRequest(
|
||||
"new owner must be an active member".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
@@ -392,12 +440,43 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
// Lock the workspace row to prevent concurrent transfers
|
||||
let _ws = sqlx::query_as::<_, Workspace>(
|
||||
"SELECT id, owner_id, name, description, avatar_url, visibility, plan, status, \
|
||||
default_role, is_personal, archived_at, created_at, updated_at, deleted_at \
|
||||
FROM workspace WHERE id = $1 AND deleted_at IS NULL FOR UPDATE",
|
||||
)
|
||||
.bind(ws.id)
|
||||
.fetch_one(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
// Verify current user is still the owner
|
||||
if _ws.owner_id != user_uid {
|
||||
return Err(AppError::Unauthorized);
|
||||
}
|
||||
|
||||
// Verify new owner is an active member (within transaction)
|
||||
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(ws.id)
|
||||
.bind(new_owner_id)
|
||||
.fetch_one(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
if !is_member {
|
||||
return Err(AppError::BadRequest(
|
||||
"new owner must be an active member".into(),
|
||||
));
|
||||
}
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE workspace_member SET role = 'owner', updated_at = $1 \
|
||||
WHERE workspace_id = $2 AND user_id = $3",
|
||||
@@ -467,8 +546,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -7,13 +7,18 @@ use crate::models::workspaces::{Workspace, WorkspaceDomain};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct AddDomainParams {
|
||||
pub domain: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateDomainParams {
|
||||
pub domain: String,
|
||||
}
|
||||
|
||||
impl WorkspaceService {
|
||||
pub async fn workspace_domains(
|
||||
&self,
|
||||
@@ -70,8 +75,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -113,8 +117,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -156,8 +159,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -229,8 +231,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -248,6 +249,49 @@ impl WorkspaceService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn workspace_update_domain(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
ws: &Workspace,
|
||||
domain_id: Uuid,
|
||||
params: UpdateDomainParams,
|
||||
) -> Result<WorkspaceDomain, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
|
||||
.await?;
|
||||
let domain = required_text(params.domain, "domain")?.to_lowercase();
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
.ctx
|
||||
.db
|
||||
.writer()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
let result = sqlx::query_as::<_, WorkspaceDomain>(
|
||||
"UPDATE workspace_domain SET domain = $1, is_verified = false, verified_at = NULL, updated_at = $2 \
|
||||
WHERE id = $3 AND workspace_id = $4 \
|
||||
RETURNING id, workspace_id, domain, verification_token_hash, is_primary, is_verified, verified_at, created_at, updated_at",
|
||||
)
|
||||
.bind(&domain)
|
||||
.bind(now)
|
||||
.bind(domain_id)
|
||||
.bind(ws.id)
|
||||
.fetch_optional(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("domain not found".into()))?;
|
||||
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn generate_domain_verification_token() -> String {
|
||||
(0..32)
|
||||
.map(|_| format!("{:02x}", rand::random::<u8>()))
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::models::workspaces::{Workspace, WorkspaceIntegration};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreateIntegrationParams {
|
||||
@@ -79,8 +79,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -146,8 +145,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -195,8 +193,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::pb::email::{EmailAddress, SendEmailRequest};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, role_level};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, role_level, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreateInvitationParams {
|
||||
@@ -108,8 +108,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -179,8 +178,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -262,8 +260,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, role_level};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, role_level, set_local_user_id};
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::workspaces::{Workspace, WorkspaceMember};
|
||||
@@ -108,8 +108,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -204,8 +203,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -268,8 +266,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -313,8 +310,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::models::workspaces::{Workspace, WorkspaceSettings};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::merge_optional_text;
|
||||
use super::util::{merge_optional_text, set_local_user_id};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateWorkspaceSettingsParams {
|
||||
@@ -50,8 +50,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -103,9 +102,18 @@ impl WorkspaceService {
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
self.find_workspace_settings(workspace_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound("workspace settings not found".into()))
|
||||
// Read from writer to avoid replication lag
|
||||
sqlx::query_as::<_, WorkspaceSettings>(
|
||||
"SELECT workspace_id, allow_public_repos, allow_member_invites, require_two_factor, \
|
||||
default_repo_visibility, default_branch_name, issue_tracking_enabled, \
|
||||
pull_requests_enabled, wiki_enabled, created_at, updated_at \
|
||||
FROM workspace_settings WHERE workspace_id = $1",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.fetch_optional(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("workspace settings not found".into()))
|
||||
}
|
||||
|
||||
async fn find_workspace_settings(
|
||||
|
||||
@@ -43,7 +43,7 @@ impl WorkspaceService {
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
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",
|
||||
"SELECT COUNT(*) FROM issue WHERE workspace_id = $1 AND deleted_at IS NULL",
|
||||
)
|
||||
.bind(ws.id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
@@ -102,14 +102,16 @@ impl WorkspaceService {
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
// Read from writer to avoid replication lag
|
||||
sqlx::query_as::<_, WorkspaceStats>(
|
||||
"SELECT workspace_id, members_count, repos_count, issues_count, pull_requests_count, \
|
||||
storage_bytes, bandwidth_bytes, build_minutes_used, last_activity_at, updated_at \
|
||||
FROM workspace_stats WHERE workspace_id = $1",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.fetch_one(self.ctx.db.reader())
|
||||
.fetch_optional(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("workspace stats not found".into()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub use crate::service::util::{
|
||||
clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum, required_text, role_level,
|
||||
clamp_limit_offset, ensure_affected, merge_optional_text, parse_enum, required_text,
|
||||
role_level, set_local_user_id,
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::models::workspaces::{Workspace, WorkspaceWebhook};
|
||||
use crate::service::WorkspaceService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text, set_local_user_id};
|
||||
|
||||
fn validate_webhook_url(url_str: &str) -> Result<(), AppError> {
|
||||
let url = Url::parse(url_str).map_err(|_| AppError::BadRequest("Invalid URL format".into()))?;
|
||||
@@ -114,8 +114,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -186,8 +185,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
@@ -230,8 +228,7 @@ impl WorkspaceService {
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
sqlx::query(set_local_user_id(user_uid))
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
Reference in New Issue
Block a user