fix(dashboard): improvements to notifications templates
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "notifications" ADD COLUMN "notificationRuleId" UUID;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_notificationRuleId_fkey" FOREIGN KEY ("notificationRuleId") REFERENCES "notification_rules"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -380,37 +380,40 @@ enum IntegrationType {
|
||||
}
|
||||
|
||||
model NotificationRule {
|
||||
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
name String
|
||||
projectId String
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
integrations Integration[]
|
||||
sendToApp Boolean @default(false)
|
||||
sendToEmail Boolean @default(false)
|
||||
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
name String
|
||||
projectId String
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
integrations Integration[]
|
||||
sendToApp Boolean @default(false)
|
||||
sendToEmail Boolean @default(false)
|
||||
/// [IPrismaNotificationRuleConfig]
|
||||
config Json
|
||||
template String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
config Json
|
||||
template String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
notifications Notification[]
|
||||
|
||||
@@map("notification_rules")
|
||||
}
|
||||
|
||||
model Notification {
|
||||
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
projectId String
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
title String
|
||||
message String
|
||||
isReadAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
sendToApp Boolean @default(false)
|
||||
sendToEmail Boolean @default(false)
|
||||
integration Integration? @relation(fields: [integrationId], references: [id])
|
||||
integrationId String? @db.Uuid
|
||||
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
projectId String
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
title String
|
||||
message String
|
||||
isReadAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
sendToApp Boolean @default(false)
|
||||
sendToEmail Boolean @default(false)
|
||||
integration Integration? @relation(fields: [integrationId], references: [id])
|
||||
integrationId String? @db.Uuid
|
||||
notificationRuleId String? @db.Uuid
|
||||
notificationRule NotificationRule? @relation(fields: [notificationRuleId], references: [id])
|
||||
/// [IPrismaNotificationPayload]
|
||||
payload Json?
|
||||
payload Json?
|
||||
|
||||
@@map("notifications")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,12 @@ import { getProjectByIdCached } from './project.service';
|
||||
|
||||
type ICreateNotification = Pick<
|
||||
Notification,
|
||||
'projectId' | 'title' | 'message' | 'integrationId' | 'payload'
|
||||
| 'projectId'
|
||||
| 'title'
|
||||
| 'message'
|
||||
| 'integrationId'
|
||||
| 'payload'
|
||||
| 'notificationRuleId'
|
||||
>;
|
||||
|
||||
export type INotificationPayload =
|
||||
@@ -118,6 +123,7 @@ export async function createNotification(notification: ICreateNotification) {
|
||||
projectId: notification.projectId,
|
||||
payload: notification.payload || Prisma.DbNull,
|
||||
...getIntegration(notification.integrationId),
|
||||
notificationRuleId: notification.notificationRuleId,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -202,9 +208,23 @@ function notificationTemplateEvent({
|
||||
rule: INotificationRuleCached;
|
||||
}) {
|
||||
if (!rule.template) return `You received a new "${payload.name}" event`;
|
||||
return rule.template
|
||||
let template = rule.template
|
||||
.replaceAll('$EVENT_NAME', payload.name)
|
||||
.replaceAll('$RULE_NAME', rule.name);
|
||||
.replaceAll('$RULE_NAME', rule.name)
|
||||
.replaceAll('{{rule_name}}', rule.name);
|
||||
|
||||
// Replace all {{xxx}} placeholders with their values
|
||||
const placeholderMatches = template.match(/{{[^}]+}}/g) || [];
|
||||
for (const match of placeholderMatches) {
|
||||
const path = match.slice(2, -2); // Remove {{ and }}
|
||||
const value = pathOr('', path.split('.'), payload);
|
||||
|
||||
if (value) {
|
||||
template = template.replaceAll(match, JSON.stringify(value));
|
||||
}
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
function notificationTemplateFunnel({
|
||||
@@ -253,6 +273,7 @@ export async function checkNotificationRulesForEvent(
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: integration.id,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -261,6 +282,7 @@ export async function checkNotificationRulesForEvent(
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: APP_NOTIFICATION_INTEGRATION_ID,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -270,6 +292,7 @@ export async function checkNotificationRulesForEvent(
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: EMAIL_NOTIFICATION_INTEGRATION_ID,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -325,13 +348,18 @@ export async function checkNotificationRulesForSessionEnd(
|
||||
// Generate notification promises
|
||||
return [
|
||||
...rule.integrations.map((integration) =>
|
||||
createNotification({ ...notification, integrationId: integration.id }),
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: integration.id,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
),
|
||||
...(rule.sendToApp
|
||||
? [
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: APP_NOTIFICATION_INTEGRATION_ID,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
@@ -340,6 +368,7 @@ export async function checkNotificationRulesForSessionEnd(
|
||||
createNotification({
|
||||
...notification,
|
||||
integrationId: EMAIL_NOTIFICATION_INTEGRATION_ID,
|
||||
notificationRuleId: rule.id,
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
|
||||
@@ -21,19 +21,19 @@ export const notificationRouter = createTRPCRouter({
|
||||
return db.notification.findMany({
|
||||
where: {
|
||||
projectId: input.projectId,
|
||||
sendToApp: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
integration: {
|
||||
include: {
|
||||
notificationRules: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
notificationRule: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user