Fix turnstile
This commit is contained in:
@@ -1,28 +1,34 @@
|
||||
import type { PayloadEmailAdapter, SendEmailOptions } from 'payload'
|
||||
|
||||
import { getSiteIntegrations } from '../payload/index.js'
|
||||
import { graphAdapter, type GraphAdapterArgs } from './graphAdapter.js'
|
||||
import { recordIntegrationStatus, type IntegrationStatus } from '../integrations/status.js'
|
||||
import { panelSmtpAdapter, type PanelSmtpAdapterArgs } from './panelSmtpAdapter.js'
|
||||
import { graphAdapter, type GraphAdapterArgs } from './graphAdapter.js'
|
||||
|
||||
type TransportIntegrations = {
|
||||
/** 'smtp' | 'graph' — chosen by the editor in SiteIntegrations. */
|
||||
emailTransport?: 'graph' | 'smtp' | null
|
||||
emailTransport?: 'smtp' | 'graph' | null
|
||||
emailStatus?: IntegrationStatus | null
|
||||
}
|
||||
|
||||
const STATUS_FIELD = 'emailStatus'
|
||||
/** Error text for the panel: first line, no stack, capped — SMTP errors don't carry the password. */
|
||||
const describe = (err: unknown) =>
|
||||
(err instanceof Error ? err.message : String(err)).split('\n')[0].slice(0, 200)
|
||||
|
||||
export type MailAdapterArgs = {
|
||||
smtp?: PanelSmtpAdapterArgs
|
||||
graph?: GraphAdapterArgs
|
||||
fallbackFromAddress?: string
|
||||
fallbackFromName?: string
|
||||
graph?: GraphAdapterArgs
|
||||
smtp?: PanelSmtpAdapterArgs
|
||||
}
|
||||
|
||||
/** True when the agency Graph credentials are present in the environment. */
|
||||
function graphAvailable(): boolean {
|
||||
return Boolean(
|
||||
process.env.GRAPH_TENANT_ID &&
|
||||
process.env.GRAPH_CLIENT_ID &&
|
||||
process.env.GRAPH_CLIENT_SECRET &&
|
||||
process.env.GRAPH_SENDER,
|
||||
process.env.GRAPH_CLIENT_ID &&
|
||||
process.env.GRAPH_CLIENT_SECRET &&
|
||||
process.env.GRAPH_SENDER,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -69,20 +75,58 @@ export const mailAdapter =
|
||||
sendEmail: async (message: SendEmailOptions) => {
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
const choice = settings.emailTransport ?? 'smtp'
|
||||
const previous = settings[STATUS_FIELD]
|
||||
const useGraph = choice === 'graph' && graphAvailable()
|
||||
const graphFallback = choice === 'graph' && !useGraph
|
||||
|
||||
if (choice === 'graph') {
|
||||
if (graphAvailable()) {
|
||||
return graph.sendEmail(message)
|
||||
}
|
||||
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.',
|
||||
)
|
||||
return smtp.sendEmail(message)
|
||||
}
|
||||
|
||||
return smtp.sendEmail(message)
|
||||
const transport = useGraph ? 'Microsoft Graph' : 'SMTP'
|
||||
const code = useGraph ? 'graph-error' : 'smtp-error'
|
||||
let result: unknown
|
||||
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 as { sent?: boolean; error?: string; detail?: string } | null
|
||||
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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user