Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(notifications): pass overrides when triggering a workflow #3928

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/notifications/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ impl Executor {

let user_id = event.user_id().to_string();
let payload = event.into_payload();
let overrides = <T as NotificationEvent>::into_overrides();

let id = self
.config
.workflows
.for_name(<T as NotificationEvent>::workflow_name());

self.trigger_workflow(user_id, id, payload).await?;
self.trigger_workflow(user_id, id, payload, overrides)
.await?;

Ok(())
}
Expand All @@ -61,12 +63,14 @@ impl Executor {
user_id: String,
trigger_name: String,
payload_data: HashMap<String, AllowedPayloadValues>,
overrides: Option<HashMap<String, serde_json::Value>>,
) -> Result<(), ExecutorError> {
self.client
.trigger(TriggerPayload {
name: trigger_name,
payload: payload_data,
to: TriggerRecipientsType::Single(TriggerRecipientBuilder::new(user_id).build()),
overrides,
})
.await?;

Expand Down Expand Up @@ -122,6 +126,7 @@ impl Executor {
.email(recipient_email)
.build(),
),
overrides: None,
})
.await?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions core/notifications/src/executor/novu/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct TriggerPayload {
pub payload: ITriggerPayload,
pub to: TriggerRecipientsType,
pub name: String,
pub overrides: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
52 changes: 52 additions & 0 deletions core/notifications/src/notification_event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde_json::Value;
use std::collections::HashMap;

use crate::{executor::AllowedPayloadValues, primitives::*};
Expand All @@ -6,6 +7,7 @@ pub trait NotificationEvent {
fn workflow_name() -> &'static str;
fn user_id(&self) -> &GaloyUserId;
fn into_payload(self) -> HashMap<String, AllowedPayloadValues>;
fn into_overrides() -> Option<HashMap<String, Value>>;
}

#[derive(Debug)]
Expand Down Expand Up @@ -39,6 +41,31 @@ impl NotificationEvent for CircleGrew {
.into_iter()
.collect()
}

fn into_overrides() -> Option<HashMap<String, Value>> {
Some(
[(
"fcm".to_string(),
Value::Object(
[(
"data".to_string(),
Value::Object(
[(
"linkTo".to_string(),
Value::String("/people/circles".to_string()),
)]
.into_iter()
.collect(),
),
)]
.into_iter()
.collect(),
),
)]
.into_iter()
.collect(),
)
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -76,4 +103,29 @@ impl NotificationEvent for CircleThresholdReached {
.into_iter()
.collect()
}

fn into_overrides() -> Option<HashMap<String, Value>> {
Some(
[(
"fcm".to_string(),
Value::Object(
[(
"data".to_string(),
Value::Object(
[(
"linkTo".to_string(),
Value::String("/people/circles".to_string()),
)]
.into_iter()
.collect(),
),
)]
.into_iter()
.collect(),
),
)]
.into_iter()
.collect(),
)
}
}
Loading