Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: notifications package

Brad Barbina6a16a0fc5ab
diff --git a/package-lock.json b/package-lock.json
index 41dd77364c..f1e8587c34 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -33299,6 +33299,7 @@
},
"devDependencies": {
"@carbon/tsconfig": "*",
+ "eslint-config-carbon": "*",
"typescript": "^5.2.2"
}
},
diff --git a/packages/notifications/.eslintrc.cjs b/packages/notifications/.eslintrc.cjs
new file mode 100644
index 0000000000..1308cba140
--- /dev/null
+++ b/packages/notifications/.eslintrc.cjs
@@ -0,0 +1,4 @@
+module.exports = {
+ root: true,
+ extends: ["carbon"],
+};
diff --git a/packages/notifications/package.json b/packages/notifications/package.json
new file mode 100644
index 0000000000..5ab3f3b504
--- /dev/null
+++ b/packages/notifications/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "@carbon/notifications",
+ "type": "module",
+ "dependencies": {
+ "@novu/node": "^2.0.1",
+ "nanoid": "5.0.4"
+ },
+ "devDependencies": {
+ "@carbon/tsconfig": "*",
+ "eslint-config-carbon": "*",
+ "typescript": "^5.2.2"
+ },
+ "exports": {
+ ".": "./src/index.ts"
+ },
+ "private": true,
+ "scripts": {
+ "clean": "rimraf .turbo node_modules dist",
+ "dev": "tsup --watch",
+ "lint": "TIMING=1 eslint \"src/**/*.ts*\""
+ },
+ "sideEffects": false,
+ "version": "*"
+}
diff --git a/packages/notifications/src/index.ts b/packages/notifications/src/index.ts
new file mode 100644
index 0000000000..594317f4bf
--- /dev/null
+++ b/packages/notifications/src/index.ts
@@ -0,0 +1,163 @@
+import { Novu } from "@novu/node";
+import { nanoid } from "nanoid";
+
+const novu = new Novu(process.env.NOVU_SECRET_KEY!);
+
+const API_ENDPOINT = "https://api.novu.co/v1";
+
+export enum TriggerEvents {
+ SalesRfqAssignmentInApp = "sales-rfq-assignment-in-app",
+ QuoteAssignmentInApp = "quote-assignment-in-app",
+ SalesOrderAssignmentInApp = "sales-order-assignment-in-app",
+ JobAssignmentInApp = "job-assignment-in-app",
+ DigitalQuoteResponseInApp = "digital-quote-response-in-app",
+}
+
+export enum NotificationTypes {
+ SalesRfqAssignment = "sales-rfq-assignment",
+ QuoteAssignment = "quote-assignment",
+ SalesOrderAssignment = "sales-order-assignment",
+ JobAssignment = "job-assignment",
+ DigitalQuoteResponse = "digital-quote-response",
+}
+
+type TriggerUser = {
+ subscriberId: string;
+ email: string;
+ fullName: string;
+ avatarUrl?: string;
+ companyId: string;
+};
+
+type TriggerPayload = {
+ name: TriggerEvents;
+ payload: any;
+ user: TriggerUser;
+ replyTo?: string;
+ tenant?: string; // NOTE: Currently no way to listen for messages with tenant, we use user id + company id for unique
+};
+
+export function getSubscriberId({
+ companyId,
+ userId,
+}: {
+ companyId: string;
+ userId: string;
+}) {
+ return `${companyId}:${userId}`;
+}
+
+export async function trigger(data: TriggerPayload) {
+ try {
+ await novu.trigger(data.name, {
+ to: {
+ ...data.user,
+ // Prefix subscriber id with team id
+ subscriberId: getSubscriberId({
+ companyId: data.user.companyId,
+ userId: data.user.subscriberId,
+ }),
+ },
+ payload: data.payload,
+ tenant: data.tenant,
+ overrides: {
+ email: {
+ replyTo: data.replyTo,
+ // @ts-ignore
+ headers: {
+ "X-Entity-Ref-ID": nanoid(),
+ },
+ },
+ },
+ });
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+export async function triggerBulk(events: TriggerPayload[]) {
+ try {
+ await novu.bulkTrigger(
+ events.map((data) => ({
+ name: data.name,
+ to: {
+ ...data.user,
+ // Prefix subscriber id with team id
+ subscriberId: getSubscriberId({
+ companyId: data.user.companyId,
+ userId: data.user.subscriberId,
+ }),
+ },
+ payload: data.payload,
+ tenant: data.tenant,
+ overrides: {
+ email: {
+ replyTo: data.replyTo,
+ headers: {
+ "X-Entity-Ref-ID": nanoid(),
+ },
+ },
+ },
+ }))
+ );
+ } catch (error) {
+ console.log(error);
+ }
+}
+
+type GetSubscriberPreferencesParams = {
+ teamId: string;
+ subscriberId: string;
+};
+
+export async function getSubscriberPreferences({
+ subscriberId,
+ teamId,
+}: GetSubscriberPreferencesParams) {
+ const response = await fetch(
+ `${API_ENDPOINT}/subscribers/${teamId}_${subscriberId}/preferences`,
+ {
+ method: "GET",
+ headers: {
+ Authorization: `ApiKey ${process.env.NOVU_API_KEY!}`,
+ },
+ }
+ );
+
+ return response.json();
+}
+
+type UpdateSubscriberPreferenceParams = {
+ subscriberId: string;
+ teamId: string;
+ templateId: string;
+ type: string;
+ enabled: boolean;
+};
+
+export async function updateSubscriberPreference({
+ subscriberId,
+ teamId,
+ templateId,
+ type,
+ enabled,
+}: UpdateSubscriberPreferenceParams) {
+ const response = await fetch(
+ `${API_ENDPOINT}/subscribers/${teamId}_${subscriberId}/preferences/${templateId}`,
+ {
+ method: "PATCH",
+ headers: {
+ Authorization: `ApiKey ${process.env.NOVU_API_KEY!}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ channel: {
+ type,
+ enabled,
+ },
+ }),
+ }
+ );
+
+ return response.json();
+}
diff --git a/packages/notifications/tsconfig.json b/packages/notifications/tsconfig.json
new file mode 100644
index 0000000000..fc3043b055
--- /dev/null
+++ b/packages/notifications/tsconfig.json
@@ -0,0 +1,10 @@
+{
+ "compilerOptions": {
+ "lib": ["ES2015"],
+ "outDir": "./dist",
+ "rootDir": "./src"
+ },
+ "exclude": ["node_modules"],
+ "extends": "@carbon/tsconfig/base.json",
+ "include": ["src"]
+}
diff --git a/packages/react/src/hooks/useNotifications.tsx b/packages/react/src/hooks/useNotifications.tsx
index 5d97c41d0e..b3e40ab643 100644
--- a/packages/react/src/hooks/useNotifications.tsx
+++ b/packages/react/src/hooks/useNotifications.tsx
@@ -1,7 +1,17 @@
import type { IMessage } from "@novu/headless";
import { HeadlessService } from "@novu/headless";
import { useCallback, useEffect, useRef, useState } from "react";
-import { useMount } from ".";
+import useMount from "./useMount";
+
+export function getSubscriberId({
+ companyId,
+ userId,
+}: {
+ companyId: string;
+ userId: string;
+}) {
+ return `${companyId}:${userId}`;
+}
export function useNotifications({
userId,
@@ -92,7 +102,7 @@ export function useNotifications({
};
useMount(() => {
- setSubscriberId(`${companyId}:${userId}`);
+ setSubscriberId(getSubscriberId({ companyId, userId }));
});
useEffect(() => {