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): method import writes the rows the BOM and BOP editors write
Raul4fd48a9a8cc6
diff --git a/.ai/plans/2026-10-21-method-import-append.md b/.ai/plans/2026-10-21-method-import-append.mdindex ce40ecfc55..cebc480026 100644--- a/.ai/plans/2026-10-21-method-import-append.md+++ b/.ai/plans/2026-10-21-method-import-append.md@@ -55,8 +55,30 @@ operations that are already on the target version.- Updates to existing lines (quantity, unit, times) and deletes.- A new version that copies another version.- The other findings from the PR #911 review: same-file sub-assembly order,- Unique ID not written by Parts with Methods, BOM unit default `EA`, the unused- Supplier Process column, and a non-numeric version that becomes 1.+ Unique ID not written by Parts with Methods, and the unused Supplier Process+ column.++## Hardening after a break-it pass++An adversarial pass on the local stack found these problems. Each fix makes the+import match the BOM and Bill of Process editors.++| Problem | Fix |+|---|---|+| Two imports of one file at the same time wrote every line twice. | `writeGroup` locks the target `makeMethod` row before it reads the lines. |+| A BOM line with no unit was stored in `EA`, even for a material stocked in yards. | The line takes the material's unit, as the BOM editor does. |+| A service or a tool was accepted as a BOM line, a part as a tool, and the part as its own line. | The import refuses them, as the editors' validators and item pickers do. |+| A negative quantity, time or cost, a tool quantity of 0, a step max below its min and an empty List step were accepted. | The import refuses them with the editors' rules. |+| A blank Operation Type became Process and a blank description stayed empty. | They take the process's type and name. The wizard no longer presets Process. |+| An Inspection operation was created with no Inspection Plan, which the editor never allows. | The import refuses Inspection operations. |+| A non-numeric Make Method Version became version 1. | The import refuses it. |+| `customFields` was null where the editors write `{}`. | The import writes `{}`. |++The parity test (`method-import-parity.test.ts`) and the guards test+(`method-import-guards.test.ts`) pin the fixes.++Open for a decision: the method imports need only `update: parts`, but the+editors' create routes need `create: parts`.## Stepsdiff --git a/.claude/rules/csv-import-system.md b/.claude/rules/csv-import-system.mdindex 9447cfec57..4439a825cd 100644--- a/.claude/rules/csv-import-system.md+++ b/.claude/rules/csv-import-system.md@@ -274,13 +274,39 @@ continue.it (they may reference it with no BOP row). A step with the same name, a toolwith the same item and a parameter with the same key are skipped. Every skipgoes to `skipped`, so re-running a file writes nothing.-- BOM lines copy `methodType` / `sourcingType` / `materialMakeMethodId` /- `storageUnitIds` from the component item, as `upsertMethodMaterial` does.+- **Rows match the editors' rows.** BOM lines copy `methodType` /+ `sourcingType` / `materialMakeMethodId` / `storageUnitIds` from the component+ item, as `upsertMethodMaterial` does, and take the component's unit when the+ file has none. A blank Operation Type takes the process's `processType` and a+ blank description the process's name, as BillOfProcess `onProcessChange`+ fills them. Step descriptions go through `toTiptapDoc`, as the step form's+ validator does. `customFields` is `{}` on lines and operations, as the+ routes' `setCustomFields` writes it.+ `apps/erp/test/database/method-import-parity.test.ts` builds one method+ through the editors' validators and building blocks and one through the+ import, and compares every column but ids and timestamps.+- **Refusals match the editors' validators** (`methodMaterialValidator`,+ `methodOperationValidator`, `operationStepValidator`,+ `operationToolValidator`): a BOM line must be a Part, Material or Consumable+ and not the part itself, with quantity 0 or more; a TOOL row's item must be a+ Tool, with quantity above 0; times, costs and lead time are 0 or more; a+ Process operation needs setup, labor and machine units, an Assembly operation+ setup and labor; step min and max are 0 or more, max at least min; a List+ step needs one non-blank option; a Make Method Version must be a positive+ number. An Inspection operation is refused, because+ `methodOperationValidator` requires an Inspection Plan and a CSV has no+ column for one. Pinned by `method-import-guards.test.ts`.+- **Concurrency.** `writeGroup` locks the target `makeMethod` row+ (`FOR UPDATE`) before it reads the version's lines, so two imports of one+ file write each line once.- Known gaps: a sub-assembly created by a `PART` row must come before its parentin the file (parts are written in order of first appearance); a `PART` row- does not write the Unique ID mapping; an unmapped BOM unit of measure is `EA`;- the Supplier Process column is unused; a non-numeric Make Method Version- becomes 1.+ does not write the Unique ID mapping; the Supplier Process column is unused;+ an Op No matches an operation in the same file by exact text ("1.0" is not+ "1"), but an existing operation by number; items are looked up by readable id+ and revision only, so a readable id shared by two item types resolves to+ either; the import needs only `update: parts`, while the editors' create+ routes need `create: parts`.> The models also include `customerStatus` / `customerType` field-mapping entries (used by> creatable lookups), but only the tables above appear in `importPermissions`.diff --git a/apps/erp/app/modules/shared/imports.models.ts b/apps/erp/app/modules/shared/imports.models.tsindex 2f945208f8..79c54de890 100644--- a/apps/erp/app/modules/shared/imports.models.ts+++ b/apps/erp/app/modules/shared/imports.models.ts@@ -357,9 +357,8 @@ const methodBopFields = {type: "enum",enumData: {description:- "The operation's classification: Process, Assembly, and Inspection run in-house; Outside Processing is subcontracted",- options: operationTypes,- default: "Process"+ "The operation's classification: Process and Assembly run in-house; Outside Processing is subcontracted. Blank takes the process's type. Inspection operations need an Inspection Plan, so add them in the app",+ options: operationTypes}},operationOrder: {diff --git a/apps/erp/app/modules/shared/shared.service.ts b/apps/erp/app/modules/shared/shared.service.tsindex c186940a8f..3f4b0d43c5 100644--- a/apps/erp/app/modules/shared/shared.service.ts+++ b/apps/erp/app/modules/shared/shared.service.ts@@ -190,7 +190,12 @@ import {setGenericQueryFilters} from "~/utils/query";import type { CustomFieldsTableType } from "../settings/types";-import type { assignableTables } from "./shared.models";+import {+ type assignableTables,+ methodItemType,+ operationTypes,+ toTiptapDoc+} from "./shared.models";import { assertMethodHasPullFrom, isStrictBackflush } from "./strict-backflush";import type { ItemModelUpload } from "./types";@@ -3092,8 +3097,11 @@ type ItemInfo = {defaultMethodType: string | null;sourcingType: string | null;revisionStatus: string | null;+ unitOfMeasureCode: string | null;};+type ProcessInfo = { name: string; processType: string | null };+type RowIssue = { row: number; reason: string };// A part's recipe the import refuses as a whole. Thrown inside the part's@@ -3133,16 +3141,25 @@ const normalizeOperationType = (value: string): string =>// rows) and the standard importer in index.ts — NOT a human CSV line number.const rowOf = (index: number) => index;-// Plain text → minimal tiptap doc for a step's rich-text description.-function plainTextToTiptap(value: string): Record<string, unknown> {- const t = (value ?? "").trim();- if (!t) return {};- return {- type: "doc",- content: [{ type: "paragraph", content: [{ type: "text", text: t }] }]- };+// A step's rich-text description, converted as the step editor converts it.+function stepDescription(value: string): Record<string, unknown> {+ return value ? toTiptapDoc(value) : {};}+// A List step's options: pipe-separated, blanks dropped.+const listValues = (s: string | undefined): string[] =>+ text(s)+ .split("|")+ .map((v) => v.trim())+ .filter(Boolean);++// A number cell that must hold 0 or more when it is filled in.+const negative = (s: string | undefined) => {+ if (text(s) === "") return false;+ const n = num(s);+ return n === undefined || n < 0;+};+function normalizeRowType(raw: string | undefined,table: string@@ -3286,7 +3303,8 @@ export async function importMethods("type","defaultMethodType","sourcingType",- "revisionStatus"+ "revisionStatus",+ "unitOfMeasureCode"]).where("companyId", "=", companyId).where("readableId", "in", Array.from(readableIds))@@ -3299,7 +3317,8 @@ export async function importMethods(type: r.type,defaultMethodType: r.defaultMethodType ?? null,sourcingType: r.sourcingType ?? null,- revisionStatus: r.revisionStatus ?? null+ revisionStatus: r.revisionStatus ?? null,+ unitOfMeasureCode: r.unitOfMeasureCode ?? null});}}@@ -3315,12 +3334,27 @@ export async function importMethods(const releaseControl = (settings?.plmReleaseControl ??"enforce") as ReleaseControl;- const { map: processMap, ambiguous: processAmbiguous } = await nameMap(- db,- "process",- companyId,- processNames- );+ // A process also gives an operation its default type and description, as+ // the Bill of Process editor fills them in when a process is picked.+ const processMap = new Map<string, string>();+ const processAmbiguous = new Set<string>();+ const processById = new Map<string, ProcessInfo>();+ if (processNames.size > 0) {+ const rows = await db+ .selectFrom("process")+ .select(["id", "name", "processType"])+ .where("companyId", "=", companyId)+ .execute();+ for (const r of rows) {+ const lower = r.name.toLowerCase();+ if (processMap.has(lower)) processAmbiguous.add(lower);+ else processMap.set(lower, r.id);+ processById.set(r.id, {+ name: r.name,+ processType: r.processType ?? null+ });+ }+ }const { map: workCenterMap, ambiguous: workCenterAmbiguous } = await nameMap(db,"workCenter",@@ -3366,6 +3400,7 @@ export async function importMethods(fileCreatedParts,processMap,processAmbiguous,+ processById,workCenterMap,workCenterAmbiguous,uomSet@@ -3382,6 +3417,7 @@ export async function importMethods(userId,itemMap,processMap,+ processById,workCenterMap,supplierMap,releaseControl@@ -3418,7 +3454,7 @@ export async function importMethods(async function nameMap(db: Kysely<KyselyDatabase>,- table: "process" | "workCenter",+ table: "workCenter",companyId: string,names: Set<string>): Promise<{ map: Map<string, string>; ambiguous: Set<string> }> {@@ -3443,11 +3479,26 @@ async function nameMap(// validation (pure, given the resolved lookups)// ---------------------------------------------------------------------------+// The operation type a BOP row gets: its own, else its process's type, as the+// Bill of Process editor fills it in when a process is picked.+function operationTypeFor(+ record: Rec,+ processId: string | undefined,+ processById: Map<string, ProcessInfo>+): string {+ return (+ normalizeOperationType(text(record.operationType)) ||+ (processId ? processById.get(processId)?.processType : null) ||+ "Process"+ );+}+type Lookups = {itemMap: Map<string, ItemInfo>;fileCreatedParts: Set<string>;processMap: Map<string, string>;processAmbiguous: Set<string>;+ processById: Map<string, ProcessInfo>;workCenterMap: Map<string, string>;workCenterAmbiguous: Set<string>;uomSet: Set<string>;@@ -3477,7 +3528,15 @@ function validateGroup(}}- for (const bucket of g.versions.values()) {+ for (const [version, bucket] of g.versions) {+ const versionNumber = num(version);+ if (text(version) && (versionNumber === undefined || versionNumber <= 0)) {+ errors.push({+ row: rowOf(firstIndexOf(bucket)),+ reason: `Make Method Version "${version}" is not a version number`+ });+ }+// Op No uniqueness within the parent.const opNos = new Set<string>();for (const e of bucket.bop) {@@ -3531,18 +3590,49 @@ function validateGroup(}}- // In-house operations need time units.- const opType =- normalizeOperationType(text(e.record.operationType)) || "Process";- if (opType !== "Outside Processing") {- if (- !text(e.record.setupUnit) ||- !text(e.record.laborUnit) ||- !text(e.record.machineUnit)- ) {+ // The Bill of Process editor's rules: in-house operations need setup and+ // labor units, a Process operation also a machine unit, and an+ // Inspection operation an Inspection Plan, which a CSV cannot carry.+ const opType = operationTypeFor(+ e.record,+ lk.processMap.get(proc),+ lk.processById+ );+ if (!(operationTypes as readonly string[]).includes(opType)) {+ errors.push({+ row: rowOf(e.index),+ reason: `Op ${opNo}: Invalid Operation Type "${opType}"`+ });+ } else if (opType === "Inspection") {+ errors.push({+ row: rowOf(e.index),+ reason: `Op ${opNo}: an Inspection operation needs an Inspection Plan; add it in the app`+ });+ } else if (opType !== "Outside Processing") {+ const missing = [+ !text(e.record.setupUnit) && "Setup",+ !text(e.record.laborUnit) && "Labor",+ opType === "Process" && !text(e.record.machineUnit) && "Machine"+ ].filter(Boolean);+ if (missing.length > 0) {errors.push({row: rowOf(e.index),- reason: `Op ${opNo}: In-house operations require Setup, Labor, and Machine units`+ reason: `Op ${opNo}: ${opType} operations require ${missing.join(", ")} units`+ });+ }+ }+ for (const [field, label] of [+ ["setupTime", "Setup Time"],+ ["laborTime", "Labor Time"],+ ["machineTime", "Machine Time"],+ ["operationMinimumCost", "Minimum Cost"],+ ["operationUnitCost", "Unit Cost"],+ ["operationLeadTime", "Lead Time"]+ ] as const) {+ if (negative(e.record[field])) {+ errors.push({+ row: rowOf(e.index),+ reason: `Op ${opNo}: ${label} must be a number of 0 or more`});}}@@ -3559,16 +3649,32 @@ function validateGroup(continue;}const compKey = key(componentId, text(e.record.componentRevision));- if (!lk.itemMap.has(compKey) && !lk.fileCreatedParts.has(compKey)) {+ const compLabel = `${componentId} rev ${text(e.record.componentRevision) || "0"}`;+ // A part this file creates is a Part; anything else must already exist.+ const compType = lk.fileCreatedParts.has(compKey)+ ? "Part"+ : lk.itemMap.get(compKey)?.type;+ if (!compType) {+ errors.push({+ row: rowOf(e.index),+ reason: `Material ${compLabel} not found`+ });+ } else if (compKey === parentKey) {errors.push({row: rowOf(e.index),- reason: `Material ${componentId} rev ${text(e.record.componentRevision) || "0"} not found`+ reason: `Material ${compLabel} is the part itself; an item cannot be added to its own BOM`+ });+ } else if (!(methodItemType as readonly string[]).includes(compType)) {+ errors.push({+ row: rowOf(e.index),+ reason: `Material ${compLabel} is a ${compType}; a BOM line takes a Part, Material or Consumable`});}- if (num(e.record.quantity) === undefined) {+ const quantity = num(e.record.quantity);+ if (quantity === undefined || quantity < 0) {errors.push({row: rowOf(e.index),- reason: "BOM row has an invalid Quantity"+ reason: "BOM row Quantity must be a number of 0 or more"});}const uom = text(e.record.unitOfMeasureCode);@@ -3596,18 +3702,42 @@ function validateGroup(reason: `Invalid Step Type "${stepType}"`});}- if (stepType === "Measurement" && !text(e.record.stepUnitOfMeasureCode)) {+ const stepUom = text(e.record.stepUnitOfMeasureCode);+ if (stepType === "Measurement" && !stepUom) {errors.push({row: rowOf(e.index),reason: "Measurement step requires a Step Unit of Measure"});+ } else if (stepUom && !lk.uomSet.has(stepUom)) {+ errors.push({+ row: rowOf(e.index),+ reason: `Step Unit of Measure "${stepUom}" not found`+ });}- if (stepType === "List" && !text(e.record.stepListValues)) {+ if (+ stepType === "List" &&+ listValues(e.record.stepListValues).length === 0+ ) {errors.push({row: rowOf(e.index),reason: "List step requires Step List Values"});}+ const min = num(e.record.stepMinValue);+ const max = num(e.record.stepMaxValue);+ if (negative(e.record.stepMinValue) || negative(e.record.stepMaxValue)) {+ errors.push({+ row: rowOf(e.index),+ reason:+ "Step Min Value and Step Max Value must be numbers of 0 or more"+ });+ } else if (min !== undefined && max !== undefined && max < min) {+ errors.push({+ row: rowOf(e.index),+ reason:+ "Step Max Value must be greater than or equal to Step Min Value"+ });+ }}for (const e of bucket.tool) {const toolId = text(e.record.toolId);@@ -3618,17 +3748,27 @@ function validateGroup(});} else {const toolKey = key(toolId, text(e.record.toolRevision));- if (!lk.itemMap.has(toolKey) && !lk.fileCreatedParts.has(toolKey)) {+ const toolLabel = `${toolId} rev ${text(e.record.toolRevision) || "0"}`;+ const toolType = lk.fileCreatedParts.has(toolKey)+ ? "Part"+ : lk.itemMap.get(toolKey)?.type;+ if (!toolType) {errors.push({row: rowOf(e.index),- reason: `Tool ${toolId} rev ${text(e.record.toolRevision) || "0"} not found`+ reason: `Tool ${toolLabel} not found`+ });+ } else if (toolType !== "Tool") {+ errors.push({+ row: rowOf(e.index),+ reason: `Tool ${toolLabel} is a ${toolType}, not a Tool`});}}- if (num(e.record.toolQuantity) === undefined) {+ const toolQuantity = num(e.record.toolQuantity);+ if (toolQuantity === undefined || toolQuantity <= 0) {errors.push({row: rowOf(e.index),- reason: "TOOL row has an invalid Quantity"+ reason: "TOOL row Quantity must be a number above 0"});}}@@ -3829,6 +3969,7 @@ async function writeGroup(userId: string;itemMap: Map<string, ItemInfo>;processMap: Map<string, string>;+ processById: Map<string, ProcessInfo>;workCenterMap: Map<string, string>;supplierMap: Map<string, string>;releaseControl: ReleaseControl;@@ -3956,6 +4097,16 @@ async function writeGroup(createdIds.set(method.version, makeMethodId);}+ // Imports into one version run one at a time: a second import waits+ // here, then reads what the first wrote and skips it.+ await trx+ .selectFrom("makeMethod")+ .select("id")+ .where("id", "=", makeMethodId)+ .where("companyId", "=", companyId)+ .forUpdate()+ .execute();+const existingMaterials = await trx.selectFrom("methodMaterial").select(["itemId", "order"])@@ -4062,10 +4213,15 @@ async function writeGroup(sourcingType,materialMakeMethodId,quantity: num(r.quantity) ?? 1,- unitOfMeasureCode: text(r.unitOfMeasureCode) || "EA",+ // The BOM editor starts a line in the material's own unit.+ unitOfMeasureCode:+ text(r.unitOfMeasureCode) ||+ component.unitOfMeasureCode ||+ "EA",order,kit: bool(r.kit),storageUnitIds,+ customFields: {},companyId,createdBy: userId,createdAt: now()@@ -4086,9 +4242,8 @@ async function writeGroup(const bopByIndex = new Map(bucket.bop.map((e) => [e.index, e.record]));for (const { index, opNo, order } of opPlan.insert) {const r = bopByIndex.get(index)!;- const opType =- normalizeOperationType(text(r.operationType)) || "Process";const processId = ctx.processMap.get(text(r.process).toLowerCase())!;+ const opType = operationTypeFor(r, processId, ctx.processById);const workCenterId = text(r.workCenter)? ctx.workCenterMap.get(text(r.workCenter).toLowerCase()): undefined;@@ -4113,7 +4268,11 @@ async function writeGroup(operationType: opType,processId,workCenterId,- description: text(r.operationDescription),+ // The editor names a new operation after its process.+ description:+ text(r.operationDescription) ||+ ctx.processById.get(processId)?.name ||+ "",setupUnit: text(r.setupUnit) || undefined,setupTime: num(r.setupTime) ?? 0,laborUnit: text(r.laborUnit) || undefined,@@ -4125,6 +4284,7 @@ async function writeGroup(operationUnitCost: num(r.operationUnitCost),operationLeadTime: num(r.operationLeadTime),workInstruction: {},+ customFields: {},companyId,createdBy: userId,createdAt: now()@@ -4168,7 +4328,7 @@ async function writeGroup({operationId,name,- description: plainTextToTiptap(text(r.stepDescription)),+ description: stepDescription(text(r.stepDescription)),type: stepType,required: bool(r.stepRequired),sortOrder,@@ -4179,12 +4339,7 @@ async function writeGroup(minValue: num(r.stepMinValue),maxValue: num(r.stepMaxValue),listValues:- stepType === "List" && text(r.stepListValues)- ? text(r.stepListValues)- .split("|")- .map((v) => v.trim())- .filter(Boolean)- : undefined,+ stepType === "List" ? listValues(r.stepListValues) : undefined,companyId,createdBy: userId,createdAt: now()@@ -4299,7 +4454,8 @@ async function resolveItem("type","defaultMethodType","sourcingType",- "revisionStatus"+ "revisionStatus",+ "unitOfMeasureCode"]).where("companyId", "=", companyId).where("readableId", "=", readableId)@@ -4314,7 +4470,8 @@ async function resolveItem(type: row.type,defaultMethodType: row.defaultMethodType ?? null,sourcingType: row.sourcingType ?? null,- revisionStatus: row.revisionStatus ?? null+ revisionStatus: row.revisionStatus ?? null,+ unitOfMeasureCode: row.unitOfMeasureCode ?? null};itemMap.set(k, info);return info;diff --git a/apps/erp/test/database/fixtures/method-import.ts b/apps/erp/test/database/fixtures/method-import.tsnew file mode 100644index 0000000000..a3dd00d0c1--- /dev/null+++ b/apps/erp/test/database/fixtures/method-import.ts@@ -0,0 +1,250 @@+// 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 { createServiceContext } from "@carbon/api/service-fn.server";+import { sql } from "kysely";+import { importMethods } from "../../../app/modules/shared/shared.service";+import { connectLocalTestDatabase } from "./local-database";+import { localServiceRoleClient } from "./local-supabase";++const USER = "system";++export type MethodImportFixture = Awaited<+ ReturnType<typeof methodImportFixture>+>;++/**+ * A scratch company for the method import. Items, all revision 0:+ * - `ASSY` and `TWIN`: Make parts, each with the Draft v1 the item interceptor+ * makes and nothing on it.+ * - `COMP-1`, `COMP-2`: bought parts in EA; `COMP-2` has a default storage+ * unit at the plant.+ * - `MAT-YD`: a material stocked in yards.+ * - `SUB`: a Make to Order sub-assembly (its own Draft v1).+ * - `CONS`: a consumable. `TOOL-1`: a tool. `SVC`: a service.+ * Processes `Machining` (Process), `Bench Assembly` (Assembly) and `Final Check`+ * (Inspection), work center `Mill`.+ */+export async function methodImportFixture() {+ const db = await connectLocalTestDatabase();+ const client = localServiceRoleClient();+ const prefix = `mimp-${crypto.randomUUID().replaceAll("-", "").slice(0, 12)}`;+ const companyId = `${prefix}-company`;+ const groupId = `${prefix}-group`;+ const locationId = `${prefix}-plant`;+ const storageUnitId = `${prefix}-bin`;+ const itemId = (readableId: string) => `${prefix}-${readableId}`;++ const items = [+ ["ASSY", "Part", "Make", "Make to Order", "EA"],+ ["TWIN", "Part", "Make", "Make to Order", "EA"],+ ["COMP-1", "Part", "Buy", "Purchase to Order", "EA"],+ ["COMP-2", "Part", "Buy", "Pull from Inventory", "EA"],+ ["MAT-YD", "Material", "Buy", "Pull from Inventory", "YD"],+ ["SUB", "Part", "Make", "Make to Order", "EA"],+ ["CONS", "Consumable", "Buy", "Pull from Inventory", "EA"],+ ["TOOL-1", "Tool", "Buy", "Pull from Inventory", "EA"],+ ["SVC", "Service", "Buy", "Purchase to Order", "EA"]+ ] as const;++ await db.transaction().execute(async (trx) => {+ await sql`SET LOCAL "app.sync_in_progress" = 'true'`.execute(trx);+ await trx+ .insertInto("companyGroup")+ .values({ id: groupId, name: prefix, createdBy: USER })+ .execute();+ await trx+ .insertInto("company")+ .values({+ id: companyId,+ name: prefix,+ companyGroupId: groupId,+ baseCurrencyCode: "USD",+ timezone: "America/New_York"+ })+ .execute();+ await trx+ .insertInto("companySettings")+ .values({ id: companyId })+ .onConflict((oc) => oc.column("id").doNothing())+ .execute();+ await trx+ .insertInto("unitOfMeasure")+ .values([+ { code: "EA", name: "Each", companyId, createdBy: USER },+ { code: "YD", name: "Yard", companyId, createdBy: USER }+ ])+ .execute();+ await trx+ .insertInto("location")+ .values({+ id: locationId,+ name: "Plant",+ addressLine1: "1 Test Way",+ city: "Testville",+ postalCode: "00000",+ timezone: "America/New_York",+ companyId,+ createdBy: USER+ })+ .execute();+ await trx+ .insertInto("storageUnit")+ .values({+ id: storageUnitId,+ name: "Bin",+ locationId,+ companyId,+ createdBy: USER+ })+ .execute();+ await trx+ .insertInto("process")+ .values([+ {+ id: `${prefix}-machining`,+ name: "Machining",+ defaultStandardFactor: "Minutes/Piece",+ processType: "Process",+ companyId,+ createdBy: USER+ },+ {+ id: `${prefix}-bench-assembly`,+ name: "Bench Assembly",+ defaultStandardFactor: "Minutes/Piece",+ processType: "Assembly",+ companyId,+ createdBy: USER+ },+ {+ id: `${prefix}-final-check`,+ name: "Final Check",+ defaultStandardFactor: "Minutes/Piece",+ processType: "Inspection",+ companyId,+ createdBy: USER+ }+ ])+ .execute();+ await trx+ .insertInto("workCenter")+ .values({+ id: `${prefix}-mill`,+ name: "Mill",+ locationId,+ companyId,+ createdBy: USER+ })+ .execute();+ await trx+ .insertInto("item")+ .values(+ items.map(+ ([readableId, type, replenishmentSystem, method, unit]) => ({+ id: itemId(readableId),+ readableId,+ name: readableId,+ type,+ replenishmentSystem,+ defaultMethodType: method,+ itemTrackingType:+ type === "Service" ? ("Non-Inventory" as const) : "Inventory",+ unitOfMeasureCode: unit,+ companyId,+ createdBy: USER+ })+ )+ )+ .execute();+ await trx+ .insertInto("pickMethod")+ .values({+ itemId: itemId("COMP-2"),+ locationId,+ defaultStorageUnitId: storageUnitId,+ companyId,+ createdBy: USER+ })+ .onConflict((oc) =>+ oc.columns(["itemId", "locationId"]).doUpdateSet({+ defaultStorageUnitId: storageUnitId+ })+ )+ .execute();+ });++ const methodOf = async (readableId: string) =>+ (+ await db+ .selectFrom("makeMethod")+ .select("id")+ .where("itemId", "=", itemId(readableId))+ .where("companyId", "=", companyId)+ .orderBy("version", "desc")+ .executeTakeFirstOrThrow()+ ).id;++ return {+ db,+ client,+ companyId,+ locationId,+ storageUnitId,+ itemId,+ processId: (name: "machining" | "bench-assembly" | "final-check") => `${prefix}-${name}`,+ workCenterId: `${prefix}-mill`,+ methodOf,+ /** The system actor, as the service functions the editors and MCP call see it. */+ ctx: client+ ? createServiceContext({+ actor: { type: "system", onBehalfOf: USER },+ company: { id: companyId, groupId },+ caller: client,+ system: () => client,+ pg: () => db,+ kv: () => {+ throw new Error("No operation here reads the cache");+ }+ })+ : null,+ async run(+ table: "bom" | "operations" | "partWithMethod",+ mappedRecords: Record<string, string>[],+ on = db+ ) {+ const summary = {+ inserted: 0,+ updated: 0,+ errors: [] as Array<{ row: number; reason: string }>,+ skipped: [] as Array<{ row: number; reason: string }>+ };+ await importMethods(on, {+ table,+ mappedRecords,+ companyId,+ userId: USER,+ summary+ });+ return summary;+ },+ async cleanup() {+ await db.transaction().execute(async (trx) => {+ await sql`SET LOCAL "app.sync_in_progress" = 'true'`.execute(trx);+ await trx.deleteFrom("company").where("id", "=", companyId).execute();+ await trx+ .deleteFrom("companyGroup")+ .where("id", "=", groupId)+ .execute();+ await sql`DROP TABLE IF EXISTS ${sql.id(`searchIndex_${companyId}`)}`.execute(+ trx+ );+ await sql`DROP TABLE IF EXISTS ${sql.id(`auditLog_${companyId}`)}`.execute(+ trx+ );+ });+ await db.destroy();+ }+ };+}diff --git a/apps/erp/test/database/method-import-append.test.ts b/apps/erp/test/database/method-import-append.test.tsindex 841cebae9e..24263c54a1 100644--- a/apps/erp/test/database/method-import-append.test.ts+++ b/apps/erp/test/database/method-import-append.test.ts@@ -283,7 +283,8 @@ describe("method import onto an existing method", () => {afterEach(async () => {await f?.cleanup();f = undefined;- });+ // Deleting the scratch company cascades through every company table.+ }, 60_000);databaseTest("appends BOM lines after the existing ones and skips materials already on it",diff --git a/apps/erp/test/database/method-import-guards.test.ts b/apps/erp/test/database/method-import-guards.test.tsnew file mode 100644index 0000000000..01275f6e4e--- /dev/null+++ b/apps/erp/test/database/method-import-guards.test.ts@@ -0,0 +1,210 @@+// 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.++// The method import refuses what the BOM and Bill of Process editors refuse,+// and two imports of one file at the same time write each line once. Runs+// against the local database; skips without one.++import { afterEach, describe, expect } from "vitest";+import {+ connectLocalTestDatabase,+ databaseTest+} from "./fixtures/local-database";+import {+ type MethodImportFixture,+ methodImportFixture+} from "./fixtures/method-import";++type Rows = Record<string, string>[];++const bom = (+ parentId: string,+ componentId: string,+ quantity: string,+ extra: Record<string, string> = {}+) => ({ rowType: "BOM", parentId, componentId, quantity, ...extra });++const bop = (+ parentId: string,+ opNo: string,+ extra: Record<string, string> = {}+) => ({+ rowType: "BOP",+ parentId,+ opNo,+ process: "Machining",+ setupUnit: "Total Minutes",+ laborUnit: "Minutes/Piece",+ machineUnit: "Minutes/Piece",+ ...extra+});++// Each file and the reasons the import gives for it. ASSY, SUB and COMP-2+// start with empty Draft methods, and none of them may change.+const REFUSALS: Array<[string, Rows, string[]]> = [+ [+ "the part itself as a BOM line",+ [bom("ASSY", "ASSY", "1")],+ [+ "Material ASSY rev 0 is the part itself; an item cannot be added to its own BOM"+ ]+ ],+ [+ "a service and a tool as BOM lines",+ [bom("ASSY", "SVC", "1"), bom("ASSY", "TOOL-1", "1")],+ [+ "Material SVC rev 0 is a Service; a BOM line takes a Part, Material or Consumable",+ "Material TOOL-1 rev 0 is a Tool; a BOM line takes a Part, Material or Consumable"+ ]+ ],+ [+ "a negative quantity",+ [bom("ASSY", "COMP-1", "-3")],+ ["BOM row Quantity must be a number of 0 or more"]+ ],+ [+ "a part as a tool, with quantity 0",+ [+ bop("COMP-2", "10"),+ {+ rowType: "TOOL",+ parentId: "COMP-2",+ opNo: "10",+ toolId: "COMP-1",+ toolQuantity: "0"+ }+ ],+ [+ "Tool COMP-1 rev 0 is a Part, not a Tool",+ "TOOL row Quantity must be a number above 0"+ ]+ ],+ [+ "a negative time, a minimum above the maximum and an empty list",+ [+ bop("SUB", "10", { setupTime: "-5" }),+ {+ rowType: "STEP",+ parentId: "SUB",+ opNo: "10",+ stepName: "Gauge",+ stepType: "Measurement",+ stepUnitOfMeasureCode: "EA",+ stepMinValue: "10",+ stepMaxValue: "2"+ },+ {+ rowType: "STEP",+ parentId: "SUB",+ opNo: "10",+ stepName: "Pick",+ stepType: "List",+ stepListValues: "|"+ }+ ],+ [+ "Op 10: Setup Time must be a number of 0 or more",+ "Step Max Value must be greater than or equal to Step Min Value",+ "List step requires Step List Values"+ ]+ ],+ [+ "an Inspection operation, from its process's type",+ [bop("SUB", "10", { process: "Final Check" })],+ [+ "Op 10: an Inspection operation needs an Inspection Plan; add it in the app"+ ]+ ],+ [+ "a version that is not a number",+ [bom("ASSY", "COMP-1", "1", { makeMethodVersion: "abc" })],+ ['Make Method Version "abc" is not a version number']+ ]+];++describe("method import guards", () => {+ let f: MethodImportFixture | undefined;+ afterEach(async () => {+ await f?.cleanup();+ f = undefined;+ // Deleting the scratch company cascades through every company table.+ }, 60_000);++ const rowCounts = async () => {+ const count = async (+ table: "methodMaterial" | "methodOperation" | "methodOperationStep"+ ) =>+ Number(+ (+ await f!.db+ .selectFrom(table)+ .select((eb) => eb.fn.countAll().as("count"))+ .where("companyId", "=", f!.companyId)+ .executeTakeFirstOrThrow()+ ).count+ );+ return {+ materials: await count("methodMaterial"),+ operations: await count("methodOperation"),+ steps: await count("methodOperationStep")+ };+ };++ databaseTest(+ "refuses the rows the editors refuse and writes nothing",+ async () => {+ f = await methodImportFixture();+ for (const [, rows, reasons] of REFUSALS) {+ const table = rows.some((r) => r.rowType !== "BOM")+ ? "operations"+ : "bom";+ const summary = await f.run(table, rows);+ expect(summary.errors.map((e) => e.reason)).toEqual(reasons);+ expect(summary.updated).toBe(0);+ }+ expect(await rowCounts()).toEqual({+ materials: 0,+ operations: 0,+ steps: 0+ });+ }+ );++ databaseTest(+ "a BOM line without a unit takes the material's own unit",+ async () => {+ f = await methodImportFixture();+ const summary = await f.run("bom", [bom("TWIN", "MAT-YD", "2")]);+ expect(summary.errors).toEqual([]);+ const line = await f.db+ .selectFrom("methodMaterial")+ .select("unitOfMeasureCode")+ .where("makeMethodId", "=", await f.methodOf("TWIN"))+ .executeTakeFirstOrThrow();+ expect(line.unitOfMeasureCode).toBe("YD");+ }+ );++ databaseTest(+ "two imports of one file at the same time write each line once",+ async () => {+ f = await methodImportFixture();+ const second = await connectLocalTestDatabase();+ try {+ const rows = [bom("ASSY", "COMP-1", "1"), bom("ASSY", "COMP-2", "2")];+ const [a, b] = await Promise.all([+ f.run("bom", rows),+ f.run("bom", rows, second)+ ]);+ expect(a.errors).toEqual([]);+ expect(b.errors).toEqual([]);+ expect(a.updated + b.updated).toBe(1);+ expect(a.skipped.length + b.skipped.length).toBe(2);+ expect((await rowCounts()).materials).toBe(2);+ } finally {+ await second.destroy();+ }+ }+ );+});diff --git a/apps/erp/test/database/method-import-parity.test.ts b/apps/erp/test/database/method-import-parity.test.tsnew file mode 100644index 0000000000..9cf4ce77cb--- /dev/null+++ b/apps/erp/test/database/method-import-parity.test.ts@@ -0,0 +1,352 @@+// 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.++// The method import writes the same rows as the BOM and Bill of Process+// editors. One method is built the way the editors' routes build it (their+// form validators, then the building blocks), the other by the import from a+// CSV with the same content; every column but ids, owners and timestamps must+// match. Runs against the local stack (database and PostgREST); skips without.++import type { KyselyDatabase } from "@carbon/database/client";+import type { Kysely } from "kysely";+import { nanoid } from "nanoid";+import { afterEach, describe, expect, it } from "vitest";+import {+ methodMaterialValidator,+ methodOperationValidator+} from "../../app/modules/items/items.models";+import {+ upsertMethodMaterial,+ upsertMethodOperation,+ upsertMethodOperationParameter,+ upsertMethodOperationStep,+ upsertMethodOperationTool+} from "../../app/modules/items/items.service";+import {+ operationParameterValidator,+ operationStepValidator,+ operationToolValidator+} from "../../app/modules/shared/shared.models";+import { hasLocalDatabase } from "./fixtures/local-database";+import { localServiceRoleClient } from "./fixtures/local-supabase";+import {+ type MethodImportFixture,+ methodImportFixture+} from "./fixtures/method-import";++const USER = "system";+const test = (name: string, fn: () => Promise<void>) =>+ it.skipIf(!hasLocalDatabase || !localServiceRoleClient())(name, fn, 120_000);++// The BOM lines and operations both paths build, as a person would enter them+// in the editors: pick the item (or process) and type what differs.+const LINES = [+ ["COMP-1", 2],+ ["COMP-2", 3],+ ["MAT-YD", 1.5],+ ["SUB", 1],+ ["CONS", 4]+] as const;++/** Every row of one make method, ids and audit columns removed, children nested by order. */+async function snapshot(db: Kysely<KyselyDatabase>, makeMethodId: string) {+ const materials = await db+ .selectFrom("methodMaterial")+ .selectAll()+ .where("makeMethodId", "=", makeMethodId)+ .orderBy("order")+ .execute();+ const operations = await db+ .selectFrom("methodOperation")+ .selectAll()+ .where("makeMethodId", "=", makeMethodId)+ .orderBy("order")+ .execute();+ // Both methods in the test have operations, so the `in` lists are never empty.+ const operationIds = operations.map((o) => o.id);+ const steps = await db+ .selectFrom("methodOperationStep")+ .selectAll()+ .where("operationId", "in", operationIds)+ .execute();+ const tools = await db+ .selectFrom("methodOperationTool")+ .selectAll()+ .where("operationId", "in", operationIds)+ .execute();+ const parameters = await db+ .selectFrom("methodOperationParameter")+ .selectAll()+ .where("operationId", "in", operationIds)+ .execute();+ const orderOf = new Map(operations.map((o) => [o.id, Number(o.order)]));+ const strip = (row: Record<string, unknown>) => {+ const {+ id: _id,+ makeMethodId: _makeMethodId,+ operationId,+ createdAt: _createdAt,+ updatedAt: _updatedAt,+ ...rest+ } = row;+ return operationId === undefined+ ? rest+ : { ...rest, operationOrder: orderOf.get(operationId as string) };+ };+ const byOrder = (rows: Record<string, unknown>[]) =>+ rows+ .map(strip)+ .sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));+ return {+ materials: materials.map(strip),+ operations: operations.map(strip),+ steps: byOrder(steps),+ tools: byOrder(tools),+ parameters: byOrder(parameters)+ };+}++describe("method import parity with the editors", () => {+ let f: MethodImportFixture | undefined;+ afterEach(async () => {+ await f?.cleanup();+ f = undefined;+ // Deleting the scratch company cascades through every company table.+ }, 60_000);++ test("an imported BOM and BOP match the editors' rows", async () => {+ f = await methodImportFixture();+ const client = f.client!;+ const viaEditor = await f.methodOf("ASSY");++ // --- the editors -----------------------------------------------------+ for (const [index, [readableId, quantity]] of LINES.entries()) {+ const item = await f.db+ .selectFrom("item")+ .select([+ "id",+ "type",+ "unitOfMeasureCode",+ "defaultMethodType",+ "sourcingType"+ ])+ .where("id", "=", f.itemId(readableId))+ .executeTakeFirstOrThrow();+ // What BillOfMaterial posts once the item is picked (onItemChange).+ const form = await methodMaterialValidator.parseAsync({+ id: nanoid(),+ makeMethodId: viaEditor,+ order: String(index + 1),+ itemType: item.type,+ kit: "false",+ methodType: item.defaultMethodType,+ sourcingType: item.sourcingType,+ itemId: item.id,+ methodOperationId: "",+ quantity: String(quantity),+ unitOfMeasureCode: item.unitOfMeasureCode,+ storageUnitIds: "{}"+ });+ const result = await upsertMethodMaterial(client, {+ ...form,+ companyId: f.companyId,+ createdBy: USER,+ customFields: {}+ });+ expect(result.error).toBeNull();+ }++ // What BillOfProcess posts after the process is picked (onProcessChange+ // fills the description, the units and the operation type).+ const operationForms = [+ {+ order: "1",+ operationType: "Process",+ processId: f.processId("machining"),+ workCenterId: f.workCenterId,+ description: "Mill pocket",+ setupTime: "5",+ laborTime: "2",+ machineTime: "3"+ },+ {+ order: "2",+ operationType: "Assembly",+ processId: f.processId("bench-assembly"),+ workCenterId: "",+ description: "Bench Assembly",+ setupTime: "0",+ laborTime: "1",+ machineTime: "0"+ }+ ];+ const operationIds: string[] = [];+ for (const values of operationForms) {+ const form = await methodOperationValidator.parseAsync({+ id: nanoid(),+ makeMethodId: viaEditor,+ operationOrder: "After Previous",+ procedureId: "",+ assemblyInstructionId: "",+ inspectionDocumentId: "",+ setupUnit: "Total Minutes",+ laborUnit: "Minutes/Piece",+ machineUnit: "Minutes/Piece",+ operationMinimumCost: "0",+ operationUnitCost: "0",+ operationLeadTime: "0",+ ...values+ });+ const { id: _id, ...operation } = form;+ const result = await upsertMethodOperation(client, {+ ...operation,+ companyId: f.companyId,+ createdBy: USER,+ customFields: {}+ });+ expect(result.error).toBeNull();+ operationIds.push(result.data!.id);+ }+ const [machining, assembly] = operationIds as [string, string];++ const steps = [+ {+ operationId: machining,+ name: "Torque",+ description: "Torque to 5 Nm",+ type: "Task",+ sortOrder: "1"+ },+ {+ operationId: assembly,+ name: "Bore",+ description: "Measure the bore",+ type: "Measurement",+ unitOfMeasureCode: "EA",+ minValue: "1",+ maxValue: "2",+ sortOrder: "1"+ }+ ];+ for (const values of steps) {+ const { id: _id, ...step } =+ await operationStepValidator.parseAsync(values);+ const result = await upsertMethodOperationStep(client, {+ ...step,+ companyId: f.companyId,+ createdBy: USER+ });+ expect(result.error).toBeNull();+ }+ {+ const { id: _id, ...tool } = await operationToolValidator.parseAsync({+ operationId: machining,+ toolId: f.itemId("TOOL-1"),+ quantity: "1"+ });+ const result = await upsertMethodOperationTool(client, {+ ...tool,+ companyId: f.companyId,+ createdBy: USER+ });+ expect(result.error).toBeNull();+ }+ {+ const { id: _id, ...parameter } =+ await operationParameterValidator.parseAsync({+ operationId: machining,+ key: "Feed",+ value: "0.2"+ });+ const result = await upsertMethodOperationParameter(client, {+ ...parameter,+ companyId: f.companyId,+ createdBy: USER+ });+ expect(result.error).toBeNull();+ }++ // --- the import, the same content as a CSV ----------------------------+ // No Unit of Measure, Operation Type or Operation Description columns:+ // the import fills them the way the editors do.+ const units = {+ setupUnit: "Total Minutes",+ laborUnit: "Minutes/Piece",+ machineUnit: "Minutes/Piece"+ };+ const summary = await f.run("partWithMethod", [+ ...LINES.map(([componentId, quantity]) => ({+ rowType: "BOM",+ parentId: "TWIN",+ componentId,+ quantity: String(quantity)+ })),+ {+ rowType: "BOP",+ parentId: "TWIN",+ opNo: "1",+ process: "Machining",+ workCenter: "Mill",+ operationDescription: "Mill pocket",+ setupTime: "5",+ laborTime: "2",+ machineTime: "3",+ ...units+ },+ {+ rowType: "BOP",+ parentId: "TWIN",+ opNo: "2",+ process: "Bench Assembly",+ laborTime: "1",+ ...units+ },+ {+ rowType: "STEP",+ parentId: "TWIN",+ opNo: "1",+ stepName: "Torque",+ stepDescription: "Torque to 5 Nm"+ },+ {+ rowType: "STEP",+ parentId: "TWIN",+ opNo: "2",+ stepName: "Bore",+ stepDescription: "Measure the bore",+ stepType: "Measurement",+ stepUnitOfMeasureCode: "EA",+ stepMinValue: "1",+ stepMaxValue: "2"+ },+ {+ rowType: "TOOL",+ parentId: "TWIN",+ opNo: "1",+ toolId: "TOOL-1",+ toolQuantity: "1"+ },+ {+ rowType: "PARAM",+ parentId: "TWIN",+ opNo: "1",+ paramKey: "Feed",+ paramValue: "0.2"+ }+ ]);+ expect(summary.errors).toEqual([]);++ const editor = await snapshot(f.db, viaEditor);+ const imported = await snapshot(f.db, await f.methodOf("TWIN"));+ expect.soft(imported.materials).toEqual(editor.materials);+ expect.soft(imported.operations).toEqual(editor.operations);+ expect.soft(imported.steps).toEqual(editor.steps);+ expect.soft(imported.tools).toEqual(editor.tools);+ expect.soft(imported.parameters).toEqual(editor.parameters);+ // The comparison covers real content, not two empty methods.+ expect(editor.materials).toHaveLength(LINES.length);+ expect(editor.operations).toHaveLength(2);+ expect(editor.steps).toHaveLength(2);+ });+});diff --git a/docs/content/docs/reference/import-export.mdx b/docs/content/docs/reference/import-export.mdxindex 7dffea6d83..ccbdbb0c85 100644--- a/docs/content/docs/reference/import-export.mdx+++ b/docs/content/docs/reference/import-export.mdx@@ -40,6 +40,8 @@ The **Bulk Import** menu on the parts and tools lists also offers three **methodA method import adds to the make method that is already there. With **Make Method Version** blank it writes into the highest version that is not archived. New BOM lines go after the existing ones. An **Op No** that matches an existing operation's order adds its steps, tools, and parameters to that operation. Rows already on the method (the same material, operation, step name, tool, or parameter key) are skipped and listed, so a file can run again safely. The import changes only a Draft version and follows the same change notice and release locks as the BOM and BOP editors. A part it refuses is listed with the reason, and none of its rows are written.+Blank cells take the values the editors fill in: a BOM line with no **Unit of Measure** takes the material's own unit, and an operation with no **Operation Type** or **Operation Description** takes its process's type and name. The import also refuses what the editors refuse: a BOM line that is the part itself or a tool or service, a tool that is not a tool item, a negative quantity, time or cost, and a step whose maximum is below its minimum. An Inspection operation needs an Inspection Plan, which a CSV cannot carry, so add Inspection operations in the app.+<Callout type="warn" title="Fixed assets and BOM components aren't importable here">The models list a `fixedAsset` import shape, but the import function's own table list omits it, so a fixed-asset CSV is rejected. Individual BOM material lines (`methodMaterial`) are likewise not wired up. Import the entities above; create assets and one-off BOM lines in the app.</Callout>