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.

- change logic to update opportunity.customerId when customer is changed

Bakuto7099b1f106fb
diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.ts
index e9e2c1f597..6d3cb299d4 100644
--- a/apps/erp/app/modules/sales/sales.service.ts
+++ b/apps/erp/app/modules/sales/sales.service.ts
@@ -2118,13 +2118,13 @@ export async function upsertQuote(
// Only update the exchange rate if the currency code has changed
const existingQuote = await client
.from("quote")
- .select("companyId, currencyCode")
+ .select("companyId, currencyCode, opportunityId")
.eq("id", quote.id)
.single();
if (existingQuote.error) return existingQuote;
- const { companyId, currencyCode } = existingQuote.data;
+ const { companyId, currencyCode, opportunityId } = existingQuote.data;
if (quote.currencyCode && currencyCode !== quote.currencyCode) {
const currency = await getCurrencyByCode(
@@ -2137,6 +2137,15 @@ export async function upsertQuote(
quote.exchangeRateUpdatedAt = new Date().toISOString();
}
}
+
+ // If customerId is being updated, also update the opportunity's customerId
+ if (quote.customerId && opportunityId) {
+ await client
+ .from("opportunity")
+ .update({ customerId: quote.customerId })
+ .eq("id", opportunityId);
+ }
+
return client
.from("quote")
.update({
@@ -2638,13 +2647,13 @@ export async function upsertSalesOrder(
// Only update the exchange rate if the currency code has changed
const existingSalesOrder = await client
.from("salesOrder")
- .select("companyId, currencyCode")
+ .select("companyId, currencyCode, opportunityId")
.eq("id", salesOrder.id)
.single();
if (existingSalesOrder.error) return existingSalesOrder;
- const { companyId, currencyCode } = existingSalesOrder.data;
+ const { companyId, currencyCode, opportunityId } = existingSalesOrder.data;
if (salesOrder.currencyCode && currencyCode !== salesOrder.currencyCode) {
const currency = await getCurrencyByCode(
@@ -2657,6 +2666,15 @@ export async function upsertSalesOrder(
salesOrder.exchangeRateUpdatedAt = new Date().toISOString();
}
}
+
+ // If customerId is being updated, also update the opportunity's customerId
+ if (salesOrder.customerId && opportunityId) {
+ await client
+ .from("opportunity")
+ .update({ customerId: salesOrder.customerId })
+ .eq("id", opportunityId);
+ }
+
return client
.from("salesOrder")
.update(sanitize(salesOrder))
@@ -2906,6 +2924,22 @@ export async function upsertSalesRFQ(
return insert;
} else {
+ // If customerId is being updated, also update the opportunity's customerId
+ if (rfq.customerId) {
+ const existingRfq = await client
+ .from("salesRfq")
+ .select("opportunityId")
+ .eq("id", rfq.id)
+ .single();
+
+ if (existingRfq.data?.opportunityId) {
+ await client
+ .from("opportunity")
+ .update({ customerId: rfq.customerId })
+ .eq("id", existingRfq.data.opportunityId);
+ }
+ }
+
return client
.from("salesRfq")
.update({
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 5b600a7db7..4931b8974d 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -72529,6 +72529,7 @@ export default {
"revisionId",
"status",
"currencyCode",
+ "customerId",
"companyId",
"createdAt",
"createdBy",
@@ -92664,6 +92665,7 @@ export default {
"quoteId",
"revisionId",
"status",
+ "customerId",
"companyId",
"createdAt",
"createdBy",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 969c8f332f..895015309d 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -25292,7 +25292,7 @@ export type Database = {
currencyCode: string | null
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string | null
+ customerId: string
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -25328,7 +25328,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -25364,7 +25364,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId?: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29345,7 +29345,7 @@ export type Database = {
currencyCode: string
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string | null
+ customerId: string
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -29377,7 +29377,7 @@ export type Database = {
currencyCode: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29409,7 +29409,7 @@ export type Database = {
currencyCode?: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId?: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 969c8f332f..895015309d 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -25292,7 +25292,7 @@ export type Database = {
currencyCode: string | null
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string | null
+ customerId: string
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -25328,7 +25328,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -25364,7 +25364,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId?: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29345,7 +29345,7 @@ export type Database = {
currencyCode: string
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string | null
+ customerId: string
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -29377,7 +29377,7 @@ export type Database = {
currencyCode: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29409,7 +29409,7 @@ export type Database = {
currencyCode?: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string | null
+ customerId?: string
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
diff --git a/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql b/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql
index f7189eef9c..896961e3d0 100644
--- a/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql
+++ b/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql
@@ -1,12 +1,9 @@
-- Make the customerId column nullable
ALTER TABLE "opportunity" ALTER COLUMN "customerId" DROP NOT NULL;
-ALTER TABLE "quote" ALTER COLUMN "customerId" DROP NOT NULL;
-ALTER TABLE "salesOrder" ALTER COLUMN "customerId" DROP NOT NULL;
-- Drop the existing foreign key constraint (currently ON DELETE CASCADE)
ALTER TABLE "opportunity" DROP CONSTRAINT IF EXISTS "opportunity_customerId_fkey";
-
-- Add the foreign key constraint with ON DELETE SET NULL
ALTER TABLE "opportunity" ADD CONSTRAINT "opportunity_customerId_fkey"
FOREIGN KEY ("customerId") REFERENCES "customer" ("id") ON DELETE SET NULL ON UPDATE CASCADE;