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.

2.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 { assertIsPost } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { getMountIntegration } from "@carbon/ee/mount.server";
import { trigger } from "@carbon/jobs";
import { getLogger } from "@carbon/logger";
import { nanoid } from "nanoid";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { z } from "zod";

const logger = getLogger("erp", "integrations-mount-publish");

export const config = {
  runtime: "nodejs"
};

const PublishRequestSchema = z.object({
  entityType: z.enum(["customer", "supplier", "item"])
});

export async function action({ request }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId, userId } = await requirePermissions(request, {
    update: "settings"
  });

  const parsed = PublishRequestSchema.safeParse({
    entityType: new URL(request.url).searchParams.get("entityType")
  });

  if (!parsed.success) {
    return data({ error: "Invalid entity type" }, { status: 400 });
  }

  const integration = await getMountIntegration(client, companyId);

  if (integration.error || !integration.data?.[0]?.active) {
    return data(
      { error: "Mount integration not found or inactive" },
      { status: 400 }
    );
  }

  // Returned to the page and written into the run's record, so the page can
  // tell when the run it started has begun and ended.
  const requestId = nanoid();

  try {
    await trigger("mount-publish", {
      companyId,
      entityTypes: [parsed.data.entityType],
      userId,
      requestId,
      trigger: "manual"
    });

    return data({ success: true, message: "Mount publish started", requestId });
  } catch (error) {
    logger.error("Failed to start Mount publish", {
      companyId,
      entityType: parsed.data.entityType,
      error
    });
    return data({ error: "Failed to start Mount publish" }, { status: 500 });
  }
}