import nodemailer from 'nodemailer' import { prisma } from './prisma' import { NotifType } from '@prisma/client' const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: Number(process.env.SMTP_PORT) || 587, secure: false, auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, }, }) const FROM = process.env.EMAIL_FROM || 'qms@acmequality.com' const APP_URL = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000' export async function sendEmail(to: string, subject: string, html: string) { if (!process.env.SMTP_HOST) { console.log('[EMAIL SKIPPED - no SMTP config]', { to, subject }) return } await transporter.sendMail({ from: FROM, to, subject, html }) } export async function createNotification( userId: string, type: NotifType, title: string, body: string, link?: string ) { return prisma.notification.create({ data: { userId, type, title, body, link }, }) } export async function notifyCAPAOverdue(capaRef: string, capaTitle: string, ownerEmail: string, ownerId: string) { const link = `${APP_URL}/qc/capas` await createNotification(ownerId, 'CAPA_OVERDUE', `${capaRef} is overdue`, `${capaTitle} has passed its due date.`, link) await sendEmail(ownerEmail, `[V11 QMS] CAPA ${capaRef} overdue`, `

Hello,

CAPA ${capaRef}: ${capaTitle} has passed its due date and requires immediate attention.

View CAPA →

`) } export async function notifyNCREscalated(ncrRef: string, ncrDesc: string, managerEmail: string, managerId: string) { const link = `${APP_URL}/qc/ncr` await createNotification(managerId, 'NCR_ESCALATED', `${ncrRef} escalated`, ncrDesc, link) await sendEmail(managerEmail, `[V11 QMS] NCR ${ncrRef} escalated`, `

NCR ${ncrRef} has been escalated: ${ncrDesc}

View NCR →

`) } export async function notifyFormReviewReady(formName: string, submissionCount: number, adminEmail: string, adminId: string) { const link = `${APP_URL}/admin/forms` await createNotification(adminId, 'FORM_REVIEW_READY', `${formName} ready for review`, `${submissionCount} submissions collected. QC review can begin.`, link) await sendEmail(adminEmail, `[V11 QMS] Form ready for QC review: ${formName}`, `

${formName} has reached ${submissionCount} submissions and is ready for QC standard review.

Review now →

`) } export async function notifySolutionConfirmed(ncrRef: string, ncrDesc: string, reporterEmail: string, reporterId: string) { const link = `${APP_URL}/qc/ncr` await createNotification(reporterId, 'SOLUTION_CONFIRMED', `Fix confirmed for ${ncrRef}`, `The issue you reported (${ncrDesc}) has been resolved. No action needed from you.`, link) await sendEmail(reporterEmail, `[V11 QMS] Fixed: ${ncrRef}`, `

Good news — the issue you reported has been resolved:

${ncrDesc}

No further action is needed from you.

`) } export function emailTemplate(title: string, body: string) { return ` ${title}
V11 QMS

${title}

${body}
This is an automated message from V11 Enterprise QMS. Do not reply.
` }