Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
import { Worker, type Job } from 'bullmq';
|
fix(ops): /health DB+Redis checks, validated env.REDIS_URL across workers, error_events 90d retention
Three audit-pass-#3 findings, all in the "wakes you at 3am" category.
- /api/public/health now runs DB SELECT 1 + Redis PING in parallel and
returns 503 + a degraded payload when either fails. Anonymous probes
(no X-Intake-Secret) still get a flat {status:'ok'} so generic uptime
monitors keep working; authenticated probes see the dep results.
- All worker entrypoints (ai, bulk, documents, email, export, import,
maintenance, notifications, reports, webhooks) and src/lib/redis.ts
now use env.REDIS_URL (Zod-validated at boot) instead of
process.env.REDIS_URL!. Previously a missing env let the app start
silently and fail at first job pickup.
- maintenance worker gains an `error-events-retention` case that
delete()s rows older than 90 days from error_events. scheduler.ts
registers it at 06:00 daily. Closes the contract from migration
0040 which declared the table "pruned at 90 days" but had no
implementation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 14:59:07 +02:00
|
|
|
import { env } from '@/lib/env';
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
|
|
|
|
|
import type { ConnectionOptions } from 'bullmq';
|
|
|
|
|
import { logger } from '@/lib/logger';
|
2026-05-06 20:44:38 +02:00
|
|
|
import { attachWorkerAudit } from '@/lib/queue/audit-helpers';
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2026-04-14 13:00:14 -04:00
|
|
|
case 'send-inquiry-confirmation': {
|
2026-05-06 22:26:41 +02:00
|
|
|
const { to, firstName, mooringNumber, contactEmail, portId, portName } = job.data as {
|
2026-04-14 13:00:14 -04:00
|
|
|
to: string;
|
|
|
|
|
firstName: string;
|
|
|
|
|
mooringNumber: string | null;
|
|
|
|
|
contactEmail: string;
|
2026-05-06 22:26:41 +02:00
|
|
|
portId?: string;
|
|
|
|
|
portName?: string;
|
2026-04-14 13:00:14 -04:00
|
|
|
};
|
|
|
|
|
const { inquiryClientConfirmation } =
|
|
|
|
|
await import('@/lib/email/templates/inquiry-client-confirmation');
|
|
|
|
|
const { sendEmail } = await import('@/lib/email/index');
|
2026-05-06 22:26:41 +02:00
|
|
|
const { resolveSubject } = await import('@/lib/email/resolve-subject');
|
2026-04-14 13:00:14 -04:00
|
|
|
const email = inquiryClientConfirmation({ firstName, mooringNumber, contactEmail });
|
2026-05-06 22:26:41 +02:00
|
|
|
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);
|
2026-04-14 13:00:14 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'send-inquiry-sales-notification': {
|
2026-05-06 22:26:41 +02:00
|
|
|
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;
|
|
|
|
|
};
|
2026-04-14 13:00:14 -04:00
|
|
|
const { inquirySalesNotification } =
|
|
|
|
|
await import('@/lib/email/templates/inquiry-sales-notification');
|
|
|
|
|
const { sendEmail } = await import('@/lib/email/index');
|
2026-05-06 22:26:41 +02:00
|
|
|
const { resolveSubject } = await import('@/lib/email/resolve-subject');
|
2026-04-14 13:00:14 -04:00
|
|
|
const notification = inquirySalesNotification({
|
|
|
|
|
fullName,
|
|
|
|
|
email,
|
|
|
|
|
phone,
|
|
|
|
|
mooringNumber,
|
|
|
|
|
crmUrl,
|
|
|
|
|
});
|
2026-05-06 22:26:41 +02:00
|
|
|
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);
|
2026-04-14 13:00:14 -04:00
|
|
|
break;
|
|
|
|
|
}
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
default:
|
|
|
|
|
logger.warn({ jobName: job.name }, 'Unknown email job');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
fix(ops): /health DB+Redis checks, validated env.REDIS_URL across workers, error_events 90d retention
Three audit-pass-#3 findings, all in the "wakes you at 3am" category.
- /api/public/health now runs DB SELECT 1 + Redis PING in parallel and
returns 503 + a degraded payload when either fails. Anonymous probes
(no X-Intake-Secret) still get a flat {status:'ok'} so generic uptime
monitors keep working; authenticated probes see the dep results.
- All worker entrypoints (ai, bulk, documents, email, export, import,
maintenance, notifications, reports, webhooks) and src/lib/redis.ts
now use env.REDIS_URL (Zod-validated at boot) instead of
process.env.REDIS_URL!. Previously a missing env let the app start
silently and fail at first job pickup.
- maintenance worker gains an `error-events-retention` case that
delete()s rows older than 90 days from error_events. scheduler.ts
registers it at 06:00 daily. Closes the contract from migration
0040 which declared the table "pruned at 90 days" but had no
implementation.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 14:59:07 +02:00
|
|
|
connection: { url: env.REDIS_URL } as ConnectionOptions,
|
Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM,
PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source
files covering clients, berths, interests/pipeline, documents/EOI,
expenses/invoices, email, notifications, dashboard, admin, and
client portal. CI/CD via Gitea Actions with Docker builds.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 11:52:51 +01:00
|
|
|
concurrency: QUEUE_CONFIGS.email.concurrency,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
emailWorker.on('failed', (job, err) => {
|
|
|
|
|
logger.error({ jobId: job?.id, jobName: job?.name, err }, 'Email job failed');
|
|
|
|
|
});
|
2026-05-06 20:44:38 +02:00
|
|
|
|
|
|
|
|
attachWorkerAudit(emailWorker, 'email');
|