omni_orchestrator/schemas/v1/api/notifications/
role.rs1use std::sync::Arc;
2use crate::DatabaseManager;
3use crate::schemas::v1::db::queries::{self as db};
4use super::types::CreateRoleNotificationRequest;
5use rocket::http::Status;
6use rocket::serde::json::{json, Json, Value};
7use rocket::{get, post, State};
8
9use libomni::types::db::v1 as types;
10use types::user::User;
11
12#[get("/platform/<platform_id>/notifications/role/<role_id>?<page>&<per_page>")]
14pub async fn list_role_notifications(
15 platform_id: i64,
16 role_id: i64,
17 page: Option<i64>,
18 per_page: Option<i64>,
19 user: User, db_manager: &State<Arc<DatabaseManager>>,
21) -> Result<Json<Value>, (Status, Json<Value>)> {
22 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
24 Ok(platform) => platform,
25 Err(_) => {
26 return Err((
27 Status::NotFound,
28 Json(json!({
29 "error": "Platform not found",
30 "message": format!("Platform with ID {} does not exist", platform_id)
31 }))
32 ));
33 }
34 };
35
36 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
38 Ok(pool) => pool,
39 Err(_) => {
40 return Err((
41 Status::InternalServerError,
42 Json(json!({
43 "error": "Database error",
44 "message": "Failed to connect to platform database"
45 }))
46 ));
47 }
48 };
49
50 let page = page.unwrap_or(0);
66 let per_page = per_page.unwrap_or(20);
67
68 match db::notification::list_role_notifications(
69 &pool,
70 role_id,
71 page,
72 per_page,
73 ).await {
74 Ok(notifications) => Ok(Json(json!({
75 "notifications": notifications,
76 "pagination": {
77 "page": page,
78 "per_page": per_page
79 }
80 }))),
81 Err(e) => {
82 log::error!("Failed to fetch role notifications: {}", e);
83 Err((
84 Status::InternalServerError,
85 Json(json!({
86 "error": "Database error",
87 "message": "Failed to fetch role notifications"
88 }))
89 ))
90 }
91 }
92}
93
94#[post("/platform/<platform_id>/notifications/role", format = "json", data = "<notification_data>")]
96pub async fn create_role_notification(
97 platform_id: i64,
98 notification_data: Json<CreateRoleNotificationRequest>,
99 user: User, db_manager: &State<Arc<DatabaseManager>>,
101) -> Result<Json<Value>, (Status, Json<Value>)> {
102 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
104 Ok(platform) => platform,
105 Err(_) => {
106 return Err((
107 Status::NotFound,
108 Json(json!({
109 "error": "Platform not found",
110 "message": format!("Platform with ID {} does not exist", platform_id)
111 }))
112 ));
113 }
114 };
115
116 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
118 Ok(pool) => pool,
119 Err(_) => {
120 return Err((
121 Status::InternalServerError,
122 Json(json!({
123 "error": "Database error",
124 "message": "Failed to connect to platform database"
125 }))
126 ));
127 }
128 };
129
130 let data = notification_data.into_inner();
142
143 match db::notification::create_role_notification(
144 &pool,
145 data.role_id,
146 &data.message,
147 &data.notification_type,
148 data.org_id,
149 data.app_id,
150 data.importance.as_deref(),
151 data.action_url.as_deref(),
152 data.action_label.as_deref(),
153 data.expires_at,
154 ).await {
155 Ok(notification) => Ok(Json(json!({
156 "message": "Role notification created successfully",
157 "notification": notification
158 }))),
159 Err(e) => {
160 log::error!("Failed to create role notification: {}", e);
161 Err((
162 Status::InternalServerError,
163 Json(json!({
164 "error": "Database error",
165 "message": "Failed to create role notification"
166 }))
167 ))
168 }
169 }
170}