fix custom workflow template (#7154)

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
This commit is contained in:
Carina Wollendorfer
2023-02-17 13:21:11 +00:00
committed by GitHub
co-authored by CarinaWolli
parent 485619602f
commit 38be2bef9a
+22 -14
View File
@@ -16,6 +16,19 @@ export type EventTypeInfo = {
length?: number | null;
};
type WebhookDataType = CalendarEvent &
EventTypeInfo & {
metadata?: { [key: string]: string };
bookingId?: number;
status?: string;
smsReminderNumber?: string;
rescheduleUid?: string;
rescheduleStartTime?: string;
rescheduleEndTime?: string;
triggerEvent: string;
createdAt: string;
};
function getZapierPayload(data: CalendarEvent & EventTypeInfo & { status?: string }): string {
const attendees = data.attendees.map((attendee) => {
return {
@@ -57,8 +70,13 @@ function getZapierPayload(data: CalendarEvent & EventTypeInfo & { status?: strin
return JSON.stringify(body);
}
function applyTemplate(template: string, data: CalendarEvent, contentType: ContentType) {
const compiled = compile(template)(data);
function applyTemplate(template: string, data: WebhookDataType, contentType: ContentType) {
const organizer = JSON.stringify(data.organizer);
const attendees = JSON.stringify(data.attendees);
const formattedData = { ...data, metadata: JSON.stringify(data.metadata), organizer, attendees };
const compiled = compile(template)(formattedData).replace(/&quot;/g, '"');
if (contentType === "application/json") {
return JSON.stringify(jsonParse(compiled));
}
@@ -79,16 +97,7 @@ const sendPayload = async (
triggerEvent: string,
createdAt: string,
webhook: Pick<Webhook, "subscriberUrl" | "appId" | "payloadTemplate">,
data: CalendarEvent &
EventTypeInfo & {
metadata?: { [key: string]: string };
bookingId?: number;
status?: string;
smsReminderNumber?: string;
rescheduleUid?: string;
rescheduleStartTime?: string;
rescheduleEndTime?: string;
}
data: Omit<WebhookDataType, "createdAt" | "triggerEvent">
) => {
const { appId, payloadTemplate: template } = webhook;
@@ -96,14 +105,13 @@ const sendPayload = async (
!template || jsonParse(template) ? "application/json" : "application/x-www-form-urlencoded";
data.description = data.description || data.additionalNotes;
let body;
/* Zapier id is hardcoded in the DB, we send the raw data for this case */
if (appId === "zapier") {
body = getZapierPayload(data);
} else if (template) {
body = applyTemplate(template, data, contentType);
body = applyTemplate(template, { ...data, triggerEvent, createdAt }, contentType);
} else {
body = JSON.stringify({
triggerEvent: triggerEvent,