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";
|
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
import { getUid } from "@lib/CalEventParser";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { EventResult } from "@lib/events/EventManager";
|
2021-11-26 11:03:43 +00:00
|
|
|
import { PartialReference } from "@lib/events/EventManager";
|
2021-09-22 19:52:38 +00:00
|
|
|
import logger from "@lib/logger";
|
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
import { CalendarEvent } from "./calendarClient";
|
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-11-26 11:03:43 +00:00
|
|
|
createMeeting(event: CalendarEvent): Promise<VideoCallData>;
|
2021-06-13 13:22:17 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
updateMeeting(bookingRef: PartialReference, event: CalendarEvent): Promise<VideoCallData>;
|
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;
|
|
|
|
}, []);
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
const getBusyVideoTimes = (withCredentials: Credential[]) =>
|
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-11-26 11:03:43 +00:00
|
|
|
const uid: string = getUid(calEvent);
|
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
|
|
|
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-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,
|
2021-11-26 11:03:43 +00:00
|
|
|
bookingRef: PartialReference | null
|
2021-11-09 16:27:33 +00:00
|
|
|
): Promise<EventResult> => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
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 =
|
2021-11-26 11:03:43 +00:00
|
|
|
credential && bookingRef
|
|
|
|
? await firstVideoAdapter.updateMeeting(bookingRef, calEvent).catch((e) => {
|
2021-11-09 16:27:33 +00:00
|
|
|
log.error("updateMeeting failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
|
|
|
: undefined;
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
if (!updatedMeeting) {
|
|
|
|
return {
|
|
|
|
type: credential.type,
|
|
|
|
success,
|
|
|
|
uid,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
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 };
|