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

1use super::super::super::db::queries as db;
2use super::types::BulkUpdateStatusRequest;
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/// Bulk update alert status
13#[put("/platform/<platform_id>/alerts/bulk-status", format = "json", data = "<update_data>")]
14pub async fn bulk_update_alert_status(
15    platform_id: i64,
16    update_data: Json<BulkUpdateStatusRequest>,
17    user: User, // Extract user from request guard
18    db_manager: &State<Arc<DatabaseManager>>,
19) -> Result<Json<Value>, (Status, Json<Value>)> {
20    // Get platform information
21    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
22        Ok(platform) => platform,
23        Err(_) => {
24            return Err((
25                Status::NotFound,
26                Json(json!({
27                    "error": "Platform not found",
28                    "message": format!("Platform with ID {} does not exist", platform_id)
29                }))
30            ));
31        }
32    };
33
34    // Get platform-specific database pool
35    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
36        Ok(pool) => pool,
37        Err(_) => {
38            return Err((
39                Status::InternalServerError,
40                Json(json!({
41                    "error": "Database error",
42                    "message": "Failed to connect to platform database"
43                }))
44            ));
45        }
46    };
47
48    let data = update_data.into_inner();
49    
50    // Validate the status is a valid value
51    match data.status.as_str() {
52        "active" | "acknowledged" | "resolved" | "auto_resolved" => {},
53        _ => return Err((
54            Status::BadRequest,
55            Json(json!({
56                "error": "Invalid status",
57                "message": "Status must be one of: active, acknowledged, resolved, auto_resolved"
58            }))
59        ))
60    }
61    
62    // Validate that at least one filter is provided
63    if data.ids.is_none() && data.service.is_none() && data.app_id.is_none() {
64        return Err((
65            Status::BadRequest,
66            Json(json!({
67                "error": "Missing filters",
68                "message": "At least one filter (ids, service, or app_id) must be provided"
69            }))
70        ));
71    }
72
73    let count = match db::alert::bulk_update_alert_status(
74        &pool,
75        data.ids,
76        data.service.as_deref(),
77        data.app_id,
78        &data.status,
79        user.id, // Use user.id instead of user_id
80        data.notes.as_deref(),
81    ).await {
82        Ok(count) => count,
83        Err(e) => {
84            log::error!("Failed to bulk update alert status: {}", e);
85            return Err((
86                Status::InternalServerError,
87                Json(json!({
88                    "error": "Database error",
89                    "message": "Failed to update alert statuses"
90                }))
91            ));
92        }
93    };
94
95    Ok(Json(json!({
96        "message": "Successfully updated alert status",
97        "count": count
98    })))
99}