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

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-08-08 22:38:03 +02:00
parent 26efb7a94d
commit 31ccfb8b5f

View File

@@ -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') {