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.

5.0 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import { getLogger } from "@carbon/logger";
import { notificationRegistry } from "./registry";
import type {
  CompanyIntegration,
  NotificationContext,
  NotificationEvent
} from "./types";

const logger = getLogger("ee", "notifications");

export class NotificationPipeline {
  constructor(private context: NotificationContext) {}

  async send(
    event: NotificationEvent,
    integrations: CompanyIntegration[]
  ): Promise<void> {
    try {
      const activeNotificationServices = integrations.filter(
        (integration) =>
          integration.active && this.isNotificationService(integration.id)
      );

      const sendPromises = activeNotificationServices.map(
        async (integration) => {
          const service = notificationRegistry.getService(integration.id);
          if (!service) {
            logger.warn("Notification service not found in registry", {
              integrationId: integration.id
            });
            return;
          }

          try {
            await service.send(event, {
              serviceRole: this.context.serviceRole || this.context.client
            });
          } catch (error) {
            logger.error("Failed to send notification via integration", {
              integrationId: integration.id,
              error
            });
          }
        }
      );

      await Promise.all(sendPromises);
    } catch (error) {
      logger.error("Failed to send notification", { error });
    }
  }

  private isNotificationService(integrationId: string): boolean {
    const notificationServiceIds = ["slack", "linear"];
    return notificationServiceIds.includes(integrationId);
  }
}

export function createNotificationPipeline(
  context: NotificationContext
): NotificationPipeline {
  return new NotificationPipeline(context);
}

export async function notifyIssueCreated(
  context: NotificationContext,
  integrations: CompanyIntegration[],
  data: {
    companyId: string;
    userId: string;
    carbonUrl: string;
    issue: {
      id: string;
      nonConformanceId: string;
      title: string;
      description: string;
      severity: string;
    };
  }
): Promise<void> {
  const pipeline = createNotificationPipeline(context);
  await pipeline.send(
    {
      type: "issue.created",
      companyId: data.companyId,
      userId: data.userId,
      carbonUrl: data.carbonUrl,
      data: data.issue
    },
    integrations
  );
}

export async function notifyIssueStatusChanged(
  context: NotificationContext,
  integrations: CompanyIntegration[],
  data: {
    companyId: string;
    userId: string;
    carbonUrl: string;
    issue: {
      id: string;
      status: string;
      nonConformanceId: string;
      title: string;
    };
  }
): Promise<void> {
  const pipeline = createNotificationPipeline(context);
  await pipeline.send(
    {
      type: "issue.status.changed",
      companyId: data.companyId,
      userId: data.userId,
      carbonUrl: data.carbonUrl,
      data: data.issue
    },
    integrations
  );
}

export async function notifyTaskStatusChanged(
  context: NotificationContext,
  integrations: CompanyIntegration[],
  data: {
    companyId: string;
    userId: string;
    carbonUrl: string;
    task: {
      id: string;
      status: string;
      issueId: string;
      title: string;
      type: "investigation" | "action" | "approval" | "review";
    };
  }
): Promise<void> {
  const pipeline = createNotificationPipeline(context);
  await pipeline.send(
    {
      type: "task.status.changed",
      companyId: data.companyId,
      userId: data.userId,
      carbonUrl: data.carbonUrl,
      data: data.task
    },
    integrations
  );
}

export async function notifyTaskAssigned(
  context: NotificationContext,
  integrations: CompanyIntegration[],
  data: {
    companyId: string;
    userId: string;
    carbonUrl: string;
    task: {
      id: string;
      table: string;
      assignee: string;
      title?: string;
    };
  }
): Promise<void> {
  const pipeline = createNotificationPipeline(context);
  await pipeline.send(
    {
      type: "task.assigned",
      companyId: data.companyId,
      userId: data.userId,
      carbonUrl: data.carbonUrl,
      data: data.task
    },
    integrations
  );
}

export async function notifyTaskNotesChanged(
  context: NotificationContext,
  integrations: CompanyIntegration[],
  data: {
    companyId: string;
    userId: string;
    carbonUrl: string;
    task: {
      id: string;
      table: string;
      notes: any; // Tiptap JSONContent
      title?: string;
    };
  }
): Promise<void> {
  const pipeline = createNotificationPipeline(context);
  await pipeline.send(
    {
      type: "task.notes.changed",
      companyId: data.companyId,
      userId: data.userId,
      carbonUrl: data.carbonUrl,
      data: data.task
    },
    integrations
  );
}