Function create_audit_log

Source
pub async fn create_audit_log(
    pool: &Pool<MySql>,
    user_id: Option<i64>,
    org_id: Option<i64>,
    action: &str,
    resource_type: &str,
    resource_id: Option<String>,
) -> Result<AuditLog>
Expand description

Creates a new audit log entry in the system.

This function records an action performed within the system, tracking who performed the action, what organization they belong to, what action was performed, on what type of resource, and which specific resource was affected. It serves as a critical component for maintaining accountability and tracking system changes.

§Arguments

  • pool - Database connection pool for executing the query
  • user_id - Optional ID of the user who performed the action (None for system actions)
  • org_id - Optional ID of the organization in which the action occurred
  • action - Description of the action performed (e.g., “create”, “update”, “delete”)
  • resource_type - Type of resource affected (e.g., “app”, “deployment”, “user”)
  • resource_id - Optional identifier of the specific resource affected

§Returns

  • Ok(AuditLog) - Successfully created audit log entry with all database-assigned fields
  • Err(anyhow::Error) - Failed to create the audit log entry

§Examples

// Log a user creating an application
create_audit_log(
    &pool,
    Some(user_id),
    Some(org_id),
    "create",
    "app",
    Some(app_id.to_string())
).await?;

// Log a system maintenance action
create_audit_log(
    &pool,
    None,
    None,
    "maintenance",
    "system",
    None
).await?;