2021-09-22 15:36:13 -03:00
|
|
|
import { Prisma, Credential } from "@prisma/client";
|
2021-09-23 05:49:17 -03:00
|
|
|
|
2021-07-14 22:19:30 -03:00
|
|
|
import { EventResult } from "@lib/events/EventManager";
|
|
|
|
import logger from "@lib/logger";
|
2021-09-23 05:49:17 -03:00
|
|
|
import { VideoCallData } from "@lib/videoClient";
|
|
|
|
|
2021-09-22 16:52:38 -03:00
|
|
|
import CalEventParser from "./CalEventParser";
|
|
|
|
import EventOrganizerMail from "./emails/EventOrganizerMail";
|
|
|
|
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
2021-09-11 04:54:13 -03:00
|
|
|
import { AppleCalendar } from "./integrations/Apple/AppleCalendarAdapter";
|
2021-09-22 16:52:38 -03:00
|
|
|
import { CalDavCalendar } from "./integrations/CalDav/CalDavCalendarAdapter";
|
|
|
|
import prisma from "./prisma";
|
2021-06-16 18:40:13 -03:00
|
|
|
|
2021-09-17 18:31:44 -03:00
|
|
|
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
|
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-06-21 20:15:29 -03:00
|
|
|
const { google } = require("googleapis");
|
2021-06-20 12:33:02 -03:00
|
|
|
|
|
|
|
const googleAuth = (credential) => {
|
2021-06-24 13:12:22 -03:00
|
|
|
const { client_secret, client_id, redirect_uris } = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
2021-06-21 12:56:14 -03:00
|
|
|
const myGoogleAuth = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
2021-06-24 13:12:22 -03:00
|
|
|
myGoogleAuth.setCredentials(credential.key);
|
2021-06-20 12:33:02 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
2021-06-20 12:33:02 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
const refreshAccessToken = () =>
|
|
|
|
myGoogleAuth
|
|
|
|
.refreshToken(credential.key.refresh_token)
|
|
|
|
.then((res) => {
|
2021-06-20 12:33:02 -03:00
|
|
|
const token = res.res.data;
|
|
|
|
credential.key.access_token = token.access_token;
|
|
|
|
credential.key.expiry_date = token.expiry_date;
|
2021-06-24 13:12:22 -03:00
|
|
|
return prisma.credential
|
|
|
|
.update({
|
2021-06-20 12:33:02 -03:00
|
|
|
where: {
|
2021-06-24 13:12:22 -03:00
|
|
|
id: credential.id,
|
2021-06-20 12:33:02 -03:00
|
|
|
},
|
|
|
|
data: {
|
2021-06-24 13:12:22 -03:00
|
|
|
key: credential.key,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(() => {
|
2021-06-20 12:33:02 -03:00
|
|
|
myGoogleAuth.setCredentials(credential.key);
|
|
|
|
return myGoogleAuth;
|
2021-06-24 13:12:22 -03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2021-06-20 12:33:02 -03:00
|
|
|
console.error("Error refreshing google token", err);
|
|
|
|
return myGoogleAuth;
|
2021-06-24 13:12:22 -03:00
|
|
|
});
|
2021-06-20 12:33:02 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
return {
|
|
|
|
getToken: () => (!isExpired() ? Promise.resolve(myGoogleAuth) : refreshAccessToken()),
|
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
};
|
|
|
|
|
2021-06-07 21:25:34 -03:00
|
|
|
function handleErrorsJson(response) {
|
2021-06-16 21:44:13 -03:00
|
|
|
if (!response.ok) {
|
2021-06-24 13:12:22 -03:00
|
|
|
response.json().then((e) => console.error("O365 Error", e));
|
2021-06-16 21:44:13 -03:00
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.json();
|
2021-04-21 19:10:48 -03:00
|
|
|
}
|
|
|
|
|
2021-06-07 21:25:34 -03:00
|
|
|
function handleErrorsRaw(response) {
|
2021-06-16 21:44:13 -03:00
|
|
|
if (!response.ok) {
|
2021-06-24 13:12:22 -03:00
|
|
|
response.text().then((e) => console.error("O365 Error", e));
|
2021-06-16 21:44:13 -03:00
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
2021-06-07 21:25:34 -03:00
|
|
|
}
|
2021-04-21 19:10:48 -03:00
|
|
|
|
|
|
|
const o365Auth = (credential) => {
|
2021-06-24 13:12:22 -03:00
|
|
|
const isExpired = (expiryDate) => expiryDate < Math.round(+new Date() / 1000);
|
2021-06-20 12:33:02 -03:00
|
|
|
|
2021-06-21 12:56:14 -03:00
|
|
|
const refreshAccessToken = (refreshToken) => {
|
2021-06-24 13:12:22 -03:00
|
|
|
return fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
|
|
body: new URLSearchParams({
|
|
|
|
scope: "User.Read Calendars.Read Calendars.ReadWrite",
|
|
|
|
client_id: process.env.MS_GRAPH_CLIENT_ID,
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
grant_type: "refresh_token",
|
|
|
|
client_secret: process.env.MS_GRAPH_CLIENT_SECRET,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) => {
|
|
|
|
credential.key.access_token = responseBody.access_token;
|
|
|
|
credential.key.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
|
|
|
return prisma.credential
|
|
|
|
.update({
|
|
|
|
where: {
|
|
|
|
id: credential.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
key: credential.key,
|
|
|
|
},
|
2021-06-20 12:33:02 -03:00
|
|
|
})
|
2021-06-24 13:12:22 -03:00
|
|
|
.then(() => credential.key.access_token);
|
|
|
|
});
|
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-06-16 21:44:13 -03:00
|
|
|
return {
|
2021-06-21 20:15:29 -03:00
|
|
|
getToken: () =>
|
|
|
|
!isExpired(credential.key.expiry_date)
|
|
|
|
? Promise.resolve(credential.key.access_token)
|
|
|
|
: refreshAccessToken(credential.key.refresh_token),
|
2021-06-16 21:44:13 -03:00
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
};
|
|
|
|
|
2021-09-22 15:36:13 -03:00
|
|
|
const userData = Prisma.validator<Prisma.UserArgs>()({
|
|
|
|
select: { name: true, email: true, timeZone: true },
|
|
|
|
});
|
|
|
|
export type Person = Prisma.UserGetPayload<typeof userData>;
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-06-28 21:30:45 -03:00
|
|
|
export interface CalendarEvent {
|
2021-06-16 21:44:13 -03:00
|
|
|
type: string;
|
|
|
|
title: string;
|
|
|
|
startTime: string;
|
|
|
|
endTime: string;
|
|
|
|
description?: string;
|
2021-09-06 06:06:33 -03:00
|
|
|
team?: {
|
|
|
|
name: string;
|
|
|
|
members: string[];
|
|
|
|
};
|
2021-06-16 21:44:13 -03:00
|
|
|
location?: string;
|
|
|
|
organizer: Person;
|
|
|
|
attendees: Person[];
|
2021-06-21 20:15:29 -03:00
|
|
|
conferenceData?: ConferenceData;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:30:45 -03:00
|
|
|
export interface ConferenceData {
|
2021-07-07 07:43:13 -03:00
|
|
|
createRequest: unknown;
|
2021-06-21 20:15:29 -03:00
|
|
|
}
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-06-28 21:30:45 -03:00
|
|
|
export interface IntegrationCalendar {
|
2021-06-21 20:15:29 -03:00
|
|
|
integration: string;
|
|
|
|
primary: boolean;
|
|
|
|
externalId: string;
|
|
|
|
name: string;
|
2021-06-14 14:45:24 -03:00
|
|
|
}
|
|
|
|
|
2021-09-22 15:36:13 -03:00
|
|
|
type BufferedBusyTime = { start: string; end: string };
|
2021-06-28 21:30:45 -03:00
|
|
|
export interface CalendarApiAdapter {
|
2021-07-07 07:43:13 -03:00
|
|
|
createEvent(event: CalendarEvent): Promise<unknown>;
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
updateEvent(uid: string, event: CalendarEvent);
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
deleteEvent(uid: string);
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-09-22 15:36:13 -03:00
|
|
|
getAvailability(
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: IntegrationCalendar[]
|
|
|
|
): Promise<BufferedBusyTime[]>;
|
2021-06-14 14:45:24 -03:00
|
|
|
|
2021-06-21 20:15:29 -03:00
|
|
|
listCalendars(): Promise<IntegrationCalendar[]>;
|
2021-05-27 19:10:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
2021-06-16 21:44:13 -03:00
|
|
|
const auth = o365Auth(credential);
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-06-16 21:44:13 -03:00
|
|
|
const translateEvent = (event: CalendarEvent) => {
|
2021-06-24 13:12:22 -03:00
|
|
|
const optional = {};
|
2021-06-16 21:44:13 -03:00
|
|
|
if (event.location) {
|
2021-06-21 20:15:29 -03:00
|
|
|
optional.location = { displayName: event.location };
|
2021-06-16 21:44:13 -03:00
|
|
|
}
|
2021-05-08 16:03:47 -03:00
|
|
|
|
2021-07-18 11:03:59 -03:00
|
|
|
return {
|
|
|
|
subject: event.title,
|
|
|
|
body: {
|
|
|
|
contentType: "HTML",
|
|
|
|
content: event.description,
|
|
|
|
},
|
|
|
|
start: {
|
|
|
|
dateTime: event.startTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
dateTime: event.endTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
attendees: event.attendees.map((attendee) => ({
|
|
|
|
emailAddress: {
|
|
|
|
address: attendee.email,
|
|
|
|
name: attendee.name,
|
|
|
|
},
|
|
|
|
type: "required",
|
|
|
|
})),
|
|
|
|
...optional,
|
|
|
|
};
|
2021-06-16 21:44:13 -03:00
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-06-21 20:15:29 -03:00
|
|
|
const integrationType = "office365_calendar";
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-06-21 20:15:29 -03:00
|
|
|
function listCalendars(): Promise<IntegrationCalendar[]> {
|
|
|
|
return auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://graph.microsoft.com/v1.0/me/calendars", {
|
|
|
|
method: "get",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
2021-04-21 19:10:48 -03:00
|
|
|
},
|
2021-06-21 20:15:29 -03:00
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) => {
|
|
|
|
return responseBody.value.map((cal) => {
|
|
|
|
const calendar: IntegrationCalendar = {
|
|
|
|
externalId: cal.id,
|
|
|
|
integration: integrationType,
|
|
|
|
name: cal.name,
|
|
|
|
primary: cal.isDefaultCalendar,
|
|
|
|
};
|
|
|
|
return calendar;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
getAvailability: (dateFrom, dateTo, selectedCalendars) => {
|
2021-09-16 07:14:48 -03:00
|
|
|
const filter = `?startdatetime=${encodeURIComponent(dateFrom)}&enddatetime=${encodeURIComponent(
|
|
|
|
dateTo
|
|
|
|
)}`;
|
2021-06-21 20:15:29 -03:00
|
|
|
return auth
|
|
|
|
.getToken()
|
|
|
|
.then((accessToken) => {
|
|
|
|
const selectedCalendarIds = selectedCalendars
|
|
|
|
.filter((e) => e.integration === integrationType)
|
|
|
|
.map((e) => e.externalId);
|
|
|
|
if (selectedCalendarIds.length == 0 && selectedCalendars.length > 0) {
|
|
|
|
// Only calendars of other integrations selected
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
selectedCalendarIds.length == 0
|
|
|
|
? listCalendars().then((cals) => cals.map((e) => e.externalId))
|
|
|
|
: Promise.resolve(selectedCalendarIds).then((x) => x)
|
|
|
|
).then((ids: string[]) => {
|
2021-07-05 16:50:54 -03:00
|
|
|
const requests = ids.map((calendarId, id) => ({
|
|
|
|
id,
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
Prefer: 'outlook.timezone="Etc/GMT"',
|
|
|
|
},
|
2021-07-31 10:00:27 -03:00
|
|
|
url: `/me/calendars/${calendarId}/calendarView${filter}`,
|
2021-07-05 16:50:54 -03:00
|
|
|
}));
|
|
|
|
|
|
|
|
return fetch("https://graph.microsoft.com/v1.0/$batch", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ requests }),
|
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) =>
|
|
|
|
responseBody.responses.reduce(
|
|
|
|
(acc, subResponse) =>
|
|
|
|
acc.concat(
|
|
|
|
subResponse.body.value.map((evt) => {
|
|
|
|
return {
|
|
|
|
start: evt.start.dateTime + "Z",
|
|
|
|
end: evt.end.dateTime + "Z",
|
|
|
|
};
|
|
|
|
})
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
);
|
2021-06-21 20:15:29 -03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
createEvent: (event: CalendarEvent) =>
|
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://graph.microsoft.com/v1.0/me/calendar/events", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) => ({
|
2021-06-01 16:16:06 -03:00
|
|
|
...responseBody,
|
|
|
|
disableConfirmationEmail: true,
|
2021-06-21 20:15:29 -03:00
|
|
|
}))
|
|
|
|
),
|
2021-06-24 13:12:22 -03:00
|
|
|
deleteEvent: (uid: string) =>
|
2021-06-21 20:15:29 -03:00
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://graph.microsoft.com/v1.0/me/calendar/events/" + uid, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
},
|
|
|
|
}).then(handleErrorsRaw)
|
|
|
|
),
|
2021-06-24 13:12:22 -03:00
|
|
|
updateEvent: (uid: string, event: CalendarEvent) =>
|
2021-06-21 20:15:29 -03:00
|
|
|
auth.getToken().then((accessToken) =>
|
|
|
|
fetch("https://graph.microsoft.com/v1.0/me/calendar/events/" + uid, {
|
|
|
|
method: "PATCH",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(translateEvent(event)),
|
|
|
|
}).then(handleErrorsRaw)
|
|
|
|
),
|
|
|
|
listCalendars,
|
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
};
|
|
|
|
|
2021-05-27 19:10:20 -03:00
|
|
|
const GoogleCalendar = (credential): CalendarApiAdapter => {
|
2021-06-24 13:12:22 -03:00
|
|
|
const auth = googleAuth(credential);
|
|
|
|
const integrationType = "google_calendar";
|
2021-06-14 14:45:24 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
return {
|
|
|
|
getAvailability: (dateFrom, dateTo, selectedCalendars) =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
2021-07-31 10:00:27 -03:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
const selectedCalendarIds = selectedCalendars
|
|
|
|
.filter((e) => e.integration === integrationType)
|
|
|
|
.map((e) => e.externalId);
|
|
|
|
if (selectedCalendarIds.length == 0 && selectedCalendars.length > 0) {
|
|
|
|
// Only calendars of other integrations selected
|
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-20 12:33:02 -03:00
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
(selectedCalendarIds.length == 0
|
|
|
|
? calendar.calendarList.list().then((cals) => cals.data.items.map((cal) => cal.id))
|
|
|
|
: Promise.resolve(selectedCalendarIds)
|
|
|
|
)
|
|
|
|
.then((calsIds) => {
|
|
|
|
calendar.freebusy.query(
|
|
|
|
{
|
|
|
|
requestBody: {
|
|
|
|
timeMin: dateFrom,
|
|
|
|
timeMax: dateTo,
|
|
|
|
items: calsIds.map((id) => ({ id: id })),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
(err, apires) => {
|
|
|
|
if (err) {
|
2021-06-06 20:10:56 -03:00
|
|
|
reject(err);
|
2021-06-24 13:12:22 -03:00
|
|
|
}
|
|
|
|
resolve(Object.values(apires.data.calendars).flatMap((item) => item["busy"]));
|
2021-06-09 16:47:03 -03:00
|
|
|
}
|
2021-06-24 13:12:22 -03:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
reject(err);
|
2021-06-09 16:47:03 -03:00
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
})
|
|
|
|
),
|
|
|
|
createEvent: (event: CalendarEvent) =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
|
|
|
const payload = {
|
|
|
|
summary: event.title,
|
|
|
|
description: event.description,
|
|
|
|
start: {
|
|
|
|
dateTime: event.startTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
dateTime: event.endTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
attendees: event.attendees,
|
|
|
|
reminders: {
|
|
|
|
useDefault: false,
|
2021-07-27 08:45:53 -03:00
|
|
|
overrides: [{ method: "email", minutes: 10 }],
|
2021-06-24 13:12:22 -03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.location) {
|
|
|
|
payload["location"] = event.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.conferenceData) {
|
|
|
|
payload["conferenceData"] = event.conferenceData;
|
|
|
|
}
|
|
|
|
|
2021-07-31 10:00:27 -03:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
calendar.events.insert(
|
|
|
|
{
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
calendarId: "primary",
|
|
|
|
resource: payload,
|
2021-06-28 17:34:27 -03:00
|
|
|
conferenceDataVersion: 1,
|
2021-06-24 13:12:22 -03:00
|
|
|
},
|
|
|
|
function (err, event) {
|
|
|
|
if (err) {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(event.data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
updateEvent: (uid: string, event: CalendarEvent) =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
|
|
|
const payload = {
|
|
|
|
summary: event.title,
|
|
|
|
description: event.description,
|
|
|
|
start: {
|
|
|
|
dateTime: event.startTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
dateTime: event.endTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
attendees: event.attendees,
|
|
|
|
reminders: {
|
|
|
|
useDefault: false,
|
2021-07-27 08:45:53 -03:00
|
|
|
overrides: [{ method: "email", minutes: 10 }],
|
2021-06-24 13:12:22 -03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.location) {
|
|
|
|
payload["location"] = event.location;
|
|
|
|
}
|
|
|
|
|
2021-07-31 10:00:27 -03:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
calendar.events.update(
|
|
|
|
{
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
calendarId: "primary",
|
|
|
|
eventId: uid,
|
|
|
|
sendNotifications: true,
|
|
|
|
sendUpdates: "all",
|
|
|
|
resource: payload,
|
|
|
|
},
|
|
|
|
function (err, event) {
|
|
|
|
if (err) {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(event.data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
deleteEvent: (uid: string) =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
2021-07-31 10:00:27 -03:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
calendar.events.delete(
|
|
|
|
{
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
calendarId: "primary",
|
|
|
|
eventId: uid,
|
|
|
|
sendNotifications: true,
|
|
|
|
sendUpdates: "all",
|
|
|
|
},
|
|
|
|
function (err, event) {
|
|
|
|
if (err) {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(event.data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
listCalendars: () =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
2021-07-31 10:00:27 -03:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
calendar.calendarList
|
|
|
|
.list()
|
|
|
|
.then((cals) => {
|
|
|
|
resolve(
|
|
|
|
cals.data.items.map((cal) => {
|
|
|
|
const calendar: IntegrationCalendar = {
|
|
|
|
externalId: cal.id,
|
|
|
|
integration: integrationType,
|
|
|
|
name: cal.summary,
|
|
|
|
primary: cal.primary,
|
|
|
|
};
|
|
|
|
return calendar;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
reject(err);
|
2021-04-21 19:10:48 -03:00
|
|
|
});
|
2021-06-24 13:12:22 -03:00
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
// factory
|
2021-06-21 20:15:29 -03:00
|
|
|
const calendars = (withCredentials): CalendarApiAdapter[] =>
|
|
|
|
withCredentials
|
|
|
|
.map((cred) => {
|
|
|
|
switch (cred.type) {
|
|
|
|
case "google_calendar":
|
|
|
|
return GoogleCalendar(cred);
|
|
|
|
case "office365_calendar":
|
|
|
|
return MicrosoftOffice365Calendar(cred);
|
2021-08-14 22:53:59 -03:00
|
|
|
case "caldav_calendar":
|
|
|
|
return new CalDavCalendar(cred);
|
2021-09-11 04:54:13 -03:00
|
|
|
case "apple_calendar":
|
|
|
|
return new AppleCalendar(cred);
|
2021-06-21 20:15:29 -03:00
|
|
|
default:
|
|
|
|
return; // unknown credential, could be legacy? In any case, ignore
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
|
2021-06-24 13:12:22 -03:00
|
|
|
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) =>
|
2021-07-15 18:34:55 -03:00
|
|
|
Promise.all(
|
|
|
|
calendars(withCredentials).map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
|
|
|
).then((results) => {
|
2021-06-21 20:15:29 -03:00
|
|
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
|
|
|
});
|
|
|
|
|
|
|
|
const listCalendars = (withCredentials) =>
|
2021-06-24 13:12:22 -03:00
|
|
|
Promise.all(calendars(withCredentials).map((c) => c.listCalendars())).then((results) =>
|
2021-08-14 22:53:59 -03:00
|
|
|
results.reduce((acc, calendars) => acc.concat(calendars), []).filter((c) => c != null)
|
2021-06-21 20:15:29 -03:00
|
|
|
);
|
|
|
|
|
2021-07-20 15:07:59 -03:00
|
|
|
const createEvent = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: CalendarEvent,
|
2021-07-25 12:05:18 -03:00
|
|
|
noMail = false,
|
2021-09-22 19:43:10 -03:00
|
|
|
maybeUid?: string,
|
|
|
|
optionalVideoCallData?: VideoCallData
|
2021-07-20 15:07:59 -03:00
|
|
|
): Promise<EventResult> => {
|
2021-09-22 19:43:10 -03:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent, maybeUid, optionalVideoCallData);
|
2021-07-25 14:15:31 -03:00
|
|
|
const uid: string = parser.getUid();
|
2021-07-21 19:46:31 -03:00
|
|
|
/*
|
|
|
|
* Matching the credential type is a workaround because the office calendar simply strips away newlines (\n and \r).
|
|
|
|
* We need HTML there. Google Calendar understands newlines and Apple Calendar cannot show HTML, so no HTML should
|
|
|
|
* be used for Google and Apple Calendar.
|
|
|
|
*/
|
2021-07-24 16:23:15 -03:00
|
|
|
const richEvent: CalendarEvent = parser.asRichEventPlain();
|
2021-06-21 20:15:29 -03:00
|
|
|
|
2021-07-14 22:19:30 -03:00
|
|
|
let success = true;
|
|
|
|
|
|
|
|
const creationResult = credential
|
|
|
|
? await calendars([credential])[0]
|
|
|
|
.createEvent(richEvent)
|
|
|
|
.catch((e) => {
|
|
|
|
log.error("createEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
})
|
|
|
|
: null;
|
2021-06-01 16:16:06 -03:00
|
|
|
|
2021-06-28 21:30:45 -03:00
|
|
|
const maybeHangoutLink = creationResult?.hangoutLink;
|
|
|
|
const maybeEntryPoints = creationResult?.entryPoints;
|
|
|
|
const maybeConferenceData = creationResult?.conferenceData;
|
|
|
|
|
2021-07-20 15:07:59 -03:00
|
|
|
if (!noMail) {
|
|
|
|
const organizerMail = new EventOrganizerMail(calEvent, uid, {
|
|
|
|
hangoutLink: maybeHangoutLink,
|
|
|
|
conferenceData: maybeConferenceData,
|
|
|
|
entryPoints: maybeEntryPoints,
|
|
|
|
});
|
2021-06-28 21:30:45 -03:00
|
|
|
|
2021-06-21 13:15:05 -03:00
|
|
|
try {
|
2021-07-20 15:07:59 -03:00
|
|
|
await organizerMail.sendEmail();
|
2021-06-21 13:15:05 -03:00
|
|
|
} catch (e) {
|
2021-07-20 15:07:59 -03:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
|
|
|
}
|
2021-06-16 21:44:13 -03:00
|
|
|
}
|
2021-06-01 16:16:06 -03:00
|
|
|
|
2021-06-16 21:44:13 -03:00
|
|
|
return {
|
2021-07-14 22:19:30 -03:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-16 21:44:13 -03:00
|
|
|
uid,
|
2021-06-21 20:15:29 -03:00
|
|
|
createdEvent: creationResult,
|
2021-07-14 22:19:30 -03:00
|
|
|
originalEvent: calEvent,
|
2021-06-16 21:44:13 -03:00
|
|
|
};
|
2021-05-27 19:10:20 -03:00
|
|
|
};
|
2021-04-21 19:10:48 -03:00
|
|
|
|
2021-07-07 09:07:18 -03:00
|
|
|
const updateEvent = async (
|
|
|
|
credential: Credential,
|
|
|
|
uidToUpdate: string,
|
2021-07-20 15:07:59 -03:00
|
|
|
calEvent: CalendarEvent,
|
2021-09-22 19:43:10 -03:00
|
|
|
noMail = false,
|
|
|
|
optionalVideoCallData?: VideoCallData
|
2021-07-14 22:19:30 -03:00
|
|
|
): Promise<EventResult> => {
|
2021-09-22 19:43:10 -03:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent, undefined, optionalVideoCallData);
|
2021-06-29 18:43:18 -03:00
|
|
|
const newUid: string = parser.getUid();
|
2021-07-24 16:23:15 -03:00
|
|
|
const richEvent: CalendarEvent = parser.asRichEventPlain();
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-07-14 22:19:30 -03:00
|
|
|
let success = true;
|
|
|
|
|
2021-06-21 20:15:29 -03:00
|
|
|
const updateResult = credential
|
2021-07-14 22:19:30 -03:00
|
|
|
? await calendars([credential])[0]
|
|
|
|
.updateEvent(uidToUpdate, richEvent)
|
|
|
|
.catch((e) => {
|
|
|
|
log.error("updateEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
})
|
2021-06-21 20:15:29 -03:00
|
|
|
: null;
|
2021-06-16 21:44:13 -03:00
|
|
|
|
2021-07-20 15:07:59 -03:00
|
|
|
if (!noMail) {
|
|
|
|
const organizerMail = new EventOrganizerRescheduledMail(calEvent, newUid);
|
2021-06-21 13:15:05 -03:00
|
|
|
try {
|
2021-07-20 15:07:59 -03:00
|
|
|
await organizerMail.sendEmail();
|
2021-06-21 13:15:05 -03:00
|
|
|
} catch (e) {
|
2021-07-20 15:07:59 -03:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
|
|
|
}
|
2021-06-16 21:44:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-07-14 22:19:30 -03:00
|
|
|
type: credential.type,
|
|
|
|
success,
|
2021-06-16 21:44:13 -03:00
|
|
|
uid: newUid,
|
2021-06-21 20:15:29 -03:00
|
|
|
updatedEvent: updateResult,
|
2021-07-14 22:19:30 -03:00
|
|
|
originalEvent: calEvent,
|
2021-06-16 21:44:13 -03:00
|
|
|
};
|
2021-06-06 20:10:56 -03:00
|
|
|
};
|
|
|
|
|
2021-07-07 09:07:18 -03:00
|
|
|
const deleteEvent = (credential: Credential, uid: string): Promise<unknown> => {
|
2021-06-16 21:44:13 -03:00
|
|
|
if (credential) {
|
|
|
|
return calendars([credential])[0].deleteEvent(uid);
|
|
|
|
}
|
2021-06-06 20:10:56 -03:00
|
|
|
|
2021-06-16 21:44:13 -03:00
|
|
|
return Promise.resolve({});
|
2021-06-06 20:10:56 -03:00
|
|
|
};
|
|
|
|
|
2021-07-24 17:24:00 -03:00
|
|
|
export { getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, listCalendars };
|