-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from calcom/feat/trigger-webhook-from-booking…
…-api Trigger webhook from booking api - MVP
- Loading branch information
Showing
4 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { TFunction } from "next-i18next"; | ||
|
||
type EventNameObjectType = { | ||
attendeeName: string; | ||
eventType: string; | ||
eventName?: string | null; | ||
host: string; | ||
location?: string; | ||
t: TFunction; | ||
}; | ||
|
||
export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView = false) { | ||
if (!eventNameObj.eventName) | ||
return eventNameObj.t("event_between_users", { | ||
eventName: eventNameObj.eventType, | ||
host: eventNameObj.host, | ||
attendeeName: eventNameObj.attendeeName, | ||
}); | ||
|
||
let eventName = eventNameObj.eventName; | ||
let locationString = ""; | ||
|
||
if (eventNameObj.eventName.includes("{LOCATION}")) { | ||
switch (eventNameObj.location) { | ||
case "inPerson": | ||
locationString = "In Person"; | ||
break; | ||
case "userPhone": | ||
case "phone": | ||
locationString = "Phone"; | ||
break; | ||
case "integrations:daily": | ||
locationString = "Cal Video"; | ||
break; | ||
case "integrations:zoom": | ||
locationString = "Zoom"; | ||
break; | ||
case "integrations:huddle01": | ||
locationString = "Huddle01"; | ||
break; | ||
case "integrations:tandem": | ||
locationString = "Tandem"; | ||
break; | ||
case "integrations:office365_video": | ||
locationString = "MS Teams"; | ||
break; | ||
case "integrations:jitsi": | ||
locationString = "Jitsi"; | ||
break; | ||
} | ||
eventName = eventName.replace("{LOCATION}", locationString); | ||
} | ||
|
||
return ( | ||
eventName | ||
// Need this for compatibility with older event names | ||
.replace("{USER}", eventNameObj.attendeeName) | ||
.replace("{ATTENDEE}", eventNameObj.attendeeName) | ||
.replace("{HOST}", eventNameObj.host) | ||
.replace("{HOST/ATTENDEE}", forAttendeeView ? eventNameObj.host : eventNameObj.attendeeName) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Webhook } from "@prisma/client"; | ||
import { compile } from "handlebars"; | ||
|
||
// import type { CalendarEvent } from "@calcom/types/Calendar"; Add this to make it strict, change data: any to CalendarEvent type | ||
|
||
type ContentType = "application/json" | "application/x-www-form-urlencoded"; | ||
|
||
function applyTemplate(template: string, data: any, contentType: ContentType) { | ||
const compiled = compile(template)(data); | ||
if (contentType === "application/json") { | ||
return JSON.stringify(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, | ||
webhook: Pick<Webhook, "subscriberUrl" | "appId" | "payloadTemplate">, | ||
data: any & { | ||
metadata?: { [key: string]: string }; | ||
rescheduleUid?: string; | ||
bookingId?: number; | ||
} | ||
) => { | ||
const { subscriberUrl, appId, payloadTemplate: template } = webhook; | ||
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"; | ||
|
||
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, | ||
}); | ||
} | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { WebhookTriggerEvents } from "@prisma/client"; | ||
|
||
import prisma from "@calcom/prisma"; | ||
|
||
export type GetSubscriberOptions = { | ||
userId: number; | ||
eventTypeId: number; | ||
triggerEvent: WebhookTriggerEvents; | ||
}; | ||
|
||
const getWebhooks = async (options: GetSubscriberOptions) => { | ||
const { userId, eventTypeId } = options; | ||
const allWebhooks = await prisma.webhook.findMany({ | ||
where: { | ||
OR: [ | ||
{ | ||
userId, | ||
}, | ||
{ | ||
eventTypeId, | ||
}, | ||
], | ||
AND: { | ||
eventTriggers: { | ||
has: options.triggerEvent, | ||
}, | ||
active: { | ||
equals: true, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
subscriberUrl: true, | ||
payloadTemplate: true, | ||
appId: true, | ||
}, | ||
}); | ||
|
||
return allWebhooks; | ||
}; | ||
|
||
export default getWebhooks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters