2022-01-06 17:28:31 +00:00
|
|
|
import { Credential, SelectedCalendar } from "@prisma/client";
|
|
|
|
import _ from "lodash";
|
|
|
|
|
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";
|
|
|
|
import notEmpty from "@calcom/lib/notEmpty";
|
|
|
|
import type { CalendarEvent, EventBusyDate } from "@calcom/types/Calendar";
|
|
|
|
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
|
|
|
/** TODO: Remove once all references are updated to app-store */
|
|
|
|
export { getCalendar };
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBusyCalendarTimes = async (
|
|
|
|
withCredentials: Credential[],
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: SelectedCalendar[]
|
|
|
|
) => {
|
|
|
|
const calendars = withCredentials
|
|
|
|
.filter((credential) => credential.type.endsWith("_calendar"))
|
|
|
|
.map((credential) => getCalendar(credential))
|
|
|
|
.filter(notEmpty);
|
|
|
|
|
2022-01-06 22:06:31 +00:00
|
|
|
let results: EventBusyDate[][] = [];
|
|
|
|
try {
|
|
|
|
results = await Promise.all(calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars)));
|
|
|
|
} catch (error) {
|
|
|
|
log.warn(error);
|
|
|
|
}
|
2022-01-06 17:28:31 +00:00
|
|
|
|
|
|
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createEvent = async (credential: Credential, calEvent: CalendarEvent): Promise<EventResult> => {
|
|
|
|
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-01-06 17:28:31 +00:00
|
|
|
): Promise<EventResult> => {
|
|
|
|
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-05-12 09:33:15 +00:00
|
|
|
.then(() => (success = true))
|
|
|
|
.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({});
|
|
|
|
};
|