omni_orchestrator/schemas/v1/api/audit_log/
create.rs

1use std::sync::Arc;
2use crate::DatabaseManager;
3use super::super::super::db::queries as db;
4use rocket::post;
5use rocket::http::Status;
6use rocket::serde::json::{json, Json, Value};
7use rocket::State;
8
9use libomni::types::db::v1 as types;
10use types::audit_log::AuditLog;
11
12/// Creates a new audit log entry in the system.
13#[post("/platform/<platform_id>/audit_log", format = "json", data = "<audit_log>")]
14pub async fn create_audit_log(
15    platform_id: i64,
16    audit_log: Json<AuditLog>,
17    db_manager: &State<Arc<DatabaseManager>>,
18) -> Result<Json<AuditLog>, (Status, Json<Value>)> {
19    // Get platform information
20    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
21        Ok(platform) => platform,
22        Err(_) => {
23            return Err((
24                Status::NotFound,
25                Json(json!({
26                    "error": "Platform not found",
27                    "message": format!("Platform with ID {} does not exist", platform_id)
28                }))
29            ));
30        }
31    };
32
33    // Get platform-specific database pool
34    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
35        Ok(pool) => pool,
36        Err(_) => {
37            return Err((
38                Status::InternalServerError,
39                Json(json!({
40                    "error": "Database error",
41                    "message": "Failed to connect to platform database"
42                }))
43            ));
44        }
45    };
46
47    match db::audit_log::create_audit_log(
48        &pool,
49        audit_log.user_id,
50        audit_log.org_id,
51        &audit_log.action,
52        &audit_log.resource_type,
53        audit_log.resource_id.clone(),
54    ).await {
55        Ok(result) => Ok(Json(result)),
56        Err(_) => Err((
57            Status::InternalServerError,
58            Json(json!({
59                "error": "Database error",
60                "message": "Failed to create audit log entry"
61            }))
62        )),
63    }
64}