omni_orchestrator/schemas/v1/api/audit_log/
app_logs.rs1use std::sync::Arc;
2use crate::DatabaseManager;
3use super::super::super::db::queries as db;
4use rocket::get;
5use rocket::http::Status;
6use rocket::serde::json::{json, Json, Value};
7use rocket::State;
8
9#[get("/platform/<platform_id>/audit_logs/<app_id>?<page>&<per_page>")]
11pub async fn list_audit_logs_for_app(
12 platform_id: i64,
13 app_id: i64,
14 page: Option<i64>,
15 per_page: Option<i64>,
16 db_manager: &State<Arc<DatabaseManager>>,
17) -> Result<Json<Value>, (Status, Json<Value>)> {
18 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
20 Ok(platform) => platform,
21 Err(_) => {
22 return Err((
23 Status::NotFound,
24 Json(json!({
25 "error": "Platform not found",
26 "message": format!("Platform with ID {} does not exist", platform_id)
27 }))
28 ));
29 }
30 };
31
32 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
34 Ok(pool) => pool,
35 Err(_) => {
36 return Err((
37 Status::InternalServerError,
38 Json(json!({
39 "error": "Database error",
40 "message": "Failed to connect to platform database"
41 }))
42 ));
43 }
44 };
45
46 let page: i64 = page.unwrap_or(1);
47 let per_page: i64 = per_page.unwrap_or(10);
48
49 let audit_logs = match db::audit_log::get_audit_logs_by_app(&pool, app_id, page, per_page).await {
50 Ok(logs) => logs,
51 Err(_) => {
52 return Err((
53 Status::InternalServerError,
54 Json(json!({
55 "error": "Database error",
56 "message": "Failed to retrieve audit logs for app"
57 }))
58 ));
59 }
60 };
61
62 let total_count = match db::audit_log::count_audit_logs_by_app(&pool, app_id).await {
63 Ok(count) => count,
64 Err(_) => {
65 return Err((
66 Status::InternalServerError,
67 Json(json!({
68 "error": "Database error",
69 "message": "Failed to count audit logs for app"
70 }))
71 ));
72 }
73 };
74
75 let total_pages = if per_page > 0 {
76 (total_count + per_page - 1) / per_page
77 } else {
78 1
79 };
80
81 let response = json!({
82 "audit_logs": audit_logs,
83 "pagination": {
84 "page": page,
85 "per_page": per_page,
86 "total_count": total_count,
87 "total_pages": total_pages
88 }
89 });
90
91 Ok(Json(response))
92}