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): persist customer location and engineering contact when creating an RFQ (#1357)

Siddharth Gaikwada70e85f958b9
diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.ts
index f9278b5d8d..00f94f808e 100644
--- a/apps/erp/app/modules/sales/sales.service.ts
+++ b/apps/erp/app/modules/sales/sales.service.ts
@@ -5756,6 +5756,7 @@ export async function upsertSalesOrderPayment(
export async function insertSalesRFQ(
client: SupabaseClient<Database>,
+ db: Kysely<KyselyDatabase>,
input: {
customerId: string;
companyId: string;
@@ -5766,6 +5767,8 @@ export async function insertSalesRFQ(
locationId?: string;
salesPersonId?: string;
customerContactId?: string;
+ customerEngineeringContactId?: string;
+ customerLocationId?: string;
customerReference?: string;
status?: "Draft" | "Ready for Quote" | "Quoted" | "Closed";
notes?: string;
@@ -5796,46 +5799,60 @@ export async function insertSalesRFQ(
rfqId = seq.data;
}
- const opportunity = await client
- .from("opportunity")
- .insert({
- companyId: input.companyId,
- customerId: input.customerId
- })
- .select("id")
- .single();
-
- if (opportunity.error) return { data: null, error: opportunity.error };
+ const rfqDate =
+ input.rfqDate ??
+ datetime
+ .today(await getCompanyTimeZone(client, input.companyId))
+ .toString();
- const rfq = await client
- .from("salesRfq")
- .insert({
- rfqId,
- customerId: input.customerId,
- customerContactId: input.customerContactId,
- customerReference: input.customerReference,
- rfqDate:
- input.rfqDate ??
- datetime
- .today(await getCompanyTimeZone(client, input.companyId))
- .toString(),
- expirationDate: input.expirationDate,
- locationId: input.locationId,
- salesPersonId: input.salesPersonId,
- status: input.status ?? "Draft",
- internalNotes: input.notes ?? null,
- customFields: input.customFields,
- opportunityId: opportunity.data.id,
- companyId: input.companyId,
- createdBy: input.createdBy,
- updatedBy: input.createdBy
- })
- .select("id, rfqId")
- .single();
-
- if (rfq.error) return { data: null, error: rfq.error };
+ // The opportunity and the RFQ are created together or not at all — a failed
+ // RFQ insert must not leave an orphaned opportunity behind.
+ try {
+ const rfq = await db.transaction().execute(async (trx) => {
+ const opportunity = await trx
+ .insertInto("opportunity")
+ .values({
+ companyId: input.companyId,
+ customerId: input.customerId
+ })
+ .returning("id")
+ .executeTakeFirstOrThrow();
+
+ return trx
+ .insertInto("salesRfq")
+ .values({
+ rfqId,
+ customerId: input.customerId,
+ customerContactId: input.customerContactId,
+ customerEngineeringContactId: input.customerEngineeringContactId,
+ customerLocationId: input.customerLocationId,
+ customerReference: input.customerReference,
+ rfqDate,
+ expirationDate: input.expirationDate,
+ locationId: input.locationId,
+ salesPersonId: input.salesPersonId,
+ status: input.status ?? "Draft",
+ internalNotes: input.notes ?? null,
+ customFields: input.customFields,
+ opportunityId: opportunity.id,
+ companyId: input.companyId,
+ createdBy: input.createdBy,
+ updatedBy: input.createdBy
+ })
+ .returning(["id", "rfqId"])
+ .executeTakeFirstOrThrow();
+ });
- return { data: { id: rfq.data.id, rfqId: rfq.data.rfqId }, error: null };
+ return { data: { id: rfq.id, rfqId: rfq.rfqId }, error: null };
+ } catch (err) {
+ return {
+ data: null,
+ error: {
+ message:
+ err instanceof Error ? err.message : "Failed to insert sales RFQ"
+ } as PostgrestError
+ };
+ }
}
export async function updateSalesRFQ(
@@ -5845,6 +5862,8 @@ export async function updateSalesRFQ(
updatedBy: string;
customerId?: string;
customerContactId?: string | null;
+ customerEngineeringContactId?: string | null;
+ customerLocationId?: string | null;
customerReference?: string | null;
rfqDate?: string;
expirationDate?: string | null;
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
index e2a378422f..ffc1d3c601 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
@@ -1,5 +1,5 @@
{
- "generated": "2026-08-08T19:49:36.245Z",
+ "generated": "2026-08-09T00:18:51.796Z",
"totalTools": 1416,
"modules": 15,
"tools": [
@@ -38723,9 +38723,10 @@
"module": "sales",
"classification": "WRITE",
"description": "insert sales r f q",
- "paramCount": 11,
+ "paramCount": 13,
"serviceParams": [
"client",
+ "db",
"input"
],
"injectAuth": [
@@ -38757,6 +38758,12 @@
"customerContactId": {
"type": "string"
},
+ "customerEngineeringContactId": {
+ "type": "string"
+ },
+ "customerLocationId": {
+ "type": "string"
+ },
"customerReference": {
"type": "string"
},
@@ -38784,7 +38791,7 @@
"module": "sales",
"classification": "WRITE",
"description": "update sales r f q",
- "paramCount": 11,
+ "paramCount": 13,
"serviceParams": [
"client",
"input"
@@ -38808,6 +38815,18 @@
"null"
]
},
+ "customerEngineeringContactId": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "customerLocationId": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
"customerReference": {
"type": [
"string",
diff --git a/apps/erp/app/routes/x+/sales-rfq+/new.tsx b/apps/erp/app/routes/x+/sales-rfq+/new.tsx
index 1916ae59e6..2a98afc0c5 100644
--- a/apps/erp/app/routes/x+/sales-rfq+/new.tsx
+++ b/apps/erp/app/routes/x+/sales-rfq+/new.tsx
@@ -16,6 +16,7 @@ import {
upsertSalesRFQLine
} from "~/modules/sales";
import { SalesRFQForm } from "~/modules/sales/ui/SalesRFQ";
+import { getDatabaseClient } from "~/services/database.server";
import { setCustomFields } from "~/utils/form";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
@@ -39,7 +40,7 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const result = await insertSalesRFQ(client, {
+ const result = await insertSalesRFQ(client, getDatabaseClient(), {
...validation.data,
rfqId: validation.data.rfqId || undefined,
companyId,