use crate::api::BearerToken;
use crate::db::models::SecurityAlert;
use crate::security::jwt::JwtManager;
use actix_web::{post, web, HttpResponse, Responder};
use serde::Deserialize;
use serde_json::json;
use uuid::Uuid;
#[derive(Deserialize)]
pub struct AlertTriggerRequest {
pub entity_id: Uuid, pub entity_type: String, pub alert_type: String, pub severity: String, pub details: serde_json::Value, }
#[post("/alerts/trigger")]
pub async fn trigger_alert(
payload: web::Json<AlertTriggerRequest>, bearer: BearerToken,
) -> impl Responder {
let token = bearer.0;
if token.is_empty() {
return HttpResponse::Unauthorized().body("Missing Authorization token");
}
let jwt_secret = std::env::var("JWT_SECRET").unwrap_or_else(|_| {
"a_very_secure_and_long_secret_key_that_is_at_least_32_bytes_long".to_string()
});
let jwt_manager = JwtManager::new(
&crate::security::secret::SecureString::new(jwt_secret),
60,
"my_app".to_string(),
"user_service".to_string(),
);
if jwt_manager.decode_token(&token).is_err() {
return HttpResponse::Unauthorized().body("Invalid or expired token");
}
let alert = SecurityAlert {
id: Uuid::new_v4(),
user_id: payload.entity_id, alert_type: payload.alert_type.clone(), alert_data: payload.details.clone(), created_at: chrono::Utc::now().naive_utc(), };
HttpResponse::Ok().json(json!({
"status": "alert_triggered",
"alert": alert
}))
}