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: set actual start and end time on maintenance start and complete

Brad Barbin88293c66f68f
diff --git a/apps/erp/app/routes/x+/maintenance+/$dispatchId.status.tsx b/apps/erp/app/routes/x+/maintenance+/$dispatchId.status.tsx
index 146f67a7f9..2d073c7399 100644
--- a/apps/erp/app/routes/x+/maintenance+/$dispatchId.status.tsx
+++ b/apps/erp/app/routes/x+/maintenance+/$dispatchId.status.tsx
@@ -3,10 +3,7 @@ import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import type { ActionFunctionArgs } from "react-router";
import { redirect } from "react-router";
-import {
- maintenanceDispatchStatus,
- upsertMaintenanceDispatch
-} from "~/modules/resources";
+import { maintenanceDispatchStatus } from "~/modules/resources";
import { path, requestReferrer } from "~/utils/path";
export async function action({ request, params }: ActionFunctionArgs) {
@@ -35,6 +32,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
.update({
status,
assignee: ["Completed", "Cancelled"].includes(status) ? null : undefined,
+ actualStartTime:
+ status === "In Progress" ? new Date().toISOString() : undefined,
updatedBy: userId
})
.eq("id", dispatchId);
diff --git a/packages/database/supabase/migrations/20251231005844_maintenance-end-time-on-complete.sql b/packages/database/supabase/migrations/20251231005844_maintenance-end-time-on-complete.sql
new file mode 100644
index 0000000000..9ec45ceeca
--- /dev/null
+++ b/packages/database/supabase/migrations/20251231005844_maintenance-end-time-on-complete.sql
@@ -0,0 +1,42 @@
+DROP TRIGGER IF EXISTS end_maintenance_events_on_complete_trigger ON "maintenanceDispatch";
+DROP FUNCTION IF EXISTS end_maintenance_events_on_complete();
+
+-- Trigger function to end all active maintenance events when dispatch is completed
+CREATE OR REPLACE FUNCTION on_maintenance_dispatch_complete()
+RETURNS TRIGGER AS $$
+BEGIN
+ -- Only run when status changes to 'Completed'
+ IF NEW.status = 'Completed' AND (OLD.status IS NULL OR OLD.status != 'Completed') THEN
+ -- End all active events (those with null endTime) for this dispatch
+ UPDATE "maintenanceDispatchEvent"
+ SET
+ "endTime" = COALESCE(NEW."completedAt", NOW()),
+ "updatedBy" = NEW."updatedBy",
+ "updatedAt" = NOW()
+ WHERE
+ "maintenanceDispatchId" = NEW.id
+ AND "endTime" IS NULL;
+
+
+ -- Set actualEndTime to NOW() if it's not already set
+ IF NEW."actualEndTime" IS NULL THEN
+ UPDATE "maintenanceDispatch"
+ SET
+ "actualEndTime" = NOW(),
+ "updatedBy" = NEW."updatedBy",
+ "updatedAt" = NOW()
+ WHERE id = NEW.id;
+ END IF;
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+-- Create trigger on maintenanceDispatch table
+DROP TRIGGER IF EXISTS end_maintenance_events_on_complete_trigger ON "maintenanceDispatch";
+
+CREATE TRIGGER on_maintenance_dispatch_complete_trigger
+ AFTER UPDATE ON "maintenanceDispatch"
+ FOR EACH ROW
+ EXECUTE FUNCTION on_maintenance_dispatch_complete();