WIP: form attachments (e-mail only) — core, endpoint, Graph large upload

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:20:02 +01:00
co-authored by Claude Fable 5.2
parent 0ba0014ed5
commit 6003b3d7d7
36 changed files with 2399 additions and 97 deletions
+51 -8
View File
@@ -7,6 +7,11 @@ import type { AdminText } from '../admin-i18n/define.js'
import type { FormsOption } from './types.js'
import { userMeetsRole } from '../access/predicates.js'
import { resolveFormAttachments } from './attachments/config.js'
import { buildFileDeliveryHook, buildFileNoticeHook } from './attachments/delivery.js'
import { buildFormSubmitEndpoint } from './attachments/endpoint.js'
import { buildFileBlock } from './attachments/fileBlock.js'
import { buildFormFilesAfterRead, buildFormFilesBeforeValidate, submissionFileFields } from './attachments/formHooks.js'
import { checkboxGroupBlock, stepBlock, timeBlock, withPlaceholder, withShowWhen } from './builderBlocks.js'
import { buildEmailLayoutHook } from './emailLayout.js'
@@ -206,15 +211,25 @@ function hideEmails(fields: Field[]): Field[] {
export function buildFormsPlugin(forms: FormsOption): Plugin {
const adminGroup = forms.adminGroup === undefined ? { en: 'Forms', pl: 'Formularze' } : forms.adminGroup
const group = adminGroup ? { group: adminGroup } : {}
const layout = forms.emailLayout ? buildEmailLayoutHook({ formsSlug: forms.formOverrides?.slug ?? 'forms' }) : undefined
const formsSlug = forms.formOverrides?.slug ?? 'forms'
const layout = forms.emailLayout ? buildEmailLayoutHook({ formsSlug }) : undefined
// Files (forms.attachments): never stored — attached to the company e-mail by
// the two hooks around the layout, and dropped (attachments/delivery).
const attachments = forms.attachments ? resolveFormAttachments(forms.attachments) : null
const fileNotice = attachments ? buildFileNoticeHook({ formsSlug, notice: attachments.notice }) : undefined
const fileDelivery = attachments ? buildFileDeliveryHook({ formsSlug }) : undefined
const beforeEmail: BeforeEmail | undefined =
layout || forms.beforeEmail
layout || forms.beforeEmail || attachments
? async (emails, args) => {
const laidOut = layout ? await layout(emails, args) : emails
return forms.beforeEmail ? forms.beforeEmail(laidOut, args) : laidOut
const noticed = fileNotice ? await fileNotice(emails, args) : emails
const laidOut = layout ? await layout(noticed, args) : noticed
const changed = forms.beforeEmail ? await forms.beforeEmail(laidOut, args) : laidOut
return fileDelivery ? fileDelivery(changed, args) : changed
}
: undefined
return formBuilderPlugin({
const fileBlockOption = forms.fields?.file
const formHooks = forms.formOverrides?.hooks
const builder = formBuilderPlugin({
...(beforeEmail ? { beforeEmail } : {}),
// Built-in types + ipal's `step`, `checkboxGroup` and `time` (builderBlocks). All are
// panel choices only — a form uses what the editor adds, nothing changes on
@@ -225,6 +240,10 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
checkboxGroup: forms.fields?.checkboxGroup === false ? false : checkboxGroupBlock,
date: builtin('date'),
email: withHint('email'),
// File (ipal): only with forms.attachments; `false` hides it, a block object replaces it.
...(attachments && fileBlockOption !== false
? { file: (typeof fileBlockOption === 'object' ? fileBlockOption : buildFileBlock(attachments)) as never }
: {}),
message: builtin('message'),
number: withHint('number'),
payment: false,
@@ -236,7 +255,7 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
time: forms.fields?.time === false ? false : timeBlock,
...Object.fromEntries(
// `true` keeps ipal's block (with "Show only when"); false / an object override it
Object.entries(forms.fields ?? {}).filter(([k, v]) => k !== 'checkboxGroup' && k !== 'step' && k !== 'time' && v !== true),
Object.entries(forms.fields ?? {}).filter(([k, v]) => k !== 'checkboxGroup' && k !== 'file' && k !== 'step' && k !== 'time' && v !== true),
),
},
...(forms.redirectRelationships
@@ -258,6 +277,15 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
...forms.formOverrides?.access,
},
admin: { ...group, ...forms.formOverrides?.admin },
...(attachments
? {
hooks: {
...formHooks,
afterRead: [...(formHooks?.afterRead ?? []), buildFormFilesAfterRead(attachments)],
beforeValidate: [...(formHooks?.beforeValidate ?? []), buildFormFilesBeforeValidate()],
},
}
: {}),
fields: ({ defaultFields }) => {
const protectedFields = hideEmails(localizeFormBuilder(defaultFields))
return forms.formOverrides?.fields ? forms.formOverrides.fields({ defaultFields: protectedFields }) : protectedFields
@@ -272,11 +300,26 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
read: submissionsReadAccess,
...forms.formSubmissionOverrides?.access,
},
admin: { ...group, defaultColumns: ['form', 'createdAt'], ...forms.formSubmissionOverrides?.admin },
admin: {
...group,
defaultColumns: attachments ? ['form', 'filesDelivery', 'createdAt'] : ['form', 'createdAt'],
...forms.formSubmissionOverrides?.admin,
},
fields: ({ defaultFields }) => {
const labelled = labelSubmissionFields(defaultFields)
const labelled = [...labelSubmissionFields(defaultFields), ...(attachments ? submissionFileFields : [])]
return forms.formSubmissionOverrides?.fields ? forms.formSubmissionOverrides.fields({ defaultFields: labelled }) : labelled
},
},
})
if (!attachments) {return builder}
// The submit endpoint for forms with files, and the resolved option where
// submitForm and the endpoint read it (payload.config.custom.ipal.formAttachments).
return async (incoming) => {
const config = await builder(incoming)
return {
...config,
custom: { ...config.custom, ipal: { ...(config.custom?.ipal as object | undefined), formAttachments: attachments } },
endpoints: [...(config.endpoints ?? []), buildFormSubmitEndpoint(attachments)],
}
}
}