import { sql } from "kysely";
import { buildNotificationEntity, emitNotificationCreated, emitNotificationCreatedBatch } from "../lib/notificationFactory.js";
import { getRecipientUserIdsForFeature, getRecipientUserIdsForRole } from "../lib/notificationRecipients.js";
function getDb(em) {
  return em.getKysely();
}
const NOTIFICATIONS_QUEUE_NAME = "notifications";
const metadata = {
  queue: NOTIFICATIONS_QUEUE_NAME,
  id: "notifications:create",
  concurrency: 5
};
async function handle(job, ctx) {
  const { payload } = job;
  if (payload.type === "create") {
    const em = ctx.resolve("em").fork();
    const eventBus = ctx.resolve("eventBus");
    const { input, tenantId, organizationId } = payload;
    const { recipientUserId, ...content } = input;
    const notification = buildNotificationEntity(em, content, recipientUserId, { tenantId, organizationId });
    await em.persist(notification).flush();
    await emitNotificationCreated(eventBus, notification, { tenantId, organizationId });
  } else if (payload.type === "create-role") {
    const em = ctx.resolve("em").fork();
    const eventBus = ctx.resolve("eventBus");
    const { input, tenantId, organizationId } = payload;
    const db = getDb(em);
    const recipientUserIds = await getRecipientUserIdsForRole(db, tenantId, input.roleId);
    if (recipientUserIds.length === 0) {
      return;
    }
    const { roleId: _roleId, ...content } = input;
    const notifications = [];
    for (const recipientUserId of recipientUserIds) {
      const notification = buildNotificationEntity(em, content, recipientUserId, { tenantId, organizationId });
      notifications.push(notification);
    }
    await em.persist(notifications).flush();
    await emitNotificationCreatedBatch(eventBus, notifications, { tenantId, organizationId });
  } else if (payload.type === "create-feature") {
    const em = ctx.resolve("em").fork();
    const eventBus = ctx.resolve("eventBus");
    const { input, tenantId, organizationId } = payload;
    const db = getDb(em);
    const recipientUserIds = await getRecipientUserIdsForFeature(db, tenantId, input.requiredFeature);
    if (recipientUserIds.length === 0) {
      return;
    }
    const notifications = [];
    const { requiredFeature: _requiredFeature, ...content } = input;
    for (const recipientUserId of recipientUserIds) {
      const notification = buildNotificationEntity(em, content, recipientUserId, { tenantId, organizationId });
      notifications.push(notification);
    }
    await em.persist(notifications).flush();
    await emitNotificationCreatedBatch(eventBus, notifications, { tenantId, organizationId });
  } else if (payload.type === "cleanup-expired") {
    const em = ctx.resolve("em").fork();
    const db = getDb(em);
    await db.updateTable("notifications").set({
      status: "dismissed",
      dismissed_at: sql`now()`
    }).where("expires_at", "<", sql`now()`).where("status", "not in", ["actioned", "dismissed"]).execute();
  }
}
export {
  NOTIFICATIONS_QUEUE_NAME,
  handle as default,
  metadata
};
//# sourceMappingURL=create-notification.worker.js.map
