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: prefill default contact ids

Sidwebworks1750f543fe1c
diff --git a/packages/database/supabase/migrations/20251224160904_contacts_simplifications.sql b/packages/database/supabase/migrations/20251224160904_contacts_simplifications.sql
index 848b60f23e..c375c24e1b 100644
--- a/packages/database/supabase/migrations/20251224160904_contacts_simplifications.sql
+++ b/packages/database/supabase/migrations/20251224160904_contacts_simplifications.sql
@@ -103,29 +103,19 @@ c AS (
FROM src
ON CONFLICT DO NOTHING
RETURNING id
-),
-
-cc AS (
- INSERT INTO "customerContact" (
- "id",
- "customerId",
- "contactId"
- )
- SELECT
- xid(),
- src."customerId",
- c.id
- FROM c
- JOIN src
- ON src.contact_id = c.id
- ON CONFLICT DO NOTHING
- RETURNING id, "customerId"
)
--- Update customer table to set salesContactId and invoicingContactId
-UPDATE customer cu
-SET "salesContactId" = cc.id, "invoicingContactId" = cc.id
-FROM cc
-WHERE cu."id" = cc."customerId";
-
+INSERT INTO "customerContact" (
+ "id",
+ "customerId",
+ "contactId"
+)
+SELECT
+ xid(),
+ src."customerId",
+ c.id
+FROM c
+JOIN src
+ ON src.contact_id = c.id
+ON CONFLICT DO NOTHING;
-- Customer table simplifications and contact email adjustments end
diff --git a/packages/database/supabase/migrations/20251225113440_supplier_simplifications.sql b/packages/database/supabase/migrations/20251225113440_supplier_simplifications.sql
index 659e09ca4a..5195e959d1 100644
--- a/packages/database/supabase/migrations/20251225113440_supplier_simplifications.sql
+++ b/packages/database/supabase/migrations/20251225113440_supplier_simplifications.sql
@@ -107,27 +107,19 @@ c AS (
FROM src
ON CONFLICT DO NOTHING
RETURNING id
-),
-sc AS (
- INSERT INTO "supplierContact" (
- "id",
- "supplierId",
- "contactId"
- )
- SELECT
- xid(),
- src."supplierId",
- c.id
- FROM c
- JOIN src
- ON src.contact_id = c.id
- ON CONFLICT DO NOTHING
- RETURNING id, "supplierId"
)
-UPDATE supplier su
-SET "purchasingContactId" = sc.id, "invoicingContactId" = sc.id
-FROM sc
-WHERE su."id" = sc."supplierId";
-
+INSERT INTO "supplierContact" (
+ "id",
+ "supplierId",
+ "contactId"
+)
+SELECT
+ xid(),
+ src."supplierId",
+ c.id
+FROM c
+JOIN src
+ ON src.contact_id = c.id
+ON CONFLICT DO NOTHING;
-- Supplier table simplifications and contact email adjustments end
\ No newline at end of file
diff --git a/packages/database/supabase/migrations/20251226171007_prefill_default_contacts.sql b/packages/database/supabase/migrations/20251226171007_prefill_default_contacts.sql
new file mode 100644
index 0000000000..09ee93c1dc
--- /dev/null
+++ b/packages/database/supabase/migrations/20251226171007_prefill_default_contacts.sql
@@ -0,0 +1,54 @@
+-- Prefill default contacts for customers and suppliers based on phone/fax matches
+
+WITH custs AS (
+ SELECT DISTINCT ON (cu.id)
+ cu.id AS "customerId",
+ cc."id" AS "contactId"
+ FROM customer cu
+ JOIN "customerContact" cc
+ ON cu.id = cc."customerId"
+ JOIN contact c
+ ON c.id = cc."contactId"
+ WHERE
+ (c."mobilePhone" IS NOT NULL AND c."mobilePhone" = cu.phone)
+ OR
+ (c.fax IS NOT NULL AND c.fax = cu.fax)
+ ORDER BY
+ cu.id,
+ (c."mobilePhone" = cu.phone) DESC,
+ cc.id
+)
+
+UPDATE customer cu
+SET
+ "salesContactId" = e."contactId",
+ "invoicingContactId" = e."contactId"
+FROM custs e
+WHERE cu.id = e."customerId";
+
+
+WITH sups AS (
+ SELECT DISTINCT ON (su.id)
+ su.id AS "supplierId",
+ sc."id" AS "contactId"
+ FROM supplier su
+ JOIN "supplierContact" sc
+ ON su.id = sc."supplierId"
+ JOIN contact c
+ ON c.id = sc."contactId"
+ WHERE
+ (c."workPhone" IS NOT NULL AND c."workPhone" = su.phone)
+ OR
+ (c.fax IS NOT NULL AND c.fax = su.fax)
+ ORDER BY
+ su.id,
+ (c."workPhone" = su.phone) DESC,
+ sc.id
+)
+
+UPDATE supplier su
+SET
+ "purchasingContactId" = s."contactId",
+ "invoicingContactId" = s."contactId"
+FROM sups s
+WHERE su.id = s."supplierId";