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(items): updateItem refuses a readable id or type change
Raul0d4c9ca114cf
diff --git a/apps/erp/app/modules/items/AGENTS.md b/apps/erp/app/modules/items/AGENTS.mdindex a49cafc15b..aa0271af0c 100644--- a/apps/erp/app/modules/items/AGENTS.md+++ b/apps/erp/app/modules/items/AGENTS.md@@ -84,7 +84,7 @@ pnpm --filter erp testEach is a `defineServiceFn` const in `items.server.ts`. The route calls it as `api.items.<name>(input)`; the API and MCP publish the ones tagged `@mcp` as `items_<name>`. It holds every check the route made. Routes validate the request, call one, and answer with `respond(request, result).redirect(…)` (a flash) or the result. Permission is `parts` throughout; where a function's permission differs from its old route's, it holds both the route's gate and the policy its write runs under (`updateMethodMaterial` / `updateMethodOperation` need create and update, `removeChangeNoticeAffectedItem` delete).-- Items — the bulk actions of the items tables and the properties panels (`x+/items+/update.tsx`, one field per post): `setItemTrackingType` (the item, then `cascadeItemTrackingType`), `setItemFields` (name, description, MPN, unit of measure), `setItemSourcing` (exactly one of replenishment system, default method type, sourcing type; `deriveItemMethodUpdate` then `updateItemMethodAndSourcing`), `setItemActive` (activating refuses an item an unreleased change notice created), `setItemPostingGroup` (on the item's cost row, created when missing), `setItemReadableId` (every revision and the type row).+- Items — the bulk actions of the items tables and the properties panels (`x+/items+/update.tsx`, one field per post): `setItemTrackingType` (the item, then `cascadeItemTrackingType`), `setItemFields` (name, description, MPN, unit of measure), `setItemSourcing` (exactly one of replenishment system, default method type, sourcing type; `deriveItemMethodUpdate` then `updateItemMethodAndSourcing`), `setItemActive` (activating refuses an item an unreleased change notice created), `setItemPostingGroup` (on the item's cost row, created when missing), `setItemReadableId` (every revision and the type row). `updateItem` writes the item row only: it refuses a `readableId` that differs from the item's and a `type` the item is not, since the type row is keyed by the readable id.- `createPart`, `updatePart`, `updateTool`, `setMaterialProperties` (`updateMaterialProperties` per material), `renameService`.- `updateItemManufacturing` and `updateItemPlanning` (the four item types' planning tabs) save, then re-plan the item (`replanAfterItemChange`, `production.server.ts`); planning refuses a responsible employee who is not an active employee.- `setItemSupersession` (the part's own supersession), `addItemPredecessor` (written on the predecessor). Both return a loop as `rule`, which the planning route shows as a field error.diff --git a/apps/erp/app/modules/items/items.server.ts b/apps/erp/app/modules/items/items.server.tsindex 8c322af40c..351e4e4ab0 100644--- a/apps/erp/app/modules/items/items.server.ts+++ b/apps/erp/app/modules/items/items.server.ts@@ -4226,6 +4226,11 @@ export const resolveItemIdFromExtractedText = defineServiceFn({/*** Update item*+ * @remarks+ * Writes the item's own fields. Refused when the item is not of `type`, or+ * when `readableId` differs from the item's: change the number with+ * `setItemReadableId`, which also renames the type row keyed by it.+ ** @mcp*/export const updateItem = defineServiceFn({diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.tsindex 89c0bf0849..1bb964f69b 100644--- a/apps/erp/app/modules/items/items.service.ts+++ b/apps/erp/app/modules/items/items.service.ts@@ -3899,6 +3899,12 @@ export async function upsertPart(return updated;}+/**+ * Writes the item row's own fields. Never the readable id or the type: the+ * type row (`part`, `tool`, …) is keyed by the readable id, so changing either+ * here would orphan it. Refused when the item is not of `type`, or when+ * `readableId` differs from the item's (`setItemReadableId` changes it).+ */export async function updateItem(client: SupabaseClient<Database>,item: z.infer<typeof itemValidator> & {@@ -3917,13 +3923,38 @@ export async function updateItem(shelfLifeTriggerProcessId: _shelfLifeTriggerProcessId,shelfLifeTriggerTiming: _shelfLifeTriggerTiming,shelfLifeCalculateFromBom: _shelfLifeCalculateFromBom,+ readableId,+ type,...row} = item;++ const current = await client+ .from("item")+ .select("readableId")+ .eq("id", item.id)+ .eq("companyId", item.companyId)+ .eq("type", type)+ .maybeSingle();+ if (current.error) return current;+ if (!current.data) {+ return { data: null, error: ruleError(`${type} ${item.id} not found`) };+ }+ if (readableId && readableId !== current.data.readableId) {+ return {+ data: null,+ error: ruleError(+ `updateItem does not change the readable id ${current.data.readableId}; use setItemReadableId`+ )+ };+ }+return client.from("item").update(sanitize(row)).eq("id", item.id)- .eq("companyId", item.companyId);+ .eq("companyId", item.companyId)+ .select("id")+ .single();}export async function upsertItemCost(diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json b/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.jsonindex 07c76ee7d3..457594d2ce 100644--- a/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json@@ -559,7 +559,7 @@{"name":"items_updateConfigurationParameterOrder","classification":"WRITE","schema":"b4f1f31e0f60","response":"bcde375ebd4c","permission":"parts:update","paginates":false},{"name":"items_updateConsumable","classification":"WRITE","schema":"e0ed6a5c0e19","response":"none","permission":"parts:update","paginates":false},{"name":"items_updateDefaultRevision","classification":"WRITE","schema":"7f8214d1083f","response":"d02966b20664","permission":"parts:update","paginates":false},- {"name":"items_updateItem","classification":"WRITE","schema":"ce8e8f42c401","response":"bcde375ebd4c","permission":"parts:update","paginates":false},+ {"name":"items_updateItem","classification":"WRITE","schema":"ce8e8f42c401","response":"3797626bd17e","permission":"parts:update","paginates":false},{"name":"items_updateItemCost","classification":"WRITE","schema":"77edb4dfa70e","response":"bcde375ebd4c","permission":"parts:update","paginates":false},{"name":"items_updateItemManufacturing","classification":"WRITE","schema":"8f541b0237ee","response":"623ca47a6c74","permission":"parts:update","paginates":false},{"name":"items_updateItemPlanning","classification":"WRITE","schema":"45e5182283f4","response":"ade4704d7338","permission":"parts:update","paginates":false},diff --git a/apps/erp/test/items-operations.test.ts b/apps/erp/test/items-operations.test.tsindex efe566acf7..efe3008ea3 100644--- a/apps/erp/test/items-operations.test.ts+++ b/apps/erp/test/items-operations.test.ts@@ -34,6 +34,7 @@ import {setItemActive,setItemPostingGroup,setItemSourcing,+ updateItem,updateItemPlanning} from "../app/modules/items/items.server";import {@@ -320,3 +321,60 @@ describe("updateItemPlanning", () => {expect(upsertItemPlanning).not.toHaveBeenCalled();});});++describe("updateItem", () => {+ const part = {+ id: "i1",+ name: "Bracket",+ replenishmentSystem: "Buy",+ defaultMethodType: "Purchase to Order",+ itemTrackingType: "Inventory",+ unitOfMeasureCode: "EA",+ type: "Part"+ } as const;++ it("refuses a readable id that differs from the item's, and writes nothing", async () => {+ const { client, writes } = fakeClient({ item: { readableId: "P-100" } });++ const result = await updateItem(contextFor(client), {+ ...part,+ readableId: "P-200"+ });++ expect(result.error).toMatchObject({ category: "rule" });+ expect(result.error?.message).toContain("setItemReadableId");+ expect(writes).toHaveLength(0);+ });++ it("refuses an item that is not of the type sent, and writes nothing", async () => {+ const { client, writes } = fakeClient({ item: null });++ const result = await updateItem(contextFor(client), {+ ...part,+ type: "Tool"+ });++ expect(result.error).toMatchObject({ category: "rule" });+ expect(writes).toHaveLength(0);+ });++ it("writes neither the readable id nor the type", async () => {+ const { client, writes } = fakeClient({ item: { readableId: "P-100" } });++ const result = await updateItem(contextFor(client), {+ ...part,+ readableId: "P-100",+ name: "Bracket, painted"+ });++ expect(result.error).toBeNull();+ expect(writes).toHaveLength(1);+ expect(writes[0]).toMatchObject({+ table: "item",+ op: "update",+ values: { name: "Bracket, painted" }+ });+ expect(writes[0].values).not.toHaveProperty("readableId");+ expect(writes[0].values).not.toHaveProperty("type");+ });+});