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(api): an id decides create vs update only when a create cannot carry one (#1808)
Side9409eae0d13
diff --git a/apps/erp/app/modules/settings/settings.service.ts b/apps/erp/app/modules/settings/settings.service.tsindex c77ff1256f..b0825958b3 100644--- a/apps/erp/app/modules/settings/settings.service.ts+++ b/apps/erp/app/modules/settings/settings.service.ts@@ -684,7 +684,10 @@ export async function getDocumentSectionsByIds(.in("id", ids);}-/** @mcp upsert */+/**+ * @mcp upsert+ * @mcp key documentSection id+ */export async function upsertDocumentSection(client: SupabaseClient<Database>,documentSection: {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 a0c0bf13b6..fd532c2da6 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@@ -1422,7 +1422,7 @@{"name":"settings_updateSuggestionNotificationSetting","classification":"WRITE","paramCount":1,"schema":"ac461e36d464","response":"bcde375ebd4c","injectAuth":"companyId+updatedBy","permission":"settings:update","paginates":false},{"name":"settings_updateSupplierQuoteNotificationSetting","classification":"WRITE","paramCount":1,"schema":"56a6d061d358","response":"bcde375ebd4c","injectAuth":"companyId","permission":"settings:update","paginates":false},{"name":"settings_updateTimeCardSetting","classification":"WRITE","paramCount":1,"schema":"9389085b800b","response":"bcde375ebd4c","injectAuth":"companyId","permission":"settings:update","paginates":false},- {"name":"settings_upsertDocumentSection","classification":"WRITE","paramCount":5,"schema":"eb83baf2cc33","response":"8394fbabd845","injectAuth":"companyId+createdBy+updatedBy","permission":"settings:create+update","paginates":false,"upsert":"id"},+ {"name":"settings_upsertDocumentSection","classification":"WRITE","paramCount":5,"schema":"d837b8b65103","response":"8394fbabd845","injectAuth":"companyId+createdBy+updatedBy","permission":"settings:create+update","paginates":false,"upsert":"documentSection(id)"},{"name":"settings_upsertDocumentTemplate","classification":"WRITE","paramCount":6,"schema":"5d28da0e0ad6","response":"bcde375ebd4c","injectAuth":"companyId+createdBy+updatedBy","permission":"settings:create+update","paginates":false},{"name":"settings_upsertItemSerialSequence","classification":"WRITE","paramCount":7,"schema":"ddbb6ec42577","response":"3797626bd17e","injectAuth":"companyId+createdBy+updatedBy","permission":"settings:create+update","paginates":false,"upsert":"id"},{"name":"shared_deleteSavedView","classification":"DESTRUCTIVE","paramCount":1,"schema":"1fbe3d41dab0","response":"bcde375ebd4c","injectAuth":"companyId","permission":"none","paginates":false},diff --git a/apps/erp/test/mcp-service-ast.test.ts b/apps/erp/test/mcp-service-ast.test.tsindex 4bb48b12c2..23e6159c97 100644--- a/apps/erp/test/mcp-service-ast.test.ts+++ b/apps/erp/test/mcp-service-ast.test.ts@@ -262,6 +262,15 @@ describe("the upsert rule", () => {row: (Shape & { createdBy: string }) | (Shape & { id: string; updatedBy: string })) {}+ /** @mcp upsert */+ export async function idOptionalOnCreate(+ client: any,+ row: { id?: string; name: string } & (+ | { createdBy: string }+ | { updatedBy: string }+ )+ ) {}+/** @mcp upsert */export async function idBothWays(client: any,@@ -312,6 +321,15 @@ describe("the upsert rule", () => {});});+ // A create that MAY carry an id makes "an id was sent" ambiguous: it could be+ // a new record under a chosen id. Only a create shape with no id field at all+ // lets the id decide.+ it("does not let id decide when a create may carry one too", () => {+ expect(() =>+ upsertRule(fns.idOptionalOnCreate!, schema("id", "name"))+ ).toThrow(/cannot say whether it creates or updates/);+ });+it("refuses an upsert whose payload cannot say, until a key is declared", () => {expect(() => upsertRule(fns.idBothWays!, schema("id", "name"))).toThrow(/cannot say whether it creates or updates.*`@mcp key <table>/sdiff --git a/scripts/lib/service-ast.ts b/scripts/lib/service-ast.tsindex 3f2157850d..c045e837e5 100644--- a/scripts/lib/service-ast.ts+++ b/scripts/lib/service-ast.ts@@ -521,13 +521,14 @@ export function branchesOnKeyPresence(/*** Whether sending `id` is what separates an update from an insert, read off the* payload parameter's TYPE: every member of its union that requires `updatedBy`- * (the update shape) has an `id`, and no other member REQUIRES one. Then a call- * with an `id` can only mean "update this record" and a call without one can- * only mean "create".+ * (the update shape) has an `id`, and no other member has an `id` AT ALL. Then+ * "an id was sent" is not a guess about intent — the service's own signature+ * says a create carries none.*- * False when both shapes require an `id` (a part's `id` is its part number on- * create and its item id on update) or neither has one (a pick method is keyed- * by item and location) — there the payload cannot say which it is.+ * False when a create may carry an `id` too, required or optional (a part's+ * `id` is its part number on create; a document section may be created under a+ * chosen id), or when neither shape has one (a pick method is keyed by item+ * and location). There an `id` proves nothing and the row has to be looked up.*/export function idDistinguishesUpdate(fn: ServiceFunctionNode,@@ -548,10 +549,7 @@ export function idDistinguishesUpdate(return (updates.every((m) => m.getProperty("id") !== undefined) &&- creates.every((m) => {- const id = m.getProperty("id");- return id === undefined || id.hasFlags(SymbolFlags.Optional);- })+ creates.every((m) => m.getProperty("id") === undefined));}return false;