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.

3.4 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 { getUser } from "@carbon/auth";
import type { Database } from "@carbon/database";
import { TiptapText } from "@carbon/files/markdown";
import { getLogger } from "@carbon/logger";
import type { SupabaseClient } from "@supabase/supabase-js";
import {
  getLinearClient,
  getLinearIssueFromExternalId,
  mapCarbonStatusToLinearStatus
} from "../../linear/lib/index.server";
import type { NotificationEvent, NotificationService } from "../types";

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

/**
 * Linear Notification Service
 * Updates Linear issues based on Carbon notification events
 */
export class LinearNotificationService implements NotificationService {
  id = "linear";
  name = "Linear";

  async send(
    event: NotificationEvent,
    context: { serviceRole: SupabaseClient<Database> }
  ): Promise<void> {
    switch (event.type) {
      case "task.status.changed": {
        if (
          !event.data.type ||
          !["action", "investigation"].includes(event.data.type)
        )
          return;

        const issue = await getLinearIssueFromExternalId(
          context.serviceRole,
          event.companyId,
          event.data.id,
          "nonConformanceActionTask"
        );

        if (!issue) return;

        const state = await linear.getWorkflowState(
          event.companyId,
          mapCarbonStatusToLinearStatus(event.data.status!)
        );

        if (!state) return;

        await linear.updateIssue(event.companyId, {
          id: issue.id,
          stateId: state.id
        });

        break;
      }

      case "task.assigned": {
        if (event.data.table !== "nonConformanceActionTask") return;

        const issue = await getLinearIssueFromExternalId(
          context.serviceRole,
          event.companyId,
          event.data.id,
          "nonConformanceActionTask"
        );

        if (!issue) return; // No linked Linear issue

        const { data: user } = await getUser(
          context.serviceRole,
          event.data.assignee
        );

        if (!user) return; // No assignee user

        const [linearUser] = await linear.getUsers(event.companyId, {
          email: user.email
        });

        if (!linearUser) return;

        await linear.updateIssue(event.companyId, {
          id: issue.id,
          assigneeId: linearUser.id
        });
        break;
      }

      case "task.notes.changed": {
        if (event.data.table !== "nonConformanceActionTask") return;

        const issue = await getLinearIssueFromExternalId(
          context.serviceRole,
          event.companyId,
          event.data.id,
          "nonConformanceActionTask"
        );

        if (!issue) return; // No linked Linear issue

        // Convert Tiptap notes to markdown for Linear
        const notes = event.data.notes;
        if (!notes) return;

        try {
          const description = TiptapText(notes).toMarkdown();

          await linear.updateIssue(event.companyId, {
            id: issue.id,
            description
          });
        } catch (e) {
          logger.error("Failed to sync notes to Linear", { error: e });
        }
        break;
      }
    }
    return;
  }
}