omni_orchestrator/schemas/v1/api/alerts/
get.rs

1use super::super::super::db::queries as db;
2use rocket::http::Status;
3use rocket::serde::json::{json, Json, Value};
4use rocket::{get, State};
5use std::sync::Arc;
6use crate::DatabaseManager;
7
8/// Get details of a specific alert including related data
9#[get("/platform/<platform_id>/alerts/<id>")]
10pub async fn get_alert(
11    platform_id: i64,
12    id: i64,
13    db_manager: &State<Arc<DatabaseManager>>,
14) -> Result<Json<Value>, (Status, Json<Value>)> {
15    // Get platform information
16    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
17        Ok(platform) => platform,
18        Err(_) => {
19            return Err((
20                Status::NotFound,
21                Json(json!({
22                    "error": "Platform not found",
23                    "message": format!("Platform with ID {} does not exist", platform_id)
24                }))
25            ));
26        }
27    };
28
29    // Get platform-specific database pool
30    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
31        Ok(pool) => pool,
32        Err(_) => {
33            return Err((
34                Status::InternalServerError,
35                Json(json!({
36                    "error": "Database error",
37                    "message": "Failed to connect to platform database"
38                }))
39            ));
40        }
41    };
42
43    let alert_data = match db::alert::get_alert_with_related_data(&pool, id).await {
44        Ok(data) => data,
45        Err(e) => {
46            log::error!("Failed to fetch alert {}: {}", id, e);
47            return Err((
48                if e.to_string().contains("no rows") {
49                    Status::NotFound
50                } else {
51                    Status::InternalServerError
52                },
53                Json(json!({
54                    "error": if e.to_string().contains("no rows") { "Alert not found" } else { "Database error" },
55                    "message": if e.to_string().contains("no rows") { 
56                        format!("Alert with ID {} does not exist", id) 
57                    } else { 
58                        "Failed to fetch alert details".to_string() 
59                    }
60                }))
61            ));
62        }
63    };
64
65    Ok(Json(json!(alert_data)))
66}