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
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { getCarbonServiceRole } from "@carbon/auth/client.server";
import {
getNotificationTopicPhrase,
NotificationEvent,
type NotificationTopic
} from "@carbon/notifications";
import { getJobDatabaseClient } from "../../../db";
import { inngest } from "../../client";
// Roll up unread, undigested notifications older than DIGEST_MIN_AGE_MIN that
// share (userId, companyId, topic) when the group has DIGEST_THRESHOLD+ rows.
// One digest row replaces them in the topbar (the hook filters out rows
// where digestedInto is set), and child rows are kept for audit/recovery
// until the purge cron drops them.
//
// Re-runs absorb new children into an existing unread digest for the same
// group instead of creating a new digest each pass — that keeps the topbar
// to one entry per topic regardless of how often the cron fires.
// The threshold and the age below are repeated in
// `util.notification_digest_has_work` (the migration that defines it). Change
// both: a stricter rule there leaves these digests unmade.
// the `scheduled-sql-constants` check (@carbon/checks) fails when they disagree.
export const DIGEST_THRESHOLD = 5;
// Minutes. Set to 0 for instant testing; production target is ~60 so users
// get a chance to see live notifications before they roll up.
export const DIGEST_MIN_AGE_MIN = 60;
const DIGEST_MAX_CANDIDATES = 5000;
type Candidate = {
id: string;
userId: string;
companyId: string;
topic: NotificationTopic;
createdAt: string;
};
type ExistingDigest = Candidate & { payload: unknown };
type Keeper = { id: string; topic: NotificationTopic; count: number | null };
/**
* The digests whose title no longer says how many notifications they hold.
* Rewriting the rest changed nothing, and was one count and one write per
* unread digest every fifteen minutes.
*/
export function staleDigests(
keepers: Keeper[],
children: ReadonlyMap<string, number>
) {
return keepers.flatMap((keeper) => {
const total = children.get(keeper.id) ?? 0;
return total === keeper.count ? [] : [{ ...keeper, total }];
});
}
function digestCount(payload: unknown): number | null {
const count = (payload as { count?: unknown } | null)?.count;
return typeof count === "number" ? count : null;
}
function bucketKey(userId: string, companyId: string, topic: string): string {
return `${userId}::${companyId}::${topic}`;
}
export const notificationDigestFunction = inngest.createFunction(
// Woken by the database, not the clock: pg_cron asks every 15 minutes
// whether a run would write anything (`util.notification_digest_has_work`)
// and only then sends this event.
{ id: "notification-digest", retries: 2, concurrency: { limit: 1 } },
{ event: "carbon/notification-digest.process" },
async ({ step, logger }) => {
const client = getCarbonServiceRole();
const work = await step.run("collect-work", async () => {
const cutoff = new Date(
Date.now() - DIGEST_MIN_AGE_MIN * 60 * 1000
).toISOString();
const [
{ data: candidateRows, error: candidateErr },
{ data: digestRows, error: digestErr }
] = await Promise.all([
client
.from("notification")
.select("id, userId, companyId, topic, createdAt")
.is("readAt", null)
.is("digestedInto", null)
.neq("event", NotificationEvent.Digest)
.lt("createdAt", cutoff)
.order("createdAt", { ascending: true })
.limit(DIGEST_MAX_CANDIDATES),
client
.from("notification")
.select("id, userId, companyId, topic, createdAt, payload")
.is("readAt", null)
.is("digestedInto", null)
.eq("event", NotificationEvent.Digest)
.order("createdAt", { ascending: true })
.limit(DIGEST_MAX_CANDIDATES)
]);
if (candidateErr) {
logger.error("Failed to load digest candidates", {
error: candidateErr
});
throw candidateErr;
}
if (digestErr) {
logger.error("Failed to load existing digests", { error: digestErr });
throw digestErr;
}
const candidates = new Map<string, Candidate[]>();
for (const row of (candidateRows ?? []) as Candidate[]) {
const key = bucketKey(row.userId, row.companyId, row.topic);
const existing = candidates.get(key);
if (existing) {
existing.push(row);
} else {
candidates.set(key, [row]);
}
}
const digests = new Map<string, ExistingDigest[]>();
for (const row of (digestRows ?? []) as ExistingDigest[]) {
const key = bucketKey(row.userId, row.companyId, row.topic);
const existing = digests.get(key);
if (existing) {
existing.push(row);
} else {
digests.set(key, [row]);
}
}
// Build the union of bucket keys — we want to consolidate stacked
// digests even when no new children are pending.
const keys = new Set<string>([...candidates.keys(), ...digests.keys()]);
return Array.from(keys).map((key) => ({
candidates: candidates.get(key) ?? [],
digests: digests.get(key) ?? [],
key
}));
});
if (work.length === 0) {
return { absorbed: 0, created: 0, groups: 0, merged: 0 };
}
const summary = await step.run("apply-digests", async () => {
let created = 0;
let merged = 0;
let absorbed = 0;
const keepers: Keeper[] = [];
for (const group of work) {
const newChildren = group.candidates;
const existingDigests = group.digests;
// Case A: no existing digest. Only act if new children meet the
// threshold; otherwise leave them alone (they may digest next pass).
if (existingDigests.length === 0) {
if (newChildren.length < DIGEST_THRESHOLD) continue;
const head = newChildren[0]!;
const description = getNotificationTopicPhrase(
head.topic,
newChildren.length
);
const { data: createdRow, error: insertError } = await client
.from("notification")
.insert({
companyId: head.companyId,
event: NotificationEvent.Digest,
payload: {
count: newChildren.length,
description,
event: NotificationEvent.Digest,
topic: head.topic
},
title: description,
topic: head.topic,
userId: head.userId
})
.select("id")
.single();
if (insertError || !createdRow?.id) {
logger.error("Failed to insert digest row", { error: insertError });
continue;
}
const { error: updateError } = await client
.from("notification")
.update({ digestedInto: createdRow.id })
.in(
"id",
newChildren.map((r) => r.id)
);
if (updateError) {
logger.error("Failed to attach children to digest", {
error: updateError
});
continue;
}
created += 1;
absorbed += newChildren.length;
continue;
}
// Case B: at least one existing unread digest. Pick the oldest as the
// keeper, repoint everything else into it, delete the other digest
// rows, and refresh the keeper's title/count.
const keeper = existingDigests[0]!;
const others = existingDigests.slice(1);
if (others.length > 0) {
const { error: repointErr } = await client
.from("notification")
.update({ digestedInto: keeper.id })
.in(
"digestedInto",
others.map((d) => d.id)
);
if (repointErr) {
logger.error("Failed to repoint stacked digest children", {
error: repointErr
});
continue;
}
const { error: deleteErr } = await client
.from("notification")
.delete()
.in(
"id",
others.map((d) => d.id)
);
if (deleteErr) {
logger.error("Failed to delete stacked digest rows", {
error: deleteErr
});
continue;
}
merged += others.length;
}
if (newChildren.length > 0) {
const { error: absorbErr } = await client
.from("notification")
.update({ digestedInto: keeper.id })
.in(
"id",
newChildren.map((r) => r.id)
);
if (absorbErr) {
logger.error("Failed to absorb children into digest", {
error: absorbErr
});
continue;
}
absorbed += newChildren.length;
}
keepers.push({
id: keeper.id,
topic: keeper.topic,
count: digestCount(keeper.payload)
});
}
// Refresh title/count from the authoritative child count: one grouped
// count for every digest, then a write only where the count moved.
// A failed count skips the refresh rather than failing the step: a
// retry would replay Case A and insert its digests a second time.
const counted =
keepers.length === 0
? []
: await getJobDatabaseClient()
.selectFrom("notification")
.select((eb) => ["digestedInto", eb.fn.countAll().as("children")])
.where(
"digestedInto",
"in",
keepers.map((keeper) => keeper.id)
)
.groupBy("digestedInto")
.execute()
.catch((error) => {
logger.error("Failed to count digest children", { error });
return null;
});
if (counted) {
const children = new Map(
counted.map((row) => [row.digestedInto ?? "", Number(row.children)])
);
for (const keeper of staleDigests(keepers, children)) {
const description = getNotificationTopicPhrase(
keeper.topic,
keeper.total
);
const { error: titleErr } = await client
.from("notification")
.update({
payload: {
count: keeper.total,
description,
event: NotificationEvent.Digest,
topic: keeper.topic
},
title: description
})
.eq("id", keeper.id);
if (titleErr) {
logger.error("Failed to refresh digest title", { error: titleErr });
}
}
}
return { absorbed, created, groups: work.length, merged };
});
return summary;
}
);