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.

1.7 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, error, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { requireFeature } from "@carbon/ee/plan.server";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { unpublishWorkflow } from "~/modules/workflows/workflows.service";
import { getDatabaseClient } from "~/services/database.server";
import { path } from "~/utils/path";

// The one route both the list row menu and the builder button post to, so there is
// exactly one place that has to remember to re-sync the trigger rows. It carries no
// body: clearing the published version is the whole operation.
export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId, userId } = await requirePermissions(request, {
    update: "workflows"
  });
  await requireFeature({
    request,
    client,
    companyId,
    feature: "WORKFLOWS",
    redirectTo: path.to.workflows
  });

  const { id } = params;
  if (!id) throw new Error("id is not found");

  const unpublished = await unpublishWorkflow(client, getDatabaseClient(), {
    workflowId: id,
    companyId,
    userId
  });

  if (!unpublished.ok) {
    return data(
      { success: false },
      await flash(
        request,
        error(null, unpublished.message ?? "Failed to unpublish")
      )
    );
  }

  return data(
    { success: true },
    await flash(request, success("Workflow unpublished"))
  );
}