import { getSiteIntegrations } from '../payload/index.js'; import { graphAdapter } from './graphAdapter.js'; import { panelSmtpAdapter } from './panelSmtpAdapter.js'; /** 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'; if (choice === 'graph') { if (graphAvailable()) { return graph.sendEmail(message); } // 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.'); return smtp.sendEmail(message); } return smtp.sendEmail(message); } }; }; //# sourceMappingURL=mailAdapter.js.map