2021-12-09 15:51:37 +00:00
|
|
|
import { Credential, DestinationCalendar, SelectedCalendar } from "@prisma/client";
|
2021-10-25 13:05:21 +00:00
|
|
|
import { TFunction } from "next-i18next";
|
2021-09-23 08:49:17 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
import { PaymentInfo } from "@ee/lib/stripe/server";
|
|
|
|
|
|
|
|
import { getUid } from "@lib/CalEventParser";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { Event, EventResult } from "@lib/events/EventManager";
|
2021-11-26 11:03:43 +00:00
|
|
|
import { AppleCalendar } from "@lib/integrations/Apple/AppleCalendarAdapter";
|
|
|
|
import { CalDavCalendar } from "@lib/integrations/CalDav/CalDavCalendarAdapter";
|
|
|
|
import {
|
|
|
|
ConferenceData,
|
2021-12-09 15:51:37 +00:00
|
|
|
GoogleCalendarApiAdapter,
|
2021-11-26 11:03:43 +00:00
|
|
|
} from "@lib/integrations/GoogleCalendar/GoogleCalendarApiAdapter";
|
2021-12-09 15:51:37 +00:00
|
|
|
import { Office365CalendarApiAdapter } from "@lib/integrations/Office365Calendar/Office365CalendarApiAdapter";
|
2021-07-15 01:19:30 +00:00
|
|
|
import logger from "@lib/logger";
|
2021-09-23 08:49:17 +00:00
|
|
|
import { VideoCallData } from "@lib/videoClient";
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
import notEmpty from "./notEmpty";
|
2021-12-07 15:48:08 +00:00
|
|
|
import { Ensure } from "./types/utils";
|
|
|
|
|
2021-09-17 21:31:44 +00:00
|
|
|
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
|
|
|
|
|
2021-10-05 22:46:48 +00:00
|
|
|
export type Person = { name: string; email: string; timeZone: string };
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
export interface EntryPoint {
|
|
|
|
entryPointType?: string;
|
|
|
|
uri?: string;
|
|
|
|
label?: string;
|
|
|
|
pin?: string;
|
|
|
|
accessCode?: string;
|
|
|
|
meetingCode?: string;
|
|
|
|
passcode?: string;
|
|
|
|
password?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AdditionInformation {
|
|
|
|
conferenceData?: ConferenceData;
|
|
|
|
entryPoints?: EntryPoint[];
|
|
|
|
hangoutLink?: string;
|
|
|
|
}
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
export interface CalendarEvent {
|
2021-06-17 00:44:13 +00:00
|
|
|
type: string;
|
|
|
|
title: string;
|
|
|
|
startTime: string;
|
|
|
|
endTime: string;
|
2021-10-25 13:05:21 +00:00
|
|
|
description?: string | null;
|
2021-09-06 09:06:33 +00:00
|
|
|
team?: {
|
|
|
|
name: string;
|
|
|
|
members: string[];
|
|
|
|
};
|
2021-10-25 13:05:21 +00:00
|
|
|
location?: string | null;
|
2021-06-17 00:44:13 +00:00
|
|
|
organizer: Person;
|
|
|
|
attendees: Person[];
|
2021-06-21 23:15:29 +00:00
|
|
|
conferenceData?: ConferenceData;
|
2021-10-25 13:05:21 +00:00
|
|
|
language: TFunction;
|
|
|
|
additionInformation?: AdditionInformation;
|
|
|
|
uid?: string | null;
|
|
|
|
videoCallData?: VideoCallData;
|
2021-11-26 11:03:43 +00:00
|
|
|
paymentInfo?: PaymentInfo | null;
|
2021-12-09 15:51:37 +00:00
|
|
|
destinationCalendar?: DestinationCalendar | null;
|
2021-06-21 23:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 15:48:08 +00:00
|
|
|
export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "externalId"> {
|
2021-10-26 16:17:24 +00:00
|
|
|
primary?: boolean;
|
|
|
|
name?: string;
|
2021-06-14 17:45:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
type EventBusyDate = Record<"start" | "end", Date | string>;
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
export interface CalendarApiAdapter {
|
2021-10-26 16:17:24 +00:00
|
|
|
createEvent(event: CalendarEvent): Promise<Event>;
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
updateEvent(uid: string, event: CalendarEvent): Promise<any>;
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
deleteEvent(uid: string): Promise<unknown>;
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
getAvailability(
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: IntegrationCalendar[]
|
2021-12-09 15:51:37 +00:00
|
|
|
): Promise<EventBusyDate[]>;
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-06-21 23:15:29 +00:00
|
|
|
listCalendars(): Promise<IntegrationCalendar[]>;
|
2021-05-27 22:10:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:35:44 +00:00
|
|
|
function getCalendarAdapterOrNull(credential: Credential): CalendarApiAdapter | null {
|
|
|
|
switch (credential.type) {
|
|
|
|
case "google_calendar":
|
2021-11-26 11:03:43 +00:00
|
|
|
return GoogleCalendarApiAdapter(credential);
|
2021-10-12 09:35:44 +00:00
|
|
|
case "office365_calendar":
|
2021-11-26 11:03:43 +00:00
|
|
|
return Office365CalendarApiAdapter(credential);
|
2021-10-12 09:35:44 +00:00
|
|
|
case "caldav_calendar":
|
2021-12-07 15:48:08 +00:00
|
|
|
return new CalDavCalendar(credential);
|
2021-10-12 09:35:44 +00:00
|
|
|
case "apple_calendar":
|
2021-12-07 15:48:08 +00:00
|
|
|
return new AppleCalendar(credential);
|
2021-10-12 09:35:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
const getBusyCalendarTimes = async (
|
2021-10-26 16:17:24 +00:00
|
|
|
withCredentials: Credential[],
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: SelectedCalendar[]
|
2021-12-09 15:51:37 +00:00
|
|
|
) => {
|
|
|
|
const adapters = withCredentials.map(getCalendarAdapterOrNull).filter(notEmpty);
|
|
|
|
const results = await Promise.all(
|
|
|
|
adapters.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
2021-06-21 23:15:29 +00:00
|
|
|
);
|
2021-12-09 15:51:37 +00:00
|
|
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
|
|
|
};
|
2021-06-21 23:15:29 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
const createEvent = async (credential: Credential, calEvent: CalendarEvent): Promise<EventResult> => {
|
|
|
|
const uid: string = getUid(calEvent);
|
2021-12-09 15:51:37 +00:00
|
|
|
const adapter = getCalendarAdapterOrNull(credential);
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
const creationResult = adapter
|
|
|
|
? await adapter.createEvent(calEvent).catch((e) => {
|
|
|
|
log.error("createEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
2021-10-26 16:17:24 +00:00
|
|
|
: undefined;
|
2021-06-01 19:16:06 +00:00
|
|
|
|
2021-06-17 00:44:13 +00:00
|
|
|
return {
|
2021-07-15 01:19:30 +00:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-17 00:44:13 +00:00
|
|
|
uid,
|
2021-06-21 23:15:29 +00:00
|
|
|
createdEvent: creationResult,
|
2021-07-15 01:19:30 +00:00
|
|
|
originalEvent: calEvent,
|
2021-06-17 00:44:13 +00:00
|
|
|
};
|
2021-05-27 22:10:20 +00:00
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-07-07 12:07:18 +00:00
|
|
|
const updateEvent = async (
|
|
|
|
credential: Credential,
|
2021-07-20 18:07:59 +00:00
|
|
|
calEvent: CalendarEvent,
|
2021-11-09 16:27:33 +00:00
|
|
|
bookingRefUid: string | null
|
2021-07-15 01:19:30 +00:00
|
|
|
): Promise<EventResult> => {
|
2021-11-26 11:03:43 +00:00
|
|
|
const uid = getUid(calEvent);
|
2021-12-09 15:51:37 +00:00
|
|
|
const adapter = getCalendarAdapterOrNull(credential);
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-12-06 19:34:31 +00:00
|
|
|
const updatedResult =
|
2021-12-09 15:51:37 +00:00
|
|
|
adapter && bookingRefUid
|
|
|
|
? await adapter.updateEvent(bookingRefUid, calEvent).catch((e) => {
|
|
|
|
log.error("updateEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
2021-11-26 11:03:43 +00:00
|
|
|
: undefined;
|
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-12-06 19:34:31 +00:00
|
|
|
updatedEvent: updatedResult,
|
2021-07-15 01:19:30 +00:00
|
|
|
originalEvent: calEvent,
|
2021-06-17 00:44:13 +00:00
|
|
|
};
|
2021-06-06 23:10:56 +00:00
|
|
|
};
|
|
|
|
|
2021-07-07 12:07:18 +00:00
|
|
|
const deleteEvent = (credential: Credential, uid: string): Promise<unknown> => {
|
2021-12-09 15:51:37 +00:00
|
|
|
const adapter = getCalendarAdapterOrNull(credential);
|
|
|
|
if (adapter) {
|
|
|
|
return adapter.deleteEvent(uid);
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-06-17 00:44:13 +00:00
|
|
|
return Promise.resolve({});
|
2021-06-06 23:10:56 +00:00
|
|
|
};
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
export { getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, getCalendarAdapterOrNull };
|