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(sales): guard quote acceptance against replay and stale status

Sidwebworks2c9ef0dd8fce
diff --git a/apps/erp/app/routes/api+/sales.digital-quote.$id.tsx b/apps/erp/app/routes/api+/sales.digital-quote.$id.tsx
index 9050941484..c465094c60 100644
--- a/apps/erp/app/routes/api+/sales.digital-quote.$id.tsx
+++ b/apps/erp/app/routes/api+/sales.digital-quote.$id.tsx
@@ -39,6 +39,20 @@ export async function action(args: ActionFunctionArgs) {
quote.data.companyId
);
+ // A quote can only be accepted or rejected while it is awaiting a response.
+ // Without this guard the unauthenticated share link can be replayed to create
+ // duplicate sales orders, or to act on an already ordered / rejected / expired
+ // quote (e.g. flipping an ordered quote back to "Lost").
+ if (
+ (type === "accept" || type === "reject") &&
+ quote.data.status !== "Sent"
+ ) {
+ return {
+ success: false,
+ message: "This quote is no longer available for a response."
+ };
+ }
+
switch (type) {
case "accept":
const digitalQuoteAcceptedBy = String(
diff --git a/packages/database/supabase/functions/convert/index.ts b/packages/database/supabase/functions/convert/index.ts
index 867031199f..2718b3c050 100644
--- a/packages/database/supabase/functions/convert/index.ts
+++ b/packages/database/supabase/functions/convert/index.ts
@@ -672,7 +672,12 @@ serve(async (req: Request) => {
const newQuoteStatus: "Ordered" | "Partial" = hasZeroQuantityLines
? "Partial"
: "Ordered";
- await trx
+ // Atomic guard: only convert a quote that is still "Sent". Both the
+ // internal "convert to order" flow and the unauthenticated digital
+ // quote accept run from "Sent" only. If a concurrent or replayed
+ // request already converted it, 0 rows update here and the throw rolls
+ // the whole transaction back — no duplicate sales order is committed.
+ const quoteStatusUpdate = await trx
.updateTable("quote")
.set({
status: newQuoteStatus,
@@ -680,7 +685,14 @@ serve(async (req: Request) => {
digitalQuoteAcceptedByEmail: digitalQuoteAcceptedByEmail ?? null,
})
.where("id", "=", quote.data.id)
- .execute();
+ .where("status", "=", "Sent")
+ .executeTakeFirst();
+
+ if (Number(quoteStatusUpdate.numUpdatedRows ?? 0) === 0) {
+ throw new Error(
+ `Quote ${quote.data.id} is no longer in a convertible state`
+ );
+ }
const customerPartSeen = new Set<string>();
const customerPartToItemInserts = quoteLines.data