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.8 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 type { Json } from "@carbon/database";
import { requireFeature } from "@carbon/ee/plan.server";
import { validationError, validator } from "@carbon/form";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { workflowCanvasStateValidator } from "~/modules/workflows/workflows.models";
import { updateWorkflowCanvasState } from "~/modules/workflows/workflows.service";
import { path } from "~/utils/path";

// Canvas state is per workflow, not per version, so the live-version lock does
// not apply here — you may pan and zoom a published version.
export async function action({ request, params }: ActionFunctionArgs) {
  assertIsPost(request);
  const { client, companyId } = 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 validation = await validator(workflowCanvasStateValidator).validate(
    await request.formData()
  );

  if (validation.error) {
    return validationError(validation.error);
  }

  const update = await updateWorkflowCanvasState(client, {
    id,
    companyId,
    canvasState: validation.data as unknown as Json
  });

  if (update.error) {
    return data({ ok: false, error: update.error.message }, { status: 500 });
  }

  return data({ ok: true });
}