2022-05-03 23:16:59 +00:00
|
|
|
import { Webhook } from "@prisma/client";
|
2022-06-16 16:21:48 +00:00
|
|
|
import { createHmac } from "crypto";
|
2021-11-22 11:37:07 +00:00
|
|
|
import { compile } from "handlebars";
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
2021-10-07 15:14:47 +00:00
|
|
|
|
2021-11-22 11:37:07 +00:00
|
|
|
type ContentType = "application/json" | "application/x-www-form-urlencoded";
|
|
|
|
|
2022-01-27 20:32:53 +00:00
|
|
|
function applyTemplate(template: string, data: CalendarEvent, contentType: ContentType) {
|
2021-11-22 11:37:07 +00:00
|
|
|
const compiled = compile(template)(data);
|
|
|
|
if (contentType === "application/json") {
|
2022-02-18 17:49:39 +00:00
|
|
|
return JSON.stringify(jsonParse(compiled));
|
2021-11-22 11:37:07 +00:00
|
|
|
}
|
|
|
|
return compiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsonParse(jsonString: string) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
} catch (e) {
|
|
|
|
// don't do anything.
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendPayload = async (
|
2022-06-16 16:21:48 +00:00
|
|
|
secretKey: string | null,
|
2021-10-07 15:14:47 +00:00
|
|
|
triggerEvent: string,
|
|
|
|
createdAt: string,
|
2022-05-03 23:16:59 +00:00
|
|
|
webhook: Pick<Webhook, "subscriberUrl" | "appId" | "payloadTemplate">,
|
2022-01-27 20:32:53 +00:00
|
|
|
data: CalendarEvent & {
|
|
|
|
metadata?: { [key: string]: string };
|
2022-02-19 00:09:07 +00:00
|
|
|
rescheduleUid?: string;
|
2022-06-16 16:21:48 +00:00
|
|
|
bookingId?: number;
|
2022-05-03 23:16:59 +00:00
|
|
|
}
|
2021-11-22 11:37:07 +00:00
|
|
|
) => {
|
2022-05-03 23:16:59 +00:00
|
|
|
const { subscriberUrl, appId, payloadTemplate: template } = webhook;
|
2021-11-22 11:37:07 +00:00
|
|
|
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";
|
|
|
|
|
2022-05-03 23:16:59 +00:00
|
|
|
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 = JSON.stringify(data);
|
|
|
|
} else if (template) {
|
|
|
|
body = applyTemplate(template, data, contentType);
|
|
|
|
} else {
|
|
|
|
body = JSON.stringify({
|
|
|
|
triggerEvent: triggerEvent,
|
|
|
|
createdAt: createdAt,
|
|
|
|
payload: data,
|
|
|
|
});
|
|
|
|
}
|
2021-11-22 11:37:07 +00:00
|
|
|
|
2022-06-16 16:21:48 +00:00
|
|
|
const secretSignature = secretKey
|
|
|
|
? createHmac("sha256", secretKey).update(`${body}`).digest("hex")
|
|
|
|
: "no-secret-provided";
|
|
|
|
|
2021-11-22 11:37:07 +00:00
|
|
|
const response = await fetch(subscriberUrl, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": contentType,
|
2022-06-16 16:21:48 +00:00
|
|
|
"X-Cal-Signature-256": secretSignature,
|
2021-11-22 11:37:07 +00:00
|
|
|
},
|
|
|
|
body,
|
2021-10-07 15:14:47 +00:00
|
|
|
});
|
|
|
|
|
2021-11-22 11:37:07 +00:00
|
|
|
const text = await response.text();
|
|
|
|
|
|
|
|
return {
|
|
|
|
ok: response.ok,
|
|
|
|
status: response.status,
|
|
|
|
message: text,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-07 15:14:47 +00:00
|
|
|
export default sendPayload;
|