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.
Change Notice History Download and Approval Rules — implementation plan
Spec / source: .ai/specs/2026-10-08-change-notice-history-download-and-approvals.md
Branch: feat/download-changenotice-and-approval-rule
Note: the 2026-10-09 review refactor replaced the per-feature helpers named in Tasks 7–10 (
changeNoticeApprovalValidator,isChangeNoticeAwaitingApproval,getChangeNoticeApprovalContext,ChangeNoticeApprovalModal) with the shared@carbon/ee/approvals/document.serverhelpers,approvalDecisionValidatorand theApprovalDecisionmodal. The spec is the current description.
Progress
- Task 1: Add the
changeOrderapproval document type (migration) - Task 2: Apply the migration and regenerate DB types
- Task 3: Approval engine and models support
changeOrder - Task 4: Dataset validator exception
- Task 5: Settings → Approval Rules shows a Change Notices card
- Task 6: Approval notifications route and describe change notices
- Task 7: Change notice server helpers (pending lock, validator, path)
- Task 8: Change notice routes (submit, approve/reject, cancel, delete, loader)
- Task 9: Change notice UI (header buttons, modal, edit lock)
- Task 10: History CSV download
- Task 11: End-to-end verification (browser walk-through passed; added change notice audit event triggers found during the test)
Dependencies
- Task 2 needs Task 1. Tasks 3–10 need Task 2 (generated enum type).
- Tasks 3, 4, 6 and 10 are independent of each other.
- Task 5 needs Task 3 (labels). Task 8 needs Tasks 3 and 7. Task 9 needs Task 8.
- Task 11 needs all.
Task 1: Add the changeOrder approval document type (migration)
Depends on: none Files:
- Create:
packages/database/supabase/migrations/{timestamp}_change-order-approvals.sql - Copy from (precedent):
packages/database/supabase/migrations/20260310005407_supplier-approvals.sql:2-3,78-109
Steps:
- Run
pnpm db:migrate:new change-order-approvals. - Write this SQL into the new file:
ALTER TYPE "approvalDocumentType" ADD VALUE IF NOT EXISTS 'changeOrder';
COMMIT;
DROP VIEW IF EXISTS "approvalRequests";
CREATE OR REPLACE VIEW "approvalRequests" WITH (SECURITY_INVOKER=true) AS
SELECT
ar."id", ar."documentType", ar."documentId", ar."status",
ar."requestedBy", ar."requestedAt", ar."decisionBy", ar."decisionAt",
ar."decisionNotes", ar."companyId", ar."createdAt",
CASE
WHEN ar."documentType" = 'purchaseOrder' THEN po."purchaseOrderId"
WHEN ar."documentType" = 'qualityDocument' THEN qd."name"
WHEN ar."documentType" = 'supplier' THEN sup."name"
WHEN ar."documentType" = 'changeOrder' THEN co."changeOrderId"
ELSE NULL
END AS "documentReadableId",
CASE
WHEN ar."documentType" = 'purchaseOrder' THEN s."name"
WHEN ar."documentType" = 'qualityDocument' THEN qd."description"
WHEN ar."documentType" = 'supplier' THEN NULL
WHEN ar."documentType" = 'changeOrder' THEN co."name"
ELSE NULL
END AS "documentDescription"
FROM "approvalRequest" ar
LEFT JOIN "purchaseOrder" po ON ar."documentType" = 'purchaseOrder' AND ar."documentId" = po."id"
LEFT JOIN "supplier" s ON po."supplierId" = s."id"
LEFT JOIN "qualityDocument" qd ON ar."documentType" = 'qualityDocument' AND ar."documentId" = qd."id"
LEFT JOIN "supplier" sup ON ar."documentType" = 'supplier' AND ar."documentId" = sup."id"
LEFT JOIN "changeOrder" co ON ar."documentType" = 'changeOrder' AND ar."documentId" = co."id" AND ar."companyId" = co."companyId";
Verify: file exists; grep -c changeOrder on it returns 5.
Out of scope: RLS, functions, triggers (approval logic lives in TypeScript).
Task 2: Apply the migration and regenerate DB types
Depends on: 1 Steps:
- Run
pnpm db:migrate. - Run
pnpm db:types.
Verify:
grep -n '"changeOrder"' packages/database/src/types.ts | grep -i approval | head
# Expected: approvalDocumentType union contains "changeOrder"
If pnpm db:migrate fails because the local DB is down, STOP and report.
Task 3: Approval engine and models support changeOrder
Depends on: 2 Files:
- Modify:
packages/ee/src/approvals/models.ts— add"changeOrder"toapprovalDocumentType; addchangeOrder: "Change Notice"toapprovalDocumentTypeLabel. - Modify:
packages/ee/src/approvals/service.ts—approveRequest: add anelse if (documentType === "changeOrder")branch aftersupplier. It updateschangeOrderwithstatus: "Implementation",updatedBy,updatedAt, whereid = documentId,companyId = companyId,status = "Engineering Complete", returnsid, and throws"Failed to update change notice status - it may no longer be in 'Engineering Complete' state"when no row comes back.rejectRequest: add a comment thatchangeOrderrejection does not change the status.
Verify: pnpm --filter @carbon/ee typecheck exits 0.
Task 4: Dataset validator exception
Depends on: 2
Files: Modify packages/database/src/datasets/validate.ts:251-256 — add changeOrder: "the seed does not create change notice approval requests" to the enumValues("approvalDocumentType", {...}) map.
Verify: pnpm db:check:datasets passes (or reports only failures that exist on main; compare if it fails).
Task 5: Settings → Approval Rules shows a Change Notices card
Depends on: 3 Files:
- Modify:
apps/erp/app/routes/x+/settings+/approval-rules.tsx—changeOrderRulesbucket; pass it toApprovalRules. - Modify:
apps/erp/app/routes/x+/settings+/approval-rules.new.tsx— accepttypeParam === "changeOrder". - Modify:
apps/erp/app/modules/settings/ui/Approvals/ApprovalRules.ee.tsx— newchangeOrderRulesprop and a 4th card "Change Notices", description "Require approval before change notices move to Implementation". Copy the Suppliers card (New Rule only whenchangeOrderRules.length === 0). - Modify:
apps/erp/app/modules/settings/ui/Approvals/ApprovalRuleCard.ee.tsx— replaceapprovalDocumentTypeLabel[documentType]and the delete-name ternary with a Lingui map:{ purchaseOrder: t\Purchase Order`, qualityDocument: t`Quality Document`, supplier: t`Supplier`, changeOrder: t`Change Notice` }; delete name ist`${label} approval rule``.
Verify: pnpm --filter erp typecheck exits 0.
Task 6: Approval notifications route and describe change notices
Depends on: 2 Files:
- Modify:
packages/jobs/src/inngest/functions/notifications/content.ts— in the approval case, add achangeOrderbranch before the generic return: readchangeOrder(changeOrderId, name, status) byid+companyId; descriptionChange notice ${changeOrderId} ${docPhrase}, referencechangeOrderId, details Name + Status. Fallback descriptionChange notice ${docPhrase}. - Modify:
apps/erp/app/routes/api+/link.ts—if (documentType === "changeOrder") return path.to.changeNoticeDetails(documentId); - Modify:
apps/erp/app/components/Layout/Topbar/Notifications.tsx— approvaltobydocumentType: qualityDocument →path.to.qualityDocument(id), supplier →path.to.supplierApproval(id), changeOrder →path.to.changeNoticeDetails(id), elsepath.to.purchaseOrderDetails(id). - Modify:
packages/notifications/src/index.ts:15— drop "no approval flow in v1" from the comment.
Verify: pnpm --filter @carbon/jobs typecheck and pnpm --filter erp typecheck exit 0.
Task 7: Change notice server helpers (pending lock, validator, path)
Depends on: 3 Files:
- Modify:
apps/erp/app/modules/items/items.models.ts— addchangeNoticeApprovalValidator(copyqualityDocumentApprovalValidator,quality.models.ts:394-399) andchangeNoticeAwaitingApprovalMessage = "This change notice is waiting for approval. Engineering changes are locked until it is approved or rejected.". - Modify:
apps/erp/app/utils/path.ts—changeNoticeApproval: (id) => generatePath(\${x}/items/change-notice/${id}/approval`)(alphabetical, afterchangeNoticeAffectedItem`). - Modify:
apps/erp/app/modules/items/items.server.ts:isChangeNoticeAwaitingApproval(changeNoticeId: string): Promise<boolean>→hasPendingApproval(getCarbonServiceRole(), "changeOrder", changeNoticeId).METHOD_LOCK_SOURCE→"changeOrder(id, status), item(revisionStatus)";MethodLockgetschangeNoticeId;resolveMethodLockfills it.checkRevisionLock: after the existing hard block, iflock.changeNoticeStatus === "Engineering Complete"andlock.changeNoticeIdand the helper returns true →{ ok: false, warn: false, message: changeNoticeAwaitingApprovalMessage }.requireChangeNoticeEditable: if scope isengineering, status isEngineering Complete, and the helper returns true →{ error: { message: changeNoticeAwaitingApprovalMessage }, data: null }.
Verify: pnpm --filter erp typecheck exits 0.
If MethodLockRow typing does not allow the extra id, widen that local type only.
Task 8: Change notice routes
Depends on: 3, 7 Files:
- Modify:
apps/erp/app/routes/x+/items+/change-notice+/$id.status.tsx:- If
fromStatus === "Engineering Complete" && toStatus === "Implementation": service role; pending → flash error "This change notice is already waiting for approval";isApprovalRequired(serviceRole, "changeOrder", companyId)→createApprovalRequest, notify approvers (copyquality-document+/update.tsx:58-104), flash success "Submitted for approval", redirect. No status change. - If
toStatus === "Cancelled": after a successful update, set any pendingchangeOrderrequest foridtoCancelledwith the service role (copy the update inquality-document+/update.tsx:178-187).
- If
- Create:
apps/erp/app/routes/x+/items+/change-notice+/$id.approval.tsx— action only. Copyquality-document+/$id.tsx:114-230; useupdate: "parts",changeNoticeApprovalValidator,"changeOrder", redirect topath.to.changeNoticeDetails(id). After an Approved result callnotifyChangeNoticeTransition({ client, event: changeNoticeStageEvent.Implementation, changeNoticeId: id, companyId, userId }). - Modify:
apps/erp/app/routes/x+/items+/change-notice+/delete.$id.tsx— before the delete, cancel pendingchangeOrderrequests foridwith the service role. - Modify:
apps/erp/app/routes/x+/items+/change-notice+/$id.tsx— loader returnsapprovalfrom newgetChangeNoticeApprovalContext(initems.server.ts, copyquality-document+/$id.tsx:52-104):{ requestId, isPending, canApprove, isApprovalRequired, lastRejection: { notes, decisionAt } | null }; only queries when status isEngineering Complete.
Verify: pnpm --filter erp typecheck exits 0.
Task 9: Change notice UI
Depends on: 8 Files:
- Create:
apps/erp/app/modules/items/ui/ChangeNotice/ChangeNoticeApprovalModal.tsx— copymodules/quality/ui/Documents/QualityDocumentApprovalModal.tsx; actionpath.to.changeNoticeApproval(id); all copy Lingui; approve text "This moves the change notice to Implementation."; reject text "The change notice stays at Engineering Complete." - Modify:
apps/erp/app/modules/items/ui/ChangeNotice/ChangeNoticeHeader.tsx(precedentQualityDocumentHeader.tsx:72-200):- At Engineering Complete +
isApprovalRequired+ not pending: Advance labelt\Submit for Approval``. - Pending: hide Advance; show
Pending Approvalbadge; ifcanApprove, Approve + Reject buttons opening the modal. lastRejectionat Engineering Complete and not pending:Approval Rejectedbadge with tooltip (notes + date).
- At Engineering Complete +
- Modify:
$id.details.tsx,$id.$affectedId.details.tsx,ChangeNoticeExplorer.tsx—isDisabled ||= approval?.isPending; affected-item disabled reason gets the pending message (Lingui).
Verify: pnpm --filter erp typecheck exits 0.
Task 10: History CSV download
Depends on: 2 Files:
- Modify:
apps/erp/app/routes/api+/audit-log.ts— ifall === "true", loopgetEntityAuditLogwithlimit: 500and growingoffsetuntil a page has fewer than 500 rows or 10,000 entries are collected. - Modify:
apps/erp/app/components/AuditLog/useAuditLog.tsx— optionaldownloadable?: boolean,downloadName?: string; pass to the drawer. - Modify:
apps/erp/app/components/AuditLog/AuditLogDrawer.tsx—AuditLogFeedgetsonEntryCountChange?(count); the drawer keeps the count in state and, whendownloadable && count > 0, renders a Download button inDrawerHeader. On click:fetch('/api/audit-log?...&all=true'), build rows (columns per spec: Date, Changed By viausePeople(), Action, Record viagetTableLabel, Record ID, Field, Old Value, New Value),downloadCsv(rows, \${downloadName ?? entityType}-history-${YYYY-MM-DD}.csv`, { fields }). Precedent:components/Table/components/Download.tsx`. - Modify:
ChangeNoticeHeader.tsx— passdownloadable: true,downloadName: changeOrderId.
Verify: pnpm --filter erp typecheck exits 0.
Out of scope: other useAuditLog callers.
Task 11: End-to-end verification
Depends on: all Steps:
pnpm --filter erp typecheck,pnpm --filter @carbon/ee typecheck,pnpm --filter @carbon/jobs typecheck— exit 0.pnpm exec biome checkon every changed file — no errors.pnpm lingui:extract— new strings extracted.pnpm db:check:datasets.- Run the ERP app and walk the spec's acceptance criteria (approval flow + download).