Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

improve: add profile to notifications templates eg {{profile.email}}

Carl-Gerhard Lindesvärd31ccfb8b5f53
diff --git a/packages/db/src/services/notification.service.ts b/packages/db/src/services/notification.service.ts
index 79f035ac5..49b224e0d 100644
--- a/packages/db/src/services/notification.service.ts
+++ b/packages/db/src/services/notification.service.ts
@@ -13,6 +13,7 @@ import type {
IServiceCreateEventPayload,
IServiceEvent,
} from './event.service';
+import { getProfileById, getProfileByIdCached } from './profile.service';
import { getProjectByIdCached } from './project.service';
type ICreateNotification = Pick<
@@ -220,7 +221,10 @@ function notificationTemplateEvent({
const value = pathOr('', path.split('.'), payload);
if (value) {
- template = template.replaceAll(match, JSON.stringify(value));
+ template = template.replaceAll(
+ match,
+ typeof value === 'object' ? JSON.stringify(value) : value,
+ );
}
}
@@ -245,6 +249,22 @@ export async function checkNotificationRulesForEvent(
) {
const project = await getProjectByIdCached(payload.projectId);
const rules = await getNotificationRulesByProjectId(payload.projectId);
+
+ // If profile is present in the template, add it to the payload (event)
+ // so we can use it in the template
+ if (
+ payload.profileId &&
+ rules.some((rule) => rule.template?.match(/{{profile\.[^}]*}}/))
+ ) {
+ const profile = await getProfileByIdCached(
+ payload.profileId,
+ payload.projectId,
+ );
+ if (profile) {
+ (payload as any).profile = profile;
+ }
+ }
+
await Promise.all(
rules.flatMap((rule) => {
if (rule.config.type === 'events') {