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.

fix: scheduled maintenance view

Brad Barbind5431d1f1f0a
diff --git a/apps/erp/app/modules/resources/ui/MaintenanceSchedule/MaintenanceSchedulesTable.tsx b/apps/erp/app/modules/resources/ui/MaintenanceSchedule/MaintenanceSchedulesTable.tsx
index 9ea2e66c83..fe87c9d134 100644
--- a/apps/erp/app/modules/resources/ui/MaintenanceSchedule/MaintenanceSchedulesTable.tsx
+++ b/apps/erp/app/modules/resources/ui/MaintenanceSchedule/MaintenanceSchedulesTable.tsx
@@ -1,4 +1,11 @@
-import { Combobox, HStack, MenuIcon, MenuItem, Status } from "@carbon/react";
+import {
+ Badge,
+ Combobox,
+ HStack,
+ MenuIcon,
+ MenuItem,
+ Status
+} from "@carbon/react";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useCallback, useMemo } from "react";
import {
@@ -39,6 +46,36 @@ const MaintenanceSchedulesTable = memo(
const permissions = usePermissions();
const allLocations = useLocations();
+ const renderDays = useCallback((row: MaintenanceSchedule) => {
+ const days = [
+ row.monday && "M",
+ row.tuesday && "Tu",
+ row.wednesday && "W",
+ row.thursday && "Th",
+ row.friday && "F",
+ row.saturday && "Sa",
+ row.sunday && "Su"
+ ].filter(Boolean);
+
+ return days.map((day) => (
+ <Badge key={day as string} variant="outline" className="mr-0.5">
+ {day}
+ </Badge>
+ ));
+ }, []);
+
+ const allDaysSelected = useCallback((row: MaintenanceSchedule) => {
+ return (
+ row.monday &&
+ row.tuesday &&
+ row.wednesday &&
+ row.thursday &&
+ row.friday &&
+ row.saturday &&
+ row.sunday
+ );
+ }, []);
+
const locationOptions = useMemo(
() =>
locations.map((location) => ({
@@ -89,10 +126,19 @@ const MaintenanceSchedulesTable = memo(
{
accessorKey: "frequency",
header: "Frequency",
- cell: (item) => {
- const frequency =
- item.getValue<(typeof maintenanceFrequency)[number]>();
- return <Enumerable value={frequency} />;
+ cell: ({ row }) => {
+ const frequency = row.original.frequency;
+ const showDays =
+ frequency === "Daily" && !allDaysSelected(row.original);
+ return (
+ <HStack>
+ {showDays ? (
+ renderDays(row.original)
+ ) : (
+ <Enumerable value={frequency} />
+ )}
+ </HStack>
+ );
},
meta: {
icon: <LuActivity />,
@@ -162,7 +208,7 @@ const MaintenanceSchedulesTable = memo(
}
}
];
- }, []);
+ }, [allDaysSelected, allLocations, renderDays]);
const renderContextMenu = useCallback(
(row: MaintenanceSchedule) => {
diff --git a/apps/erp/app/routes/x+/resources+/scheduled-maintenance.tsx b/apps/erp/app/routes/x+/resources+/scheduled-maintenance.tsx
index 9cb5369d3d..e39eb8b0d7 100644
--- a/apps/erp/app/routes/x+/resources+/scheduled-maintenance.tsx
+++ b/apps/erp/app/routes/x+/resources+/scheduled-maintenance.tsx
@@ -1,10 +1,14 @@
+import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { flash } from "@carbon/auth/session.server";
import { VStack } from "@carbon/react";
+
import type { LoaderFunctionArgs } from "react-router";
-import { Outlet, useLoaderData } from "react-router";
+import { data, Outlet, useLoaderData } from "react-router";
import {
getLocationsList,
- getMaintenanceSchedulesByLocation
+ getMaintenanceSchedulesByLocation,
+ MaintenanceSchedule
} from "~/modules/resources";
import MaintenanceSchedulesTable from "~/modules/resources/ui/MaintenanceSchedule/MaintenanceSchedulesTable";
import type { Handle } from "~/utils/handle";
@@ -57,8 +61,23 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
);
+ if (!schedules.data) {
+ return data(
+ {
+ data: [] as MaintenanceSchedule[],
+ count: 0,
+ locations: locationsList,
+ locationId: selectedLocationId
+ },
+ await flash(
+ request,
+ error(schedules.error, "Failed to load maintenance schedules")
+ )
+ );
+ }
+
return {
- data: schedules.data ?? [],
+ data: (schedules.data ?? []) as MaintenanceSchedule[],
count: schedules.count ?? 0,
locations: locationsList,
locationId: selectedLocationId
diff --git a/packages/database/supabase/migrations/20251231003301_scheduled-maintenance-fix.sql b/packages/database/supabase/migrations/20251231003301_scheduled-maintenance-fix.sql
new file mode 100644
index 0000000000..278524a334
--- /dev/null
+++ b/packages/database/supabase/migrations/20251231003301_scheduled-maintenance-fix.sql
@@ -0,0 +1,67 @@
+DROP FUNCTION get_maintenance_schedules_by_location;
+CREATE OR REPLACE FUNCTION get_maintenance_schedules_by_location(
+ p_company_id TEXT,
+ p_location_id TEXT
+)
+RETURNS TABLE (
+ "id" TEXT,
+ "name" TEXT,
+ "description" TEXT,
+ "workCenterId" TEXT,
+ "frequency" "maintenanceFrequency",
+ "priority" "maintenanceDispatchPriority",
+ "estimatedDuration" INTEGER,
+ "active" BOOLEAN,
+ "lastGeneratedAt" TIMESTAMP WITH TIME ZONE,
+ "nextDueAt" TIMESTAMP WITH TIME ZONE,
+ "companyId" TEXT,
+ "createdBy" TEXT,
+ "createdAt" TIMESTAMP WITH TIME ZONE,
+ "updatedBy" TEXT,
+ "updatedAt" TIMESTAMP WITH TIME ZONE,
+ "monday" BOOLEAN,
+ "tuesday" BOOLEAN,
+ "wednesday" BOOLEAN,
+ "thursday" BOOLEAN,
+ "friday" BOOLEAN,
+ "saturday" BOOLEAN,
+ "sunday" BOOLEAN,
+ "locationId" TEXT,
+ "workCenterName" TEXT,
+ "locationName" TEXT
+) AS $$
+BEGIN
+ RETURN QUERY
+ SELECT
+ ms."id",
+ ms."name",
+ ms."description",
+ ms."workCenterId",
+ ms."frequency",
+ ms."priority",
+ ms."estimatedDuration",
+ ms."active",
+ ms."lastGeneratedAt",
+ ms."nextDueAt",
+ ms."companyId",
+ ms."createdBy",
+ ms."createdAt",
+ ms."updatedBy",
+ ms."updatedAt",
+ ms."monday",
+ ms."tuesday",
+ ms."wednesday",
+ ms."thursday",
+ ms."friday",
+ ms."saturday",
+ ms."sunday",
+ wc."locationId",
+ wc."name" AS "workCenterName",
+ l."name" AS "locationName"
+ FROM "maintenanceSchedule" ms
+ INNER JOIN "workCenter" wc ON ms."workCenterId" = wc."id"
+ INNER JOIN "location" l ON wc."locationId" = l."id"
+ WHERE ms."companyId" = p_company_id
+ AND wc."locationId" = p_location_id;
+END;
+$$ LANGUAGE plpgsql SECURITY INVOKER;