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: delete customer

Brad Barbin60c6f41746f3
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 7f11de9e5d..5cbf18b62e 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -86,6 +86,13 @@ export async function convertQuoteToOrder(
});
}
+export async function deleteCustomer(
+ client: SupabaseClient<Database>,
+ customerId: string
+) {
+ return client.from("customer").delete().eq("id", customerId);
+}
+
export async function deleteCustomerContact(
client: SupabaseClient<Database>,
customerId: string,
diff --git a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
index 99cf3d666a..3347121946 100644
--- a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
@@ -1,9 +1,9 @@
-import { MenuIcon, MenuItem } from "@carbon/react";
+import { MenuIcon, MenuItem, useDisclosure } from "@carbon/react";
import { formatDate } from "@carbon/utils";
import { useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
-import { memo, useMemo } from "react";
-import { LuPencil } from "react-icons/lu";
+import { memo, useMemo, useState } from "react";
+import { LuPencil, LuTrash } from "react-icons/lu";
import {
CustomerAvatar,
EmployeeAvatar,
@@ -12,6 +12,7 @@ import {
Table,
} from "~/components";
import { Enumerable } from "~/components/Enumerable";
+import { ConfirmDelete } from "~/components/Modals";
import { usePermissions } from "~/hooks";
import { useCustomColumns } from "~/hooks/useCustomColumns";
import type { Customer, CustomerStatus } from "~/modules/sales";
@@ -29,6 +30,10 @@ const CustomersTable = memo(
const navigate = useNavigate();
const permissions = usePermissions();
const [people] = usePeople();
+ const deleteModal = useDisclosure();
+ const [selectedCustomer, setSelectedCustomer] = useState<Customer | null>(
+ null
+ );
const customColumns = useCustomColumns<Customer>("customer");
const columns = useMemo<ColumnDef<Customer>[]>(() => {
@@ -161,12 +166,24 @@ const CustomersTable = memo(
// eslint-disable-next-line react/display-name
() => (row: Customer) =>
(
- <MenuItem onClick={() => navigate(path.to.customer(row.id!))}>
- <MenuIcon icon={<LuPencil />} />
- Edit Customer
- </MenuItem>
+ <>
+ <MenuItem onClick={() => navigate(path.to.customer(row.id!))}>
+ <MenuIcon icon={<LuPencil />} />
+ Edit
+ </MenuItem>
+ <MenuItem
+ disabled={!permissions.can("delete", "sales")}
+ onClick={() => {
+ setSelectedCustomer(row);
+ deleteModal.onOpen();
+ }}
+ >
+ <MenuIcon icon={<LuTrash />} />
+ Delete
+ </MenuItem>
+ </>
),
- [navigate]
+ [navigate, deleteModal, permissions]
);
return (
@@ -205,6 +222,22 @@ const CustomersTable = memo(
}
renderContextMenu={renderContextMenu}
/>
+ {selectedCustomer && selectedCustomer.id && (
+ <ConfirmDelete
+ action={path.to.deleteCustomer(selectedCustomer.id)}
+ isOpen={deleteModal.isOpen}
+ name={selectedCustomer.name!}
+ text={`Are you sure you want to delete ${selectedCustomer.name!}? This cannot be undone.`}
+ onCancel={() => {
+ deleteModal.onClose();
+ setSelectedCustomer(null);
+ }}
+ onSubmit={() => {
+ deleteModal.onClose();
+ setSelectedCustomer(null);
+ }}
+ />
+ )}
</>
);
}
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.delete.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.delete.tsx
new file mode 100644
index 0000000000..03b69c1cf8
--- /dev/null
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.delete.tsx
@@ -0,0 +1,32 @@
+import { json, redirect } from "@remix-run/react";
+
+import { assertIsPost, error } from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { flash } from "@carbon/auth/session.server";
+import type { ActionFunctionArgs } from "@vercel/remix";
+import { deleteCustomer } from "~/modules/sales";
+import { path } from "~/utils/path";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client } = await requirePermissions(request, {
+ delete: "sales",
+ });
+
+ const { customerId } = params;
+ if (!customerId) throw new Error("Could not find customerId");
+
+ const customerDelete = await deleteCustomer(client, customerId);
+
+ if (customerDelete.error) {
+ return json(
+ path.to.customers,
+ await flash(
+ request,
+ error(customerDelete.error, "Failed to delete customer")
+ )
+ );
+ }
+
+ throw redirect(path.to.customers);
+}
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index d42601a080..d49a7701b3 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -210,6 +210,7 @@ export const path = {
generatePath(`${x}/resources/contractors/delete/${id}`),
deleteCurrency: (id: string) =>
generatePath(`${x}/accounting/currencies/delete/${id}`),
+ deleteCustomer: (id: string) => generatePath(`${x}/customer/${id}/delete`),
deleteCustomerContact: (customerId: string, id: string) =>
generatePath(`${x}/customer/${customerId}/contacts/delete/${id}`),
deleteCustomerLocation: (customerId: string, id: string) =>