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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// 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 } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import { getDatabaseErrorMessage, redirect } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData, useParams } from "react-router";
import { customerPartValidator } from "~/modules/items/items.models";
import { getItem, upsertItemCustomerPart } from "~/modules/items/items.service";
import CustomerPartForm from "~/modules/items/ui/Item/CustomerPartForm";
import { path } from "~/utils/path";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client } = await requirePermissions(request, {
view: "parts",
role: "employee"
});
const { itemId } = params;
if (!itemId) throw new Error("Could not find itemId");
const itemData = await getItem(client, itemId);
const readableId = itemData?.data?.readableIdWithRevision;
return {
readableId
};
}
export async function action({ request, params }: ActionFunctionArgs) {
assertIsPost(request);
const { client, companyId } = await requirePermissions(request, {
create: "parts"
});
const { itemId } = params;
if (!itemId) throw new Error("Could not find itemId");
const formData = await request.formData();
const validation = await validator(customerPartValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
// biome-ignore lint/correctness/noUnusedVariables: suppressed due to migration
const { id, ...d } = validation.data;
const createCustomerPart = await upsertItemCustomerPart(client, {
...d,
companyId
});
if (createCustomerPart.error) {
const flashMessage =
// duplicate = the unique constraint on ("customerId", "itemId")
getDatabaseErrorMessage(
createCustomerPart.error,
"Failed to create customer part",
{ duplicate: "Customer Part record already defined for customer" }
);
throw redirect(
path.to.partSales(itemId),
await flash(request, error(createCustomerPart.error, flashMessage))
);
}
throw redirect(path.to.partSales(itemId));
}
export default function NewCustomerPartRoute() {
const { itemId } = useParams();
const { readableId } = useLoaderData<typeof loader>();
if (!itemId) throw new Error("itemId not found");
const initialValues = {
itemId: itemId,
customerId: "",
customerPartId: "",
customerPartRevision: "",
readableId: readableId ?? ""
};
return <CustomerPartForm initialValues={initialValues} />;
}