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
fix: don't allow active make methods to be overwritten
Brad Barbinc250d16dbab3
diff --git a/apps/erp/app/modules/items/ui/Item/MakeMethodTools.tsx b/apps/erp/app/modules/items/ui/Item/MakeMethodTools.tsxindex baec56d67c..aa9757cda6 100644--- a/apps/erp/app/modules/items/ui/Item/MakeMethodTools.tsx+++ b/apps/erp/app/modules/items/ui/Item/MakeMethodTools.tsx@@ -1,4 +1,5 @@-import { Number, Submit, ValidatedForm } from "@carbon/form";+import { useCarbon } from "@carbon/auth";+import { Number, SelectControlled, Submit, ValidatedForm } from "@carbon/form";import {Alert,AlertTitle,@@ -51,7 +52,7 @@ import {import { Link, useFetcher, useParams } from "react-router";import { Hidden, Item, useConfigurableItems } from "~/components/Form";import { Confirm } from "~/components/Modals";-import { usePermissions } from "~/hooks";+import { usePermissions, useUser } from "~/hooks";import type { MethodItemType } from "~/modules/shared";import { path } from "~/utils/path";import {@@ -116,6 +117,97 @@ const MakeMethodTools = ({setSelectedVersion(activeMethod);}, [itemId, activeMethod]);+ // State for Get and Save Method modals+ const { carbon } = useCarbon();+ const {+ company: { id: companyId }+ } = useUser();++ // State for Get Method modal - source versions+ const [sourceMakeMethods, setSourceMakeMethods] = useState<+ { label: JSX.Element; value: string }[]+ >([]);+ const [selectedSourceMethod, setSelectedSourceMethod] = useState<+ string | null+ >(null);++ // State for Save Method modal - target versions+ const [targetMakeMethods, setTargetMakeMethods] = useState<+ { label: JSX.Element; value: string }[]+ >([]);+ const [selectedTargetMethod, setSelectedTargetMethod] = useState<+ string | null+ >(null);++ const getSourceMakeMethods = async (sourceItemId: string) => {+ setSourceMakeMethods([]);+ setSelectedSourceMethod(null);+ if (!carbon) return;+ const { data, error } = await carbon+ .from("makeMethod")+ .select("id, version, status")+ .eq("itemId", sourceItemId)+ .eq("companyId", companyId)+ .order("version", { ascending: false });++ if (error) {+ toast.error(error.message);+ }++ // For source, we can select any version (Draft, Active, or Archived)+ setSourceMakeMethods(+ data?.map(({ id, version, status }) => ({+ label: (+ <div className="flex items-center gap-2">+ <Badge variant="outline">V{version}</Badge>{" "}+ <MakeMethodVersionStatus status={status} />+ </div>+ ),+ value: id+ })) ?? []+ );++ if (data?.length === 1) {+ setSelectedSourceMethod(data[0].id);+ }+ };++ const getTargetMakeMethods = async (targetItemId: string) => {+ setTargetMakeMethods([]);+ setSelectedTargetMethod(null);+ if (!carbon) return;+ const { data, error } = await carbon+ .from("makeMethod")+ .select("id, version, status")+ .eq("itemId", targetItemId)+ .eq("companyId", companyId)+ .order("version", { ascending: false });++ if (error) {+ toast.error(error.message);+ }++ // Only Draft versions can be overwritten - Active and Archived are read-only+ const availableVersions =+ data?.filter(({ status }) => status === "Draft") ?? [];++ setTargetMakeMethods(+ availableVersions.map(({ id, version, status }) => ({+ label: (+ <div className="flex items-center gap-2">+ <Badge variant="outline">V{version}</Badge>{" "}+ <MakeMethodVersionStatus status={status} />+ </div>+ ),+ value: id+ }))+ );++ if (availableVersions.length === 1) {+ setSelectedTargetMethod(availableVersions[0].id);+ }+ };+return (<Fragment key={itemId}><Menubar>@@ -124,7 +216,9 @@ const MakeMethodTools = ({<MenubarItemisLoading={isGetMethodLoading}isDisabled={- !permissions.can("update", "parts") || isGetMethodLoading+ !permissions.can("update", "parts") ||+ isGetMethodLoading ||+ activeMethod.status !== "Draft" // Can only overwrite Draft versions}leftIcon={<LuGitBranch />}onClick={getMethodModal.onOpen}@@ -255,6 +349,8 @@ const MakeMethodTools = ({onOpenChange={(open) => {if (!open) {getMethodModal.onClose();+ setSourceMakeMethods([]);+ setSelectedSourceMethod(null);}}}>@@ -269,25 +365,52 @@ const MakeMethodTools = ({<ModalHeader><ModalTitle>Get Method</ModalTitle><ModalDescription>- Overwrite the item method with the source method+ Overwrite the current version with the source method</ModalDescription></ModalHeader><ModalBody>- <Hidden name="targetId" value={itemId} />+ <Hidden name="targetId" value={activeMethodId} /><VStack spacing={4}><Alert variant="destructive" className="mt-4"><LuTriangleAlert className="h-4 w-4" /><AlertTitle>- This will overwrite the existing manufacturing method+ This will overwrite version {activeMethod.version} of this+ manufacturing method</AlertTitle></Alert><Item- name="sourceId"- label={t`Source Method`}+ name="itemId"+ label={t`Source Item`}type={type}- blacklist={configurableItemIds}+ blacklist={[itemId, ...configurableItemIds]}includeInactive={includeInactive}replenishmentSystem="Make"+ onChange={(value) => {+ if (value) {+ getSourceMakeMethods(value?.value);+ } else {+ setSourceMakeMethods([]);+ setSelectedSourceMethod(null);+ }+ }}+ />+ <SelectControlled+ name="sourceId"+ options={sourceMakeMethods}+ label={t`Source Version`}+ value={selectedSourceMethod ?? undefined}+ onChange={(value) => {+ if (value) {+ setSelectedSourceMethod(value?.value);+ } else {+ setSelectedSourceMethod(null);+ }+ }}+ placeholder={+ sourceMakeMethods.length === 0+ ? t`Select an item first`+ : undefined+ }/><div className="flex items-center space-x-2"><Checkbox@@ -312,7 +435,10 @@ const MakeMethodTools = ({<Button onClick={getMethodModal.onClose} variant="secondary">Cancel</Button>- <Submit isDisabled={!hasMethodParts} variant="destructive">+ <Submit+ isDisabled={!hasMethodParts || !selectedSourceMethod}+ variant="destructive"+ >Confirm</Submit></ModalFooter>@@ -327,6 +453,8 @@ const MakeMethodTools = ({onOpenChange={(open) => {if (!open) {saveMethodModal.onClose();+ setTargetMakeMethods([]);+ setSelectedTargetMethod(null);}}}>@@ -341,19 +469,45 @@ const MakeMethodTools = ({<ModalHeader><ModalTitle>Save Method</ModalTitle><ModalDescription>- Overwrite the target manufacturing method with the item method+ Save version {activeMethod.version} to another item's method</ModalDescription></ModalHeader><ModalBody>- <Hidden name="sourceId" value={itemId} />+ <Hidden name="sourceId" value={activeMethodId} /><VStack spacing={4}><Item- name="targetId"- label={t`Target Method`}+ name="itemId"+ label={t`Target Item`}type={type}includeInactive={includeInactive}blacklist={[itemId, ...configurableItemIds]}replenishmentSystem="Make"+ onChange={(value) => {+ if (value) {+ getTargetMakeMethods(value?.value);+ } else {+ setTargetMakeMethods([]);+ setSelectedTargetMethod(null);+ }+ }}+ />+ <SelectControlled+ name="targetId"+ options={targetMakeMethods}+ label={t`Target Version`}+ value={selectedTargetMethod ?? undefined}+ onChange={(value) => {+ if (value) {+ setSelectedTargetMethod(value?.value);+ } else {+ setSelectedTargetMethod(null);+ }+ }}+ placeholder={+ targetMakeMethods.length === 0+ ? t`No draft versions available`+ : undefined+ }/><div className="flex items-center space-x-2"><Checkbox@@ -377,7 +531,7 @@ const MakeMethodTools = ({<Button onClick={saveMethodModal.onClose} variant="secondary">Cancel</Button>- <Submit isDisabled={!hasMethodParts}>+ <Submit isDisabled={!hasMethodParts || !selectedTargetMethod}><Trans>Confirm</Trans></Submit></ModalFooter>diff --git a/apps/erp/app/modules/production/ui/Jobs/JobMakeMethodTools.tsx b/apps/erp/app/modules/production/ui/Jobs/JobMakeMethodTools.tsxindex 2ed95d0a79..779757789a 100644--- a/apps/erp/app/modules/production/ui/Jobs/JobMakeMethodTools.tsx+++ b/apps/erp/app/modules/production/ui/Jobs/JobMakeMethodTools.tsx@@ -230,8 +230,12 @@ const JobMakeMethodTools = ({ makeMethod }: { makeMethod?: JobMakeMethod }) => {toast.error(error.message);}+ // Only Draft versions can be overwritten - Active and Archived are read-only+ const availableVersions =+ data?.filter(({ status }) => status === "Draft") ?? [];+setMakeMethods(- data?.map(({ id, version, status }) => ({+ availableVersions.map(({ id, version, status }) => ({label: (<div className="flex items-center gap-2"><Badge variant="outline">V{version}</Badge>{" "}@@ -239,11 +243,11 @@ const JobMakeMethodTools = ({ makeMethod }: { makeMethod?: JobMakeMethod }) => {</div>),value: id- })) ?? []+ })));- if (data?.length === 1) {- setSelectedMakeMethod(data[0].id);+ if (availableVersions.length === 1) {+ setSelectedMakeMethod(availableVersions[0].id);}};@@ -518,6 +522,11 @@ const JobMakeMethodTools = ({ makeMethod }: { makeMethod?: JobMakeMethod }) => {setSelectedMakeMethod(null);}}}+ placeholder={+ makeMethods.length === 0+ ? t`No draft versions available`+ : undefined+ }/><div className="flex items-center space-x-2"><Checkboxdiff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteMakeMethodTools.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteMakeMethodTools.tsxindex d93cdf2e6d..d2ccbf6dc9 100644--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteMakeMethodTools.tsx+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteMakeMethodTools.tsx@@ -202,8 +202,12 @@ const QuoteMakeMethodTools = () => {toast.error(error.message);}+ // Only Draft versions can be overwritten - Active and Archived are read-only+ const availableVersions =+ data?.filter(({ status }) => status === "Draft") ?? [];+setMakeMethods(- data?.map(({ id, version, status }) => ({+ availableVersions.map(({ id, version, status }) => ({label: (<div className="flex items-center gap-2"><Badge variant="outline">V{version}</Badge>{" "}@@ -211,11 +215,11 @@ const QuoteMakeMethodTools = () => {</div>),value: id- })) ?? []+ })));- if (data?.length === 1) {- setSelectedMakeMethod(data[0].id);+ if (availableVersions.length === 1) {+ setSelectedMakeMethod(availableVersions[0].id);}};@@ -503,6 +507,11 @@ const QuoteMakeMethodTools = () => {setSelectedMakeMethod(null);}}}+ placeholder={+ makeMethods.length === 0+ ? t`No draft versions available`+ : undefined+ }/><div className="flex items-center space-x-2"><Checkboxdiff --git a/apps/erp/app/routes/x+/items+/methods+/get.tsx b/apps/erp/app/routes/x+/items+/methods+/get.tsxindex 18cec2e73f..c62e4678a2 100644--- a/apps/erp/app/routes/x+/items+/methods+/get.tsx+++ b/apps/erp/app/routes/x+/items+/methods+/get.tsx@@ -3,7 +3,7 @@ import { getCarbonServiceRole } from "@carbon/auth/client.server";import { validationError, validator } from "@carbon/form";import type { ActionFunctionArgs } from "react-router";import { redirect } from "react-router";-import { copyItem, getMethodValidator } from "~/modules/items";+import { copyItem, copyMakeMethod, getMethodValidator } from "~/modules/items";import { path, requestReferrer } from "~/utils/path";export async function action({ request }: ActionFunctionArgs) {@@ -20,11 +20,23 @@ export async function action({ request }: ActionFunctionArgs) {return validationError(validation.error);}- const upsert = await copyItem(serviceRole, {- ...validation.data,- companyId,- userId- });+ // Check if we're dealing with makeMethod IDs (format: make_xxxxx)+ // MakeMethodTools.tsx now sends makeMethod IDs directly+ const isMakeMethodId = (id: string) => id.startsWith("make_");++ const upsert =+ isMakeMethodId(validation.data.sourceId) ||+ isMakeMethodId(validation.data.targetId)+ ? await copyMakeMethod(serviceRole, {+ ...validation.data,+ companyId,+ userId+ })+ : await copyItem(serviceRole, {+ ...validation.data,+ companyId,+ userId+ });if (upsert.error) {return {diff --git a/apps/erp/app/routes/x+/items+/methods+/save.tsx b/apps/erp/app/routes/x+/items+/methods+/save.tsxindex e67fb293ed..3bb8d8dfe0 100644--- a/apps/erp/app/routes/x+/items+/methods+/save.tsx+++ b/apps/erp/app/routes/x+/items+/methods+/save.tsx@@ -3,7 +3,7 @@ import { getCarbonServiceRole } from "@carbon/auth/client.server";import { validationError, validator } from "@carbon/form";import type { ActionFunctionArgs } from "react-router";import { redirect } from "react-router";-import { copyItem, getMethodValidator } from "~/modules/items";+import { copyItem, copyMakeMethod, getMethodValidator } from "~/modules/items";import { path, requestReferrer } from "~/utils/path";export async function action({ request }: ActionFunctionArgs) {@@ -20,11 +20,23 @@ export async function action({ request }: ActionFunctionArgs) {return validationError(validation.error);}- const upsert = await copyItem(serviceRole, {- ...validation.data,- companyId,- userId- });+ // Check if we're dealing with makeMethod IDs (format: make_xxxxx)+ // MakeMethodTools.tsx now sends makeMethod IDs directly+ const isMakeMethodId = (id: string) => id.startsWith("make_");++ const upsert =+ isMakeMethodId(validation.data.sourceId) ||+ isMakeMethodId(validation.data.targetId)+ ? await copyMakeMethod(serviceRole, {+ ...validation.data,+ companyId,+ userId+ })+ : await copyItem(serviceRole, {+ ...validation.data,+ companyId,+ userId+ });if (upsert.error) {return {