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.0 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 { ActionFunctionArgs } from "react-router";
import {
  requireEditableChangeNoticeRoute,
  updateChangeNotice
} from "~/modules/items/items.service";

// The two rich-text header columns. They used to be written straight from the
// browser via the supabase client, which left no server-side place to enforce
// the engineering lock — hence this action.
const CONTENT_FIELDS = ["reasonForChange", "description"] as const;

type ContentField = (typeof CONTENT_FIELDS)[number];

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

  const { id } = params;
  if (!id) throw new Error("Could not find id");
  const locked = await requireEditableChangeNoticeRoute(request, {
    client,
    changeNoticeId: params.id,
    companyId,
    scope: "engineering"
  });
  if (locked) return locked;

  const formData = await request.formData();
  const field = formData.get("field");
  const content = formData.get("content");

  if (
    typeof field !== "string" ||
    !CONTENT_FIELDS.includes(field as ContentField) ||
    typeof content !== "string"
  ) {
    return { error: { message: "Invalid form data" }, data: null };
  }

  let parsed: unknown;
  try {
    parsed = JSON.parse(content);
  } catch {
    return { error: { message: "Invalid content" }, data: null };
  }

  const result = await updateChangeNotice(client, {
    id,
    companyId,
    [field]: parsed,
    updatedBy: userId
  });

  if (result.error) {
    return { error: { message: "Failed to update change notice" }, data: null };
  }

  return { error: null, data: result.data };
}