58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
/**
|
|
* Maps OutboxEvent eventType strings to BullMQ queue names.
|
|
*
|
|
* The convention is `<domain>.<entity>.<verb>`, e.g.
|
|
* `billing.subscription.updated` or `mail.welcome.send`. The router
|
|
* picks the queue by the first segment (domain) but composite prefixes
|
|
* such as `tenant.subscription.` can override individual mappings.
|
|
*
|
|
* See docs/architecture/outbox-pattern.md.
|
|
*/
|
|
|
|
/** Built-in routing prefixes. Order matters: longest-prefix wins. */
|
|
export const DEFAULT_OUTBOX_ROUTING: ReadonlyArray<{ prefix: string; queue: string }> = [
|
|
{ prefix: "tenant.subscription.", queue: "outbox.notification" },
|
|
{ prefix: "mail.", queue: "outbox.mail" },
|
|
{ prefix: "webhook.", queue: "outbox.webhook" },
|
|
{ prefix: "notification.", queue: "outbox.notification" },
|
|
{ prefix: "audit.", queue: "outbox.audit" },
|
|
{ prefix: "workflow.", queue: "outbox.workflow" },
|
|
{ prefix: "search.index.", queue: "outbox.search-index" },
|
|
{ prefix: "marketing.lead.", queue: "outbox.marketing" },
|
|
{ prefix: "form.submission.", queue: "outbox.form-submission-route" },
|
|
{ prefix: "billing.", queue: "outbox.notification" },
|
|
{ prefix: "plugin.hook.", queue: "outbox.plugin-hook" },
|
|
{ prefix: "plugin.capability-use.", queue: "outbox.plugin-capability-use" },
|
|
];
|
|
|
|
export type EventRoutingRule = { prefix: string; queue: string };
|
|
|
|
export type EventRouterResolveOptions = {
|
|
rules?: ReadonlyArray<EventRoutingRule>;
|
|
};
|
|
|
|
/**
|
|
* Resolve the queue name for an eventType. Returns `undefined` when no
|
|
* routing rule matches — the caller (dispatcher) treats this as a
|
|
* dead-letter condition.
|
|
*/
|
|
export function resolveOutboxQueueName(
|
|
eventType: string,
|
|
options: EventRouterResolveOptions = {}
|
|
): string | undefined {
|
|
if (typeof eventType !== "string" || eventType.length === 0) {
|
|
return undefined;
|
|
}
|
|
const rules = options.rules ?? DEFAULT_OUTBOX_ROUTING;
|
|
// Longest matching prefix wins.
|
|
let best: { prefix: string; queue: string } | undefined;
|
|
for (const rule of rules) {
|
|
if (eventType.startsWith(rule.prefix)) {
|
|
if (!best || rule.prefix.length > best.prefix.length) {
|
|
best = rule;
|
|
}
|
|
}
|
|
}
|
|
return best?.queue;
|
|
}
|