Skip to main content

agentics_persistence/db/challenge_creation/
audit.rs

1use serde_json::Value;
2use sqlx::{PgPool, Postgres, Transaction};
3
4use agentics_domain::models::ids::{
5    AdminServiceTokenId, ChallengeReviewAuditEventId, ChallengeReviewRecordId, HumanId,
6};
7use agentics_error::Result;
8
9/// Input for appending a review_record audit event.
10#[derive(Debug, Clone)]
11pub struct CreateChallengeReviewRecordAuditEventInput {
12    pub event_id: ChallengeReviewAuditEventId,
13    pub review_record_id: ChallengeReviewRecordId,
14    pub actor_human_id: Option<HumanId>,
15    pub actor_admin_service_token_id: Option<AdminServiceTokenId>,
16    pub actor_display: Option<String>,
17    pub action: String,
18    pub message: String,
19    pub metadata: Value,
20}
21
22/// Append a review_record audit event.
23pub async fn create_challenge_review_audit_event(
24    pool: &PgPool,
25    input: &CreateChallengeReviewRecordAuditEventInput,
26) -> Result<()> {
27    let mut tx = pool.begin().await?;
28    create_challenge_review_audit_event_tx(&mut tx, input).await?;
29    tx.commit().await?;
30    Ok(())
31}
32
33/// Creates challenge review record audit event tx after validating caller inputs.
34pub(super) async fn create_challenge_review_audit_event_tx(
35    tx: &mut Transaction<'_, Postgres>,
36    input: &CreateChallengeReviewRecordAuditEventInput,
37) -> Result<()> {
38    sqlx::query(
39        r#"
40        INSERT INTO challenge_review_audit_events (
41            id,
42            review_record_id,
43            actor_human_id,
44            actor_admin_service_token_id,
45            actor_display,
46            action,
47            message,
48            metadata_json
49        )
50        VALUES ($1::uuid, $2::uuid, $3::uuid, $4::uuid, $5, $6, $7, $8)
51        "#,
52    )
53    .bind(input.event_id.as_str())
54    .bind(input.review_record_id.as_str())
55    .bind(input.actor_human_id.as_ref().map(HumanId::as_str))
56    .bind(
57        input
58            .actor_admin_service_token_id
59            .as_ref()
60            .map(AdminServiceTokenId::as_str),
61    )
62    .bind(&input.actor_display)
63    .bind(&input.action)
64    .bind(&input.message)
65    .bind(&input.metadata)
66    .execute(&mut **tx)
67    .await?;
68
69    Ok(())
70}