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 non-conformance

Brad Barbind87ffa4363ad
diff --git a/apps/erp/app/modules/quality/quality.service.ts b/apps/erp/app/modules/quality/quality.service.ts
index 93f4352fa5..94e829cfe7 100644
--- a/apps/erp/app/modules/quality/quality.service.ts
+++ b/apps/erp/app/modules/quality/quality.service.ts
@@ -12,12 +12,9 @@ import type {
export async function deleteNonConformance(
client: SupabaseClient<Database>,
- nonConformanceTypeId: string
+ nonConformanceId: string
) {
- return client
- .from("nonConformanceType")
- .delete()
- .eq("id", nonConformanceTypeId);
+ return client.from("nonConformance").delete().eq("id", nonConformanceId);
}
export async function deleteNonConformanceType(
diff --git a/apps/erp/app/modules/quality/ui/NonConformance/NonConformanceIcons.tsx b/apps/erp/app/modules/quality/ui/NonConformance/NonConformanceIcons.tsx
new file mode 100644
index 0000000000..1a9e519e19
--- /dev/null
+++ b/apps/erp/app/modules/quality/ui/NonConformance/NonConformanceIcons.tsx
@@ -0,0 +1,40 @@
+import { cn } from "@carbon/react";
+import { BsExclamationSquareFill } from "react-icons/bs";
+import { HighPriorityIcon } from "~/assets/icons/HighPriorityIcon";
+import { LowPriorityIcon } from "~/assets/icons/LowPriorityIcon";
+import { MediumPriorityIcon } from "~/assets/icons/MediumPriorityIcon";
+import type {
+ nonConformancePriority,
+ nonConformanceSource,
+} from "../../quality.models";
+import { LuFactory, LuShoppingCart } from "react-icons/lu";
+
+export function getPriorityIcon(
+ priority: (typeof nonConformancePriority)[number],
+ overdue: boolean
+) {
+ switch (priority) {
+ case "Critical":
+ return <BsExclamationSquareFill className="text-red-500" />;
+ case "High":
+ return <HighPriorityIcon className={cn(overdue ? "text-red-500" : "")} />;
+ case "Medium":
+ return (
+ <MediumPriorityIcon className={cn(overdue ? "text-red-500" : "")} />
+ );
+ case "Low":
+ return <LowPriorityIcon />;
+ }
+}
+
+export function getSourceIcon(
+ source: (typeof nonConformanceSource)[number],
+ overdue: boolean
+) {
+ switch (source) {
+ case "Internal":
+ return <LuFactory />;
+ case "External":
+ return <LuShoppingCart />;
+ }
+}
diff --git a/apps/erp/app/modules/quality/ui/NonConformance/NonConformancesTable.tsx b/apps/erp/app/modules/quality/ui/NonConformance/NonConformancesTable.tsx
new file mode 100644
index 0000000000..2283a478c6
--- /dev/null
+++ b/apps/erp/app/modules/quality/ui/NonConformance/NonConformancesTable.tsx
@@ -0,0 +1,287 @@
+import { MenuIcon, MenuItem, useDisclosure } from "@carbon/react";
+import { useNavigate } from "@remix-run/react";
+import type { ColumnDef } from "@tanstack/react-table";
+import { memo, useCallback, useMemo, useState } from "react";
+import {
+ LuBookMarked,
+ LuCalendar,
+ LuChartNoAxesColumnIncreasing,
+ LuCircleGauge,
+ LuDna,
+ LuMap,
+ LuOctagonX,
+ LuPencil,
+ LuTrash,
+ LuUser,
+} from "react-icons/lu";
+import { EmployeeAvatar, Hyperlink, New, Table } from "~/components";
+
+import { usePermissions } from "~/hooks";
+import { path } from "~/utils/path";
+import { flushSync } from "react-dom";
+import { ConfirmDelete } from "~/components/Modals";
+import type { ListItem } from "~/types";
+import type { NonConformance } from "../../types";
+import { getPriorityIcon, getSourceIcon } from "./NonConformanceIcons";
+import NonConformanceStatus from "./NonConformanceStatus";
+import { Enumerable } from "~/components/Enumerable";
+import { useCustomColumns } from "~/hooks/useCustomColumns";
+import { useLocations } from "~/components/Form/Location";
+import { formatDate } from "@carbon/utils";
+import {
+ nonConformancePriority,
+ nonConformanceSource,
+ nonConformanceStatus,
+} from "../../quality.models";
+import { usePeople } from "~/stores/people";
+
+type NonConformancesTableProps = {
+ data: NonConformance[];
+ types: ListItem[];
+ count: number;
+};
+
+const NonConformancesTable = memo(
+ ({ data, types, count }: NonConformancesTableProps) => {
+ const navigate = useNavigate();
+ const permissions = usePermissions();
+ const deleteDisclosure = useDisclosure();
+ const [selectedNonConformance, setSelectedNonConformance] =
+ useState<NonConformance | null>(null);
+
+ const customColumns = useCustomColumns<NonConformance>("nonConformance");
+ const locations = useLocations();
+ const [people] = usePeople();
+
+ const columns = useMemo<ColumnDef<NonConformance>[]>(() => {
+ const defaultColumns: ColumnDef<NonConformance>[] = [
+ {
+ accessorKey: "nonConformanceId",
+ header: "Name",
+ cell: ({ row }) => (
+ <Hyperlink to={path.to.nonConformance(row.original.id!)}>
+ <div className="flex flex-col gap-0">
+ <span className="text-sm font-medium">
+ {row.original.nonConformanceId}
+ </span>
+ <span className="text-xs text-muted-foreground">
+ {row.original.name}
+ </span>
+ </div>
+ </Hyperlink>
+ ),
+ meta: {
+ icon: <LuBookMarked />,
+ },
+ },
+ {
+ accessorKey: "status",
+ header: "Status",
+ cell: ({ row }) => (
+ <NonConformanceStatus status={row.original.status} />
+ ),
+ meta: {
+ icon: <LuCircleGauge />,
+ filter: {
+ type: "static",
+ options: nonConformanceStatus.map((status) => ({
+ label: status,
+ value: status,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "nonConformanceTypeId",
+ header: "Type",
+ cell: ({ row }) => (
+ <Enumerable
+ value={
+ types.find(
+ (type) => type.id === row.original.nonConformanceTypeId
+ )?.name ?? null
+ }
+ />
+ ),
+ meta: {
+ icon: <LuOctagonX />,
+ filter: {
+ type: "static",
+ options: types.map((type) => ({
+ label: type.name,
+ value: type.id,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "priority",
+ header: "Priority",
+ cell: ({ row }) => (
+ <div className="flex gap-2 items-center">
+ {getPriorityIcon(row.original.priority ?? "Low", false)}
+ {row.original.priority}
+ </div>
+ ),
+ meta: {
+ icon: <LuChartNoAxesColumnIncreasing />,
+ filter: {
+ type: "static",
+ options: nonConformancePriority.map((priority) => ({
+ label: priority,
+ value: priority,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "source",
+ header: "Source",
+ cell: ({ row }) => (
+ <div className="flex gap-2 items-center">
+ {getSourceIcon(row.original.source ?? "Internal", false)}
+ {row.original.source}
+ </div>
+ ),
+ meta: {
+ icon: <LuDna />,
+ filter: {
+ type: "static",
+ options: nonConformanceSource.map((source) => ({
+ label: source,
+ value: source,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "locationId",
+ header: "Location",
+ cell: ({ row }) => (
+ <Enumerable
+ value={
+ locations.find(
+ (location) => location.value === row.original.locationId
+ )?.label ?? null
+ }
+ />
+ ),
+ meta: {
+ icon: <LuMap />,
+ filter: {
+ type: "static",
+ options: locations.map((location) => ({
+ label: location.label,
+ value: location.value,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "assignee",
+ header: "Assignee",
+ cell: ({ row }) => (
+ <EmployeeAvatar employeeId={row.original.assignee} />
+ ),
+ meta: {
+ filter: {
+ type: "static",
+ options: people.map((employee) => ({
+ value: employee.id,
+ label: employee.name,
+ })),
+ },
+ icon: <LuUser />,
+ },
+ },
+ {
+ accessorKey: "openDate",
+ header: "Open Date",
+ cell: ({ row }) => formatDate(row.original.openDate),
+ meta: {
+ icon: <LuCalendar />,
+ },
+ },
+ {
+ accessorKey: "closeDate",
+ header: "Closed Date",
+ cell: ({ row }) => formatDate(row.original.closeDate),
+ meta: {
+ icon: <LuCalendar />,
+ },
+ },
+ ];
+ return [...defaultColumns, ...customColumns];
+ }, [customColumns, locations, people, types]);
+
+ const renderContextMenu = useCallback(
+ (row: NonConformance) => {
+ return (
+ <>
+ <MenuItem
+ disabled={!permissions.can("update", "quality")}
+ onClick={() => {
+ navigate(`${path.to.nonConformance(row.id!)}`);
+ }}
+ >
+ <MenuIcon icon={<LuPencil />} />
+ Edit Non-Conformance
+ </MenuItem>
+ <MenuItem
+ destructive
+ disabled={!permissions.can("delete", "quality")}
+ onClick={() => {
+ flushSync(() => {
+ setSelectedNonConformance(row);
+ });
+ deleteDisclosure.onOpen();
+ }}
+ >
+ <MenuIcon icon={<LuTrash />} />
+ Delete Non-Conformance
+ </MenuItem>
+ </>
+ );
+ },
+ [navigate, permissions, deleteDisclosure]
+ );
+
+ return (
+ <>
+ <Table<NonConformance>
+ data={data}
+ columns={columns}
+ count={count}
+ primaryAction={
+ permissions.can("create", "quality") && (
+ <New label="Non-Conformance" to={path.to.newNonConformance} />
+ )
+ }
+ renderContextMenu={renderContextMenu}
+ title="Non-Conformances"
+ table="nonConformance"
+ withSavedView
+ />
+ {deleteDisclosure.isOpen && selectedNonConformance && (
+ <ConfirmDelete
+ action={path.to.deleteNonConformance(selectedNonConformance.id!)}
+ isOpen
+ onCancel={() => {
+ setSelectedNonConformance(null);
+ deleteDisclosure.onClose();
+ }}
+ onSubmit={() => {
+ setSelectedNonConformance(null);
+ deleteDisclosure.onClose();
+ }}
+ name={selectedNonConformance.name ?? "non-conformance"}
+ text="Are you sure you want to delete this non-conformance?"
+ />
+ )}
+ </>
+ );
+ }
+);
+
+NonConformancesTable.displayName = "NonConformancesTable";
+export default NonConformancesTable;
diff --git a/apps/erp/app/routes/x+/non-conformance+/delete.$id.tsx b/apps/erp/app/routes/x+/non-conformance+/delete.$id.tsx
new file mode 100644
index 0000000000..8d10a580fe
--- /dev/null
+++ b/apps/erp/app/routes/x+/non-conformance+/delete.$id.tsx
@@ -0,0 +1,38 @@
+import { error, success } from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { flash } from "@carbon/auth/session.server";
+import type { ActionFunctionArgs } from "@vercel/remix";
+import { json, redirect } from "@vercel/remix";
+import { deleteNonConformance } from "~/modules/quality";
+import { path } from "~/utils/path";
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ const { client } = await requirePermissions(request, {
+ delete: "quality",
+ });
+
+ const { id } = params;
+
+ if (!id) throw new Error("id is not found");
+
+ const mutation = await deleteNonConformance(client, id);
+ if (mutation.error) {
+ return json(
+ {
+ success: false,
+ },
+ await flash(
+ request,
+ error(mutation.error, "Failed to delete non-conformance workflow")
+ )
+ );
+ }
+
+ throw redirect(
+ path.to.nonConformances,
+ await flash(
+ request,
+ success("Successfully deleted non-conformance workflow")
+ )
+ );
+}
diff --git a/apps/erp/app/routes/x+/quality+/non-conformances.tsx b/apps/erp/app/routes/x+/quality+/non-conformances.tsx
new file mode 100644
index 0000000000..3487760a6e
--- /dev/null
+++ b/apps/erp/app/routes/x+/quality+/non-conformances.tsx
@@ -0,0 +1,63 @@
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { VStack } from "@carbon/react";
+import { Outlet, useLoaderData } from "@remix-run/react";
+import type { LoaderFunctionArgs } from "@vercel/remix";
+import { json } from "@vercel/remix";
+import {
+ getNonConformances,
+ getNonConformanceTypesList,
+} from "~/modules/quality";
+import NonConformancesTable from "~/modules/quality/ui/NonConformance/NonConformancesTable";
+import type { Handle } from "~/utils/handle";
+import { path } from "~/utils/path";
+import { getGenericQueryFilters } from "~/utils/query";
+
+export const handle: Handle = {
+ breadcrumb: "Non-Conformances",
+ to: path.to.nonConformances,
+};
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "quality",
+ role: "employee",
+ });
+
+ const url = new URL(request.url);
+ const searchParams = new URLSearchParams(url.search);
+ const search = searchParams.get("search");
+ const { limit, offset, sorts, filters } =
+ getGenericQueryFilters(searchParams);
+
+ const [nonConformances, nonConformanceTypes] = await Promise.all([
+ getNonConformances(client, companyId, {
+ search,
+ limit,
+ offset,
+ sorts,
+ filters,
+ }),
+ getNonConformanceTypesList(client, companyId),
+ ]);
+
+ return json({
+ nonConformances: nonConformances.data ?? [],
+ count: nonConformances.count ?? 0,
+ types: nonConformanceTypes.data ?? [],
+ });
+}
+
+export default function NonConformancesRoute() {
+ const { nonConformances, count, types } = useLoaderData<typeof loader>();
+
+ return (
+ <VStack spacing={0} className="h-full">
+ <NonConformancesTable
+ data={nonConformances}
+ count={count}
+ types={types}
+ />
+ <Outlet />
+ </VStack>
+ );
+}
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.ts
index 2b1a62d5d0..5456ce7346 100644
--- a/apps/erp/app/utils/path.ts
+++ b/apps/erp/app/utils/path.ts
@@ -447,7 +447,7 @@ export const path = {
deleteMethodOperationTool: (id: string) =>
generatePath(`${x}/items/methods/operation/tool/delete/${id}`),
deleteNonConformance: (id: string) =>
- generatePath(`${x}/quality/non-conformances/delete/${id}`),
+ generatePath(`${x}/non-conformance/delete/${id}`),
deleteNonConformanceWorkflow: (id: string) =>
generatePath(`${x}/non-conformance-workflow/delete/${id}`),
deleteNonConformanceType: (id: string) =>