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 { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import type { JSONContent } from "@carbon/react";
import { Menubar, VStack } from "@carbon/react";
import { redirect } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import { Suspense } from "react";
import type { LoaderFunctionArgs } from "react-router";
import { Await, useLoaderData, useParams } from "react-router";
import { CadModel } from "~/components";
import { useRouteData } from "~/hooks";
import { usePermissions } from "~/hooks/usePermissions";
import {
getMakeMethodById,
getMakeMethods,
getMethodMaterialsByMakeMethod,
getMethodOperationsByMakeMethodId,
getRevisionLock
} from "~/modules/items/items.service";
import type { ToolSummary } from "~/modules/items/types";
import {
BillOfMaterial,
BillOfProcess,
MakeMethodTools
} from "~/modules/items/ui/Item";
import { getModelByItemId, getTagsList } from "~/modules/shared/shared.service";
import type { MethodItemType, MethodType } from "~/modules/shared/types";
import { path } from "~/utils/path";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts"
});
const { itemId, makeMethodId } = params;
if (!itemId) throw new Error("Could not find itemId");
if (!makeMethodId) throw new Error("Could not find makeMethodId");
const [makeMethod, methodMaterials, methodOperations, tags, revisionLock] =
await Promise.all([
getMakeMethodById(client, makeMethodId, companyId),
getMethodMaterialsByMakeMethod(client, makeMethodId),
getMethodOperationsByMakeMethodId(client, makeMethodId),
getTagsList(client, companyId, "operation"),
getRevisionLock(client, { itemId, companyId })
]);
if (makeMethod.error) {
throw redirect(
path.to.toolDetails(itemId),
await flash(
request,
error(makeMethod.error, "Failed to load make method")
)
);
}
if (methodOperations.error) {
throw redirect(
path.to.toolDetails(itemId),
await flash(
request,
error(methodOperations.error, "Failed to load method operations")
)
);
}
if (methodMaterials.error) {
throw redirect(
path.to.toolDetails(itemId),
await flash(
request,
error(methodMaterials.error, "Failed to load method materials")
)
);
}
return {
makeMethod: makeMethod.data,
methodMaterials:
methodMaterials.data?.map((m) => ({
...m,
description: m.item?.name ?? "",
methodOperationId: m.methodOperationId ?? undefined,
methodType: m.methodType as MethodType,
itemType: m.itemType as MethodItemType
})) ?? [],
methodOperations:
methodOperations.data?.map((operation) => ({
...operation,
description: operation.description ?? "",
procedureId: operation.procedureId ?? undefined,
assemblyInstructionId: operation.assemblyInstructionId ?? undefined,
inspectionDocumentId: operation.inspectionDocumentId ?? undefined,
operationSupplierProcessId:
operation.operationSupplierProcessId ?? undefined,
operationMinimumCost: operation.operationMinimumCost ?? 0,
operationLeadTime: operation.operationLeadTime ?? 0,
operationUnitCost: operation.operationUnitCost ?? 0,
tags: operation.tags ?? [],
workCenterId: operation.workCenterId ?? undefined,
workInstruction: operation.workInstruction as JSONContent | null
})) ?? [],
model: getModelByItemId(client, makeMethod.data.itemId),
makeMethods: getMakeMethods(client, makeMethod.data.itemId, companyId),
tags: tags.data ?? [],
revisionStatus: revisionLock.revisionStatus,
releaseControl: revisionLock.releaseControl
};
}
export default function ToolMakeMethodPage() {
const { t } = useLingui();
const loaderData = useLoaderData<typeof loader>();
const permissions = usePermissions();
const {
makeMethod,
makeMethods,
methodMaterials,
methodOperations,
tags,
revisionStatus,
releaseControl
} = loaderData;
const { itemId, makeMethodId } = useParams();
if (!itemId) throw new Error("Could not find itemId");
if (!makeMethodId) throw new Error("Could not find makeMethodId");
const toolData = useRouteData<{
toolSummary: ToolSummary;
}>(path.to.tool(itemId));
return (
<VStack spacing={4} className="p-4">
<Suspense fallback={<Menubar />}>
<Await resolve={makeMethods}>
{(makeMethods) => (
<MakeMethodTools
itemId={makeMethod.itemId}
makeMethods={makeMethods.data ?? []}
type="Tool"
currentMethodId={makeMethod.id}
/>
)}
</Await>
</Suspense>
<BillOfProcess
key={`bop:${makeMethodId}`}
makeMethod={makeMethod}
materials={methodMaterials}
// @ts-expect-error
operations={methodOperations}
tags={tags}
revisionStatus={revisionStatus}
releaseControl={releaseControl}
/>
<BillOfMaterial
key={`bom:${makeMethodId}`}
makeMethod={makeMethod}
// @ts-expect-error TS2322 - TODO: fix type
materials={methodMaterials}
operations={methodOperations}
replenishmentSystem={toolData?.toolSummary?.replenishmentSystem}
revisionStatus={revisionStatus}
releaseControl={releaseControl}
/>
<Suspense fallback={null}>
<Await resolve={loaderData.model}>
{(model) => (
<CadModel
key={`cad:${model.itemId}`}
isReadOnly={!permissions.can("update", "parts")}
metadata={{
itemId: model?.itemId ?? undefined
}}
modelUpload={model ?? null}
title={t`CAD Model`}
uploadClassName="aspect-square min-h-[420px] max-h-[70vh]"
viewerClassName="aspect-square min-h-[420px] max-h-[70vh]"
/>
)}
</Await>
</Suspense>
</VStack>
);
}