Refactors video integrations (#1037)
* Fixes error types * Type fixes * Refactors video meeting handling * More type fixes * Type fixes * More fixes * Makes language non optional * Adds missing translations * Apply suggestions from code review Co-authored-by: Alex Johansson <alexander@n1s.se> * Feedback Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Alex Johansson <alexander@n1s.se>
This commit is contained in:
17 files changed
+618
-612
No files matched your search
+96
-72
@@ -1,7 +1,11 @@
|
||||
import { Credential } from "@prisma/client";
|
||||
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
||||
import { Calendar as OfficeCalendar } from "@microsoft/microsoft-graph-types-beta";
|
||||
import { Credential, Prisma, SelectedCalendar } from "@prisma/client";
|
||||
import { GetTokenResponse } from "google-auth-library/build/src/auth/oauth2client";
|
||||
import { Auth, calendar_v3, google } from "googleapis";
|
||||
import { TFunction } from "next-i18next";
|
||||
|
||||
import { EventResult } from "@lib/events/EventManager";
|
||||
import { Event, EventResult } from "@lib/events/EventManager";
|
||||
import logger from "@lib/logger";
|
||||
import { VideoCallData } from "@lib/videoClient";
|
||||
|
||||
@@ -14,34 +18,34 @@ import prisma from "./prisma";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { google } = require("googleapis");
|
||||
|
||||
const googleAuth = (credential) => {
|
||||
const { client_secret, client_id, redirect_uris } = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
||||
const googleAuth = (credential: Credential) => {
|
||||
const { client_secret, client_id, redirect_uris } = JSON.parse(process.env.GOOGLE_API_CREDENTIALS!).web;
|
||||
const myGoogleAuth = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
||||
myGoogleAuth.setCredentials(credential.key);
|
||||
const googleCredentials = credential.key as Auth.Credentials;
|
||||
myGoogleAuth.setCredentials(googleCredentials);
|
||||
|
||||
// FIXME - type errors IDK Why this is a protected method ¯\_(ツ)_/¯
|
||||
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
||||
|
||||
const refreshAccessToken = () =>
|
||||
myGoogleAuth
|
||||
.refreshToken(credential.key.refresh_token)
|
||||
.then((res) => {
|
||||
const token = res.res.data;
|
||||
credential.key.access_token = token.access_token;
|
||||
credential.key.expiry_date = token.expiry_date;
|
||||
// FIXME - type errors IDK Why this is a protected method ¯\_(ツ)_/¯
|
||||
.refreshToken(googleCredentials.refresh_token)
|
||||
.then((res: GetTokenResponse) => {
|
||||
const token = res.res?.data;
|
||||
googleCredentials.access_token = token.access_token;
|
||||
googleCredentials.expiry_date = token.expiry_date;
|
||||
return prisma.credential
|
||||
.update({
|
||||
where: {
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: credential.key,
|
||||
key: googleCredentials as Prisma.InputJsonValue,
|
||||
},
|
||||
})
|
||||
.then(() => {
|
||||
myGoogleAuth.setCredentials(credential.key);
|
||||
myGoogleAuth.setCredentials(googleCredentials);
|
||||
return myGoogleAuth;
|
||||
});
|
||||
})
|
||||
@@ -71,13 +75,21 @@ function handleErrorsRaw(response: Response) {
|
||||
return response.text();
|
||||
}
|
||||
|
||||
const o365Auth = (credential) => {
|
||||
const isExpired = (expiryDate) => expiryDate < Math.round(+new Date() / 1000);
|
||||
type O365AuthCredentials = {
|
||||
expiry_date: number;
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
};
|
||||
|
||||
const refreshAccessToken = (refreshToken) => {
|
||||
const o365Auth = (credential: Credential) => {
|
||||
const isExpired = (expiryDate: number) => expiryDate < Math.round(+new Date() / 1000);
|
||||
const o365AuthCredentials = credential.key as O365AuthCredentials;
|
||||
|
||||
const refreshAccessToken = (refreshToken: string) => {
|
||||
return fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
// FIXME types - IDK how to type this TBH
|
||||
body: new URLSearchParams({
|
||||
scope: "User.Read Calendars.Read Calendars.ReadWrite",
|
||||
client_id: process.env.MS_GRAPH_CLIENT_ID,
|
||||
@@ -88,26 +100,26 @@ const o365Auth = (credential) => {
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
credential.key.access_token = responseBody.access_token;
|
||||
credential.key.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
||||
o365AuthCredentials.access_token = responseBody.access_token;
|
||||
o365AuthCredentials.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
||||
return prisma.credential
|
||||
.update({
|
||||
where: {
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: credential.key,
|
||||
key: o365AuthCredentials,
|
||||
},
|
||||
})
|
||||
.then(() => credential.key.access_token);
|
||||
.then(() => o365AuthCredentials.access_token);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
getToken: () =>
|
||||
!isExpired(credential.key.expiry_date)
|
||||
? Promise.resolve(credential.key.access_token)
|
||||
: refreshAccessToken(credential.key.refresh_token),
|
||||
!isExpired(o365AuthCredentials.expiry_date)
|
||||
? Promise.resolve(o365AuthCredentials.access_token)
|
||||
: refreshAccessToken(o365AuthCredentials.refresh_token),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -146,24 +158,22 @@ export interface CalendarEvent {
|
||||
conferenceData?: ConferenceData;
|
||||
language: TFunction;
|
||||
additionInformation?: AdditionInformation;
|
||||
/** If this property exist it we can assume it's a reschedule/update */
|
||||
uid?: string | null;
|
||||
videoCallData?: VideoCallData;
|
||||
}
|
||||
|
||||
export interface ConferenceData {
|
||||
createRequest: unknown;
|
||||
createRequest: calendar_v3.Schema$CreateConferenceRequest;
|
||||
}
|
||||
|
||||
export interface IntegrationCalendar {
|
||||
integration: string;
|
||||
primary: boolean;
|
||||
externalId: string;
|
||||
name: string;
|
||||
export interface IntegrationCalendar extends Partial<SelectedCalendar> {
|
||||
primary?: boolean;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
type BufferedBusyTime = { start: string; end: string };
|
||||
export interface CalendarApiAdapter {
|
||||
createEvent(event: CalendarEvent): Promise<unknown>;
|
||||
createEvent(event: CalendarEvent): Promise<Event>;
|
||||
|
||||
updateEvent(uid: string, event: CalendarEvent): Promise<any>;
|
||||
|
||||
@@ -178,15 +188,10 @@ export interface CalendarApiAdapter {
|
||||
listCalendars(): Promise<IntegrationCalendar[]>;
|
||||
}
|
||||
|
||||
const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
const MicrosoftOffice365Calendar = (credential: Credential): CalendarApiAdapter => {
|
||||
const auth = o365Auth(credential);
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
const optional = {};
|
||||
if (event.location) {
|
||||
optional.location = { displayName: event.location };
|
||||
}
|
||||
|
||||
return {
|
||||
subject: event.title,
|
||||
body: {
|
||||
@@ -208,7 +213,7 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
},
|
||||
type: "required",
|
||||
})),
|
||||
...optional,
|
||||
location: event.location ? { displayName: event.location } : undefined,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -224,13 +229,13 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
},
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
.then((responseBody: { value: OfficeCalendar[] }) => {
|
||||
return responseBody.value.map((cal) => {
|
||||
const calendar: IntegrationCalendar = {
|
||||
externalId: cal.id,
|
||||
externalId: cal.id ?? "No Id",
|
||||
integration: integrationType,
|
||||
name: cal.name,
|
||||
primary: cal.isDefaultCalendar,
|
||||
name: cal.name ?? "No calendar name",
|
||||
primary: cal.isDefaultCalendar ?? false,
|
||||
};
|
||||
return calendar;
|
||||
});
|
||||
@@ -248,17 +253,18 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
.then((accessToken) => {
|
||||
const selectedCalendarIds = selectedCalendars
|
||||
.filter((e) => e.integration === integrationType)
|
||||
.map((e) => e.externalId);
|
||||
if (selectedCalendarIds.length == 0 && selectedCalendars.length > 0) {
|
||||
.map((e) => e.externalId)
|
||||
.filter(Boolean);
|
||||
if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) {
|
||||
// Only calendars of other integrations selected
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
return (
|
||||
selectedCalendarIds.length == 0
|
||||
? listCalendars().then((cals) => cals.map((e) => e.externalId))
|
||||
: Promise.resolve(selectedCalendarIds).then((x) => x)
|
||||
).then((ids: string[]) => {
|
||||
selectedCalendarIds.length === 0
|
||||
? listCalendars().then((cals) => cals.map((e) => e.externalId).filter(Boolean) || [])
|
||||
: Promise.resolve(selectedCalendarIds)
|
||||
).then((ids) => {
|
||||
const requests = ids.map((calendarId, id) => ({
|
||||
id,
|
||||
method: "GET",
|
||||
@@ -268,6 +274,13 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
url: `/me/calendars/${calendarId}/calendarView${filter}`,
|
||||
}));
|
||||
|
||||
type BatchResponse = {
|
||||
responses: SubResponse[];
|
||||
};
|
||||
type SubResponse = {
|
||||
body: { value: { start: { dateTime: string }; end: { dateTime: string } }[] };
|
||||
};
|
||||
|
||||
return fetch("https://graph.microsoft.com/v1.0/$batch", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -277,9 +290,9 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
body: JSON.stringify({ requests }),
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) =>
|
||||
.then((responseBody: BatchResponse) =>
|
||||
responseBody.responses.reduce(
|
||||
(acc, subResponse) =>
|
||||
(acc: BufferedBusyTime[], subResponse) =>
|
||||
acc.concat(
|
||||
subResponse.body.value.map((evt) => {
|
||||
return {
|
||||
@@ -295,6 +308,7 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return Promise.reject([]);
|
||||
});
|
||||
},
|
||||
createEvent: (event: CalendarEvent) =>
|
||||
@@ -337,7 +351,7 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
};
|
||||
};
|
||||
|
||||
const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
const GoogleCalendar = (credential: Credential): CalendarApiAdapter => {
|
||||
const auth = googleAuth(credential);
|
||||
const integrationType = "google_calendar";
|
||||
|
||||
@@ -352,14 +366,16 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
const selectedCalendarIds = selectedCalendars
|
||||
.filter((e) => e.integration === integrationType)
|
||||
.map((e) => e.externalId);
|
||||
if (selectedCalendarIds.length == 0 && selectedCalendars.length > 0) {
|
||||
if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) {
|
||||
// Only calendars of other integrations selected
|
||||
resolve([]);
|
||||
return;
|
||||
}
|
||||
|
||||
(selectedCalendarIds.length == 0
|
||||
? calendar.calendarList.list().then((cals) => cals.data.items.map((cal) => cal.id))
|
||||
(selectedCalendarIds.length === 0
|
||||
? calendar.calendarList
|
||||
.list()
|
||||
.then((cals) => cals.data.items?.map((cal) => cal.id).filter(Boolean) || [])
|
||||
: Promise.resolve(selectedCalendarIds)
|
||||
)
|
||||
.then((calsIds) => {
|
||||
@@ -375,6 +391,7 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
// @ts-ignore FIXME
|
||||
resolve(Object.values(apires.data.calendars).flatMap((item) => item["busy"]));
|
||||
}
|
||||
);
|
||||
@@ -388,7 +405,7 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
createEvent: (event: CalendarEvent) =>
|
||||
new Promise((resolve, reject) =>
|
||||
auth.getToken().then((myGoogleAuth) => {
|
||||
const payload = {
|
||||
const payload: calendar_v3.Schema$Event = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
@@ -422,14 +439,15 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
{
|
||||
auth: myGoogleAuth,
|
||||
calendarId: "primary",
|
||||
resource: payload,
|
||||
requestBody: payload,
|
||||
conferenceDataVersion: 1,
|
||||
},
|
||||
function (err, event) {
|
||||
if (err) {
|
||||
if (err || !event?.data) {
|
||||
console.error("There was an error contacting google calendar service: ", err);
|
||||
return reject(err);
|
||||
}
|
||||
// @ts-ignore FIXME
|
||||
return resolve(event.data);
|
||||
}
|
||||
);
|
||||
@@ -438,7 +456,7 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
updateEvent: (uid: string, event: CalendarEvent) =>
|
||||
new Promise((resolve, reject) =>
|
||||
auth.getToken().then((myGoogleAuth) => {
|
||||
const payload = {
|
||||
const payload: calendar_v3.Schema$Event = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
@@ -471,14 +489,14 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
eventId: uid,
|
||||
sendNotifications: true,
|
||||
sendUpdates: "all",
|
||||
resource: payload,
|
||||
requestBody: payload,
|
||||
},
|
||||
function (err, event) {
|
||||
if (err) {
|
||||
console.error("There was an error contacting google calendar service: ", err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
return resolve(event?.data);
|
||||
}
|
||||
);
|
||||
})
|
||||
@@ -503,7 +521,7 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
console.error("There was an error contacting google calendar service: ", err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
return resolve(event?.data);
|
||||
}
|
||||
);
|
||||
})
|
||||
@@ -519,15 +537,15 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
.list()
|
||||
.then((cals) => {
|
||||
resolve(
|
||||
cals.data.items.map((cal) => {
|
||||
cals.data.items?.map((cal) => {
|
||||
const calendar: IntegrationCalendar = {
|
||||
externalId: cal.id,
|
||||
externalId: cal.id ?? "No id",
|
||||
integration: integrationType,
|
||||
name: cal.summary,
|
||||
primary: cal.primary,
|
||||
name: cal.summary ?? "No name",
|
||||
primary: cal.primary ?? false,
|
||||
};
|
||||
return calendar;
|
||||
})
|
||||
}) || []
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
@@ -576,7 +594,12 @@ const calendars = (withCredentials: Credential[]): CalendarApiAdapter[] =>
|
||||
})
|
||||
.flatMap((item) => (item ? [item as CalendarApiAdapter] : []));
|
||||
|
||||
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) =>
|
||||
const getBusyCalendarTimes = (
|
||||
withCredentials: Credential[],
|
||||
dateFrom: string,
|
||||
dateTo: string,
|
||||
selectedCalendars: SelectedCalendar[]
|
||||
) =>
|
||||
Promise.all(
|
||||
calendars(withCredentials).map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
||||
).then((results) => {
|
||||
@@ -588,7 +611,7 @@ const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalenda
|
||||
* @param withCredentials
|
||||
* @deprecated
|
||||
*/
|
||||
const listCalendars = (withCredentials) =>
|
||||
const listCalendars = (withCredentials: Credential[]) =>
|
||||
Promise.all(calendars(withCredentials).map((c) => c.listCalendars())).then((results) =>
|
||||
results.reduce((acc, calendars) => acc.concat(calendars), []).filter((c) => c != null)
|
||||
);
|
||||
@@ -609,14 +632,15 @@ const createEvent = async (
|
||||
|
||||
let success = true;
|
||||
|
||||
const creationResult: any = credential
|
||||
const creationResult = credential
|
||||
? await calendars([credential])[0]
|
||||
.createEvent(richEvent)
|
||||
.catch((e) => {
|
||||
log.error("createEvent failed", e, calEvent);
|
||||
success = false;
|
||||
return undefined;
|
||||
})
|
||||
: null;
|
||||
: undefined;
|
||||
|
||||
const metadata: AdditionInformation = {};
|
||||
if (creationResult) {
|
||||
|
||||
@@ -1,233 +0,0 @@
|
||||
import { Credential } from "@prisma/client";
|
||||
import short from "short-uuid";
|
||||
import { v5 as uuidv5 } from "uuid";
|
||||
|
||||
import CalEventParser from "@lib/CalEventParser";
|
||||
import { getIntegrationName } from "@lib/emails/helpers";
|
||||
import { EventResult } from "@lib/events/EventManager";
|
||||
import logger from "@lib/logger";
|
||||
|
||||
import { CalendarEvent, AdditionInformation, EntryPoint } from "./calendarClient";
|
||||
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
||||
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
||||
import VideoEventAttendeeMail from "./emails/VideoEventAttendeeMail";
|
||||
import VideoEventOrganizerMail from "./emails/VideoEventOrganizerMail";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[lib] dailyVideoClient"] });
|
||||
|
||||
const translator = short();
|
||||
|
||||
export interface DailyVideoCallData {
|
||||
type: string;
|
||||
id: string;
|
||||
password: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
function handleErrorsJson(response: Response) {
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
const dailyCredential = process.env.DAILY_API_KEY;
|
||||
|
||||
interface DailyVideoApiAdapter {
|
||||
dailyCreateMeeting(event: CalendarEvent): Promise<any>;
|
||||
|
||||
dailyUpdateMeeting(uid: string, event: CalendarEvent): Promise<any>;
|
||||
|
||||
dailyDeleteMeeting(uid: string): Promise<unknown>;
|
||||
|
||||
getAvailability(dateFrom, dateTo): Promise<any>;
|
||||
}
|
||||
|
||||
const DailyVideo = (credential: Credential): DailyVideoApiAdapter => {
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
// Documentation at: https://docs.daily.co/reference#list-rooms
|
||||
// added a 1 hour buffer for room expiration and room entry
|
||||
const exp = Math.round(new Date(event.endTime).getTime() / 1000) + 60 * 60;
|
||||
const nbf = Math.round(new Date(event.startTime).getTime() / 1000) - 60 * 60;
|
||||
return {
|
||||
privacy: "private",
|
||||
properties: {
|
||||
enable_new_call_ui: true,
|
||||
enable_prejoin_ui: true,
|
||||
enable_knocking: true,
|
||||
enable_screenshare: true,
|
||||
enable_chat: true,
|
||||
exp: exp,
|
||||
nbf: nbf,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
getAvailability: () => {
|
||||
return credential;
|
||||
},
|
||||
dailyCreateMeeting: (event: CalendarEvent) =>
|
||||
fetch("https://api.daily.co/v1/rooms", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + dailyCredential,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsJson),
|
||||
dailyDeleteMeeting: (uid: string) =>
|
||||
fetch("https://api.daily.co/v1/rooms/" + uid, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: "Bearer " + dailyCredential,
|
||||
},
|
||||
}).then(handleErrorsJson),
|
||||
dailyUpdateMeeting: (uid: string, event: CalendarEvent) =>
|
||||
fetch("https://api.daily.co/v1/rooms/" + uid, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + dailyCredential,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsJson),
|
||||
};
|
||||
};
|
||||
|
||||
// factory
|
||||
const videoIntegrations = (withCredentials): DailyVideoApiAdapter[] =>
|
||||
withCredentials
|
||||
.map((cred) => {
|
||||
return DailyVideo(cred);
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
const getBusyVideoTimes: (withCredentials) => Promise<unknown[]> = (withCredentials) =>
|
||||
Promise.all(videoIntegrations(withCredentials).map((c) => c.getAvailability())).then((results) =>
|
||||
results.reduce((acc, availability) => acc.concat(availability), [])
|
||||
);
|
||||
|
||||
const dailyCreateMeeting = async (credential: Credential, calEvent: CalendarEvent): Promise<EventResult> => {
|
||||
const parser: CalEventParser = new CalEventParser(calEvent);
|
||||
const uid: string = parser.getUid();
|
||||
|
||||
if (!credential) {
|
||||
throw new Error(
|
||||
"Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set."
|
||||
);
|
||||
}
|
||||
|
||||
let success = true;
|
||||
|
||||
const creationResult = await videoIntegrations([credential])[0]
|
||||
.dailyCreateMeeting(calEvent)
|
||||
.catch((e) => {
|
||||
log.error("createMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
});
|
||||
|
||||
const currentRoute = process.env.BASE_URL;
|
||||
|
||||
const videoCallData: DailyVideoCallData = {
|
||||
type: "Daily.co Video",
|
||||
id: creationResult.name,
|
||||
password: creationResult.password,
|
||||
url: currentRoute + "/call/" + uid,
|
||||
};
|
||||
|
||||
const entryPoint: EntryPoint = {
|
||||
entryPointType: getIntegrationName(videoCallData),
|
||||
uri: videoCallData.url,
|
||||
label: calEvent.language("enter_meeting"),
|
||||
pin: "",
|
||||
};
|
||||
|
||||
const additionInformation: AdditionInformation = {
|
||||
entryPoints: [entryPoint],
|
||||
};
|
||||
const emailEvent = { ...calEvent, uid, additionInformation, videoCallData };
|
||||
|
||||
try {
|
||||
const organizerMail = new VideoEventOrganizerMail(emailEvent);
|
||||
await organizerMail.sendEmail();
|
||||
} catch (e) {
|
||||
console.error("organizerMail.sendEmail failed", e);
|
||||
}
|
||||
|
||||
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
||||
try {
|
||||
const attendeeMail = new VideoEventAttendeeMail(emailEvent);
|
||||
await attendeeMail.sendEmail();
|
||||
} catch (e) {
|
||||
console.error("attendeeMail.sendEmail failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: "daily",
|
||||
success,
|
||||
uid,
|
||||
createdEvent: creationResult,
|
||||
originalEvent: calEvent,
|
||||
};
|
||||
};
|
||||
|
||||
const dailyUpdateMeeting = async (credential: Credential, calEvent: CalendarEvent): Promise<EventResult> => {
|
||||
const newUid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
||||
|
||||
if (!credential) {
|
||||
throw new Error(
|
||||
"Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set."
|
||||
);
|
||||
}
|
||||
|
||||
let success = true;
|
||||
|
||||
const updateResult =
|
||||
credential && calEvent.uid
|
||||
? await videoIntegrations([credential])[0]
|
||||
.dailyUpdateMeeting(calEvent.uid, calEvent)
|
||||
.catch((e) => {
|
||||
log.error("updateMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
})
|
||||
: null;
|
||||
|
||||
const emailEvent = { ...calEvent, uid: newUid };
|
||||
|
||||
try {
|
||||
const organizerMail = new EventOrganizerRescheduledMail(emailEvent);
|
||||
await organizerMail.sendEmail();
|
||||
} catch (e) {
|
||||
console.error("organizerMail.sendEmail failed", e);
|
||||
}
|
||||
|
||||
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
||||
try {
|
||||
const attendeeMail = new EventAttendeeRescheduledMail(emailEvent);
|
||||
await attendeeMail.sendEmail();
|
||||
} catch (e) {
|
||||
console.error("attendeeMail.sendEmail failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: credential.type,
|
||||
success,
|
||||
uid: newUid,
|
||||
updatedEvent: updateResult,
|
||||
originalEvent: calEvent,
|
||||
};
|
||||
};
|
||||
|
||||
const dailyDeleteMeeting = (credential: Credential, uid: string): Promise<unknown> => {
|
||||
if (credential) {
|
||||
return videoIntegrations([credential])[0].dailyDeleteMeeting(uid);
|
||||
}
|
||||
|
||||
return Promise.resolve({});
|
||||
};
|
||||
|
||||
export { getBusyVideoTimes, dailyCreateMeeting, dailyUpdateMeeting, dailyDeleteMeeting };
|
||||
+22
-1
@@ -1,4 +1,9 @@
|
||||
export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: number; code?: unknown } {
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: number; code?: string } {
|
||||
if (cause instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
return cause;
|
||||
}
|
||||
if (cause instanceof Error) {
|
||||
return cause;
|
||||
}
|
||||
@@ -9,3 +14,19 @@ export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: numb
|
||||
|
||||
return new Error(`Unhandled error of type '${typeof cause}''`);
|
||||
}
|
||||
|
||||
export function handleErrorsJson(response: Response) {
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export function handleErrorsRaw(response: Response) {
|
||||
if (!response.ok) {
|
||||
response.text().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.text();
|
||||
}
|
||||
+28
-44
@@ -3,20 +3,27 @@ import async from "async";
|
||||
import merge from "lodash/merge";
|
||||
import { v5 as uuidv5 } from "uuid";
|
||||
|
||||
import { CalendarEvent, AdditionInformation, createEvent, updateEvent } from "@lib/calendarClient";
|
||||
import { dailyCreateMeeting, dailyUpdateMeeting } from "@lib/dailyVideoClient";
|
||||
import { AdditionInformation, CalendarEvent, createEvent, updateEvent } from "@lib/calendarClient";
|
||||
import EventAttendeeMail from "@lib/emails/EventAttendeeMail";
|
||||
import EventAttendeeRescheduledMail from "@lib/emails/EventAttendeeRescheduledMail";
|
||||
import { DailyEventResult, FAKE_DAILY_CREDENTIAL } from "@lib/integrations/Daily/DailyVideoApiAdapter";
|
||||
import { ZoomEventResult } from "@lib/integrations/Zoom/ZoomVideoApiAdapter";
|
||||
import { LocationType } from "@lib/location";
|
||||
import prisma from "@lib/prisma";
|
||||
import { Ensure } from "@lib/types/utils";
|
||||
import { createMeeting, updateMeeting, VideoCallData } from "@lib/videoClient";
|
||||
|
||||
export type Event = AdditionInformation & { name: string; id: string; disableConfirmationEmail?: boolean } & (
|
||||
| ZoomEventResult
|
||||
| DailyEventResult
|
||||
);
|
||||
|
||||
export interface EventResult {
|
||||
type: string;
|
||||
success: boolean;
|
||||
uid: string;
|
||||
createdEvent?: AdditionInformation & { name: string; id: string; disableConfirmationEmail?: boolean };
|
||||
updatedEvent?: AdditionInformation & { name: string; id: string; disableConfirmationEmail?: boolean };
|
||||
createdEvent?: Event;
|
||||
updatedEvent?: Event;
|
||||
originalEvent: CalendarEvent;
|
||||
videoCallData?: VideoCallData;
|
||||
}
|
||||
@@ -44,9 +51,6 @@ interface GetLocationRequestFromIntegrationRequest {
|
||||
location: string;
|
||||
}
|
||||
|
||||
//const to idenfity a daily event location
|
||||
const dailyLocation = "integrations:daily";
|
||||
|
||||
export default class EventManager {
|
||||
calendarCredentials: Array<Credential>;
|
||||
videoCredentials: Array<Credential>;
|
||||
@@ -61,16 +65,9 @@ export default class EventManager {
|
||||
this.videoCredentials = credentials.filter((cred) => cred.type.endsWith("_video"));
|
||||
|
||||
//for Daily.co video, temporarily pushes a credential for the daily-video-client
|
||||
|
||||
const hasDailyIntegration = process.env.DAILY_API_KEY;
|
||||
const dailyCredential: Credential = {
|
||||
id: +new Date().getTime(),
|
||||
type: "daily_video",
|
||||
key: { apikey: process.env.DAILY_API_KEY },
|
||||
userId: +new Date().getTime(),
|
||||
};
|
||||
if (hasDailyIntegration) {
|
||||
this.videoCredentials.push(dailyCredential);
|
||||
this.videoCredentials.push(FAKE_DAILY_CREDENTIAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +78,7 @@ export default class EventManager {
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
public async create(event: CalendarEvent): Promise<CreateUpdateResult> {
|
||||
public async create(event: Ensure<CalendarEvent, "language">): Promise<CreateUpdateResult> {
|
||||
let evt = EventManager.processLocation(event);
|
||||
const isDedicated = evt.location ? EventManager.isDedicatedIntegration(evt.location) : null;
|
||||
|
||||
@@ -103,13 +100,14 @@ export default class EventManager {
|
||||
results = results.concat(await this.createAllCalendarEvents(evt, isDedicated));
|
||||
|
||||
const referencesToCreate: Array<PartialReference> = results.map((result: EventResult) => {
|
||||
const isDailyResult = result.type === "daily";
|
||||
let uid = "";
|
||||
if (isDailyResult && result.createdEvent) {
|
||||
uid = result.createdEvent.name.toString();
|
||||
}
|
||||
if (!isDailyResult && result.createdEvent) {
|
||||
uid = result.createdEvent.id.toString();
|
||||
if (result.createdEvent) {
|
||||
const isDailyResult = result.type === "daily_video";
|
||||
if (isDailyResult) {
|
||||
uid = (result.createdEvent as DailyEventResult).name.toString();
|
||||
} else {
|
||||
uid = (result.createdEvent as ZoomEventResult).id.toString();
|
||||
}
|
||||
}
|
||||
return {
|
||||
type: result.type,
|
||||
@@ -132,11 +130,11 @@ export default class EventManager {
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
public async update(event: CalendarEvent): Promise<CreateUpdateResult> {
|
||||
public async update(event: Ensure<CalendarEvent, "uid">): Promise<CreateUpdateResult> {
|
||||
let evt = EventManager.processLocation(event);
|
||||
|
||||
if (!evt.uid) {
|
||||
throw new Error("missing uid");
|
||||
throw new Error("You called eventManager.update without an `uid`. This should never happen.");
|
||||
}
|
||||
|
||||
// Get details of existing booking.
|
||||
@@ -163,9 +161,7 @@ export default class EventManager {
|
||||
throw new Error("booking not found");
|
||||
}
|
||||
|
||||
const isDedicated = evt.location
|
||||
? EventManager.isDedicatedIntegration(evt.location) || evt.location === dailyLocation
|
||||
: null;
|
||||
const isDedicated = evt.location ? EventManager.isDedicatedIntegration(evt.location) : null;
|
||||
|
||||
let results: Array<EventResult> = [];
|
||||
|
||||
@@ -259,15 +255,11 @@ export default class EventManager {
|
||||
* @param event
|
||||
* @private
|
||||
*/
|
||||
private createVideoEvent(event: CalendarEvent): Promise<EventResult> {
|
||||
private createVideoEvent(event: Ensure<CalendarEvent, "language">): Promise<EventResult> {
|
||||
const credential = this.getVideoCredential(event);
|
||||
|
||||
const isDaily = event.location === dailyLocation;
|
||||
|
||||
if (credential && !isDaily) {
|
||||
if (credential) {
|
||||
return createMeeting(credential, event);
|
||||
} else if (credential && isDaily) {
|
||||
return dailyCreateMeeting(credential, event);
|
||||
} else {
|
||||
return Promise.reject("No suitable credentials given for the requested integration name.");
|
||||
}
|
||||
@@ -307,9 +299,8 @@ export default class EventManager {
|
||||
*/
|
||||
private updateVideoEvent(event: CalendarEvent, booking: PartialBooking) {
|
||||
const credential = this.getVideoCredential(event);
|
||||
const isDaily = event.location === dailyLocation;
|
||||
|
||||
if (credential && !isDaily) {
|
||||
if (credential) {
|
||||
const bookingRef = booking ? booking.references.filter((ref) => ref.type === credential.type)[0] : null;
|
||||
const evt = { ...event, uid: bookingRef?.uid };
|
||||
return updateMeeting(credential, evt).then((returnVal: EventResult) => {
|
||||
@@ -320,13 +311,6 @@ export default class EventManager {
|
||||
return returnVal;
|
||||
});
|
||||
} else {
|
||||
if (credential && isDaily) {
|
||||
const bookingRefUid = booking
|
||||
? booking.references.filter((ref) => ref.type === "daily")[0].uid
|
||||
: null;
|
||||
const evt = { ...event, uid: bookingRefUid };
|
||||
return dailyUpdateMeeting(credential, evt);
|
||||
}
|
||||
return Promise.reject("No suitable credentials given for the requested integration name.");
|
||||
}
|
||||
}
|
||||
@@ -345,7 +329,7 @@ export default class EventManager {
|
||||
private static isDedicatedIntegration(location: string): boolean {
|
||||
// Hard-coded for now, because Zoom and Google Meet are both integrations, but one is dedicated, the other one isn't.
|
||||
|
||||
return location === "integrations:zoom" || location === dailyLocation;
|
||||
return location === "integrations:zoom" || location === "integrations:daily";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -385,7 +369,7 @@ export default class EventManager {
|
||||
* @param event
|
||||
* @private
|
||||
*/
|
||||
private static processLocation(event: CalendarEvent): CalendarEvent {
|
||||
private static processLocation<T extends CalendarEvent>(event: T): T {
|
||||
// If location is set to an integration location
|
||||
// Build proper transforms for evt object
|
||||
// Extend evt object with those transformations
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { Credential } from "@prisma/client";
|
||||
|
||||
import { CalendarEvent } from "@lib/calendarClient";
|
||||
import { handleErrorsJson } from "@lib/errors";
|
||||
import prisma from "@lib/prisma";
|
||||
import { VideoApiAdapter } from "@lib/videoClient";
|
||||
|
||||
export interface DailyReturnType {
|
||||
/** Long UID string ie: 987b5eb5-d116-4a4e-8e2c-14fcb5710966 */
|
||||
id: string;
|
||||
/** Not a real name, just a random generated string ie: "ePR84NQ1bPigp79dDezz" */
|
||||
name: string;
|
||||
api_created: boolean;
|
||||
privacy: "private" | "public";
|
||||
/** https://api-demo.daily.co/ePR84NQ1bPigp79dDezz */
|
||||
url: string;
|
||||
created_at: string;
|
||||
config: {
|
||||
nbf: number;
|
||||
exp: number;
|
||||
enable_chat: boolean;
|
||||
enable_knocking: boolean;
|
||||
enable_prejoin_ui: boolean;
|
||||
enable_new_call_ui: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface DailyEventResult {
|
||||
id: string;
|
||||
name: string;
|
||||
api_created: boolean;
|
||||
privacy: string;
|
||||
url: string;
|
||||
created_at: string;
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface DailyVideoCallData {
|
||||
type: string;
|
||||
id: string;
|
||||
password: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
type DailyKey = {
|
||||
apikey: string;
|
||||
};
|
||||
|
||||
export const FAKE_DAILY_CREDENTIAL: Credential = {
|
||||
id: +new Date().getTime(),
|
||||
type: "daily_video",
|
||||
key: { apikey: process.env.DAILY_API_KEY },
|
||||
userId: +new Date().getTime(),
|
||||
};
|
||||
|
||||
const DailyVideoApiAdapter = (credential: Credential): VideoApiAdapter => {
|
||||
const dailyApiToken = (credential.key as DailyKey).apikey;
|
||||
|
||||
function postToDailyAPI(endpoint: string, body: Record<string, any>) {
|
||||
return fetch("https://api.daily.co/v1" + endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + dailyApiToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
async function createOrUpdateMeeting(endpoint: string, event: CalendarEvent) {
|
||||
if (!event.uid) {
|
||||
throw new Error("We need need the booking uid to create the Daily reference in DB");
|
||||
}
|
||||
const response = await postToDailyAPI(endpoint, translateEvent(event));
|
||||
const dailyEvent: DailyReturnType = await handleErrorsJson(response);
|
||||
const res = await postToDailyAPI("/meeting-tokens", {
|
||||
properties: { room_name: dailyEvent.name, is_owner: true },
|
||||
});
|
||||
const meetingToken: { token: string } = await handleErrorsJson(res);
|
||||
await prisma.dailyEventReference.create({
|
||||
data: {
|
||||
dailyurl: dailyEvent.url,
|
||||
dailytoken: meetingToken.token,
|
||||
booking: {
|
||||
connect: {
|
||||
uid: event.uid,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return dailyEvent;
|
||||
}
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
// Documentation at: https://docs.daily.co/reference#list-rooms
|
||||
// added a 1 hour buffer for room expiration and room entry
|
||||
const exp = Math.round(new Date(event.endTime).getTime() / 1000) + 60 * 60;
|
||||
const nbf = Math.round(new Date(event.startTime).getTime() / 1000) - 60 * 60;
|
||||
return {
|
||||
privacy: "private",
|
||||
properties: {
|
||||
enable_new_call_ui: true,
|
||||
enable_prejoin_ui: true,
|
||||
enable_knocking: true,
|
||||
enable_screenshare: true,
|
||||
enable_chat: true,
|
||||
exp: exp,
|
||||
nbf: nbf,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
/** Daily doesn't need to return busy times, so we return empty */
|
||||
getAvailability: () => {
|
||||
return Promise.resolve([]);
|
||||
},
|
||||
createMeeting: async (event: CalendarEvent) => createOrUpdateMeeting("/rooms", event),
|
||||
deleteMeeting: (uid: string) =>
|
||||
fetch("https://api.daily.co/v1/rooms/" + uid, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: "Bearer " + dailyApiToken,
|
||||
},
|
||||
}).then(handleErrorsJson),
|
||||
updateMeeting: (uid: string, event: CalendarEvent) => createOrUpdateMeeting("/rooms/" + uid, event),
|
||||
};
|
||||
};
|
||||
|
||||
export default DailyVideoApiAdapter;
|
||||
@@ -0,0 +1,205 @@
|
||||
import { Credential } from "@prisma/client";
|
||||
|
||||
import { CalendarEvent } from "@lib/calendarClient";
|
||||
import { handleErrorsJson, handleErrorsRaw } from "@lib/errors";
|
||||
import prisma from "@lib/prisma";
|
||||
import { VideoApiAdapter } from "@lib/videoClient";
|
||||
|
||||
/** @link https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/meetingcreate */
|
||||
export interface ZoomEventResult {
|
||||
created_at: string;
|
||||
duration: number;
|
||||
host_id: string;
|
||||
id: number;
|
||||
join_url: string;
|
||||
settings: {
|
||||
alternative_hosts: string;
|
||||
approval_type: number;
|
||||
audio: string;
|
||||
auto_recording: string;
|
||||
close_registration: boolean;
|
||||
cn_meeting: boolean;
|
||||
enforce_login: boolean;
|
||||
enforce_login_domains: string;
|
||||
global_dial_in_countries: string[];
|
||||
global_dial_in_numbers: {
|
||||
city: string;
|
||||
country: string;
|
||||
country_name: string;
|
||||
number: string;
|
||||
type: string;
|
||||
}[];
|
||||
breakout_room: {
|
||||
enable: boolean;
|
||||
rooms: {
|
||||
name: string;
|
||||
participants: string[];
|
||||
}[];
|
||||
host_video: boolean;
|
||||
in_meeting: boolean;
|
||||
join_before_host: boolean;
|
||||
mute_upon_entry: boolean;
|
||||
participant_video: boolean;
|
||||
registrants_confirmation_email: boolean;
|
||||
use_pmi: boolean;
|
||||
waiting_room: boolean;
|
||||
watermark: boolean;
|
||||
registrants_email_notification: boolean;
|
||||
};
|
||||
start_time: string;
|
||||
start_url: string;
|
||||
status: string;
|
||||
timezone: string;
|
||||
topic: string;
|
||||
type: number;
|
||||
uuid: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface ZoomToken {
|
||||
scope: "meeting:write";
|
||||
expires_in: number;
|
||||
token_type: "bearer";
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
}
|
||||
|
||||
const zoomAuth = (credential: Credential) => {
|
||||
const credentialKey = credential.key as unknown as ZoomToken;
|
||||
const isExpired = (expiryDate: number) => expiryDate < +new Date();
|
||||
const authHeader =
|
||||
"Basic " +
|
||||
Buffer.from(process.env.ZOOM_CLIENT_ID + ":" + process.env.ZOOM_CLIENT_SECRET).toString("base64");
|
||||
|
||||
const refreshAccessToken = (refreshToken: string) =>
|
||||
fetch("https://zoom.us/oauth/token", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
refresh_token: refreshToken,
|
||||
grant_type: "refresh_token",
|
||||
}),
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(async (responseBody) => {
|
||||
// Store new tokens in database.
|
||||
await prisma.credential.update({
|
||||
where: {
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: responseBody,
|
||||
},
|
||||
});
|
||||
credentialKey.access_token = responseBody.access_token;
|
||||
credentialKey.expires_in = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
||||
return credentialKey.access_token;
|
||||
});
|
||||
|
||||
return {
|
||||
getToken: () =>
|
||||
!isExpired(credentialKey.expires_in)
|
||||
? Promise.resolve(credentialKey.access_token)
|
||||
: refreshAccessToken(credentialKey.refresh_token),
|
||||
};
|
||||
};
|
||||
|
||||
const ZoomVideoApiAdapter = (credential: Credential): VideoApiAdapter => {
|
||||
const auth = zoomAuth(credential);
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
// Documentation at: https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/meetingcreate
|
||||
return {
|
||||
topic: event.title,
|
||||
type: 2, // Means that this is a scheduled meeting
|
||||
start_time: event.startTime,
|
||||
duration: (new Date(event.endTime).getTime() - new Date(event.startTime).getTime()) / 60000,
|
||||
//schedule_for: "string", TODO: Used when scheduling the meeting for someone else (needed?)
|
||||
timezone: event.attendees[0].timeZone,
|
||||
//password: "string", TODO: Should we use a password? Maybe generate a random one?
|
||||
agenda: event.description,
|
||||
settings: {
|
||||
host_video: true,
|
||||
participant_video: true,
|
||||
cn_meeting: false, // TODO: true if host meeting in China
|
||||
in_meeting: false, // TODO: true if host meeting in India
|
||||
join_before_host: true,
|
||||
mute_upon_entry: false,
|
||||
watermark: false,
|
||||
use_pmi: false,
|
||||
approval_type: 2,
|
||||
audio: "both",
|
||||
auto_recording: "none",
|
||||
enforce_login: false,
|
||||
registrants_email_notification: true,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
getAvailability: () => {
|
||||
return auth
|
||||
.getToken()
|
||||
.then(
|
||||
// TODO Possibly implement pagination for cases when there are more than 300 meetings already scheduled.
|
||||
(accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/users/me/meetings?type=scheduled&page_size=300", {
|
||||
method: "get",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
},
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
return responseBody.meetings.map((meeting: { start_time: string; duration: number }) => ({
|
||||
start: meeting.start_time,
|
||||
end: new Date(
|
||||
new Date(meeting.start_time).getTime() + meeting.duration * 60000
|
||||
).toISOString(),
|
||||
}));
|
||||
})
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
/* Prevents booking failure when Zoom Token is expired */
|
||||
return [];
|
||||
});
|
||||
},
|
||||
createMeeting: (event: CalendarEvent) =>
|
||||
auth.getToken().then((accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/users/me/meetings", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsJson)
|
||||
),
|
||||
deleteMeeting: (uid: string) =>
|
||||
auth.getToken().then((accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
},
|
||||
}).then(handleErrorsRaw)
|
||||
),
|
||||
updateMeeting: (uid: string, event: CalendarEvent) =>
|
||||
auth.getToken().then((accessToken: string) =>
|
||||
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsRaw)
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
export default ZoomVideoApiAdapter;
|
||||
@@ -2,12 +2,17 @@ import { Booking } from "@prisma/client";
|
||||
|
||||
import { LocationType } from "@lib/location";
|
||||
|
||||
export type BookingConfirmBody = {
|
||||
confirmed: boolean;
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type BookingCreateBody = {
|
||||
email: string;
|
||||
end: string;
|
||||
eventTypeId: number;
|
||||
guests: string[];
|
||||
location?: LocationType;
|
||||
location: LocationType;
|
||||
name: string;
|
||||
notes: string;
|
||||
rescheduleUid?: string;
|
||||
|
||||
+2
-4
@@ -1,5 +1,3 @@
|
||||
export type RequiredNotNull<T> = {
|
||||
[P in keyof T]: NonNullable<T[P]>;
|
||||
export type Ensure<T, K extends keyof T> = Omit<T, K> & {
|
||||
[EK in K]-?: NonNullable<T[EK]>;
|
||||
};
|
||||
|
||||
export type Ensure<T, K extends keyof T> = T & RequiredNotNull<Pick<T, K>>;
|
||||
+65
-192
@@ -3,29 +3,24 @@ import short from "short-uuid";
|
||||
import { v5 as uuidv5 } from "uuid";
|
||||
|
||||
import CalEventParser from "@lib/CalEventParser";
|
||||
import "@lib/emails/EventMail";
|
||||
import { getIntegrationName } from "@lib/emails/helpers";
|
||||
import { EventResult } from "@lib/events/EventManager";
|
||||
import logger from "@lib/logger";
|
||||
|
||||
import { CalendarEvent, AdditionInformation, EntryPoint } from "./calendarClient";
|
||||
import { AdditionInformation, CalendarEvent, EntryPoint } from "./calendarClient";
|
||||
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
||||
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
||||
import VideoEventAttendeeMail from "./emails/VideoEventAttendeeMail";
|
||||
import VideoEventOrganizerMail from "./emails/VideoEventOrganizerMail";
|
||||
import prisma from "./prisma";
|
||||
import DailyVideoApiAdapter from "./integrations/Daily/DailyVideoApiAdapter";
|
||||
import ZoomVideoApiAdapter from "./integrations/Zoom/ZoomVideoApiAdapter";
|
||||
import { Ensure } from "./types/utils";
|
||||
|
||||
const log = logger.getChildLogger({ prefix: ["[lib] videoClient"] });
|
||||
|
||||
const translator = short();
|
||||
|
||||
export interface ZoomToken {
|
||||
scope: "meeting:write";
|
||||
expires_in: number;
|
||||
token_type: "bearer";
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
}
|
||||
|
||||
export interface VideoCallData {
|
||||
type: string;
|
||||
id: string;
|
||||
@@ -33,176 +28,27 @@ export interface VideoCallData {
|
||||
url: string;
|
||||
}
|
||||
|
||||
function handleErrorsJson(response: Response) {
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
type EventBusyDate = Record<"start" | "end", Date>;
|
||||
|
||||
function handleErrorsRaw(response: Response) {
|
||||
if (!response.ok) {
|
||||
response.text().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.text();
|
||||
}
|
||||
|
||||
const zoomAuth = (credential: Credential) => {
|
||||
const credentialKey = credential.key as unknown as ZoomToken;
|
||||
const isExpired = (expiryDate: number) => expiryDate < +new Date();
|
||||
const authHeader =
|
||||
"Basic " +
|
||||
Buffer.from(process.env.ZOOM_CLIENT_ID + ":" + process.env.ZOOM_CLIENT_SECRET).toString("base64");
|
||||
|
||||
const refreshAccessToken = (refreshToken: string) =>
|
||||
fetch("https://zoom.us/oauth/token", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
refresh_token: refreshToken,
|
||||
grant_type: "refresh_token",
|
||||
}),
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(async (responseBody) => {
|
||||
// Store new tokens in database.
|
||||
await prisma.credential.update({
|
||||
where: {
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: responseBody,
|
||||
},
|
||||
});
|
||||
credentialKey.access_token = responseBody.access_token;
|
||||
credentialKey.expires_in = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
||||
return credentialKey.access_token;
|
||||
});
|
||||
|
||||
return {
|
||||
getToken: () =>
|
||||
!isExpired(credentialKey.expires_in)
|
||||
? Promise.resolve(credentialKey.access_token)
|
||||
: refreshAccessToken(credentialKey.refresh_token),
|
||||
};
|
||||
};
|
||||
|
||||
interface VideoApiAdapter {
|
||||
export interface VideoApiAdapter {
|
||||
createMeeting(event: CalendarEvent): Promise<any>;
|
||||
|
||||
updateMeeting(uid: string, event: CalendarEvent): Promise<any>;
|
||||
|
||||
deleteMeeting(uid: string): Promise<unknown>;
|
||||
|
||||
getAvailability(dateFrom: string, dateTo: string): Promise<any>;
|
||||
getAvailability(dateFrom?: string, dateTo?: string): Promise<EventBusyDate[]>;
|
||||
}
|
||||
|
||||
const ZoomVideo = (credential: Credential): VideoApiAdapter => {
|
||||
const auth = zoomAuth(credential);
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
// Documentation at: https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/meetingcreate
|
||||
return {
|
||||
topic: event.title,
|
||||
type: 2, // Means that this is a scheduled meeting
|
||||
start_time: event.startTime,
|
||||
duration: (new Date(event.endTime).getTime() - new Date(event.startTime).getTime()) / 60000,
|
||||
//schedule_for: "string", TODO: Used when scheduling the meeting for someone else (needed?)
|
||||
timezone: event.attendees[0].timeZone,
|
||||
//password: "string", TODO: Should we use a password? Maybe generate a random one?
|
||||
agenda: event.description,
|
||||
settings: {
|
||||
host_video: true,
|
||||
participant_video: true,
|
||||
cn_meeting: false, // TODO: true if host meeting in China
|
||||
in_meeting: false, // TODO: true if host meeting in India
|
||||
join_before_host: true,
|
||||
mute_upon_entry: false,
|
||||
watermark: false,
|
||||
use_pmi: false,
|
||||
approval_type: 2,
|
||||
audio: "both",
|
||||
auto_recording: "none",
|
||||
enforce_login: false,
|
||||
registrants_email_notification: true,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
getAvailability: () => {
|
||||
return auth
|
||||
.getToken()
|
||||
.then(
|
||||
// TODO Possibly implement pagination for cases when there are more than 300 meetings already scheduled.
|
||||
(accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/users/me/meetings?type=scheduled&page_size=300", {
|
||||
method: "get",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
},
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
return responseBody.meetings.map((meeting) => ({
|
||||
start: meeting.start_time,
|
||||
end: new Date(
|
||||
new Date(meeting.start_time).getTime() + meeting.duration * 60000
|
||||
).toISOString(),
|
||||
}));
|
||||
})
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
/* Prevents booking failure when Zoom Token is expired */
|
||||
return [];
|
||||
});
|
||||
},
|
||||
createMeeting: (event: CalendarEvent) =>
|
||||
auth.getToken().then((accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/users/me/meetings", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsJson)
|
||||
),
|
||||
deleteMeeting: (uid: string) =>
|
||||
auth.getToken().then((accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
},
|
||||
}).then(handleErrorsRaw)
|
||||
),
|
||||
updateMeeting: (uid: string, event: CalendarEvent) =>
|
||||
auth.getToken().then((accessToken) =>
|
||||
fetch("https://api.zoom.us/v2/meetings/" + uid, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: "Bearer " + accessToken,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event)),
|
||||
}).then(handleErrorsRaw)
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
// factory
|
||||
const videoIntegrations = (withCredentials: Credential[]): VideoApiAdapter[] =>
|
||||
const getVideoAdapters = (withCredentials: Credential[]): VideoApiAdapter[] =>
|
||||
withCredentials.reduce<VideoApiAdapter[]>((acc, cred) => {
|
||||
switch (cred.type) {
|
||||
case "zoom_video":
|
||||
acc.push(ZoomVideo(cred));
|
||||
acc.push(ZoomVideoApiAdapter(cred));
|
||||
break;
|
||||
case "daily_video":
|
||||
acc.push(DailyVideoApiAdapter(cred));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -211,11 +57,14 @@ const videoIntegrations = (withCredentials: Credential[]): VideoApiAdapter[] =>
|
||||
}, []);
|
||||
|
||||
const getBusyVideoTimes: (withCredentials: Credential[]) => Promise<unknown[]> = (withCredentials) =>
|
||||
Promise.all(videoIntegrations(withCredentials).map((c) => c.getAvailability())).then((results) =>
|
||||
Promise.all(getVideoAdapters(withCredentials).map((c) => c.getAvailability())).then((results) =>
|
||||
results.reduce((acc, availability) => acc.concat(availability), [])
|
||||
);
|
||||
|
||||
const createMeeting = async (credential: Credential, calEvent: CalendarEvent): Promise<EventResult> => {
|
||||
const createMeeting = async (
|
||||
credential: Credential,
|
||||
calEvent: Ensure<CalendarEvent, "language">
|
||||
): Promise<EventResult> => {
|
||||
const parser: CalEventParser = new CalEventParser(calEvent);
|
||||
const uid: string = parser.getUid();
|
||||
|
||||
@@ -227,20 +76,35 @@ const createMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
|
||||
let success = true;
|
||||
|
||||
const creationResult = await videoIntegrations([credential])[0]
|
||||
.createMeeting(calEvent)
|
||||
.catch((e) => {
|
||||
log.error("createMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
});
|
||||
const videoAdapters = getVideoAdapters([credential]);
|
||||
const [firstVideoAdapter] = videoAdapters;
|
||||
const createdMeeting = await firstVideoAdapter.createMeeting(calEvent).catch((e) => {
|
||||
log.error("createMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
});
|
||||
|
||||
if (!createdMeeting) {
|
||||
return {
|
||||
type: credential.type,
|
||||
success,
|
||||
uid,
|
||||
originalEvent: calEvent,
|
||||
};
|
||||
}
|
||||
|
||||
const videoCallData: VideoCallData = {
|
||||
type: credential.type,
|
||||
id: creationResult.id,
|
||||
password: creationResult.password,
|
||||
url: creationResult.join_url,
|
||||
id: createdMeeting.id,
|
||||
password: createdMeeting.password,
|
||||
url: createdMeeting.join_url,
|
||||
};
|
||||
|
||||
if (credential.type === "daily_video") {
|
||||
videoCallData.type = "Daily.co Video";
|
||||
videoCallData.id = createdMeeting.name;
|
||||
videoCallData.url = process.env.BASE_URL + "/call/" + uid;
|
||||
}
|
||||
|
||||
const entryPoint: EntryPoint = {
|
||||
entryPointType: getIntegrationName(videoCallData),
|
||||
uri: videoCallData.url,
|
||||
@@ -261,7 +125,7 @@ const createMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
console.error("organizerMail.sendEmail failed", e);
|
||||
}
|
||||
|
||||
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
||||
if (!createdMeeting || !createdMeeting.disableConfirmationEmail) {
|
||||
try {
|
||||
const attendeeMail = new VideoEventAttendeeMail(emailEvent);
|
||||
await attendeeMail.sendEmail();
|
||||
@@ -274,7 +138,7 @@ const createMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
type: credential.type,
|
||||
success,
|
||||
uid,
|
||||
createdEvent: creationResult,
|
||||
createdEvent: createdMeeting,
|
||||
originalEvent: calEvent,
|
||||
videoCallData: videoCallData,
|
||||
};
|
||||
@@ -289,17 +153,26 @@ const updateMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
);
|
||||
}
|
||||
|
||||
if (!calEvent.uid) {
|
||||
throw new Error("You can't update an meeting without it's UID.");
|
||||
}
|
||||
|
||||
let success = true;
|
||||
|
||||
const updateResult =
|
||||
credential && calEvent.uid
|
||||
? await videoIntegrations([credential])[0]
|
||||
.updateMeeting(calEvent.uid, calEvent)
|
||||
.catch((e) => {
|
||||
log.error("updateMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
})
|
||||
: null;
|
||||
const [firstVideoAdapter] = getVideoAdapters([credential]);
|
||||
const updatedMeeting = await firstVideoAdapter.updateMeeting(calEvent.uid, calEvent).catch((e) => {
|
||||
log.error("updateMeeting failed", e, calEvent);
|
||||
success = false;
|
||||
});
|
||||
|
||||
if (!updatedMeeting) {
|
||||
return {
|
||||
type: credential.type,
|
||||
success,
|
||||
uid: calEvent.uid,
|
||||
originalEvent: calEvent,
|
||||
};
|
||||
}
|
||||
|
||||
const emailEvent = { ...calEvent, uid: newUid };
|
||||
|
||||
@@ -310,7 +183,7 @@ const updateMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
console.error("organizerMail.sendEmail failed", e);
|
||||
}
|
||||
|
||||
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
||||
if (!updatedMeeting.disableConfirmationEmail) {
|
||||
try {
|
||||
const attendeeMail = new EventAttendeeRescheduledMail(emailEvent);
|
||||
await attendeeMail.sendEmail();
|
||||
@@ -323,14 +196,14 @@ const updateMeeting = async (credential: Credential, calEvent: CalendarEvent): P
|
||||
type: credential.type,
|
||||
success,
|
||||
uid: newUid,
|
||||
updatedEvent: updateResult,
|
||||
updatedEvent: updatedMeeting,
|
||||
originalEvent: calEvent,
|
||||
};
|
||||
};
|
||||
|
||||
const deleteMeeting = (credential: Credential, uid: string): Promise<unknown> => {
|
||||
if (credential) {
|
||||
return videoIntegrations([credential])[0].deleteMeeting(uid);
|
||||
return getVideoAdapters([credential])[0].deleteMeeting(uid);
|
||||
}
|
||||
|
||||
return Promise.resolve({});
|
||||
|
||||
Reference in new issue
Block a user