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(jobs): drop notification recipients who are no longer company members

naveenkash00a056ab24ca
diff --git a/.claude/rules/user-employee-job-relationships.md b/.claude/rules/user-employee-job-relationships.md
index 755878468f..b9cdc41b62 100644
--- a/.claude/rules/user-employee-job-relationships.md
+++ b/.claude/rules/user-employee-job-relationships.md
@@ -81,5 +81,11 @@ is the separate `job` table from `20240909194622_jobs.sql`).
- Permission company-ID array `"0"` means "all companies" — don't string-match a literal company id only.
- Deactivating an employee (`deactivateEmployee` in `users.server.ts`) removes the company from
`userPermission.permissions`, deletes the `userToCompany` and `employeeJob` rows, sets
- `employee.active = false`, and invalidates the Redis claims cache.
+ `employee.active = false`, and invalidates the Redis claims cache. It does **not** clear
+ `user.active` or scrub stored userId references (assignee columns, `workCenterEmployee`,
+ notification-group settings arrays).
+- Notification fan-out (`notify.ts` `resolve-recipients` in `packages/jobs`) filters resolved
+ recipients against `userToCompany` for the notification's company — a missing membership row
+ (i.e. a deactivated user) is dropped before any in-app/email/Slack delivery, regardless of how
+ the recipient list was built.
- Permission/claims helper SQL functions are redefined across many migrations — always read the newest.
diff --git a/packages/jobs/src/inngest/functions/notifications/notify.ts b/packages/jobs/src/inngest/functions/notifications/notify.ts
index c534c78e8c..6136cbb8e0 100644
--- a/packages/jobs/src/inngest/functions/notifications/notify.ts
+++ b/packages/jobs/src/inngest/functions/notifications/notify.ts
@@ -285,7 +285,29 @@ export const notifyFunction = inngest.createFunction(
}
// Don't notify the sender about their own action.
if (payload.from) ids = ids.filter((id) => id !== payload.from);
- return [...new Set(ids)];
+ ids = [...new Set(ids)];
+
+ // Deactivation deletes the userToCompany row but leaves stale userId
+ // references behind (assignee columns, notification-group settings,
+ // workCenterEmployee), so keep only current company members — otherwise
+ // ex-employees keep receiving email/Slack notifications.
+ if (ids.length > 0) {
+ const members = await client
+ .from("userToCompany")
+ .select("userId")
+ .eq("companyId", payload.companyId)
+ .in("userId", ids);
+ if (members.error) {
+ console.error(
+ "Failed to filter recipients by company membership",
+ members.error
+ );
+ throw members.error;
+ }
+ const memberIds = new Set(members.data.map((m) => m.userId));
+ ids = ids.filter((id) => memberIds.has(id));
+ }
+ return ids;
});
if (userIds.length === 0) {