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(issue-edge-fn): handle split-duplicated trackedEntity in jobCompleteInventory + surface real error

Sidwebworks8d4dabbb2f6c
diff --git a/apps/erp/app/routes/x+/job+/$jobId.complete.tsx b/apps/erp/app/routes/x+/job+/$jobId.complete.tsx
index 3f1fd8875d..e79a25126d 100644
--- a/apps/erp/app/routes/x+/job+/$jobId.complete.tsx
+++ b/apps/erp/app/routes/x+/job+/$jobId.complete.tsx
@@ -7,6 +7,7 @@ import { msg } from "@lingui/core/macro";
import type { ActionFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { jobCompleteValidator } from "~/modules/production";
+import { getEdgeFunctionErrorMessage } from "~/utils/error";
import type { Handle } from "~/utils/handle";
import { path, requestReferrer } from "~/utils/path";
@@ -128,12 +129,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
});
if (issue.error) {
+ const message = await getEdgeFunctionErrorMessage(
+ issue.error,
+ "Failed to receive leftovers to inventory"
+ );
throw redirect(
requestReferrer(request) ?? path.to.job(jobId),
- await flash(
- request,
- error(issue.error, "Failed to receive leftovers to inventory")
- )
+ await flash(request, error(issue.error, message))
);
}
}
@@ -153,9 +155,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
});
if (issue.error) {
+ const message = await getEdgeFunctionErrorMessage(
+ issue.error,
+ "Failed to complete job"
+ );
throw redirect(
requestReferrer(request) ?? path.to.job(jobId),
- await flash(request, error(issue.error, "Failed to complete job"))
+ await flash(request, error(issue.error, message))
);
}
}
diff --git a/apps/erp/app/utils/error.ts b/apps/erp/app/utils/error.ts
new file mode 100644
index 0000000000..0e772945ee
--- /dev/null
+++ b/apps/erp/app/utils/error.ts
@@ -0,0 +1,35 @@
+/**
+ * Extract the user-facing error message from a Supabase
+ * `functions.invoke()` failure.
+ *
+ * `supabase-js` wraps non-2xx edge-function responses in `FunctionsHttpError`
+ * where the response body lives on `error.context: Response`. The body is
+ * never parsed by the SDK, so callers that just read `error.message` get the
+ * generic wrapper text ("Edge Function returned a non-2xx status code") and
+ * lose the real message we set inside the edge function (e.g.
+ * `{ success: false, message: "Tracked entity not found" }`).
+ *
+ * Mirrors the pattern in
+ * `apps/mes/app/routes/x+/issue-tracked-entity.tsx` so error surfacing is
+ * consistent across the two apps.
+ */
+export async function getEdgeFunctionErrorMessage(
+ err: unknown,
+ fallback: string
+): Promise<string> {
+ const ctx = (err as { context?: Response })?.context;
+ if (ctx && typeof ctx.clone === "function") {
+ try {
+ const body = await ctx.clone().json();
+ if (body && typeof body.message === "string") {
+ return body.message;
+ }
+ } catch {
+ // body wasn't JSON or already consumed — fall through
+ }
+ }
+ if (err && typeof (err as { message?: string }).message === "string") {
+ return (err as { message: string }).message;
+ }
+ return fallback;
+}
diff --git a/packages/database/supabase/functions/issue/index.ts b/packages/database/supabase/functions/issue/index.ts
index 522e93ddb1..206b023171 100644
--- a/packages/database/supabase/functions/issue/index.ts
+++ b/packages/database/supabase/functions/issue/index.ts
@@ -967,11 +967,21 @@ serve(async (req: Request) => {
.execute();
if (jobMakeMethod.requiresBatchTracking) {
+ // A single jobMakeMethod can have multiple trackedEntity rows
+ // sharing attributes->>'Job Make Method' once a split has occurred:
+ // the split flow (see line ~2604) copies the parent's full
+ // `attributes` blob onto the child. The parent gets marked
+ // Consumed and tagged with `Split Entity ID`. Filter Consumed
+ // and pick the newest live row to mirror the serial branch
+ // below, which already does `.neq("status", "Consumed")`.
const trackedEntity = await client
.from("trackedEntity")
.select("*")
.eq("attributes->>Job Make Method", jobMakeMethod.id!)
- .single();
+ .neq("status", "Consumed")
+ .order("createdAt", { ascending: false })
+ .limit(1)
+ .maybeSingle();
if (!trackedEntity.data) {
throw new Error("Tracked entity not found");
diff --git a/packages/locale/locales/de/erp.po b/packages/locale/locales/de/erp.po
index a48654c602..e39679c88b 100644
--- a/packages/locale/locales/de/erp.po
+++ b/packages/locale/locales/de/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Job-Traveler-PDFs enthalten Arbeitsanweisungen."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/en/erp.po b/packages/locale/locales/en/erp.po
index 4434ea5ef7..e42c8ac3b6 100644
--- a/packages/locale/locales/en/erp.po
+++ b/packages/locale/locales/en/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Job traveler PDFs include work instructions."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/es/erp.po b/packages/locale/locales/es/erp.po
index 6ac67dcfa3..0f57125518 100644
--- a/packages/locale/locales/es/erp.po
+++ b/packages/locale/locales/es/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Los PDF del viajero de trabajo incluyen instrucciones de trabajo."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/fr/erp.po b/packages/locale/locales/fr/erp.po
index 454548235f..803c7598ed 100644
--- a/packages/locale/locales/fr/erp.po
+++ b/packages/locale/locales/fr/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Les PDF de bon de travail incluent les instructions de travail."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/hi/erp.po b/packages/locale/locales/hi/erp.po
index 89c0b92bb7..f78b5ae052 100644
--- a/packages/locale/locales/hi/erp.po
+++ b/packages/locale/locales/hi/erp.po
@@ -6980,7 +6980,7 @@ msgstr "जॉब ट्रैवलर पीडीएफ में कार
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/it/erp.po b/packages/locale/locales/it/erp.po
index 18ae5431ef..31d50a142f 100644
--- a/packages/locale/locales/it/erp.po
+++ b/packages/locale/locales/it/erp.po
@@ -6980,7 +6980,7 @@ msgstr "I PDF del foglio di lavoro includono istruzioni di lavoro."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/ja/erp.po b/packages/locale/locales/ja/erp.po
index 6ce8f996d0..77e6950687 100644
--- a/packages/locale/locales/ja/erp.po
+++ b/packages/locale/locales/ja/erp.po
@@ -6980,7 +6980,7 @@ msgstr "ジョブトラベラーPDFに作業指示が含まれています。"
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/pl/erp.po b/packages/locale/locales/pl/erp.po
index 3784e7431e..b017f03e18 100644
--- a/packages/locale/locales/pl/erp.po
+++ b/packages/locale/locales/pl/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Pliki PDF podróżnika zadania zawierają instrukcje pracy."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/pt/erp.po b/packages/locale/locales/pt/erp.po
index 37eda268f3..a583eee2ab 100644
--- a/packages/locale/locales/pt/erp.po
+++ b/packages/locale/locales/pt/erp.po
@@ -6980,7 +6980,7 @@ msgstr "Os PDFs do viajante de trabalho incluem instruções de trabalho."
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/ru/erp.po b/packages/locale/locales/ru/erp.po
index af698c0e32..b86bd1f3e9 100644
--- a/packages/locale/locales/ru/erp.po
+++ b/packages/locale/locales/ru/erp.po
@@ -6980,7 +6980,7 @@ msgstr "PDF-документы маршрутного листа включаю
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249
diff --git a/packages/locale/locales/zh/erp.po b/packages/locale/locales/zh/erp.po
index 7ea4274503..e991fbd65e 100644
--- a/packages/locale/locales/zh/erp.po
+++ b/packages/locale/locales/zh/erp.po
@@ -6980,7 +6980,7 @@ msgstr "工作单据PDF包含工作说明。"
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:203
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx:293
#: apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx:489
-#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:14
+#: apps/erp/app/routes/x+/job+/$jobId.complete.tsx:15
#: apps/erp/app/routes/x+/job+/$jobId.tsx:36
#: apps/erp/app/routes/x+/job+/new.tsx:28
#: apps/erp/app/routes/x+/part+/$itemId.tsx:249