fix: webhook template (#25783)

This commit is contained in:
Anik Dhabal Babu authored and GitHub committed 2025-12-12 11:55:09 +00:00
1 parent 728e1173cb
commit eb0cf10f67
2 files changed
+52 -1

No files matched your search

@@ -122,6 +122,47 @@ describe("_onFormSubmission", () => {
});
expect(sendGenericWebhookPayload).toHaveBeenCalledTimes(1);
});
it("should normalize identifiers with spaces to hyphens in rootData for webhook templates", async () => {
const formWithSpaces = {
...mockForm,
fields: [
{
id: "field-1",
identifier: "attendee name",
label: "Attendee Name",
type: "text",
required: false,
},
] as Field[],
};
const responseWithSpaces: FormResponse = {
"field-1": { label: "Attendee Name", value: "John Doe" },
};
const mockWebhook: GetWebhooksReturnType[number] = {
id: "wh-1",
secret: "secret",
subscriberUrl: "https://example.com/webhook",
payloadTemplate: null,
appId: null,
eventTriggers: [WebhookTriggerEvents.FORM_SUBMITTED],
time: null,
timeUnit: null,
};
vi.mocked(getWebhooks).mockResolvedValueOnce([mockWebhook]);
await _onFormSubmission(formWithSpaces, responseWithSpaces, responseId);
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
expect.objectContaining({
rootData: expect.objectContaining({
"attendee-name": "John Doe", // Spaces replaced with hyphens for template access
}),
})
);
});
});
describe("Workflows", () => {
@@ -20,6 +20,15 @@ import getFieldIdentifier from "./getFieldIdentifier";
const moduleLogger = logger.getSubLogger({ prefix: ["routing-forms/lib/formSubmissionUtils"] });
/**
* Normalizes an identifier for use in Handlebars templates by replacing spaces with hyphens.
* This allows webhook payload templates to use {{identifier-with-hyphens}} syntax
* even when the form field identifier contains spaces (e.g., "attendee name" → "attendee-name").
*/
function normalizeIdentifierForHandlebars(identifier: string): string {
return identifier.replace(/\s+/g, "-");
}
type SelectFieldWebhookResponse = string | number | string[] | { label: string; id: string | null };
export type FORM_SUBMITTED_WEBHOOK_RESPONSES = Record<
string,
@@ -184,7 +193,8 @@ export async function _onFormSubmission(
rootData: {
// Send responses unwrapped at root level for backwards compatibility
...Object.entries(fieldResponsesByIdentifier).reduce((acc, [key, value]) => {
acc[key] = value.value;
const normalizedKey = normalizeIdentifierForHandlebars(key);
acc[normalizedKey] = value.value;
return acc;
}, {} as Record<string, FormResponse[keyof FormResponse]["value"]>),
},