2021-09-22 19:52:38 +00:00
|
|
|
import { Credential } from "@prisma/client";
|
2021-07-15 01:19:30 +00:00
|
|
|
import short from "short-uuid";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { v5 as uuidv5 } from "uuid";
|
|
|
|
|
|
|
|
import CalEventParser from "@lib/CalEventParser";
|
2021-10-26 16:17:24 +00:00
|
|
|
import "@lib/emails/EventMail";
|
2021-07-20 18:40:41 +00:00
|
|
|
import { getIntegrationName } from "@lib/emails/helpers";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { EventResult } from "@lib/events/EventManager";
|
|
|
|
import logger from "@lib/logger";
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
import { AdditionInformation, CalendarEvent, EntryPoint } from "./calendarClient";
|
2021-09-22 19:52:38 +00:00
|
|
|
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
|
|
|
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
|
|
|
import VideoEventAttendeeMail from "./emails/VideoEventAttendeeMail";
|
|
|
|
import VideoEventOrganizerMail from "./emails/VideoEventOrganizerMail";
|
2021-10-26 16:17:24 +00:00
|
|
|
import DailyVideoApiAdapter from "./integrations/Daily/DailyVideoApiAdapter";
|
|
|
|
import ZoomVideoApiAdapter from "./integrations/Zoom/ZoomVideoApiAdapter";
|
|
|
|
import { Ensure } from "./types/utils";
|
2021-07-15 01:19:30 +00:00
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[lib] videoClient"] });
|
2021-06-16 22:56:02 +00:00
|
|
|
|
|
|
|
const translator = short();
|
|
|
|
|
|
|
|
export interface VideoCallData {
|
|
|
|
type: string;
|
|
|
|
id: string;
|
|
|
|
password: string;
|
|
|
|
url: string;
|
|
|
|
}
|
2021-06-14 16:13:54 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
type EventBusyDate = Record<"start" | "end", Date>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
export interface VideoApiAdapter {
|
2021-06-16 22:56:02 +00:00
|
|
|
createMeeting(event: CalendarEvent): Promise<any>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
updateMeeting(uid: string, event: CalendarEvent): Promise<any>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-28 20:05:37 +00:00
|
|
|
deleteMeeting(uid: string): Promise<unknown>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
getAvailability(dateFrom?: string, dateTo?: string): Promise<EventBusyDate[]>;
|
2021-06-13 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// factory
|
2021-10-26 16:17:24 +00:00
|
|
|
const getVideoAdapters = (withCredentials: Credential[]): VideoApiAdapter[] =>
|
2021-09-22 18:36:13 +00:00
|
|
|
withCredentials.reduce<VideoApiAdapter[]>((acc, cred) => {
|
|
|
|
switch (cred.type) {
|
|
|
|
case "zoom_video":
|
2021-10-26 16:17:24 +00:00
|
|
|
acc.push(ZoomVideoApiAdapter(cred));
|
|
|
|
break;
|
|
|
|
case "daily_video":
|
|
|
|
acc.push(DailyVideoApiAdapter(cred));
|
2021-09-22 18:36:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const getBusyVideoTimes: (withCredentials: Credential[]) => Promise<unknown[]> = (withCredentials) =>
|
2021-10-26 16:17:24 +00:00
|
|
|
Promise.all(getVideoAdapters(withCredentials).map((c) => c.getAvailability())).then((results) =>
|
2021-07-15 01:19:30 +00:00
|
|
|
results.reduce((acc, availability) => acc.concat(availability), [])
|
|
|
|
);
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
const createMeeting = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: Ensure<CalendarEvent, "language">
|
|
|
|
): Promise<EventResult> => {
|
2021-10-25 13:05:21 +00:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent);
|
2021-07-25 17:15:31 +00:00
|
|
|
const uid: string = parser.getUid();
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
if (!credential) {
|
2021-07-15 01:19:30 +00:00
|
|
|
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."
|
|
|
|
);
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
const videoCallData: VideoCallData = {
|
|
|
|
type: credential.type,
|
2021-10-26 16:17:24 +00:00
|
|
|
id: createdMeeting.id,
|
|
|
|
password: createdMeeting.password,
|
|
|
|
url: createdMeeting.join_url,
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
2021-06-16 21:40:13 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (credential.type === "daily_video") {
|
|
|
|
videoCallData.type = "Daily.co Video";
|
|
|
|
videoCallData.id = createdMeeting.name;
|
|
|
|
videoCallData.url = process.env.BASE_URL + "/call/" + uid;
|
|
|
|
}
|
|
|
|
|
2021-07-20 18:40:41 +00:00
|
|
|
const entryPoint: EntryPoint = {
|
|
|
|
entryPointType: getIntegrationName(videoCallData),
|
|
|
|
uri: videoCallData.url,
|
2021-10-25 13:05:21 +00:00
|
|
|
label: calEvent.language("enter_meeting"),
|
2021-07-20 18:40:41 +00:00
|
|
|
pin: videoCallData.password,
|
|
|
|
};
|
|
|
|
|
|
|
|
const additionInformation: AdditionInformation = {
|
|
|
|
entryPoints: [entryPoint],
|
|
|
|
};
|
|
|
|
|
2021-11-09 16:27:33 +00:00
|
|
|
calEvent.additionInformation = additionInformation;
|
|
|
|
calEvent.videoCallData = videoCallData;
|
2021-10-25 13:05:21 +00:00
|
|
|
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-11-09 16:27:33 +00:00
|
|
|
const organizerMail = new VideoEventOrganizerMail(calEvent);
|
2021-06-21 16:15:05 +00:00
|
|
|
await organizerMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-16 22:56:02 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (!createdMeeting || !createdMeeting.disableConfirmationEmail) {
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-11-09 16:27:33 +00:00
|
|
|
const attendeeMail = new VideoEventAttendeeMail(calEvent);
|
2021-06-21 16:15:05 +00:00
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-16 22:56:02 +00:00
|
|
|
uid,
|
2021-10-26 16:17:24 +00:00
|
|
|
createdEvent: createdMeeting,
|
2021-07-15 01:19:30 +00:00
|
|
|
originalEvent: calEvent,
|
2021-09-22 22:43:10 +00:00
|
|
|
videoCallData: videoCallData,
|
2021-06-16 22:56:02 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-11-09 16:27:33 +00:00
|
|
|
const updateMeeting = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: CalendarEvent,
|
|
|
|
bookingRefUid: string | null
|
|
|
|
): Promise<EventResult> => {
|
|
|
|
const uid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
2021-06-17 00:44:13 +00:00
|
|
|
|
|
|
|
if (!credential) {
|
2021-07-15 01:19:30 +00:00
|
|
|
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."
|
|
|
|
);
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
const [firstVideoAdapter] = getVideoAdapters([credential]);
|
2021-11-09 16:27:33 +00:00
|
|
|
const updatedMeeting =
|
|
|
|
credential && bookingRefUid
|
|
|
|
? await firstVideoAdapter.updateMeeting(bookingRefUid, calEvent).catch((e) => {
|
|
|
|
log.error("updateMeeting failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
|
|
|
: undefined;
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-11-09 16:27:33 +00:00
|
|
|
const organizerMail = new EventOrganizerRescheduledMail(calEvent);
|
2021-06-21 16:15:05 +00:00
|
|
|
await organizerMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-17 00:44:13 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (!updatedMeeting.disableConfirmationEmail) {
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-11-09 16:27:33 +00:00
|
|
|
const attendeeMail = new EventAttendeeRescheduledMail(calEvent);
|
2021-06-21 16:15:05 +00:00
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
2021-07-15 01:19:30 +00:00
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-11-09 16:27:33 +00:00
|
|
|
uid,
|
2021-10-26 16:17:24 +00:00
|
|
|
updatedEvent: updatedMeeting,
|
2021-07-15 01:19:30 +00:00
|
|
|
originalEvent: calEvent,
|
2021-06-17 00:44:13 +00:00
|
|
|
};
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 20:05:37 +00:00
|
|
|
const deleteMeeting = (credential: Credential, uid: string): Promise<unknown> => {
|
2021-06-16 22:56:02 +00:00
|
|
|
if (credential) {
|
2021-10-26 16:17:24 +00:00
|
|
|
return getVideoAdapters([credential])[0].deleteMeeting(uid);
|
2021-06-16 22:56:02 +00:00
|
|
|
}
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
return Promise.resolve({});
|
2021-06-13 13:22:17 +00:00
|
|
|
};
|
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
export { getBusyVideoTimes, createMeeting, updateMeeting, deleteMeeting };
|