Skip to content

Commit

Permalink
chore(notifications): boilerplate for documents event
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit authored and bodymindarts committed Feb 12, 2024
1 parent 64087d7 commit c0ab329
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions core/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ message NotificationEvent {
oneof data {
CircleGrew circle_grew = 1;
CircleThresholdReached circle_threshold_reached = 2;
DocumentsSubmitted documents_submitted = 3;
}
}

Expand All @@ -159,3 +160,7 @@ message CircleThresholdReached {
CircleTimeFrame time_frame = 3;
uint32 threshold = 4;
}

message DocumentsSubmitted {
string user_id = 1;
}
9 changes: 9 additions & 0 deletions core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,13 @@ impl NotificationsApp {
self.executor.notify(event).await?;
Ok(())
}

#[instrument(name = "app.handle_documents_submitted", skip(self), err)]
pub async fn handle_documents_submitted(
&self,
event: DocumentsSubmitted,
) -> Result<(), ApplicationError> {
self.executor.notify(event).await?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion core/notifications/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Executor {
})
}

pub async fn notify<T: NotificationEventNew>(&self, event: T) -> Result<(), ExecutorError> {
pub async fn notify<T: NotificationEvent>(&self, event: T) -> Result<(), ExecutorError> {
let settings = self.settings.find_for_user_id(event.user_id()).await?;
if !settings.should_send_notification(
UserNotificationChannel::Push,
Expand Down
12 changes: 12 additions & 0 deletions core/notifications/src/grpc/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ impl NotificationsService for Notifications {
})
.await?
}
Some(proto::NotificationEvent {
data:
Some(proto::notification_event::Data::DocumentsSubmitted(
proto::DocumentsSubmitted { user_id },
)),
}) => {
self.app
.handle_documents_submitted(notification_event::DocumentsSubmitted {
user_id: GaloyUserId::from(user_id),
})
.await?
}
_ => return Err(Status::invalid_argument("event is required")),
}

Expand Down
6 changes: 6 additions & 0 deletions core/notifications/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ impl Messages {
.to_string();
LocalizedMessage { title, body }
}

pub fn documents_submitted(locale: &str, _event: &DocumentsSubmitted) -> LocalizedMessage {
let title = t!("documents_submitted.title", locale = locale).to_string();
let body = t!("documents_submitted.body", locale = locale).to_string();
LocalizedMessage { title, body }
}
}

#[cfg(test)]
Expand Down
25 changes: 22 additions & 3 deletions core/notifications/src/notification_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct CircleGrew {
pub all_time_circle_size: u32,
}

pub trait NotificationEventNew {
pub trait NotificationEvent {
fn user_id(&self) -> &GaloyUserId;
fn deep_link(&self) -> DeepLink;
fn to_localized_msg(&self, locale: GaloyLocale) -> LocalizedMessage;
}

impl NotificationEventNew for CircleGrew {
impl NotificationEvent for CircleGrew {
fn user_id(&self) -> &GaloyUserId {
&self.user_id
}
Expand All @@ -41,7 +41,7 @@ pub struct CircleThresholdReached {
pub threshold: u32,
}

impl NotificationEventNew for CircleThresholdReached {
impl NotificationEvent for CircleThresholdReached {
fn user_id(&self) -> &GaloyUserId {
&self.user_id
}
Expand All @@ -54,3 +54,22 @@ impl NotificationEventNew for CircleThresholdReached {
Messages::circle_threshold_reached(locale.as_ref(), self)
}
}

#[derive(Debug)]
pub struct DocumentsSubmitted {
pub user_id: GaloyUserId,
}

impl NotificationEvent for DocumentsSubmitted {
fn user_id(&self) -> &GaloyUserId {
&self.user_id
}

fn deep_link(&self) -> DeepLink {
DeepLink::None
}

fn to_localized_msg(&self, locale: GaloyLocale) -> LocalizedMessage {
Messages::documents_submitted(locale.as_ref(), self)
}
}

0 comments on commit c0ab329

Please sign in to comment.