Skip to content

Commit

Permalink
fix(notifications): threshold reached proto interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Jan 31, 2024
1 parent 707b78f commit a48a370
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
13 changes: 10 additions & 3 deletions core/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ message HandleNotificationEventResponse { }
message NotificationEvent {
oneof data {
CircleGrew circle_grew = 1;
ThresholdReached threshold_reached = 2;
CircleThresholdReached circle_threshold_reached = 2;
}
}

Expand All @@ -148,7 +148,14 @@ message CircleGrew {
uint32 all_time_circle_size = 4;
}

message ThresholdReached {
enum CircleTimeFrame {
MONTH = 0;
ALL_TIME = 1;
}

message CircleThresholdReached {
string user_id = 1;
uint32 threshold = 2;
CircleType circle_type = 2;
CircleTimeFrame time_frame = 3;
uint32 threshold = 4;
}
2 changes: 1 addition & 1 deletion core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl NotificationsApp {
#[instrument(name = "app.handle_threshold_reached", skip(self), err)]
pub async fn handle_threshold_reached(
&self,
event: ThresholdReached,
event: CircleThresholdReached,
) -> Result<(), ApplicationError> {
self.executor.notify(event).await?;
Ok(())
Expand Down
14 changes: 10 additions & 4 deletions core/notifications/src/executor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub struct NovuConfig {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NovuWorkflows {
#[serde(default = "default_circle_grew_workflow_id")]
pub circle_grew: String,
#[serde(default = "default_circle_threshold_reached_workflow_id")]
pub threshold_reached: String,
}

Expand All @@ -25,12 +27,16 @@ impl NovuWorkflows {
impl Default for NovuWorkflows {
fn default() -> Self {
Self {
circle_grew: dummy_workflow(),
threshold_reached: dummy_workflow(),
circle_grew: default_circle_grew_workflow_id(),
threshold_reached: default_circle_threshold_reached_workflow_id(),
}
}
}

fn dummy_workflow() -> String {
String::from("dummy-workflow")
fn default_circle_grew_workflow_id() -> String {
String::from("circle_grew")
}

fn default_circle_threshold_reached_workflow_id() -> String {
String::from("circle_threshold_reached")
}
9 changes: 9 additions & 0 deletions core/notifications/src/grpc/server/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ impl From<proto::CircleType> for CircleType {
}
}
}

impl From<proto::CircleTimeFrame> for CircleTimeFrame {
fn from(c_type: proto::CircleTimeFrame) -> Self {
match c_type {
proto::CircleTimeFrame::Month => CircleTimeFrame::Month,
proto::CircleTimeFrame::AllTime => CircleTimeFrame::AllTime,
}
}
}
22 changes: 17 additions & 5 deletions core/notifications/src/grpc/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,26 @@ impl NotificationsService for Notifications {
}
Some(proto::NotificationEvent {
data:
Some(proto::notification_event::Data::ThresholdReached(proto::ThresholdReached {
user_id,
threshold,
})),
Some(proto::notification_event::Data::CircleThresholdReached(
proto::CircleThresholdReached {
user_id,
circle_type,
time_frame,
threshold,
},
)),
}) => {
let circle_type = proto::CircleType::try_from(circle_type)
.map(primitives::CircleType::from)
.map_err(|e| Status::invalid_argument(e.to_string()))?;
let time_frame = proto::CircleTimeFrame::try_from(time_frame)
.map(primitives::CircleTimeFrame::from)
.map_err(|e| Status::invalid_argument(e.to_string()))?;
self.app
.handle_threshold_reached(notification_event::ThresholdReached {
.handle_threshold_reached(notification_event::CircleThresholdReached {
user_id: GaloyUserId::from(user_id),
circle_type,
time_frame,
threshold,
})
.await?
Expand Down
24 changes: 18 additions & 6 deletions core/notifications/src/notification_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ impl NotificationEvent for CircleGrew {
}

#[derive(Debug)]
pub struct ThresholdReached {
pub struct CircleThresholdReached {
pub user_id: GaloyUserId,
pub circle_type: CircleType,
pub time_frame: CircleTimeFrame,
pub threshold: u32,
}

impl NotificationEvent for ThresholdReached {
impl NotificationEvent for CircleThresholdReached {
fn workflow_name() -> &'static str {
"threshold_reached"
}
Expand All @@ -57,10 +59,20 @@ impl NotificationEvent for ThresholdReached {
}

fn into_payload(self) -> HashMap<String, AllowedPayloadValues> {
[(
"threshold".to_string(),
AllowedPayloadValues::Number(self.threshold as i32),
)]
[
(
"threshold".to_string(),
AllowedPayloadValues::Number(self.threshold as i32),
),
(
"circle_type".to_string(),
AllowedPayloadValues::String(self.circle_type.to_string()),
),
(
"circle_time_frame".to_string(),
AllowedPayloadValues::String(self.time_frame.to_string()),
),
]
.into_iter()
.collect()
}
Expand Down
15 changes: 15 additions & 0 deletions core/notifications/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,18 @@ impl std::fmt::Display for CircleType {
}
}
}

#[derive(Debug)]
pub enum CircleTimeFrame {
Month,
AllTime,
}

impl std::fmt::Display for CircleTimeFrame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CircleTimeFrame::Month => write!(f, "month"),
CircleTimeFrame::AllTime => write!(f, "all_time"),
}
}
}

0 comments on commit a48a370

Please sign in to comment.