/** * Create a notification from a factory event, evaluate rules, and dispatch * to configured outputs. This is the main entry point for the notification * system; call it from the sweep, health, or any event handler. */ import { newNotificationId, resolveRules, shouldNotify, type Notification, type NotificationCategory, type NotificationSeverity, } from './types.ts' import { saveNotification, loadRules } from './store.ts' import { dispatchDiscord, dispatchWake, type OutputResult } from './outputs.ts' import { pushForNotification } from '../push/notify.ts' export type CreateNotificationInput = { projectId: string title: string body: string severity: NotificationSeverity category: NotificationCategory itemId?: string source?: string dryRun?: boolean } export type CreateNotificationResult = { created: boolean notification: Notification | null outputs: OutputResult[] skippedReason?: string } export async function createNotification(input: CreateNotificationInput): Promise { const rules = await loadRules() const rule = resolveRules(rules, input.projectId) if (!shouldNotify(rule, input.category, input.severity)) { return { created: false, notification: null, outputs: [], skippedReason: `Rules filtered out ${input.category}/${input.severity} for project ${input.projectId}`, } } const notification: Notification = { id: newNotificationId(), projectId: input.projectId, title: input.title, body: input.body, severity: input.severity, category: input.category, status: 'unread', itemId: input.itemId ?? null, source: input.source ?? 'factory', createdAt: new Date().toISOString(), } if (!input.dryRun) { await saveNotification(notification) } const outputs: OutputResult[] = [] if (!input.dryRun) { outputs.push(await dispatchDiscord(notification, rule)) outputs.push(await dispatchWake(notification, rule)) const push = await pushForNotification(notification) outputs.push({ output: 'push', sent: 'sent' in push && push.sent > 0, ...('skipped' in push && push.skipped ? { error: push.skipped } : {}) }) } return { created: true, notification, outputs } }