Skip to content

Commit ad74386

Browse files
authored
fix(plugin-form-builder): expose formSubmissionID as a variable to be used in emails (#14429)
### What? Adds the `formSubmissionID` as a variable in `submissionData` so it can be used in email templates via double curly brackets `{{formSubmissionID}}`. ### Why? The plugin determines when emails are sent and what data is available to be used. Currently this is being done via the collection `beforeChange` and the `formSubmissionID` does not exist at this point because the doc hasn't been fully created yet. This causes an issue for users wanting to including a direct link to the submission in the email. ### How? 1. Moves the `sendEmail` functionality into the collection `afterChange` hook. 2. In addition, it takes the `formSubmissionID` and passes it along with the rest of the submission data. Issue reported by client.
1 parent f29a07f commit ad74386

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

packages/plugin-form-builder/src/collections/FormSubmissions/hooks/sendEmail.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import type { CollectionBeforeChangeHook } from 'payload'
1+
import type { CollectionAfterChangeHook } from 'payload'
22

33
import type { Email, FormattedEmail, FormBuilderPluginConfig } from '../../../types.js'
44

55
import { serializeLexical } from '../../../utilities/lexical/serializeLexical.js'
66
import { replaceDoubleCurlys } from '../../../utilities/replaceDoubleCurlys.js'
77
import { serializeSlate } from '../../../utilities/slate/serializeSlate.js'
88

9-
type BeforeChangeParams = Parameters<CollectionBeforeChangeHook>[0]
9+
type AfterChangeParams = Parameters<CollectionAfterChangeHook>[0]
1010

1111
export const sendEmail = async (
12-
beforeChangeParameters: BeforeChangeParams,
12+
afterChangeParameters: AfterChangeParams,
1313
formConfig: FormBuilderPluginConfig,
14-
): Promise<BeforeChangeParams['data']> => {
15-
const { data, operation, req } = beforeChangeParameters
16-
17-
if (operation === 'create') {
14+
) => {
15+
if (afterChangeParameters.operation === 'create') {
1816
const {
19-
data: { id: formSubmissionID },
17+
data,
18+
doc: { id: formSubmissionID },
2019
req: { locale, payload },
21-
} = beforeChangeParameters
22-
23-
const { form: formID, submissionData } = data || {}
20+
req,
21+
} = afterChangeParameters
2422

23+
const { form: formID, submissionData: submissionDataFromProps } = data || {}
2524
const { beforeEmail, defaultToEmail, formOverrides } = formConfig || {}
2625

2726
try {
@@ -34,6 +33,14 @@ export const sendEmail = async (
3433

3534
const emails = form.emails as Email[]
3635

36+
const submissionData = [
37+
...submissionDataFromProps,
38+
{
39+
field: 'formSubmissionID',
40+
value: String(formSubmissionID),
41+
},
42+
]
43+
3744
if (emails && emails.length) {
3845
const formattedEmails: FormattedEmail[] = await Promise.all(
3946
emails.map(async (email: Email): Promise<FormattedEmail> => {
@@ -76,7 +83,7 @@ export const sendEmail = async (
7683
let emailsToSend = formattedEmails
7784

7885
if (typeof beforeEmail === 'function') {
79-
emailsToSend = await beforeEmail(formattedEmails, beforeChangeParameters)
86+
emailsToSend = await beforeEmail(formattedEmails, afterChangeParameters)
8087
}
8188

8289
await Promise.all(
@@ -101,6 +108,4 @@ export const sendEmail = async (
101108
payload.logger.error({ err, msg })
102109
}
103110
}
104-
105-
return data
106111
}

packages/plugin-form-builder/src/collections/FormSubmissions/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ export const generateSubmissionCollection = (
100100
: defaultFields,
101101
hooks: {
102102
...(formConfig?.formSubmissionOverrides?.hooks || {}),
103+
afterChange: [
104+
(data) => sendEmail(data, formConfig),
105+
...(formConfig?.formSubmissionOverrides?.hooks?.afterChange || []),
106+
],
103107
beforeChange: [
104108
(data) => createCharge(data, formConfig),
105-
(data) => sendEmail(data, formConfig),
106109
...(formConfig?.formSubmissionOverrides?.hooks?.beforeChange || []),
107110
],
108111
},

0 commit comments

Comments
 (0)