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.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { requirePermissions } from "@carbon/auth/auth.server";
import {
  actionTaskPermissions,
  isActionTaskEntityType
} from "@carbon/ee/action-task-entity";
import {
  getLinearClient,
  getLinearIssueFromExternalId
} from "@carbon/ee/linear.server";
import { type JSONContent, TiptapText } from "@carbon/files/markdown";
import { getLogger } from "@carbon/logger";
import type { ActionFunction } from "react-router";
import { data } from "react-router";
import { requireChangeNoticeEditable } from "~/modules/items/items.service";
import { getActionTaskWithParent } from "~/modules/shared/shared.service";

const logger = getLogger("erp", "integrations-linear-issue-sync-notes");

const linear = getLinearClient();

export const action: ActionFunction = async ({ request }) => {
  if (request.method !== "POST") {
    return data({ success: false, message: "Method not allowed" }, 405);
  }

  const form = await request.formData();
  const actionId = form.get("actionId") as string;
  const entityType = form.get("entityType") as string | null;
  const notesStr = form.get("notes") as string;

  if (!actionId) {
    return data({ success: false, message: "Missing actionId" }, 400);
  }

  if (!isActionTaskEntityType(entityType)) {
    return data({ success: false, message: "Invalid entityType" }, 400);
  }

  const { companyId, client } = await requirePermissions(
    request,
    actionTaskPermissions(entityType)
  );

  if (entityType === "changeOrderActionTask") {
    const task = await getActionTaskWithParent(
      client,
      entityType,
      actionId,
      companyId
    );

    const locked = task.parentId
      ? await requireChangeNoticeEditable(client, {
          changeNoticeId: task.parentId,
          companyId,
          scope: "workflow"
        })
      : { error: { message: "Could not find change notice" } };

    if (locked) {
      return data({ success: false, message: locked.error.message }, 400);
    }
  }

  // Parse the notes JSON
  let notes: JSONContent | null = null;
  try {
    notes = notesStr ? JSON.parse(notesStr) : null;
  } catch {
    return data({ success: false, message: "Invalid notes format" }, 400);
  }

  // Get the linked Linear issue
  const issue = await getLinearIssueFromExternalId(
    client,
    companyId,
    actionId,
    entityType
  );

  if (!issue) {
    return { success: true, message: "No linked Linear issue" };
  }

  if (!notes) {
    return { success: true, message: "No notes to sync" };
  }

  try {
    // Convert Tiptap notes to markdown for Linear
    const description = TiptapText(notes).toMarkdown();

    await linear.updateIssue(companyId, {
      id: issue.id,
      description
    });

    return { success: true, message: "Notes synced to Linear" };
  } catch (error) {
    logger.error("Failed to sync notes to Linear", { error: error });
    return data(
      { success: false, message: "Failed to sync notes to Linear" },
      500
    );
  }
};