import { getSiteIntegrations } from '../payload/index.js'; import { recordIntegrationStatus } from '../integrations/status.js'; import { panelSmtpAdapter } from './panelSmtpAdapter.js'; import { graphAdapter } from './graphAdapter.js'; const STATUS_FIELD = 'emailStatus'; /** Error text for the panel: first line, no stack, capped — SMTP errors don't carry the password. */ const describe = (err)=>(err instanceof Error ? err.message : String(err)).split('\n')[0].slice(0, 200); /** True when the agency Graph credentials are present in the environment. */ function graphAvailable() { return Boolean(process.env.GRAPH_TENANT_ID && process.env.GRAPH_CLIENT_ID && process.env.GRAPH_CLIENT_SECRET && process.env.GRAPH_SENDER); } /** * Dispatcher email adapter: wired into the config ONCE, but picks the transport * (SMTP or Graph) per send by reading `emailTransport` from SiteIntegrations. * This is what makes the choice switchable in the panel — Payload builds the * email adapter at boot and can't swap it at runtime, so instead of choosing * between two adapters at boot we install one that delegates on every send. * * Availability guard: Graph only runs if its agency credentials exist in env * (this is *our* Exchange). If the panel says 'graph' but env isn't set up, * we DON'T silently fail — we log clearly and fall back to SMTP, so a client * flipping the switch without the backing config still gets mail out (over SMTP) * rather than silent nothing. If neither is usable, the send reports an error. * * @example * // payload.config.ts * import { mailAdapter } from '@intecion/ipal-kit' * email: mailAdapter() */ export const mailAdapter = (args = {})=>(deps)=>{ // Build both delegates once; each still resolves its own config per send. const smtp = panelSmtpAdapter({ fallbackFromAddress: args.fallbackFromAddress, fallbackFromName: args.fallbackFromName, ...args.smtp })(deps); const graph = graphAdapter({ fallbackFromAddress: args.fallbackFromAddress, fallbackFromName: args.fallbackFromName, ...args.graph })(deps); const { payload } = deps; return { name: 'ipal-mail-dispatcher', defaultFromAddress: smtp.defaultFromAddress, defaultFromName: smtp.defaultFromName, sendEmail: async (message)=>{ const settings = await getSiteIntegrations(payload); const choice = settings.emailTransport ?? 'smtp'; const previous = settings[STATUS_FIELD]; const useGraph = choice === 'graph' && graphAvailable(); const graphFallback = choice === 'graph' && !useGraph; if (graphFallback) { // Panel asked for Graph but the agency creds aren't configured for // this project. Fall back to SMTP rather than silently dropping mail. payload.logger.warn('[ipal] Transport set to Graph but GRAPH_* env vars are missing; falling back to SMTP.'); } const transport = useGraph ? 'Microsoft Graph' : 'SMTP'; const code = useGraph ? 'graph-error' : 'smtp-error'; let result; try { result = await (useGraph ? graph : smtp).sendEmail(message); } catch (err) { await recordIntegrationStatus(payload, STATUS_FIELD, previous, { state: 'degraded', code, message: `${transport}: ${describe(err)}` }); throw err // callers (form-builder, Send test) still see the failure ; } // The delegates report failure as { sent: false, error, detail? } instead // of throwing (a broken mailbox must not crash a form submission). const failed = result; if (failed && typeof failed === 'object' && failed.sent === false) { await recordIntegrationStatus(payload, STATUS_FIELD, previous, { state: 'degraded', code, message: `${transport}: ${describe(failed.detail ?? failed.error ?? 'not sent')}` }); return result; } // Graph selected but unavailable is a configuration problem even if SMTP delivered. await recordIntegrationStatus(payload, STATUS_FIELD, previous, graphFallback ? { state: 'degraded', code: 'graph-unavailable', message: 'Microsoft Graph is selected but not configured on the server — e-mails are sent over SMTP.' } : { state: 'ok' }); return result; } }; }; //# sourceMappingURL=mailAdapter.js.map