calcom/lib/webhooks/sendPayload.tsx
Syed Ali Shahbaz af89de8004
Wrong language in emails (#1541)
* test --wip

* --wip

* --wip

* --wip

* split language into organizer-attendees

* name fix for tAttendees

* --WIP

* added attendee locale migration, --WIP

* --wip

* fixed check types --wip

* updated person language type

* test snapshot updated

* --wip

* --WIP

* --WIP

* --WIP

* test changes revert

* cleanup

* removed extra space from test

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-01-27 13:32:53 -07:00

65 lines
1.5 KiB
TypeScript

import { compile } from "handlebars";
import { CalendarEvent } from "@lib/integrations/calendar/interfaces/Calendar";
type ContentType = "application/json" | "application/x-www-form-urlencoded";
function applyTemplate(template: string, data: CalendarEvent, contentType: ContentType) {
const compiled = compile(template)(data);
if (contentType === "application/json") {
return jsonParse(compiled);
}
return compiled;
}
function jsonParse(jsonString: string) {
try {
return JSON.parse(jsonString);
} catch (e) {
// don't do anything.
}
return false;
}
const sendPayload = async (
triggerEvent: string,
createdAt: string,
subscriberUrl: string,
data: CalendarEvent & {
metadata?: { [key: string]: string };
},
template?: string | null
) => {
if (!subscriberUrl || !data) {
throw new Error("Missing required elements to send webhook payload.");
}
const contentType =
!template || jsonParse(template) ? "application/json" : "application/x-www-form-urlencoded";
const body = template
? applyTemplate(template, data, contentType)
: JSON.stringify({
triggerEvent: triggerEvent,
createdAt: createdAt,
payload: data,
});
const response = await fetch(subscriberUrl, {
method: "POST",
headers: {
"Content-Type": contentType,
},
body,
});
const text = await response.text();
return {
ok: response.ok,
status: response.status,
message: text,
};
};
export default sendPayload;