WIP: sending method as an explicit choice, kept in payload-kv (superseded: the choice moves to a stored field)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 22:30:03 +01:00
co-authored by Claude Fable 5.2
parent 68a2555277
commit c6841a1674
34 changed files with 740 additions and 96 deletions
+47 -2
View File
@@ -1,10 +1,13 @@
import type { Field, GlobalConfig } from 'payload'
import { ValidationError } from 'payload'
import { userMeetsRole } from '../../modules/access/index.js'
import { isMailMethod, setMailMethod } from '../../modules/email/method.js'
import { INTEGRATION_SECRET_FIELDS, maskSecretsOnRestore, SMTP_PASSWORD_BOUND_TO, writeOnlySecrets } from '../../modules/secrets/writeOnly.js'
import { indexNowFields } from '../../modules/seo/indexNow.js'
import { analyticsFields } from './fields/analytics.js'
import { buildSmtpFields } from './fields/smtp.js'
import { buildSmtpFields, shownMailMethod } from './fields/smtp.js'
import { turnstileFields } from './fields/turnstile.js'
type BuildSiteIntegrationsArgs = {
@@ -87,13 +90,46 @@ export function buildSiteIntegrations({
hooks: {
// a restore answers with the version document: the secrets are masked there too
...(writeOnly ? { afterRead: [maskSecretsOnRestore] } : {}),
afterChange: [
async ({ context, doc, req }) => {
if (isMailMethod(context?.iaplMailMethod)) {
await setMailMethod(req.payload, context.iaplMailMethod)
// (the answer was read before this write: it must show what was just chosen)
if (doc && typeof doc === 'object') {(doc as Record<string, unknown>).emailMethod = context.iaplMailMethod}
} else if (context?.iaplMailMethod === null) {
await setMailMethod(req.payload, null)
}
return doc
},
],
// What the caller sent for the sending method, before Payload merges the stored document into the data.
beforeChange: [
// Keys edited in the panel → clear that integration's old status, so a stale
// "not working" banner doesn't linger until the next form submission.
({ context, data, originalDoc }) => {
async ({ context, data, originalDoc, req }) => {
if (context?.ipalIntegrationStatus || !data) {return data}
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
// "Sending method" (the virtual field `emailMethod`): the choice is written to the key-value store
// once the global is saved (afterChange). The stored `emailTransport` follows where it can say it.
// (`data` is merged with the stored document by now; what the caller really sent was noted before the
// merge — beforeOperation below. Without that note: a value that differs from the stored one was sent.)
const noted = context?.iaplMailSent as { method?: unknown; transport?: boolean } | undefined
const sent = noted ? noted.method : data.emailMethod !== originalDoc?.emailMethod ? data.emailMethod : undefined
// (a virtual field's value is not checked against its options by Payload)
if (sent !== undefined && sent !== null && !(isMailMethod(sent) && (sent !== 'relay' || mailRelay))) {
throw new ValidationError({ errors: [{ message: 'This is not a sending method.', path: 'emailMethod' }], global: 'site-integrations' }, req.t)
}
if (isMailMethod(sent)) {
data.emailMethod = sent
const before = await shownMailMethod(req.payload, (originalDoc ?? {}) as never, mailRelay).catch(() => null)
if (before !== data.emailMethod) {data.emailStatus = cleared}
context.iaplMailMethod = data.emailMethod
data.emailTransport = data.emailMethod === 'graph' ? 'graph' : 'smtp'
} else if ((noted ? noted.transport : true) && changed('emailTransport')) {
// written the old way (a script from before 1.17.1): it means what it meant — the order of 1.17.0
context.iaplMailMethod = null
}
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {data.turnstileStatus = cleared}
if (Object.keys(data).some((k) => (k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
data.emailStatus = cleared
@@ -101,6 +137,15 @@ export function buildSiteIntegrations({
return data
},
],
beforeOperation: [
({ args, context, operation }) => {
const data = (args as { data?: Record<string, unknown> } | undefined)?.data
if (operation === 'update' && data && typeof data === 'object' && context) {
context.iaplMailSent = { method: data.emailMethod ?? undefined, transport: 'emailTransport' in data }
}
return args
},
],
},
label: { en: 'Site integrations', pl: 'Integracje' },
}