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(mes): keep an explicitly selected completed serial from snapping back
Sidwebworksbc420749f723
diff --git a/apps/mes/app/components/JobOperation/hooks/useOperation.tsx b/apps/mes/app/components/JobOperation/hooks/useOperation.tsxindex 13d0fd3c0b..f095a1a8e3 100644--- a/apps/mes/app/components/JobOperation/hooks/useOperation.tsx+++ b/apps/mes/app/components/JobOperation/hooks/useOperation.tsx@@ -16,6 +16,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { useParams, useRevalidator } from "react-router";import { useUrlParams, useUser } from "~/hooks";import { isSerialEntityIncompleteForOperation } from "~/services/operations.service";+import { shouldAdvanceToNextSerialUnit } from "~/services/serial-advancement";import type {JobMaterial,JobOperationParameter,@@ -303,8 +304,24 @@ export function useOperation({uncompletedEntities.some((entity) => entity.id === trackedEntityParam);if (isFirstOperation) {- // Auto-select the next unit once the selected one is done (or none yet).- if (!selectedIsIncomplete) onAdvanceToUnit?.(uncompletedEntities[0]);+ // Auto-advance is edge-triggered off the held unit: advance on arrival+ // with nothing selected, or when the held unit itself completes — never+ // over an explicit selection of an already-complete unit (going back to+ // review/re-print is allowed).+ if (+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: trackedEntityParam,+ selectedIsIncomplete,+ heldEntityId: heldEntityRef.current+ })+ ) {+ heldEntityRef.current = null;+ onAdvanceToUnit?.(uncompletedEntities[0]);+ } else {+ heldEntityRef.current = selectedIsIncomplete+ ? trackedEntityParam+ : null;+ }return;}diff --git a/apps/mes/app/services/operations.serial-advancement.test.ts b/apps/mes/app/services/operations.serial-advancement.test.tsnew file mode 100644index 0000000000..b9d187fefc--- /dev/null+++ b/apps/mes/app/services/operations.serial-advancement.test.ts@@ -0,0 +1,58 @@+import { describe, expect, it } from "vitest";+import { shouldAdvanceToNextSerialUnit } from "./serial-advancement";++describe("shouldAdvanceToNextSerialUnit (first operation)", () => {+ it("advances on arrival when nothing is selected", () => {+ expect(+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: null,+ selectedIsIncomplete: false,+ heldEntityId: null+ })+ ).toBe(true);+ });++ it("stays on a selected incomplete unit", () => {+ expect(+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: "SN-5",+ selectedIsIncomplete: true,+ heldEntityId: "SN-5"+ })+ ).toBe(false);+ });++ it("advances when the held unit completes", () => {+ expect(+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: "SN-5",+ selectedIsIncomplete: false,+ heldEntityId: "SN-5"+ })+ ).toBe(true);+ });++ it("does not override an explicit selection of a completed unit", () => {+ // The operator went back to SN-2 (already complete) while holding SN-5 —+ // the regression: this used to snap back to the next incomplete unit.+ expect(+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: "SN-2",+ selectedIsIncomplete: false,+ heldEntityId: "SN-5"+ })+ ).toBe(false);+ });++ it("keeps a completed selection stable across revalidations", () => {+ // After the explicit selection the held unit is cleared; realtime+ // revalidations re-run the decision and must not start advancing.+ expect(+ shouldAdvanceToNextSerialUnit({+ selectedEntityId: "SN-2",+ selectedIsIncomplete: false,+ heldEntityId: null+ })+ ).toBe(false);+ });+});diff --git a/apps/mes/app/services/serial-advancement.ts b/apps/mes/app/services/serial-advancement.tsnew file mode 100644index 0000000000..1d4dd309c1--- /dev/null+++ b/apps/mes/app/services/serial-advancement.ts@@ -0,0 +1,23 @@+// Standalone (like allocation.ts) so unit tests can import it without pulling+// the operations service barrel, whose transitive imports need macro transforms.++/**+ * First-operation auto-advance decision (`useOperation`). On the first+ * operation units flow #1..#N without scanning, so we auto-select the next+ * incomplete unit — but only on arrival with nothing selected, or when the+ * unit the operator was holding itself completes. An explicit selection of an+ * already-complete unit (going back to review or re-print a label) must never+ * be overridden.+ */+export function shouldAdvanceToNextSerialUnit({+ selectedEntityId,+ selectedIsIncomplete,+ heldEntityId+}: {+ selectedEntityId: string | null;+ selectedIsIncomplete: boolean;+ heldEntityId: string | null;+}): boolean {+ if (selectedIsIncomplete) return false;+ return !selectedEntityId || selectedEntityId === heldEntityId;+}