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: make sure customerLocation and supplierLocation get deleted correctly

Rob Carrington613b829827d1
diff --git a/apps/carbon/app/modules/purchasing/purchasing.service.ts b/apps/carbon/app/modules/purchasing/purchasing.service.ts
index 05da40806c..46aa80aa65 100644
--- a/apps/carbon/app/modules/purchasing/purchasing.service.ts
+++ b/apps/carbon/app/modules/purchasing/purchasing.service.ts
@@ -72,11 +72,23 @@ export async function deleteSupplierLocation(
supplierId: string,
supplierLocationId: string
) {
- return client
+ const { data: supplierLocation } = await client
.from("supplierLocation")
- .delete()
+ .select("addressId")
.eq("supplierId", supplierId)
- .eq("id", supplierLocationId);
+ .eq("id", supplierLocationId)
+ .single();
+
+ if (supplierLocation?.addressId) {
+ return client.from("address").delete().eq("id", supplierLocation.addressId);
+ } else {
+ // The supplierLocation should always have an addressId, but just in case
+ return client
+ .from("supplierLocation")
+ .delete()
+ .eq("supplierId", supplierId)
+ .eq("id", supplierLocationId);
+ }
}
export async function deleteSupplierProcess(
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 155ee6fa31..57c6d05ee4 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -101,11 +101,23 @@ export async function deleteCustomerLocation(
customerId: string,
customerLocationId: string
) {
- return client
+ const { data: customerLocation } = await client
.from("customerLocation")
- .delete()
+ .select("addressId")
.eq("customerId", customerId)
- .eq("id", customerLocationId);
+ .eq("id", customerLocationId)
+ .single();
+
+ if (customerLocation?.addressId) {
+ return client.from("address").delete().eq("id", customerLocation.addressId);
+ } else {
+ // The customerLocation should always have an addressId, but just in case
+ return client
+ .from("customerLocation")
+ .delete()
+ .eq("customerId", customerId)
+ .eq("id", customerLocationId);
+ }
}
export async function deleteCustomerStatus(
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.locations.delete.$customerLocationId.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.locations.delete.$customerLocationId.tsx
index 55b61bf6e3..c77006a130 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.locations.delete.$customerLocationId.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.locations.delete.$customerLocationId.tsx
@@ -28,12 +28,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
customerLocationId
);
if (deleteCustomerLocationError) {
+ const errorMessage =
+ deleteCustomerLocationError.code === "23503"
+ ? "Customer location is used elsewhere, cannot delete"
+ : "Failed to delete customer location";
+
throw redirect(
path.to.customerLocations(customerId),
- await flash(
- request,
- error(deleteCustomerLocationError, "Failed to delete customer location")
- )
+ await flash(request, error(deleteCustomerLocationError, errorMessage))
);
}
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.delete.$supplierLocationId.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.delete.$supplierLocationId.tsx
index 50d5b6c79c..9929097fb2 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.delete.$supplierLocationId.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.delete.$supplierLocationId.tsx
@@ -28,12 +28,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
supplierLocationId
);
if (deleteSupplierLocationError) {
+ const errorMessage =
+ deleteSupplierLocationError.code === "23503"
+ ? "Supplier location is used elsewhere, cannot delete"
+ : "Failed to delete supplier location";
throw redirect(
path.to.supplierLocations(supplierId),
- await flash(
- request,
- error(deleteSupplierLocationError, "Failed to delete supplier location")
- )
+ await flash(request, error(deleteSupplierLocationError, errorMessage))
);
}