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.2 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 { validationError, validator } from "@carbon/form";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { workflowValidator } from "~/modules/workflows/workflows.models";
import { updateWorkflow } from "~/modules/workflows/workflows.service";
import { path } from "~/utils/path";

// Name and description only. This route never accepts an ownerId — a workflow
// runs with its owner's permissions, so ownership is taken, never assigned.
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 formData = await request.formData();
  // The builder header renames in place — it must stay on the canvas, so it opts
  // out of the list redirect with `type=inline`.
  const inline = formData.get("type") === "inline";
  const validation = await validator(workflowValidator).validate(formData);

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

  const update = await updateWorkflow(client, {
    id,
    companyId,
    name: validation.data.name,
    description: validation.data.description,
    updatedBy: userId
  });

  if (update.error) {
    return data(
      { success: false },
      await flash(request, error(update.error, "Failed to update workflow"))
    );
  }

  if (inline) return data({ success: true });

  throw redirect(
    path.to.workflows,
    await flash(request, success("Updated workflow"))
  );
}