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(inventory): reject cross-location and name-colliding storage-unit CSV rows
sidgaikwad44518a64aa75
diff --git a/.claude/rules/csv-import-system.md b/.claude/rules/csv-import-system.mdindex 8cfc568a09..90fc58d14b 100644--- a/.claude/rules/csv-import-system.md+++ b/.claude/rules/csv-import-system.md@@ -83,7 +83,11 @@ unique **per location** (`storageUnit_name_locationId_key`), both in-file dedupmatch-existing-to-update key on `(locationId, lower(name))` — NOT `classifyImportRow`'sname-only dedup. A csv `id` still writes an `externalIntegrationMapping` for id-basedre-import. Updates deliberately never change `locationId` (avoids the "cannot move a-unit with children" interceptor). `storageTypeNames` resolve case-insensitively against+unit with children" interceptor); a unit's location is **immutable via import**, so a row+whose csv id resolves to a unit in a DIFFERENT location than the row states is reported as+a row error (not a silent move), and an id-matched rename onto a name another unit already+owns in that location is likewise reported rather than crashing the batch on+`storageUnit_name_locationId_key`. `storageTypeNames` resolve case-insensitively againstexisting company `storageType` rows, **creating** any missing ones (mirrors the creatableStorageTypes combobox). `parentName` is applied in a **second pass** after all inserts —individual `UPDATE`s outside the insert transaction — so a parent defined later in thediff --git a/packages/database/supabase/functions/import-csv/index.ts b/packages/database/supabase/functions/import-csv/index.tsindex cc442de5d7..22fa490be9 100644--- a/packages/database/supabase/functions/import-csv/index.ts+++ b/packages/database/supabase/functions/import-csv/index.ts@@ -2527,6 +2527,22 @@ serve(async (req: Request) => {// that index and roll back the whole import).const alreadyMappedEntityIds = new Set(externalIdMap.values());+ // Stored location of each already-mapped entity. A csv-id match that+ // resolves to a unit in a DIFFERENT location than the row states is a+ // conflict, not a move — used below to reject it instead of silently+ // renaming the wrong unit or crashing on storageUnit_name_locationId_key.+ const mappedEntityIds = Array.from(alreadyMappedEntityIds);+ const entityLocation = new Map<string, string>(+ (mappedEntityIds.length > 0+ ? await db+ .selectFrom("storageUnit")+ .select(["id", "locationId"])+ .where("id", "in", mappedEntityIds)+ .execute()+ : []+ ).map((r) => [r.id, r.locationId])+ );+// Storage unit names are unique per location// (storageUnit_name_locationId_key), so the natural key for both in-file// dedup and match-existing-to-update is (locationId + lowercased name).@@ -2667,6 +2683,48 @@ serve(async (req: Request) => {});continue;}+ // Resolve the row to an existing unit. A csv-id match is honored only+ // when it is in the SAME location the row states — a storage unit's+ // location is immutable via import. Updates never touch locationId, so+ // a cross-location id match would otherwise silently rename the unit in+ // its old location (ignoring the CSV Location) or crash the whole+ // import on storageUnit_name_locationId_key.+ const matchedByCsvId = id ? externalIdMap.get(id) : undefined;+ const csvIdLocation =+ matchedByCsvId !== undefined+ ? entityLocation.get(matchedByCsvId)+ : undefined;+ if (+ matchedByCsvId !== undefined &&+ csvIdLocation !== undefined &&+ csvIdLocation !== locationId+ ) {+ summary.errors.push({+ row: rowIndex,+ reason: `Unique ID "${id}" already belongs to a storage unit in a different location; import cannot move a storage unit between locations`,+ });+ continue;+ }+ const matchedByName =+ matchedByCsvId === undefined ? naturalKeyMap.get(key) : undefined;+ // An id-matched update renames the unit to `name`. If a DIFFERENT unit+ // in this location already owns that name, the rename would violate+ // storageUnit_name_locationId_key — report it instead of crashing.+ if (matchedByCsvId !== undefined) {+ const nameOwner = naturalKeyMap.get(key);+ if (nameOwner !== undefined && nameOwner !== matchedByCsvId) {+ summary.errors.push({+ row: rowIndex,+ reason: `A different storage unit named "${name}" already exists in this location`,+ });+ continue;+ }+ }+ const existingEntityId = matchedByCsvId ?? matchedByName;++ // Row is valid — claim its dedup slots now (after the guards, so a+ // rejected row never blocks a later legitimate row with the same+ // name/id from being processed).seenNaturalKeys.add(key);if (id) seenCsvIds.add(id);@@ -2684,11 +2742,6 @@ serve(async (req: Request) => {});}- const matchedByCsvId = id ? externalIdMap.get(id) : undefined;- const matchedByName =- matchedByCsvId === undefined ? naturalKeyMap.get(key) : undefined;- const existingEntityId = matchedByCsvId ?? matchedByName;-if (existingEntityId !== undefined) {// Deliberately does not touch locationId (the match/natural key)// to avoid the "cannot move a unit with children" interceptor.