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: add filters to kanban view in mes

Brad Barbin223f3dcf70a0
diff --git a/apps/mes/app/routes/x+/operations.tsx b/apps/mes/app/routes/x+/operations.tsx
index 3f1dc25336..fb8cc01ce0 100644
--- a/apps/mes/app/routes/x+/operations.tsx
+++ b/apps/mes/app/routes/x+/operations.tsx
@@ -162,19 +162,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
};
}) ?? []) satisfies Item[],
processes: processes.data ?? [],
- salesOrders: Object.entries(
- filteredOperations?.reduce((acc, op) => {
- if (op.salesOrderId) {
- acc[op.salesOrderId] = op.salesOrderReadableId;
- }
- return acc;
- }, {} as Record<string, string>) ?? {}
- ).map(([id, readableId]) => ({ id, readableId })),
+ workCenters: workCenters.data ?? [],
});
}
export default function Operations() {
- const { columns, items, processes } = useLoaderData<typeof loader>();
+ const { columns, items, processes, workCenters } =
+ useLoaderData<typeof loader>();
const [params] = useUrlParams();
const { hasFilters, clearFilters } = useFilters();
@@ -186,9 +180,9 @@ export default function Operations() {
header: "Work Center",
filter: {
type: "static",
- options: columns.map((col) => ({
- label: col.title,
- value: col.id,
+ options: workCenters.map((col) => ({
+ label: col.name!,
+ value: col.id!,
})),
},
},
@@ -198,14 +192,19 @@ export default function Operations() {
pluralHeader: "Processes",
filter: {
type: "static",
- options: processes.map((p) => ({
- label: p.name,
- value: p.id,
- })),
+ options: processes
+ .filter(
+ (p): p is { id: string; name: string } =>
+ p.id != null && p.name != null
+ )
+ .map((p) => ({
+ label: p.name,
+ value: p.id,
+ })),
},
},
];
- }, [columns, processes]);
+ }, [processes, workCenters]);
return (
<>
diff --git a/apps/mes/app/services/operation.server.ts b/apps/mes/app/services/operation.server.ts
index 4068481eda..cf1b8e0a54 100644
--- a/apps/mes/app/services/operation.server.ts
+++ b/apps/mes/app/services/operation.server.ts
@@ -1,84 +1,40 @@
-import { error, getCarbonServiceRole } from "@carbon/auth";
-import { requirePermissions } from "@carbon/auth/auth.server";
-import { flash } from "@carbon/auth/session.server";
-import type { LoaderFunctionArgs } from "@vercel/remix";
-import { defer, redirect } from "@vercel/remix";
-import { makeDurations } from "~/utils/durations";
+import { createCookieSessionStorage, redirect } from "@vercel/remix";
import { path } from "~/utils/path";
-import type { OperationWithDetails } from "./operations.service";
-import {
- getJobByOperationId,
- getJobFiles,
- getJobMaterialsByOperationId,
- getJobOperationById,
- getProductionEventsForJobOperation,
- getProductionQuantitiesForJobOperation,
- getThumbnailPathByItemId,
-} from "./operations.service";
-export async function loader({ request, params }: LoaderFunctionArgs) {
- const { userId, companyId } = await requirePermissions(request, {});
+const MES_FILTERS_KEY = "mes-filters";
- const { operationId } = params;
- if (!operationId) throw new Error("Operation ID is required");
+const sessionStorage = createCookieSessionStorage({
+ cookie: {
+ name: MES_FILTERS_KEY,
+ path: "/",
+ },
+});
- const serviceRole = await getCarbonServiceRole();
-
- const [events, quantities, job, operation] = await Promise.all([
- getProductionEventsForJobOperation(serviceRole, {
- operationId,
- userId,
- }),
- getProductionQuantitiesForJobOperation(serviceRole, operationId),
- getJobByOperationId(serviceRole, operationId),
- getJobOperationById(serviceRole, operationId),
- ]);
-
- if (job.error) {
- throw redirect(
- path.to.operations,
- await flash(request, error(job.error, "Failed to fetch job"))
- );
- }
-
- if (operation.error) {
- throw redirect(
- path.to.operations,
- await flash(request, error(operation.error, "Failed to fetch operation"))
- );
- }
+export async function getFilters(
+ request: Request
+): Promise<string | undefined> {
+ const session = await sessionStorage.getSession(
+ request.headers.get("Cookie")
+ );
+ return session.get(MES_FILTERS_KEY) as string | undefined;
+}
- if (!job.data.itemId) {
- throw redirect(
- path.to.operations,
- await flash(request, error("Item ID is required", "Failed to fetch item"))
- );
- }
+export async function setFilters(request: Request, filters: string) {
+ const session = await sessionStorage.getSession(
+ request.headers.get("Cookie")
+ );
+ session.set(MES_FILTERS_KEY, filters);
+ return sessionStorage.commitSession(session);
+}
- const thumbnailPath = await getThumbnailPathByItemId(
- serviceRole,
- job.data.itemId
+export async function destroySession(request: Request) {
+ const session = await sessionStorage.getSession(
+ request.headers.get("Cookie")
);
- return defer({
- events: events.data ?? [],
- quantities: (quantities.data ?? []).reduce(
- (acc, curr) => {
- if (curr.type === "Scrap") {
- acc.scrap += curr.quantity;
- } else if (curr.type === "Production") {
- acc.production += curr.quantity;
- } else if (curr.type === "Rework") {
- acc.rework += curr.quantity;
- }
- return acc;
- },
- { scrap: 0, production: 0, rework: 0 }
- ),
- job: job.data,
- files: getJobFiles(serviceRole, companyId, job.data),
- materials: getJobMaterialsByOperationId(serviceRole, operation.data?.[0]),
- operation: makeDurations(operation.data?.[0]) as OperationWithDetails,
- thumbnailPath,
+ return redirect(path.to.root, {
+ headers: {
+ "Set-Cookie": await sessionStorage.destroySession(session),
+ },
});
}