import type { ResourceStep } from "@voidy/framework"; import type { Resource, VoidyClient } from "@voidy/framework"; import type { ResourceCache } from "@voidy/framework"; import type { NotificationResource } from "./types"; import { createNotificationWorker } from "../queue"; export const notificationStep: ResourceStep = { name: "notification", match(resource: Resource): boolean { return resource.type === "notification"; }, process(resource: Resource, { cache }): Resource | null { const notif = resource as NotificationResource; if (!notif.id || !notif.execute) { console.warn(`[NotificationStep] Skipping invalid notification: missing id or execute`); return null; } cache.set("notification", notif.id, notif); return notif; }, finalize(client: VoidyClient, cache: ResourceCache): void { const notifications = cache.getAll("notification"); if (notifications.size === 0) return; createNotificationWorker(async (job) => { const { notificationId, ...data } = job.data as { notificationId: string } & Record; const resource = notifications.get(notificationId); if (!resource) { console.warn(`[NotificationWorker] Unknown notification: ${notificationId}`); return; } try { await resource.execute({ client, resource, data }); } catch (err) { console.error(`[NotificationWorker] Error in notification "${notificationId}":`, err); } }); console.log(`[Notifications] Worker started, ${notifications.size} notification type(s) registered`); }, };