33 lines
784 B
Rust
33 lines
784 B
Rust
use crate::api::ApiError;
|
|
use serde_json::Value;
|
|
use sqlx::PgPool;
|
|
use time::OffsetDateTime;
|
|
use uuid::Uuid;
|
|
|
|
pub async fn record(
|
|
pool: &PgPool,
|
|
actor_user_id: Option<Uuid>,
|
|
action: &str,
|
|
target_type: Option<&str>,
|
|
target_id: Option<&str>,
|
|
metadata: Option<Value>,
|
|
) -> Result<(), ApiError> {
|
|
let now = OffsetDateTime::now_utc();
|
|
sqlx::query(
|
|
r#"
|
|
INSERT INTO audit_log (id, actor_user_id, action, target_type, target_id, metadata, created_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
"#,
|
|
)
|
|
.bind(Uuid::new_v4())
|
|
.bind(actor_user_id)
|
|
.bind(action)
|
|
.bind(target_type)
|
|
.bind(target_id)
|
|
.bind(metadata)
|
|
.bind(now)
|
|
.execute(pool)
|
|
.await
|
|
.map_err(|_| ApiError::Internal)?;
|
|
Ok(())
|
|
}
|