use std::sync::Arc;
use sqlx::PgPool;
use tonic::{Request, Response, Status};
use crate::metrics::{MetricsRecorder, NoopMetrics};
use crate::proto::udb::core::notification::services::v1 as notif_pb;
use crate::proto::udb::core::notification::services::v1::notification_service_server::NotificationService;
use crate::runtime::DataBrokerRuntime;
use crate::runtime::channels::ChannelManager;
pub use crate::proto::udb::core::notification::services::v1::notification_service_server::NotificationServiceServer;
use super::DataBrokerService;
mod config;
mod delivery;
mod errors;
mod events;
mod handlers;
mod model;
mod store;
#[cfg(test)]
mod tests;
pub(crate) use config::{NOTIFICATION_DELIVERY_BATCH, notification_delivery_interval};
#[cfg(feature = "http-client")]
pub(crate) use delivery::run_notification_delivery_worker_once;
use errors::notification_capability_status;
pub struct NotificationServiceImpl {
pub(crate) pg_pool: Option<PgPool>,
pub(crate) runtime: Option<Arc<DataBrokerRuntime>>,
pub(crate) outbox_relation: Option<String>,
pub(crate) channels: Option<ChannelManager>,
pub(crate) metrics: Arc<dyn MetricsRecorder>,
}
impl NotificationServiceImpl {
pub fn new() -> Self {
Self {
pg_pool: None,
runtime: None,
outbox_relation: None,
channels: None,
metrics: Arc::new(NoopMetrics),
}
}
pub fn with_postgres(mut self, pool: Option<PgPool>) -> Self {
self.pg_pool = pool;
self
}
pub(crate) fn with_runtime(mut self, runtime: Option<Arc<DataBrokerRuntime>>) -> Self {
self.runtime = runtime;
self
}
pub(crate) fn require_runtime(&self) -> Result<&DataBrokerRuntime, Status> {
self.runtime.as_deref().ok_or_else(|| {
notification_capability_status(
"native_entity_dispatch",
"runtime_native_entity_dispatch",
"notification service requires runtime native entity dispatch",
)
})
}
pub(crate) fn with_metrics(mut self, metrics: Arc<dyn MetricsRecorder>) -> Self {
self.metrics = metrics;
self
}
pub(crate) fn with_channels(mut self, channels: Option<ChannelManager>) -> Self {
self.channels = channels;
self
}
pub(crate) fn with_outbox(mut self, relation: Option<String>) -> Self {
self.outbox_relation = relation;
self
}
pub(crate) fn require_pool(&self) -> Result<&PgPool, Status> {
self.pg_pool.as_ref().ok_or_else(|| {
notification_capability_status(
"postgres_store",
"postgres_store",
"notification service requires a Postgres-backed store (no PG pool configured)",
)
})
}
}
impl Default for NotificationServiceImpl {
fn default() -> Self {
Self::new()
}
}
#[tonic::async_trait]
impl NotificationService for NotificationServiceImpl {
async fn send_notification(
&self,
request: Request<notif_pb::SendNotificationRequest>,
) -> Result<Response<notif_pb::SendNotificationResponse>, Status> {
handlers::send_notification(self, request).await
}
async fn get_notification(
&self,
request: Request<notif_pb::GetNotificationRequest>,
) -> Result<Response<notif_pb::GetNotificationResponse>, Status> {
handlers::get_notification(self, request).await
}
async fn list_notifications(
&self,
request: Request<notif_pb::ListNotificationsRequest>,
) -> Result<Response<notif_pb::ListNotificationsResponse>, Status> {
handlers::list_notifications(self, request).await
}
async fn retry_notification(
&self,
request: Request<notif_pb::RetryNotificationRequest>,
) -> Result<Response<notif_pb::RetryNotificationResponse>, Status> {
handlers::retry_notification(self, request).await
}
async fn report_delivery(
&self,
request: Request<notif_pb::ReportDeliveryRequest>,
) -> Result<Response<notif_pb::ReportDeliveryResponse>, Status> {
handlers::report_delivery(self, request).await
}
async fn upsert_template(
&self,
request: Request<notif_pb::UpsertTemplateRequest>,
) -> Result<Response<notif_pb::UpsertTemplateResponse>, Status> {
handlers::upsert_template(self, request).await
}
async fn get_template(
&self,
request: Request<notif_pb::GetTemplateRequest>,
) -> Result<Response<notif_pb::GetTemplateResponse>, Status> {
handlers::get_template(self, request).await
}
async fn list_templates(
&self,
request: Request<notif_pb::ListTemplatesRequest>,
) -> Result<Response<notif_pb::ListTemplatesResponse>, Status> {
handlers::list_templates(self, request).await
}
async fn get_delivery_stats(
&self,
request: Request<notif_pb::GetDeliveryStatsRequest>,
) -> Result<Response<notif_pb::GetDeliveryStatsResponse>, Status> {
handlers::get_delivery_stats(self, request).await
}
async fn set_preference(
&self,
request: Request<notif_pb::SetPreferenceRequest>,
) -> Result<Response<notif_pb::SetPreferenceResponse>, Status> {
handlers::set_preference(self, request).await
}
async fn get_preference(
&self,
request: Request<notif_pb::GetPreferenceRequest>,
) -> Result<Response<notif_pb::GetPreferenceResponse>, Status> {
handlers::get_preference(self, request).await
}
async fn list_preferences(
&self,
request: Request<notif_pb::ListPreferencesRequest>,
) -> Result<Response<notif_pb::ListPreferencesResponse>, Status> {
handlers::list_preferences(self, request).await
}
}
impl DataBrokerService {
pub(crate) fn build_notification_service(&self) -> NotificationServiceImpl {
let runtime = self.runtime.load_full();
let pg_pool = runtime
.native_store_pool_for_service("notification", true, "")
.ok();
let outbox = runtime.config().cdc.outbox_relation();
let channels = Some(runtime.channels().clone());
NotificationServiceImpl::new()
.with_postgres(pg_pool)
.with_runtime(Some(runtime))
.with_outbox(Some(outbox))
.with_channels(channels)
.with_metrics(self.metrics.clone())
}
}