Files
appks/service/workspace/audit.rs
T
zhenyi dca717be10 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
2026-06-07 18:44:01 +08:00

69 lines
2.2 KiB
Rust

use uuid::Uuid;
use crate::error::AppError;
use crate::models::common::{EventType, JsonValue, Role, TargetType};
use crate::models::workspaces::{Workspace, WorkspaceAuditLog};
use crate::service::WorkspaceService;
use crate::session::Session;
use super::util::clamp_limit_offset;
impl WorkspaceService {
pub async fn workspace_audit_logs(
&self,
ctx: &Session,
ws: &Workspace,
limit: i64,
offset: i64,
) -> Result<Vec<WorkspaceAuditLog>, AppError> {
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
self.ensure_workspace_role_at_least(user_uid, &ws, Role::Admin)
.await?;
let (limit, offset) = clamp_limit_offset(limit, offset);
sqlx::query_as::<_, WorkspaceAuditLog>(
"SELECT id, workspace_id, actor_id, action, target_type, target_id, \
ip_address, user_agent, metadata, created_at FROM workspace_audit_log \
WHERE workspace_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
)
.bind(ws.id)
.bind(limit)
.bind(offset)
.fetch_all(self.ctx.db.reader())
.await
.map_err(AppError::Database)
}
#[allow(clippy::too_many_arguments)]
pub async fn workspace_log_audit(
&self,
workspace_id: Uuid,
actor_id: Uuid,
action: EventType,
target_type: Option<TargetType>,
target_id: Option<Uuid>,
ip_address: Option<String>,
user_agent: Option<String>,
metadata: Option<JsonValue>,
) -> Result<(), AppError> {
sqlx::query(
"INSERT INTO workspace_audit_log (id, workspace_id, actor_id, action, target_type, \
target_id, ip_address, user_agent, metadata, created_at) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
)
.bind(Uuid::now_v7())
.bind(workspace_id)
.bind(actor_id)
.bind(action)
.bind(target_type)
.bind(target_id)
.bind(ip_address)
.bind(user_agent)
.bind(metadata)
.bind(chrono::Utc::now())
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Ok(())
}
}