2022-01-06 17:28:31 +00:00
|
|
|
import { Credential, SelectedCalendar } from "@prisma/client";
|
2022-06-19 15:02:00 +00:00
|
|
|
import { createHash } from "crypto";
|
2022-01-06 17:28:31 +00:00
|
|
|
import _ from "lodash";
|
2022-06-19 15:02:00 +00:00
|
|
|
import cache from "memory-cache";
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
|
|
|
import getApps from "@calcom/app-store/utils";
|
|
|
|
import { getUid } from "@calcom/lib/CalEventParser";
|
2022-03-16 23:36:43 +00:00
|
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
2022-03-23 22:00:30 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2022-06-17 18:34:41 +00:00
|
|
|
import type { CalendarEvent, EventBusyDate, NewCalendarEventType } from "@calcom/types/Calendar";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { EventResult } from "@calcom/types/EventManager";
|
2022-01-06 17:28:31 +00:00
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
export const getCalendarCredentials = (credentials: Array<Credential>, userId: number) => {
|
|
|
|
const calendarCredentials = getApps(credentials)
|
|
|
|
.filter((app) => app.type.endsWith("_calendar"))
|
|
|
|
.flatMap((app) => {
|
|
|
|
const credentials = app.credentials.flatMap((credential) => {
|
|
|
|
const calendar = getCalendar(credential);
|
|
|
|
return app && calendar && app.variant === "calendar"
|
|
|
|
? [{ integration: app, credential, calendar }]
|
|
|
|
: [];
|
2022-01-06 17:28:31 +00:00
|
|
|
});
|
2022-03-23 22:00:30 +00:00
|
|
|
return credentials.length ? credentials : [];
|
2022-01-06 17:28:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return calendarCredentials;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getConnectedCalendars = async (
|
|
|
|
calendarCredentials: ReturnType<typeof getCalendarCredentials>,
|
|
|
|
selectedCalendars: { externalId: string }[]
|
|
|
|
) => {
|
|
|
|
const connectedCalendars = await Promise.all(
|
|
|
|
calendarCredentials.map(async (item) => {
|
|
|
|
const { calendar, integration, credential } = item;
|
|
|
|
|
|
|
|
const credentialId = credential.id;
|
|
|
|
try {
|
|
|
|
const cals = await calendar.listCalendars();
|
|
|
|
const calendars = _(cals)
|
|
|
|
.map((cal) => ({
|
|
|
|
...cal,
|
|
|
|
primary: cal.primary || null,
|
|
|
|
isSelected: selectedCalendars.some((selected) => selected.externalId === cal.externalId),
|
|
|
|
}))
|
|
|
|
.sortBy(["primary"])
|
|
|
|
.value();
|
|
|
|
const primary = calendars.find((item) => item.primary) ?? calendars[0];
|
|
|
|
if (!primary) {
|
|
|
|
throw new Error("No primary calendar found");
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
integration,
|
|
|
|
credentialId,
|
|
|
|
primary,
|
|
|
|
calendars,
|
|
|
|
};
|
|
|
|
} catch (_error) {
|
|
|
|
const error = getErrorFromUnknown(_error);
|
|
|
|
return {
|
|
|
|
integration,
|
|
|
|
credentialId,
|
|
|
|
error: {
|
|
|
|
message: error.message,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return connectedCalendars;
|
|
|
|
};
|
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
const CACHING_TIME = 30_000; // 30 seconds
|
|
|
|
|
|
|
|
const getCachedResults = (
|
2022-01-06 17:28:31 +00:00
|
|
|
withCredentials: Credential[],
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: SelectedCalendar[]
|
|
|
|
) => {
|
2022-06-19 15:02:00 +00:00
|
|
|
const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar"));
|
|
|
|
const calendars = calendarCredentials.map((credential) => getCalendar(credential));
|
|
|
|
const results = calendars.map(async (c, i) => {
|
|
|
|
/** Filter out nulls */
|
|
|
|
if (!c) return [];
|
|
|
|
/** We rely on the index so we can match credentials with calendars */
|
|
|
|
const { id, type } = calendarCredentials[i];
|
|
|
|
/** We just pass the calendars that matched the credential type,
|
|
|
|
* TODO: Migrate credential type or appId
|
|
|
|
*/
|
|
|
|
const passedSelectedCalendars = selectedCalendars.filter((sc) => sc.integration === type);
|
|
|
|
/** We extract external Ids so we don't cache too much */
|
|
|
|
const selectedCalendarIds = passedSelectedCalendars.map((sc) => sc.externalId);
|
|
|
|
/** We create a unque hash key based on the input data */
|
|
|
|
const cacheKey = createHash("md5").update(JSON.stringify({ id, selectedCalendarIds })).digest("hex");
|
|
|
|
/** Check if we already have cached data and return */
|
|
|
|
const cachedAvailability = cache.get(cacheKey);
|
|
|
|
if (cachedAvailability) return cachedAvailability;
|
|
|
|
/** If we don't then we actually fetch external calendars (which can be very slow) */
|
|
|
|
const availability = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars);
|
|
|
|
/** We save the availability to a few seconds so recurrent calls are nearly instant */
|
|
|
|
cache.put(cacheKey, availability, CACHING_TIME);
|
|
|
|
return availability;
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
return Promise.all(results);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBusyCalendarTimes = async (
|
|
|
|
withCredentials: Credential[],
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: SelectedCalendar[]
|
|
|
|
) => {
|
2022-01-06 22:06:31 +00:00
|
|
|
let results: EventBusyDate[][] = [];
|
|
|
|
try {
|
2022-06-19 15:02:00 +00:00
|
|
|
results = await getCachedResults(withCredentials, dateFrom, dateTo, selectedCalendars);
|
2022-01-06 22:06:31 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.warn(error);
|
|
|
|
}
|
2022-01-06 17:28:31 +00:00
|
|
|
|
|
|
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
|
|
|
};
|
|
|
|
|
2022-06-17 18:34:41 +00:00
|
|
|
export const createEvent = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: CalendarEvent
|
|
|
|
): Promise<EventResult<NewCalendarEventType>> => {
|
2022-01-06 17:28:31 +00:00
|
|
|
const uid: string = getUid(calEvent);
|
|
|
|
const calendar = getCalendar(credential);
|
|
|
|
let success = true;
|
|
|
|
|
2022-03-28 18:07:13 +00:00
|
|
|
// Check if the disabledNotes flag is set to true
|
|
|
|
if (calEvent.hideCalendarNotes) {
|
2022-04-07 18:22:11 +00:00
|
|
|
calEvent.additionalNotes = "Notes have been hidden by the organiser"; // TODO: i18n this string?
|
2022-03-28 18:07:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 02:23:38 +00:00
|
|
|
// TODO: Surfice success/error messages coming from apps to improve end user visibility
|
2022-01-06 17:28:31 +00:00
|
|
|
const creationResult = calendar
|
|
|
|
? await calendar.createEvent(calEvent).catch((e) => {
|
2022-04-02 23:43:28 +00:00
|
|
|
log.error("createEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
return undefined;
|
|
|
|
})
|
2022-01-06 17:28:31 +00:00
|
|
|
: undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: credential.type,
|
|
|
|
success,
|
|
|
|
uid,
|
|
|
|
createdEvent: creationResult,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateEvent = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: CalendarEvent,
|
2022-05-16 20:20:09 +00:00
|
|
|
bookingRefUid: string | null,
|
|
|
|
externalCalendarId: string | null
|
2022-06-17 18:34:41 +00:00
|
|
|
): Promise<EventResult<NewCalendarEventType>> => {
|
2022-01-06 17:28:31 +00:00
|
|
|
const uid = getUid(calEvent);
|
|
|
|
const calendar = getCalendar(credential);
|
2022-05-12 09:33:15 +00:00
|
|
|
let success = false;
|
|
|
|
if (bookingRefUid === "") {
|
|
|
|
log.error("updateEvent failed", "bookingRefUid is empty", calEvent, credential);
|
|
|
|
}
|
2022-01-06 17:28:31 +00:00
|
|
|
const updatedResult =
|
|
|
|
calendar && bookingRefUid
|
2022-05-12 09:33:15 +00:00
|
|
|
? await calendar
|
2022-05-16 20:20:09 +00:00
|
|
|
.updateEvent(bookingRefUid, calEvent, externalCalendarId)
|
2022-06-17 18:34:41 +00:00
|
|
|
.then((event) => {
|
|
|
|
success = true;
|
|
|
|
return event;
|
|
|
|
})
|
2022-05-12 09:33:15 +00:00
|
|
|
.catch((e) => {
|
|
|
|
log.error("updateEvent failed", e, calEvent);
|
|
|
|
return undefined;
|
|
|
|
})
|
2022-01-06 17:28:31 +00:00
|
|
|
: undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: credential.type,
|
|
|
|
success,
|
|
|
|
uid,
|
|
|
|
updatedEvent: updatedResult,
|
|
|
|
originalEvent: calEvent,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-13 19:47:15 +00:00
|
|
|
export const deleteEvent = (credential: Credential, uid: string, event: CalendarEvent): Promise<unknown> => {
|
2022-01-06 17:28:31 +00:00
|
|
|
const calendar = getCalendar(credential);
|
|
|
|
if (calendar) {
|
2022-01-13 19:47:15 +00:00
|
|
|
return calendar.deleteEvent(uid, event);
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve({});
|
|
|
|
};
|