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

1use super::super::super::db::queries as db;
2use super::types::UpdateAlertStatusRequest;
3use rocket::http::Status;
4use rocket::serde::json::{json, Json, Value};
5use rocket::{put, State};
6use std::sync::Arc;
7use crate::DatabaseManager;
8
9use libomni::types::db::v1 as types;
10use types::user::User;
11
12/// Update an alert's status
13#[put("/platform/<platform_id>/alerts/<id>/status", format = "json", data = "<status_data>")]
14pub async fn update_alert_status(
15    platform_id: i64,
16    id: i64,
17    status_data: Json<UpdateAlertStatusRequest>,
18    user: User, // Extract user from request guard
19    db_manager: &State<Arc<DatabaseManager>>,
20) -> Result<Json<Value>, (Status, Json<Value>)> {
21    // Get platform information
22    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
23        Ok(platform) => platform,
24        Err(_) => {
25            return Err((
26                Status::NotFound,
27                Json(json!({
28                    "error": "Platform not found",
29                    "message": format!("Platform with ID {} does not exist", platform_id)
30                }))
31            ));
32        }
33    };
34
35    // Get platform-specific database pool
36    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
37        Ok(pool) => pool,
38        Err(_) => {
39            return Err((
40                Status::InternalServerError,
41                Json(json!({
42                    "error": "Database error",
43                    "message": "Failed to connect to platform database"
44                }))
45            ));
46        }
47    };
48
49    let data = status_data.into_inner();
50    
51    // Validate the status is a valid value
52    match data.status.as_str() {
53        "active" | "acknowledged" | "resolved" | "auto_resolved" => {},
54        _ => return Err((
55            Status::BadRequest,
56            Json(json!({
57                "error": "Invalid status",
58                "message": "Status must be one of: active, acknowledged, resolved, auto_resolved"
59            }))
60        ))
61    }
62    
63    let user_id = user.id;
64
65    let updated_alert = match db::alert::update_alert_status(
66        &pool,
67        id,
68        &data.status,
69        Some(user_id),
70        data.notes.as_deref(),
71    ).await {
72        Ok(alert) => alert,
73        Err(e) => {
74            log::error!("Failed to update alert status: {}", e);
75            return Err((
76                if e.to_string().contains("no rows") {
77                    Status::NotFound
78                } else {
79                    Status::InternalServerError
80                },
81                Json(json!({
82                    "error": if e.to_string().contains("no rows") { "Alert not found" } else { "Database error" },
83                    "message": if e.to_string().contains("no rows") { 
84                        format!("Alert with ID {} does not exist", id) 
85                    } else { 
86                        "Failed to update alert status".to_string() 
87                    }
88                }))
89            ));
90        }
91    };
92
93    Ok(Json(json!({
94        "message": "Alert status updated successfully",
95        "alert": updated_alert
96    })))
97}