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(inventory): make receipt Pending transition atomic with voided guard

sidgaikwadbbe8dfd6a97a
diff --git a/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx b/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
index b667650d3a..45034db3fc 100644
--- a/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
+++ b/apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx
@@ -164,13 +164,20 @@ export async function action({ request, params }: ActionFunctionArgs) {
lines: lines ?? []
});
+ // Make the transition to Pending atomic with the voided guard above: the
+ // early check and this update are separated by rule evaluation + reconcile,
+ // so a concurrent void could slip in between. Conditioning the write on the
+ // status still not being "Voided" (and detecting a zero-row match) closes
+ // that race — a voided receipt won't be flipped back to Pending and reposted.
const setPendingState = await client
.from("receipt")
.update({
status: "Pending"
})
.eq("id", receiptId)
- .eq("companyId", companyId);
+ .eq("companyId", companyId)
+ .neq("status", "Voided")
+ .select("id");
if (setPendingState.error) {
throw redirect(
@@ -182,6 +189,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
+ if (!setPendingState.data?.length) {
+ throw redirect(
+ path.to.receipt(receiptId),
+ await flash(request, error(null, "Cannot post a voided receipt"))
+ );
+ }
+
try {
const receiptMetadata = await serviceRole
.from("receipt")