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 8ba626c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
9 changes: 8 additions & 1 deletion core/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ message CircleGrew {
uint32 all_time_circle_size = 4;
}

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

message ThresholdReached {
string user_id = 1;
uint32 threshold = 2;
CircleType circle_type = 2;
CircleTimeFrame time_frame = 3;
uint32 threshold = 4;
}
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,
}
}
}
10 changes: 10 additions & 0 deletions core/notifications/src/grpc/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,22 @@ impl NotificationsService for Notifications {
data:
Some(proto::notification_event::Data::ThresholdReached(proto::ThresholdReached {
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 {
user_id: GaloyUserId::from(user_id),
circle_type,
time_frame,
threshold,
})
.await?
Expand Down
20 changes: 16 additions & 4 deletions core/notifications/src/notification_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl NotificationEvent for CircleGrew {
#[derive(Debug)]
pub struct ThresholdReached {
pub user_id: GaloyUserId,
pub circle_type: CircleType,
pub time_frame: CircleTimeFrame,
pub threshold: u32,
}

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 8ba626c

Please sign in to comment.