Files
pn-new-crm/src/lib/queue/workers/email.ts
Matt Ciaccio 1a87f28fd4 feat(notifications): wire the notification-digest scheduler (R2-H16)
The 'notification-digest' cron entry in scheduler.ts was registered
but had no handler — admins configured a daily digest time/timezone
at /admin/reminders and got fire-as-they-hit notifications instead.

New runNotificationDigest() service:
- Loads per-port reminder config; skips ports with digestEnabled=false
- Compares the current hour in the port's configured timezone to the
  configured digest time; only fires when the hour matches (cron is
  hourly, so this gate ensures exactly one digest per port per day).
- For every user with a port-role on that port, batches their unread
  notifications from the last 24h (capped at 20 inline + "and N more"
  link to the inbox) into a single digest email.
- Marks the included rows as email_sent so tomorrow's digest doesn't
  resend them.

New email template at notification-digest.ts renders the per-row
type/title/description with deep-link to the in-app inbox.

Email worker now routes case 'notification-digest' to the dispatcher.

1175/1175 vitest passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 23:51:51 +02:00

108 lines
3.8 KiB
TypeScript

import { Worker, type Job } from 'bullmq';
import { env } from '@/lib/env';
import type { ConnectionOptions } from 'bullmq';
import { logger } from '@/lib/logger';
import { attachWorkerAudit } from '@/lib/queue/audit-helpers';
import { QUEUE_CONFIGS } from '@/lib/queue';
export const emailWorker = new Worker(
'email',
async (job: Job) => {
logger.info({ jobId: job.id, jobName: job.name }, 'Processing email job');
switch (job.name) {
case 'inbox-sync': {
const { accountId } = job.data as { accountId: string };
const { syncInbox } = await import('@/lib/services/email-threads.service');
await syncInbox(accountId);
break;
}
case 'send-inquiry-confirmation': {
const { to, firstName, mooringNumber, contactEmail, portId, portName } = job.data as {
to: string;
firstName: string;
mooringNumber: string | null;
contactEmail: string;
portId?: string;
portName?: string;
};
const { inquiryClientConfirmation } =
await import('@/lib/email/templates/inquiry-client-confirmation');
const { sendEmail } = await import('@/lib/email/index');
const { resolveSubject } = await import('@/lib/email/resolve-subject');
const email = inquiryClientConfirmation({ firstName, mooringNumber, contactEmail });
const subject = await resolveSubject({
key: 'inquiry_client_confirmation',
portId,
fallback: email.subject,
tokens: {
portName: portName ?? 'Port Nimara',
recipientName: firstName,
mooringNumber: mooringNumber ?? '',
},
});
await sendEmail(to, subject, email.html, undefined, email.text, portId);
break;
}
case 'send-inquiry-sales-notification': {
const { to, fullName, email, phone, mooringNumber, crmUrl, portId, portName } =
job.data as {
to: string;
fullName: string;
email: string;
phone: string;
mooringNumber: string | null;
crmUrl: string;
portId?: string;
portName?: string;
};
const { inquirySalesNotification } =
await import('@/lib/email/templates/inquiry-sales-notification');
const { sendEmail } = await import('@/lib/email/index');
const { resolveSubject } = await import('@/lib/email/resolve-subject');
const notification = inquirySalesNotification({
fullName,
email,
phone,
mooringNumber,
crmUrl,
});
const subject = await resolveSubject({
key: 'inquiry_sales_notification',
portId,
fallback: notification.subject,
tokens: {
portName: portName ?? 'Port Nimara',
clientName: fullName,
mooringNumber: mooringNumber ?? '',
email,
},
});
await sendEmail(to, subject, notification.html, undefined, notification.text, portId);
break;
}
case 'notification-digest': {
// Recurring scheduler entry (hourly). The dispatcher gates on
// each port's configured digest time + timezone so this is a
// cheap no-op for hours that don't match.
const { runNotificationDigest } =
await import('@/lib/services/notification-digest.service');
await runNotificationDigest();
break;
}
default:
logger.warn({ jobName: job.name }, 'Unknown email job');
}
},
{
connection: { url: env.REDIS_URL } as ConnectionOptions,
concurrency: QUEUE_CONFIGS.email.concurrency,
},
);
emailWorker.on('failed', (job, err) => {
logger.error({ jobId: job?.id, jobName: job?.name, err }, 'Email job failed');
});
attachWorkerAudit(emailWorker, 'email');