use uuid::Uuid;
pub async fn record_audit(
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
action: &str,
actor: Option<Uuid>,
subject_type: &str,
subject_id: Option<Uuid>,
detail: serde_json::Value,
) -> Result<(), sqlx::Error> {
backbone_auditlog::application::service::append(
exec,
backbone_auditlog::application::service::AuditEvent {
event_type: backbone_auditlog::domain::entity::AuditEventType::DataChange,
action: action.to_string(),
subject_type: Some(if subject_type.contains('.') {
subject_type.to_string()
} else {
format!("event.{subject_type}s")
}),
subject_id: subject_id.map(|id| id.to_string()),
changed: Some(detail),
reason: None,
status: backbone_auditlog::domain::entity::AuditStatus::Success,
actor: actor.map(|id| id.to_string()),
},
)
.await
.map(|_| ())
}
pub async fn record_audit_on_pool(
pool: &sqlx::PgPool,
action: &str,
actor: Option<Uuid>,
subject_type: &str,
subject_id: Option<Uuid>,
detail: serde_json::Value,
) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
if let Some(scope) = backbone_orm::org_scope::current_org_scope() {
backbone_orm::org_scope::bind_org_scope_on(&mut tx, &scope).await?;
}
record_audit(&mut *tx, action, actor, subject_type, subject_id, detail).await?;
tx.commit().await?;
Ok(())
}