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.

feat: udpate foreign keys to make emails unique

Brad Barbincd815a82cf4f
diff --git a/apps/carbon/app/modules/purchasing/purchasing.service.ts b/apps/carbon/app/modules/purchasing/purchasing.service.ts
index 2a8a81121d..cad53c4ba5 100644
--- a/apps/carbon/app/modules/purchasing/purchasing.service.ts
+++ b/apps/carbon/app/modules/purchasing/purchasing.service.ts
@@ -60,11 +60,27 @@ export async function deleteSupplierContact(
supplierId: string,
supplierContactId: string
) {
- return client
+ const supplierContact = await client
.from("supplierContact")
- .delete()
+ .select("contactId")
.eq("supplierId", supplierId)
- .eq("id", supplierContactId);
+ .eq("id", supplierContactId)
+ .single();
+ if (supplierContact.data) {
+ const [contactDelete, supplierContactDelete] = await Promise.all([
+ client.from("contact").delete().eq("id", supplierContact.data.contactId),
+ client
+ .from("supplierContact")
+ .delete()
+ .eq("supplierId", supplierId)
+ .eq("id", supplierContactId),
+ ]);
+
+ if (contactDelete.error) {
+ return contactDelete;
+ }
+ return supplierContactDelete;
+ }
}
export async function deleteSupplierLocation(
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index ef5a3eae49..7c459b75db 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -101,11 +101,27 @@ export async function deleteCustomerContact(
customerId: string,
customerContactId: string
) {
- return client
+ const customerContact = await client
.from("customerContact")
- .delete()
+ .select("contactId")
.eq("customerId", customerId)
- .eq("id", customerContactId);
+ .eq("id", customerContactId)
+ .single();
+ if (customerContact.data) {
+ const [contactDelete, customerContactDelete] = await Promise.all([
+ client.from("contact").delete().eq("id", customerContact.data.contactId),
+ client
+ .from("customerContact")
+ .delete()
+ .eq("customerId", customerId)
+ .eq("id", customerContactId),
+ ]);
+
+ if (contactDelete.error) {
+ return contactDelete;
+ }
+ return customerContactDelete;
+ }
}
export async function deleteCustomerLocation(
diff --git a/apps/carbon/app/routes/x+/_layout.tsx b/apps/carbon/app/routes/x+/_layout.tsx
index 949f215637..1814e27051 100644
--- a/apps/carbon/app/routes/x+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/_layout.tsx
@@ -133,7 +133,7 @@ export default function AuthenticatedRoute() {
<div className="w-full h-full flex-1 overflow-hidden">
<main className="h-full flex flex-col flex-1 max-w-[100vw] sm:max-w-[calc(100vw-56px)] overflow-hidden bg-muted">
<Topbar />
- <main className="flex-1 overflow-hidden h-[calc(100dvh-49px)]">
+ <main className="flex-1 overflow-y-auto scrollbar-hide h-[calc(100dvh-49px)]">
<Outlet />
</main>
</main>
diff --git a/packages/database/supabase/migrations/20241205132822_unique-contact-email.sql b/packages/database/supabase/migrations/20241205132822_unique-contact-email.sql
index c31ed9a2ca..e356f48ff7 100644
--- a/packages/database/supabase/migrations/20241205132822_unique-contact-email.sql
+++ b/packages/database/supabase/migrations/20241205132822_unique-contact-email.sql
@@ -1,3 +1,21 @@
+ALTER TABLE "quote" DROP CONSTRAINT IF EXISTS "quote_customerContactId_fkey",
+ADD CONSTRAINT "quote_customerContactId_fkey"
+FOREIGN KEY ("customerContactId")
+REFERENCES "contact"(id)
+ON DELETE SET NULL;
+
+ALTER TABLE "salesOrder" DROP CONSTRAINT IF EXISTS "salesOrder_customerContactId_fkey",
+ADD CONSTRAINT "salesOrder_customerContactId_fkey"
+FOREIGN KEY ("customerContactId")
+REFERENCES "contact"(id)
+ON DELETE SET NULL;
+
+ALTER TABLE "purchaseOrder" DROP CONSTRAINT IF EXISTS "purchaseOrder_supplierContactId_fkey",
+ADD CONSTRAINT "purchaseOrder_supplierContactId_fkey"
+FOREIGN KEY ("supplierContactId")
+REFERENCES "contact"(id)
+ON DELETE SET NULL;
+
WITH
duplicates AS (