This example demonstrates how to send a digest notification email by batching different types of notifications from multiple workflows using the Novu Framework. The digest notification email is sent to the user with a summary of all the notifications that have been triggered by different workflows.
Summary worklflow to digest all notifications from different workflows
import { CronExpression, workflow } from "@novu/framework";
import { renderLinearDigestEmail } from "../../emails/linear-digest";
import {
ticketAssignedPayloadSchema,
commentOnTicketPayloadSchema,
summaryWorkflowPayloadSchema,
} from "./schemas";
export const summaryWorkflow = workflow(
"multi-workflow-digest",
async ({ step }) => {
const digestedNotifications = await step.digest(
"digest-all-notifications",
async () => {
return {
cron: CronExpression.EVERY_DAY_AT_8AM,
};
}
);
await step.email("send-email", async () => {
return {
subject: `${digestedNotifications.events.length} unread notifications on Novu - Linear`,
body: renderLinearDigestEmail(digestedNotifications.events),
};
});
},
{
payloadSchema: summaryWorkflowPayloadSchema,
}
);
Workflow to send notification when someone comments on a ticket
export const someoneCommentedOnTicket = workflow(
"someone-commented-on-ticket",
async ({ step, payload, subscriber }) => {
await step.inApp("send-inbox-notification", async () => {
return {
body: `${payload.comment.author.userName} commented ${payload.comment.text} on ticket ${payload.ticket.id}`,
};
});
await step.custom("digest-the-message", async () => {
try {
await summaryWorkflow.trigger({
to: subscriber?.subscriberId as string,
payload: payload,
});
return {
success: true,
error: null,
};
} catch (error) {
return {
success: false,
error: error,
};
}
});
},
{
payloadSchema: commentOnTicketPayloadSchema,
}
);
Workflow to send notification when a ticket is assigned to the user
export const ticketAssigned = workflow(
"ticket-assigned-to-user",
async ({ step, payload, subscriber }) => {
await step.inApp("send-inbox-notification", async () => {
return {
body: `${payload.assign.author.userName} assigned ticket ${payload.ticket.id} ${payload.ticket.title} to you.`,
};
});
await step.custom("digest-the-message", async () => {
try {
await summaryWorkflow.trigger({
to: subscriber?.subscriberId as string,
payload: payload,
});
return {
success: true,
error: null,
};
} catch (error) {
return {
success: false,
error: error,
};
}
});
},
{
payloadSchema: ticketAssignedPayloadSchema,
}
);