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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(items): notify change notice assignees instead of the whole company (#1397)
Naveen Kashyapf8ff54b4c4ed
diff --git a/apps/erp/app/modules/agent/kb/docs/reference/change-orders/lifecycle.md b/apps/erp/app/modules/agent/kb/docs/reference/change-orders/lifecycle.mdindex ab2796f018..5cd25f2925 100644--- a/apps/erp/app/modules/agent/kb/docs/reference/change-orders/lifecycle.md+++ b/apps/erp/app/modules/agent/kb/docs/reference/change-orders/lifecycle.md@@ -9,15 +9,17 @@ every open stage — closes it, after which **Reopen** returns it to **"Draft"**## The stages- - **Draft**: The change notice is being written. Pick affected items, set change types, and edit each draft method. Nothing is broadcast.- - **Start**: Work is underway. Advancing to Start broadcasts a notification to the team.- - **Engineering Complete**: The design edits are finished and ready for implementation review. Silent, no broadcast.- - **Implementation**: The release stage. The engineering content freezes here, and the **"Release"** action becomes available; it reviews every affected item's diff and applies the change. Advancing here broadcasts.- - **Done**: Released and locked. The change notice becomes a read-only historical record; broadcasts on entry.+ - **Draft**: The change notice is being written. Pick affected items, set change types, and edit each draft method. No notification is sent.+ - **Start**: Work is underway. Advancing to Start notifies the assignee and every action-task assignee.+ - **Engineering Complete**: The design edits are finished and ready for implementation review. Silent, no notification.+ - **Implementation**: The release stage. The engineering content freezes here, and the **"Release"** action becomes available; it reviews every affected item's diff and applies the change. Advancing here notifies.+ - **Done**: Released and locked. The change notice becomes a read-only historical record; notifies on entry.- **Cancelled**: The off-ramp. The header's **"Cancel"** action closes the change notice from any open stage without releasing anything. Locked like **"Done"**, but reversible: **"Reopen"** returns it to **"Draft"**.-The **"Start"**, **"Implementation"**, and **"Done"** transitions each notify the team; **"Engineering-Complete"** is silent. Once a change notice reaches **"Done"** it is locked — every editor is read-only.+The **"Start"**, **"Implementation"**, and **"Done"** transitions each notify the people on the change+notice: the assignee, plus everyone assigned an action task. Whoever made the transition isn't notified of+their own action, and a change notice with no assignee and no assigned tasks advances silently. **"Engineering+Complete"** is silent for everyone. Once a change notice reaches **"Done"** it is locked — every editor is read-only.**"Cancelled"** is locked too, but not lost: cancelling keeps every affected item's draft, and **"Reopen"**takes the change notice back to **"Draft"** to pick up where it left off. Only **"Done"** is permanent.Deleting a change notice is the destructive path — that discards each affected item's draft and any newdiff --git a/apps/erp/app/modules/agent/kb/guides/change-order.md b/apps/erp/app/modules/agent/kb/guides/change-order.mdindex ca3f514179..7171be3a64 100644--- a/apps/erp/app/modules/agent/kb/guides/change-order.md+++ b/apps/erp/app/modules/agent/kb/guides/change-order.md@@ -75,7 +75,8 @@ supplier, part number, price, and order quantities on the line before you releasAdvance the change notice through its stages: **Draft**, then **"Start"**, then **"Engineering Complete"**,then **"Implementation"**. It only moves forward, one step at a time, and **"Start"**, **"Implementation"**,-and **"Done"** each notify the team. There's no separate approval step to configure — the stages are the+and **"Done"** each notify the assignee and the action-task assignees, so the people working the change hear+about every stage it enters. There's no separate approval step to configure — the stages are theworkflow, and the change notice is its own gate. If the change doesn't pan out, you're not trapped in theforward march either: a **"Cancel"** action sits in the header at every open stage. Cancelling locks thechange notice but keeps every draft, and **"Reopen"** takes it back to **Draft** later with your editsdiff --git a/apps/erp/app/modules/items/AGENTS.md b/apps/erp/app/modules/items/AGENTS.mdindex 83fb0a8da9..1fa1408885 100644--- a/apps/erp/app/modules/items/AGENTS.md+++ b/apps/erp/app/modules/items/AGENTS.md@@ -90,7 +90,7 @@ Change-order code follows the one-service/models/server-per-module convention### Key CO concepts-- **Stage flow** — `changeOrderStatus`: `Draft` → `Start` → `Engineering Complete` → `Implementation` → `Done`, plus a `Cancelled` off-ramp. Table-driven, one step forward (`changeNoticeStatusTransitions` / `isAllowedChangeNoticeTransition` — the check is purely `allowed.includes(to)`, NOT directional), with two backward **reopen** edges: `Implementation → Engineering Complete` and `Cancelled → Draft`. Both are ⋮-menu items in `ChangeNoticeHeader`. `Done` is terminal — releasing already activated methods and wrote supersessions, so it cannot be reopened. **The reopen edge must stay LAST in the `Implementation` array**: the header reads `transitions[status]?.[0]` for its advance/release action. `changeNoticeOpenStatuses` = every stage before Done. `Start`/`Implementation`/`Done` broadcast a team notification (`changeNoticeBroadcastStages`).+- **Stage flow** — `changeOrderStatus`: `Draft` → `Start` → `Engineering Complete` → `Implementation` → `Done`, plus a `Cancelled` off-ramp. Table-driven, one step forward (`changeNoticeStatusTransitions` / `isAllowedChangeNoticeTransition` — the check is purely `allowed.includes(to)`, NOT directional), with two backward **reopen** edges: `Implementation → Engineering Complete` and `Cancelled → Draft`. Both are ⋮-menu items in `ChangeNoticeHeader`. `Done` is terminal — releasing already activated methods and wrote supersessions, so it cannot be reopened. **The reopen edge must stay LAST in the `Implementation` array**: the header reads `transitions[status]?.[0]` for its advance/release action. `changeNoticeOpenStatuses` = every stage before Done. `Start`/`Implementation`/`Done` notify the CO assignee + action-task assignees (`changeNoticeNotifyStages`; `notifyChangeNoticeTransition` resolves the recipients — not a company-wide broadcast).- **Two lock scopes** (`items.models.ts`) — `isChangeNoticeLocked(status)` is true at `Done`/`Cancelled` (fully closed). On top of that: `canEditChangeNoticeEngineering(status)` additionally excludes `Implementation` and gates affected items (add/remove/change-type), the staged BOM/BOP drafts, cutover config, and `reasonForChange`/`description`; `canEditChangeNoticeWorkflow(status)` is the closed-only test and gates action tasks, assignee, dates and priority — those stay editable at `Implementation`, which is the whole point of that stage. (`canEditChangeNotice` was split into these two and no longer exists.) Server enforcement is `requireChangeNoticeEditable(client, { changeNoticeId, companyId, scope })` in `items.server.ts`; the UI `isDisabled` is cosmetic on top of it. BOM/BOP mutations are covered centrally — `checkRevisionLock` also resolves the owning CO's status, so all ~17 `methods+/` routes inherit the lock without their own guard. Note `item.changeOrderId` is a PERMANENT back-link (unlike `makeMethod.changeOrderId`, cleared at release), so it must never be used to resolve ownership.- **Change type (capability matrix)** — `changeOrderAffectedItem.changeType` ∈ `Version | Revision | Replacement Part | New Part` (`changeNoticeChangeTypes`) drives both the editable surface and the release action. Two axes: *is there a predecessor* and *same part number*. The **Add Affected Item** modal (`AffectedItemForm`) is **Parts-only**: the existing-item picker filters to `validItemTypes={["Part"]}` and the New Part mini-form always mints a Part — no Tool path in the UI (the service's `addChangeNoticeAffectedItem`/`createChangeNoticeDraftMethod` still technically accept Tools, but no route surfaces them). The modal's two branches (New Part vs existing-item) are separate `ValidatedForm`s that MUST carry distinct `key` props — same-slot same-type reconciliation would otherwise share one RVF store, hydrating controlled-field defaults only for whichever branch mounts first (New Part's replenishment/tracking Selects would then submit `""` → enum error).- `Version` — a new Draft method version on the **same item** (BoM/BoP edits); **no** supersession.diff --git a/apps/erp/app/modules/items/items.models.ts b/apps/erp/app/modules/items/items.models.tsindex 41bbe87000..8b5afbd39a 100644--- a/apps/erp/app/modules/items/items.models.ts+++ b/apps/erp/app/modules/items/items.models.ts@@ -1013,7 +1013,7 @@ export const changeNoticeType = ["Documentation"] as const;-// V1 stage flow (forward, one step at a time). Broadcast on Start /+// V1 stage flow (forward, one step at a time). Notifies on Start /// Implementation / Done; silent on Draft / Engineering Complete. "Cancelled" is// the off-ramp: a CO can be closed from any open stage and reopened to Draft.export const changeNoticeStatus = [@@ -1099,9 +1099,12 @@ export function isAllowedChangeNoticeTransition(return (allowed as readonly string[]).includes(to);}-// The stages that broadcast a team notification on entry.-export const changeNoticeBroadcastStages: (typeof changeNoticeStatus)[number][] =- ["Start", "Implementation", "Done"];+// The stages that notify the CO assignee + action-task assignees on entry.+export const changeNoticeNotifyStages: (typeof changeNoticeStatus)[number][] = [+ "Start",+ "Implementation",+ "Done"+];// "Open" = every stage before the record is closed at Done. Used by the item// open-CO alert and the single-open-CO guard.diff --git a/apps/erp/app/modules/items/items.server.ts b/apps/erp/app/modules/items/items.server.tsindex d20bead040..737c98e025 100644--- a/apps/erp/app/modules/items/items.server.ts+++ b/apps/erp/app/modules/items/items.server.ts@@ -323,18 +323,18 @@ export async function requireEditableChangeNoticeRoute();}-// Maps a broadcast stage to its notification event. Only Start / Implementation-// / Done broadcast (PRD §3.1); Draft / Engineering Complete are silent, so-// callers simply don't invoke this for those stages.+// Maps a notifying stage to its notification event. Only Start / Implementation+// / Done notify; Draft / Engineering Complete are silent, so callers simply+// don't invoke this for those stages.export const changeNoticeStageEvent: Record<string, NotificationEvent> = {Start: NotificationEvent.ChangeNoticeStarted,Implementation: NotificationEvent.ChangeNoticeImplementation,Done: NotificationEvent.ChangeNoticeDone};-// Broadcasts a CO stage to the whole team (best-effort). Recipient is the seeded-// "All Employees" group — NOT companyGroupId, which is the currency/subsidiary-// grouping and has no user members.+// Notifies the people involved in the CO — its assignee plus every action-task+// assignee — on a stage entry (best-effort). Deliberately NOT a company-wide+// broadcast; the notify function dedupes and drops the acting user itself.export async function notifyChangeNoticeTransition(args: {client: SupabaseClient<Database>;event: NotificationEvent;@@ -343,30 +343,54 @@ export async function notifyChangeNoticeTransition(args: {userId: string;}): Promise<void> {try {- const employeeGroup = await args.client- .from("group")- .select("id")- .eq("companyId", args.companyId)- .eq("isEmployeeTypeGroup", true)- .eq("name", "All Employees")- .single();-- if (employeeGroup.error || !employeeGroup.data) {- logger.error(- "Failed to resolve All Employees group for CO notification",- {- error: employeeGroup.error,- companyId: args.companyId- }- );+ const [changeNotice, actionTasks] = await Promise.all([+ args.client+ .from("changeOrder")+ .select("assignee")+ .eq("id", args.changeNoticeId)+ .eq("companyId", args.companyId)+ .single(),+ args.client+ .from("changeOrderActionTask")+ .select("assignee")+ .eq("changeOrderId", args.changeNoticeId)+ .eq("companyId", args.companyId)+ .not("assignee", "is", null)+ ]);++ if (changeNotice.error) {+ logger.error("Failed to resolve change notice for CO notification", {+ error: changeNotice.error,+ changeNoticeId: args.changeNoticeId,+ companyId: args.companyId+ });return;}+ if (actionTasks.error) {+ logger.error("Failed to resolve CO action-task assignees", {+ error: actionTasks.error,+ changeNoticeId: args.changeNoticeId,+ companyId: args.companyId+ });+ }++ const userIds = [+ ...new Set(+ [+ changeNotice.data?.assignee,+ ...(actionTasks.data ?? []).map((task) => task.assignee)+ ].filter((id): id is string => !!id)+ )+ ];++ if (userIds.length === 0) return;+await trigger("notify", {event: args.event,companyId: args.companyId,documentId: args.changeNoticeId,- recipient: { type: "group", groupIds: [employeeGroup.data.id] },+ recipient: { type: "users", userIds },from: args.userId});} catch (e) {diff --git a/apps/erp/app/routes/x+/items+/change-notice+/$id.status.tsx b/apps/erp/app/routes/x+/items+/change-notice+/$id.status.tsxindex 5c04903fab..883c732851 100644--- a/apps/erp/app/routes/x+/items+/change-notice+/$id.status.tsx+++ b/apps/erp/app/routes/x+/items+/change-notice+/$id.status.tsx@@ -5,7 +5,7 @@ import { validationError, validator } from "@carbon/form";import type { ActionFunctionArgs } from "react-router";import { redirect } from "react-router";import {- changeNoticeBroadcastStages,+ changeNoticeNotifyStages,changeNoticeStatusValidator,updateChangeNoticeStatus} from "~/modules/items";@@ -76,9 +76,10 @@ export async function action({ request, params }: ActionFunctionArgs) {}}- // Broadcast to the company team only on the stages that broadcast on entry- // (Start / Implementation / Done). Best-effort; never blocks the redirect.- if (changeNoticeBroadcastStages.includes(toStatus)) {+ // Notify the CO assignee + action-task assignees only on the stages that+ // notify on entry (Start / Implementation / Done). Best-effort; never blocks+ // the redirect.+ if (changeNoticeNotifyStages.includes(toStatus)) {await notifyChangeNoticeTransition({client,event: changeNoticeStageEvent[toStatus],diff --git a/docs/content/docs/reference/change-orders/lifecycle.mdx b/docs/content/docs/reference/change-orders/lifecycle.mdxindex 23202eb61c..e59cc27331 100644--- a/docs/content/docs/reference/change-orders/lifecycle.mdx+++ b/docs/content/docs/reference/change-orders/lifecycle.mdx@@ -11,16 +11,18 @@ every open stage — closes it, after which **Reopen** returns it to **"Draft"**## The stages<StatusFlow>- <Status name="Draft">The change notice is being written. Pick affected items, set change types, and edit each draft method. Nothing is broadcast.</Status>- <Status name="Start">Work is underway. Advancing to Start broadcasts a notification to the team.</Status>- <Status name="Engineering Complete">The design edits are finished and ready for implementation review. Silent, no broadcast.</Status>- <Status name="Implementation" accent>The release stage. The engineering content freezes here, and the **"Release"** action becomes available; it reviews every affected item's diff and applies the change. Advancing here broadcasts.</Status>- <Status name="Done" terminal>Released and locked. The change notice becomes a read-only historical record; broadcasts on entry.</Status>+ <Status name="Draft">The change notice is being written. Pick affected items, set change types, and edit each draft method. No notification is sent.</Status>+ <Status name="Start">Work is underway. Advancing to Start notifies the assignee and every action-task assignee.</Status>+ <Status name="Engineering Complete">The design edits are finished and ready for implementation review. Silent, no notification.</Status>+ <Status name="Implementation" accent>The release stage. The engineering content freezes here, and the **"Release"** action becomes available; it reviews every affected item's diff and applies the change. Advancing here notifies.</Status>+ <Status name="Done" terminal>Released and locked. The change notice becomes a read-only historical record; notifies on entry.</Status><Status name="Cancelled" terminal>The off-ramp. The header's **"Cancel"** action closes the change notice from any open stage without releasing anything. Locked like **"Done"**, but reversible: **"Reopen"** returns it to **"Draft"**.</Status></StatusFlow>-The **"Start"**, **"Implementation"**, and **"Done"** transitions each notify the team; **"Engineering-Complete"** is silent. Once a change notice reaches **"Done"** it is locked — every editor is read-only.+The **"Start"**, **"Implementation"**, and **"Done"** transitions each notify the people on the change+notice: the assignee, plus everyone assigned an action task. Whoever made the transition isn't notified of+their own action, and a change notice with no assignee and no assigned tasks advances silently. **"Engineering+Complete"** is silent for everyone. Once a change notice reaches **"Done"** it is locked — every editor is read-only.**"Cancelled"** is locked too, but not lost: cancelling keeps every affected item's draft, and **"Reopen"**takes the change notice back to **"Draft"** to pick up where it left off. Only **"Done"** is permanent.Deleting a change notice is the destructive path — that discards each affected item's draft and any newdiff --git a/docs/content/guides/change-order.mdx b/docs/content/guides/change-order.mdxindex 17e54fa2b0..666dc95feb 100644--- a/docs/content/guides/change-order.mdx+++ b/docs/content/guides/change-order.mdx@@ -91,7 +91,8 @@ supplier, part number, price, and order quantities on the line before you releasAdvance the change notice through its stages: **Draft**, then **"Start"**, then **"Engineering Complete"**,then **"Implementation"**. It only moves forward, one step at a time, and **"Start"**, **"Implementation"**,-and **"Done"** each notify the team. There's no separate approval step to configure — the stages are the+and **"Done"** each notify the assignee and the action-task assignees, so the people working the change hear+about every stage it enters. There's no separate approval step to configure — the stages are theworkflow, and the change notice is its own gate. If the change doesn't pan out, you're not trapped in theforward march either: a **"Cancel"** action sits in the header at every open stage. Cancelling locks thechange notice but keeps every draft, and **"Reopen"** takes it back to **Draft** later with your edits