Skip to content

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.

fix(mes): show the unit and limits when recording a Measurement step (#1981)

BettrCallRaul1221665cc29f
diff --git a/apps/mes/app/components/JobOperation/components/BatchRecordModal.tsx b/apps/mes/app/components/JobOperation/components/BatchRecordModal.tsx
index cbaa8e3e73..882fa7a7d5 100644
--- a/apps/mes/app/components/JobOperation/components/BatchRecordModal.tsx
+++ b/apps/mes/app/components/JobOperation/components/BatchRecordModal.tsx
@@ -29,6 +29,7 @@ import type { BatchStep } from "~/services/operations.service";
import { usePeople } from "~/stores";
import { path } from "~/utils/path";
import FileDropzone from "../../FileDropzone";
+import { useMeasurementLimits } from "./Step";
// How a step type is captured in the grid: a typed cell per job (values differ
// part to part), or a tick per job with one shared payload (a task done, a
@@ -260,10 +261,10 @@ export function BatchRecordModal({
);
};
- const spec =
- step.minValue !== null && step.maxValue !== null
- ? t`Between ${step.minValue} and ${step.maxValue} ${step.unitOfMeasureCode ?? ""}`
- : null;
+ // The column header carries the unit; the limits read as in the job's own
+ // record window.
+ const limits = useMeasurementLimits(step);
+ const spec = kind === "number" ? limits : null;
return (
<Modal
diff --git a/apps/mes/app/components/JobOperation/components/Step.tsx b/apps/mes/app/components/JobOperation/components/Step.tsx
index 293ec8693c..9634e095b9 100644
--- a/apps/mes/app/components/JobOperation/components/Step.tsx
+++ b/apps/mes/app/components/JobOperation/components/Step.tsx
@@ -63,7 +63,10 @@ import { DateTime } from "~/components";
import { ProcedureStepTypeIcon } from "~/components/Icons";
import ItemThumbnail from "~/components/ItemThumbnail";
import { useUser } from "~/hooks";
-import { stepRecordValidator } from "~/services/models";
+import {
+ measurementStepRecordValidator,
+ stepRecordValidator
+} from "~/services/models";
import type { JobOperationStep } from "~/services/types";
import { useItems, usePeople } from "~/stores";
import { getPrivateUrl, path } from "~/utils/path";
@@ -97,6 +100,24 @@ export function hasStepDescription(
return documentHasImages(doc);
}
+// The limits a Measurement must fall within, in its unit, as the step lists
+// and the record windows state them.
+export function useMeasurementLimits(step: {
+ minValue: number | null;
+ maxValue: number | null;
+ unitOfMeasureCode: string | null;
+}) {
+ const { t } = useLingui();
+ const { minValue, maxValue } = step;
+ const unitOfMeasureCode = step.unitOfMeasureCode ?? "";
+ if (minValue !== null && maxValue !== null) {
+ return t`Must be between ${minValue} and ${maxValue} ${unitOfMeasureCode}`;
+ }
+ if (minValue !== null) return t`Must be > ${minValue} ${unitOfMeasureCode}`;
+ if (maxValue !== null) return t`Must be < ${maxValue} ${unitOfMeasureCode}`;
+ return null;
+}
+
export function StepsListItem({
activeStep,
step,
@@ -116,9 +137,8 @@ export function StepsListItem({
}) {
const fetcher = useFetcher<{ success: boolean }>();
const user = useUser();
- const { t } = useLingui();
- const { name, description, type, unitOfMeasureCode, minValue, maxValue } =
- step;
+ const { name, description, type } = step;
+ const limits = useMeasurementLimits(step);
const hasDescription = hasStepDescription(description);
const disclosure = useDisclosure({
@@ -145,15 +165,7 @@ export function StepsListItem({
</span>
</HStack>
{type === "Measurement" && (
- <span className="text-xs text-muted-foreground">
- {minValue !== null && maxValue !== null
- ? t`Must be between ${minValue} and ${maxValue} ${unitOfMeasureCode}`
- : minValue !== null
- ? t`Must be > ${minValue} ${unitOfMeasureCode}`
- : maxValue !== null
- ? t`Must be < ${maxValue} ${unitOfMeasureCode}`
- : null}
- </span>
+ <span className="text-xs text-muted-foreground">{limits}</span>
)}
</VStack>
{!compact && (
@@ -492,6 +504,7 @@ export function RecordModal({
const { t } = useLingui();
const { carbon } = useCarbon();
const { company } = useUser();
+ const limits = useMeasurementLimits(attribute);
const [file, setFile] = useState<File | null>(null);
const [filePath, setFilePath] = useState<string | null>(null);
// Bumped on every drop/remove so a stale in-flight upload can't set state
@@ -559,7 +572,11 @@ export function RecordModal({
<ModalContent>
<ValidatedForm
method="post"
- validator={stepRecordValidator}
+ validator={
+ attribute.type === "Measurement"
+ ? measurementStepRecordValidator
+ : stepRecordValidator
+ }
action={path.to.record}
defaultValues={{
index: activeStep,
@@ -567,7 +584,9 @@ export function RecordModal({
value:
record?.value ??
(attribute.type === "Timestamp" ? new Date().toISOString() : ""),
- numericValue: record?.numericValue ?? 0,
+ // Empty until recorded: a prefilled 0 could be recorded as a
+ // real measurement.
+ numericValue: record?.numericValue ?? undefined,
userValue: record?.userValue ?? ""
}}
fetcher={fetcher}
@@ -614,7 +633,12 @@ export function RecordModal({
<InputField name="value" label="" size="lg" />
)}
{attribute.type === "Measurement" && (
- <Number name="numericValue" label="" size="lg" />
+ <Number
+ name="numericValue"
+ label={attribute.unitOfMeasureCode ?? ""}
+ helperText={limits ?? undefined}
+ size="lg"
+ />
)}
{attribute.type === "Timestamp" && (
<DateTimePicker name="value" label="" size="lg" />
diff --git a/apps/mes/app/routes/x+/record.tsx b/apps/mes/app/routes/x+/record.tsx
index 721bb95333..91bde9161b 100644
--- a/apps/mes/app/routes/x+/record.tsx
+++ b/apps/mes/app/routes/x+/record.tsx
@@ -36,6 +36,10 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId
});
+ if (attributeRecord.refusal) {
+ return data({}, await flash(request, error(null, attributeRecord.refusal)));
+ }
+
if (attributeRecord.error) {
return data(
{},
diff --git a/apps/mes/app/services/models.ts b/apps/mes/app/services/models.ts
index 8b6a0cf6e4..eda0eac4c5 100644
--- a/apps/mes/app/services/models.ts
+++ b/apps/mes/app/services/models.ts
@@ -124,6 +124,12 @@ export const stepRecordValidator = z.object({
userValue: zfd.text(z.string().optional())
});
+// The record window starts a Measurement step empty, so recording one needs a
+// number: an empty field must not record a blank measurement.
+export const measurementStepRecordValidator = stepRecordValidator.extend({
+ numericValue: zfd.numeric(z.number({ error: "Enter the measurement" }))
+});
+
// One step recorded for several batch members at once (the batch view's
// Record). Rows are the members the operator filled in; unchanged ones are
// never sent, so a re-open can't re-trigger a step's backflush.
diff --git a/apps/mes/app/services/operations.service.ts b/apps/mes/app/services/operations.service.ts
index 29cc403cab..52b09c6112 100644
--- a/apps/mes/app/services/operations.service.ts
+++ b/apps/mes/app/services/operations.service.ts
@@ -2173,6 +2173,8 @@ export async function getWorkCentersByCompany(
return { data: mergedData, error: null };
}
+// `refusal` is a message for the operator: a Measurement step was sent with no
+// value.
export async function insertAttributeRecord(
client: SupabaseClient<Database>,
data: z.infer<typeof stepRecordValidator> & {
@@ -2185,23 +2187,33 @@ export async function insertAttributeRecord(
// step id would overwrite — and re-tenant — another company's record.
const step = await client
.from("jobOperationStep")
- .select("id")
+ .select("id, type")
.eq("id", data.jobOperationStepId)
.eq("companyId", data.companyId)
.maybeSingle();
- if (step.error) return step;
+ if (step.error) return { error: step.error, refusal: null };
if (!step.data) {
log.warn("insertAttributeRecord: step not found in company", {
companyId: data.companyId,
jobOperationStepId: data.jobOperationStepId
});
- return notFoundResponse("Job operation step not found");
+ return {
+ error: notFoundResponse("Job operation step not found").error,
+ refusal: null
+ };
+ }
+
+ // The record window refuses an empty measurement (measurementStepRecordValidator);
+ // this route validates with the general validator, so repeat the rule here.
+ if (step.data.type === "Measurement" && data.numericValue === undefined) {
+ return { error: null, refusal: "Enter the measurement" };
}
- return client.from("jobOperationStepRecord").upsert(data, {
+ const record = await client.from("jobOperationStepRecord").upsert(data, {
onConflict: "jobOperationStepId, index",
ignoreDuplicates: false
});
+ return { error: record.error, refusal: null };
}
// The batch view's Record: one step written for several members in one upsert,
diff --git a/packages/locale/locales/de/mes.po b/packages/locale/locales/de/mes.po
index 2b2a5242a3..db330a4e8c 100644
--- a/packages/locale/locales/de/mes.po
+++ b/packages/locale/locales/de/mes.po
@@ -303,12 +303,6 @@ msgstr "Los-Gesamtbetrag"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Hinter dem Ziel um {daysBehindTarget} Tag(e)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Zwischen {0} und {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Blockiert durch {0}"
diff --git a/packages/locale/locales/en/mes.po b/packages/locale/locales/en/mes.po
index 3f9bfa8790..d6bf15cf9c 100644
--- a/packages/locale/locales/en/mes.po
+++ b/packages/locale/locales/en/mes.po
@@ -303,12 +303,6 @@ msgstr "batch total"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Behind target by {daysBehindTarget} day(s)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Between {0} and {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Blocked by {0}"
diff --git a/packages/locale/locales/es/mes.po b/packages/locale/locales/es/mes.po
index 14225f233c..266575a2b8 100644
--- a/packages/locale/locales/es/mes.po
+++ b/packages/locale/locales/es/mes.po
@@ -303,12 +303,6 @@ msgstr "total del lote de producción"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Retrasado por {daysBehindTarget} día(s)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Entre {0} y {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Bloqueado por {0}"
diff --git a/packages/locale/locales/fr/mes.po b/packages/locale/locales/fr/mes.po
index 1b948d04ef..7fc88c51a4 100644
--- a/packages/locale/locales/fr/mes.po
+++ b/packages/locale/locales/fr/mes.po
@@ -303,12 +303,6 @@ msgstr "total du lot de production"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "En retard de {daysBehindTarget} jour(s)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Entre {0} et {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Bloqué par {0}"
diff --git a/packages/locale/locales/hi/mes.po b/packages/locale/locales/hi/mes.po
index b4627a1c6f..c3cae44041 100644
--- a/packages/locale/locales/hi/mes.po
+++ b/packages/locale/locales/hi/mes.po
@@ -303,12 +303,6 @@ msgstr "बैच कुल"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "लक्ष्य से {daysBehindTarget} दिन(दिन) पीछे"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "{0} और {1} के बीच {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "{0} द्वारा अवरुद्ध"
diff --git a/packages/locale/locales/it/mes.po b/packages/locale/locales/it/mes.po
index b295c66659..a0442afb78 100644
--- a/packages/locale/locales/it/mes.po
+++ b/packages/locale/locales/it/mes.po
@@ -303,12 +303,6 @@ msgstr "totale Lotto di produzione"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "In ritardo rispetto al target di {daysBehindTarget} giorno(i)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Tra {0} e {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Bloccato da {0}"
diff --git a/packages/locale/locales/ja/mes.po b/packages/locale/locales/ja/mes.po
index ee0a5905cf..ee1946a7b1 100644
--- a/packages/locale/locales/ja/mes.po
+++ b/packages/locale/locales/ja/mes.po
@@ -303,12 +303,6 @@ msgstr "バッチ合計"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "目標より{daysBehindTarget}日遅れ"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "{0}から{1} {2}まで"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "{0} によりブロック中"
diff --git a/packages/locale/locales/ko/mes.po b/packages/locale/locales/ko/mes.po
index 9eff3929db..a23b726427 100644
--- a/packages/locale/locales/ko/mes.po
+++ b/packages/locale/locales/ko/mes.po
@@ -303,12 +303,6 @@ msgstr "배치 총계"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "목표에서 {daysBehindTarget}일 뒤짐"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "{0}과 {1} 사이의 {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "{0}에 의해 차단됨"
diff --git a/packages/locale/locales/pl/mes.po b/packages/locale/locales/pl/mes.po
index d38ab21a12..30e1d7ce69 100644
--- a/packages/locale/locales/pl/mes.po
+++ b/packages/locale/locales/pl/mes.po
@@ -303,12 +303,6 @@ msgstr "razem partia produkcyjna"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Opóźnione o {daysBehindTarget} dni"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Między {0} a {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Zablokowane przez {0}"
diff --git a/packages/locale/locales/pt/mes.po b/packages/locale/locales/pt/mes.po
index b614ecef3f..955402a435 100644
--- a/packages/locale/locales/pt/mes.po
+++ b/packages/locale/locales/pt/mes.po
@@ -303,12 +303,6 @@ msgstr "total do lote de produção"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Atraso de {daysBehindTarget} dia(s)"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Entre {0} e {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Bloqueado por {0}"
diff --git a/packages/locale/locales/ru/mes.po b/packages/locale/locales/ru/mes.po
index c415f280eb..2fbbddeb59 100644
--- a/packages/locale/locales/ru/mes.po
+++ b/packages/locale/locales/ru/mes.po
@@ -303,12 +303,6 @@ msgstr "итого серии"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Отставание на {daysBehindTarget} дн."
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "Между {0} и {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Заблокировано {0}"
diff --git a/packages/locale/locales/tr/mes.po b/packages/locale/locales/tr/mes.po
index 31bd786cf1..e7f115018d 100644
--- a/packages/locale/locales/tr/mes.po
+++ b/packages/locale/locales/tr/mes.po
@@ -303,12 +303,6 @@ msgstr "parti toplamı"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "Hedeften {daysBehindTarget} gün geride"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "{0} ile {1} arasında {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "Engelli edildi: {0}"
diff --git a/packages/locale/locales/zh/mes.po b/packages/locale/locales/zh/mes.po
index e6895b8ef2..edb79e2040 100644
--- a/packages/locale/locales/zh/mes.po
+++ b/packages/locale/locales/zh/mes.po
@@ -303,12 +303,6 @@ msgstr "生产批次总计"
msgid "Behind target by {daysBehindTarget} day(s)"
msgstr "落后于目标 {daysBehindTarget} 天"
-#. placeholder {0}: step.minValue
-#. placeholder {1}: step.maxValue
-#. placeholder {2}: step.unitOfMeasureCode ?? ""
-msgid "Between {0} and {1} {2}"
-msgstr "{0} 至 {1} {2}"
-
#. placeholder {0}: column.blockingDispatchReadableId
msgid "Blocked by {0}"
msgstr "被 {0} 阻塞"