2021-06-18 00:44:41 +00:00
|
|
|
import EventOrganizerMail from "./emails/EventOrganizerMail";
|
2021-06-16 22:56:02 +00:00
|
|
|
import EventAttendeeMail from "./emails/EventAttendeeMail";
|
2021-06-18 00:44:41 +00:00
|
|
|
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
2021-06-17 00:44:13 +00:00
|
|
|
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
2021-06-29 21:43:18 +00:00
|
|
|
import prisma from "./prisma";
|
2021-07-07 12:07:18 +00:00
|
|
|
import { Credential } from "@prisma/client";
|
2021-06-29 21:43:18 +00:00
|
|
|
import CalEventParser from "./CalEventParser";
|
2021-07-15 01:19:30 +00:00
|
|
|
import { EventResult } from "@lib/events/EventManager";
|
|
|
|
import logger from "@lib/logger";
|
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[lib] calendarClient"] });
|
2021-06-16 21:40:13 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-06-21 23:15:29 +00:00
|
|
|
const { google } = require("googleapis");
|
2021-06-20 15:33:02 +00:00
|
|
|
|
|
|
|
const googleAuth = (credential) => {
|
2021-06-24 16:12:22 +00:00
|
|
|
const { client_secret, client_id, redirect_uris } = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
2021-06-21 15:56:14 +00:00
|
|
|
const myGoogleAuth = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
2021-06-24 16:12:22 +00:00
|
|
|
myGoogleAuth.setCredentials(credential.key);
|
2021-06-20 15:33:02 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
2021-06-20 15:33:02 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
const refreshAccessToken = () =>
|
|
|
|
myGoogleAuth
|
|
|
|
.refreshToken(credential.key.refresh_token)
|
|
|
|
.then((res) => {
|
2021-06-20 15:33:02 +00:00
|
|
|
const token = res.res.data;
|
|
|
|
credential.key.access_token = token.access_token;
|
|
|
|
credential.key.expiry_date = token.expiry_date;
|
2021-06-24 16:12:22 +00:00
|
|
|
return prisma.credential
|
|
|
|
.update({
|
2021-06-20 15:33:02 +00:00
|
|
|
where: {
|
2021-06-24 16:12:22 +00:00
|
|
|
id: credential.id,
|
2021-06-20 15:33:02 +00:00
|
|
|
},
|
|
|
|
data: {
|
2021-06-24 16:12:22 +00:00
|
|
|
key: credential.key,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(() => {
|
2021-06-20 15:33:02 +00:00
|
|
|
myGoogleAuth.setCredentials(credential.key);
|
|
|
|
return myGoogleAuth;
|
2021-06-24 16:12:22 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2021-06-20 15:33:02 +00:00
|
|
|
console.error("Error refreshing google token", err);
|
|
|
|
return myGoogleAuth;
|
2021-06-24 16:12:22 +00:00
|
|
|
});
|
2021-06-20 15:33:02 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
return {
|
|
|
|
getToken: () => (!isExpired() ? Promise.resolve(myGoogleAuth) : refreshAccessToken()),
|
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
2021-06-08 00:25:34 +00:00
|
|
|
function handleErrorsJson(response) {
|
2021-06-17 00:44:13 +00:00
|
|
|
if (!response.ok) {
|
2021-06-24 16:12:22 +00:00
|
|
|
response.json().then((e) => console.error("O365 Error", e));
|
2021-06-17 00:44:13 +00:00
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.json();
|
2021-04-21 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:25:34 +00:00
|
|
|
function handleErrorsRaw(response) {
|
2021-06-17 00:44:13 +00:00
|
|
|
if (!response.ok) {
|
2021-06-24 16:12:22 +00:00
|
|
|
response.text().then((e) => console.error("O365 Error", e));
|
2021-06-17 00:44:13 +00:00
|
|
|
throw Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
2021-06-08 00:25:34 +00:00
|
|
|
}
|
2021-04-21 22:10:48 +00:00
|
|
|
|
|
|
|
const o365Auth = (credential) => {
|
2021-06-24 16:12:22 +00:00
|
|
|
const isExpired = (expiryDate) => expiryDate < Math.round(+new Date() / 1000);
|
2021-06-20 15:33:02 +00:00
|
|
|
|
2021-06-21 15:56:14 +00:00
|
|
|
const refreshAccessToken = (refreshToken) => {
|
2021-06-24 16:12:22 +00: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 15:33:02 +00:00
|
|
|
})
|
2021-06-24 16:12:22 +00:00
|
|
|
.then(() => credential.key.access_token);
|
|
|
|
});
|
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-06-17 00:44:13 +00:00
|
|
|
return {
|
2021-06-21 23:15:29 +00:00
|
|
|
getToken: () =>
|
|
|
|
!isExpired(credential.key.expiry_date)
|
|
|
|
? Promise.resolve(credential.key.access_token)
|
|
|
|
: refreshAccessToken(credential.key.refresh_token),
|
2021-06-17 00:44:13 +00:00
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
2021-06-06 23:10:56 +00:00
|
|
|
interface Person {
|
2021-06-21 23:15:29 +00:00
|
|
|
name?: string;
|
|
|
|
email: string;
|
|
|
|
timeZone: string;
|
2021-06-06 23:10:56 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
description?: string;
|
|
|
|
location?: string;
|
|
|
|
organizer: Person;
|
|
|
|
attendees: Person[];
|
2021-06-21 23:15:29 +00:00
|
|
|
conferenceData?: ConferenceData;
|
|
|
|
}
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
export interface ConferenceData {
|
2021-07-07 10:43:13 +00:00
|
|
|
createRequest: unknown;
|
2021-06-21 23:15:29 +00:00
|
|
|
}
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
export interface IntegrationCalendar {
|
2021-06-21 23:15:29 +00:00
|
|
|
integration: string;
|
|
|
|
primary: boolean;
|
|
|
|
externalId: string;
|
|
|
|
name: string;
|
2021-06-14 17:45:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
export interface CalendarApiAdapter {
|
2021-07-07 10:43:13 +00:00
|
|
|
createEvent(event: CalendarEvent): Promise<unknown>;
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
updateEvent(uid: string, event: CalendarEvent);
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
deleteEvent(uid: string);
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-07-07 10:43:13 +00:00
|
|
|
getAvailability(dateFrom, dateTo, selectedCalendars: IntegrationCalendar[]): Promise<unknown>;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
2021-06-17 00:44:13 +00:00
|
|
|
const auth = o365Auth(credential);
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-06-17 00:44:13 +00:00
|
|
|
const translateEvent = (event: CalendarEvent) => {
|
2021-06-24 16:12:22 +00:00
|
|
|
const optional = {};
|
2021-06-17 00:44:13 +00:00
|
|
|
if (event.location) {
|
2021-06-21 23:15:29 +00:00
|
|
|
optional.location = { displayName: event.location };
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
2021-05-08 19:03:47 +00:00
|
|
|
|
2021-07-18 14:03:59 +00: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-17 00:44:13 +00:00
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-06-21 23:15:29 +00:00
|
|
|
const integrationType = "office365_calendar";
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-06-21 23:15:29 +00: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 22:10:48 +00:00
|
|
|
},
|
2021-06-21 23:15:29 +00: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-06-24 16:12:22 +00:00
|
|
|
const filter = "?$filter=start/dateTime ge '" + dateFrom + "' and end/dateTime le '" + dateTo + "'";
|
2021-06-21 23:15:29 +00: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[]) => {
|
|
|
|
const urls = ids.map(
|
|
|
|
(calendarId) =>
|
2021-06-24 16:12:22 +00:00
|
|
|
"https://graph.microsoft.com/v1.0/me/calendars/" + calendarId + "/events" + filter
|
2021-06-21 23:15:29 +00:00
|
|
|
);
|
|
|
|
return Promise.all(
|
|
|
|
urls.map((url) =>
|
|
|
|
fetch(url, {
|
|
|
|
method: "get",
|
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer " + accessToken,
|
|
|
|
Prefer: 'outlook.timezone="Etc/GMT"',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(handleErrorsJson)
|
|
|
|
.then((responseBody) =>
|
|
|
|
responseBody.value.map((evt) => ({
|
|
|
|
start: evt.start.dateTime + "Z",
|
|
|
|
end: evt.end.dateTime + "Z",
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
)
|
2021-06-24 16:12:22 +00:00
|
|
|
).then((results) => results.reduce((acc, events) => acc.concat(events), []));
|
2021-06-21 23:15:29 +00: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 19:16:06 +00:00
|
|
|
...responseBody,
|
|
|
|
disableConfirmationEmail: true,
|
2021-06-21 23:15:29 +00:00
|
|
|
}))
|
|
|
|
),
|
2021-06-24 16:12:22 +00:00
|
|
|
deleteEvent: (uid: string) =>
|
2021-06-21 23:15:29 +00: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 16:12:22 +00:00
|
|
|
updateEvent: (uid: string, event: CalendarEvent) =>
|
2021-06-21 23:15:29 +00: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 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
2021-05-27 22:10:20 +00:00
|
|
|
const GoogleCalendar = (credential): CalendarApiAdapter => {
|
2021-06-24 16:12:22 +00:00
|
|
|
const auth = googleAuth(credential);
|
|
|
|
const integrationType = "google_calendar";
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
return {
|
|
|
|
getAvailability: (dateFrom, dateTo, selectedCalendars) =>
|
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
auth.getToken().then((myGoogleAuth) => {
|
|
|
|
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
|
|
|
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 15:33:02 +00:00
|
|
|
|
2021-06-24 16:12:22 +00: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 23:10:56 +00:00
|
|
|
reject(err);
|
2021-06-24 16:12:22 +00:00
|
|
|
}
|
|
|
|
resolve(Object.values(apires.data.calendars).flatMap((item) => item["busy"]));
|
2021-06-09 19:47:03 +00:00
|
|
|
}
|
2021-06-24 16:12:22 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
reject(err);
|
2021-06-09 19:47:03 +00:00
|
|
|
});
|
2021-06-24 16:12:22 +00: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,
|
|
|
|
overrides: [{ method: "email", minutes: 60 }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.location) {
|
|
|
|
payload["location"] = event.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.conferenceData) {
|
|
|
|
payload["conferenceData"] = event.conferenceData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
|
|
|
calendar.events.insert(
|
|
|
|
{
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
calendarId: "primary",
|
|
|
|
resource: payload,
|
2021-06-28 20:34:27 +00:00
|
|
|
conferenceDataVersion: 1,
|
2021-06-24 16:12:22 +00: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,
|
|
|
|
overrides: [{ method: "email", minutes: 60 }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.location) {
|
|
|
|
payload["location"] = event.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
|
|
|
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) => {
|
|
|
|
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
|
|
|
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) => {
|
|
|
|
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
|
|
|
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 22:10:48 +00:00
|
|
|
});
|
2021-06-24 16:12:22 +00:00
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2021-04-21 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// factory
|
2021-06-21 23:15:29 +00:00
|
|
|
const calendars = (withCredentials): CalendarApiAdapter[] =>
|
|
|
|
withCredentials
|
|
|
|
.map((cred) => {
|
|
|
|
switch (cred.type) {
|
|
|
|
case "google_calendar":
|
|
|
|
return GoogleCalendar(cred);
|
|
|
|
case "office365_calendar":
|
|
|
|
return MicrosoftOffice365Calendar(cred);
|
|
|
|
default:
|
|
|
|
return; // unknown credential, could be legacy? In any case, ignore
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
|
2021-06-24 16:12:22 +00:00
|
|
|
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) =>
|
2021-07-15 21:34:55 +00:00
|
|
|
Promise.all(
|
|
|
|
calendars(withCredentials).map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
|
|
|
).then((results) => {
|
2021-06-21 23:15:29 +00:00
|
|
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
|
|
|
});
|
|
|
|
|
|
|
|
const listCalendars = (withCredentials) =>
|
2021-06-24 16:12:22 +00:00
|
|
|
Promise.all(calendars(withCredentials).map((c) => c.listCalendars())).then((results) =>
|
|
|
|
results.reduce((acc, calendars) => acc.concat(calendars), [])
|
2021-06-21 23:15:29 +00:00
|
|
|
);
|
|
|
|
|
2021-07-20 18:07:59 +00:00
|
|
|
const createEvent = async (
|
|
|
|
credential: Credential,
|
|
|
|
calEvent: CalendarEvent,
|
2021-07-25 15:05:18 +00:00
|
|
|
noMail = false,
|
|
|
|
maybeUid: string = null
|
2021-07-20 18:07:59 +00:00
|
|
|
): Promise<EventResult> => {
|
2021-06-29 21:43:18 +00:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent);
|
2021-07-25 15:05:18 +00:00
|
|
|
const uid: string = maybeUid ?? parser.getUid();
|
2021-06-29 21:43:18 +00:00
|
|
|
const richEvent: CalendarEvent = parser.asRichEvent();
|
2021-06-21 23:15:29 +00:00
|
|
|
|
2021-07-15 01:19:30 +00: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 19:16:06 +00:00
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
const maybeHangoutLink = creationResult?.hangoutLink;
|
|
|
|
const maybeEntryPoints = creationResult?.entryPoints;
|
|
|
|
const maybeConferenceData = creationResult?.conferenceData;
|
|
|
|
|
2021-07-20 18:07:59 +00:00
|
|
|
if (!noMail) {
|
|
|
|
const organizerMail = new EventOrganizerMail(calEvent, uid, {
|
|
|
|
hangoutLink: maybeHangoutLink,
|
|
|
|
conferenceData: maybeConferenceData,
|
|
|
|
entryPoints: maybeEntryPoints,
|
|
|
|
});
|
2021-06-29 00:30:45 +00:00
|
|
|
|
2021-07-20 18:07:59 +00:00
|
|
|
const attendeeMail = new EventAttendeeMail(calEvent, uid, {
|
|
|
|
hangoutLink: maybeHangoutLink,
|
|
|
|
conferenceData: maybeConferenceData,
|
|
|
|
entryPoints: maybeEntryPoints,
|
|
|
|
});
|
2021-06-29 00:30:45 +00:00
|
|
|
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-07-20 18:07:59 +00:00
|
|
|
await organizerMail.sendEmail();
|
2021-06-21 16:15:05 +00:00
|
|
|
} catch (e) {
|
2021-07-20 18:07:59 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
|
|
|
try {
|
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
|
|
|
}
|
2021-06-21 16:15:05 +00:00
|
|
|
}
|
2021-06-17 00:44:13 +00:00
|
|
|
}
|
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,
|
|
|
|
uidToUpdate: string,
|
2021-07-20 18:07:59 +00:00
|
|
|
calEvent: CalendarEvent,
|
2021-07-24 20:24:00 +00:00
|
|
|
noMail = false
|
2021-07-15 01:19:30 +00:00
|
|
|
): Promise<EventResult> => {
|
2021-06-29 21:43:18 +00:00
|
|
|
const parser: CalEventParser = new CalEventParser(calEvent);
|
|
|
|
const newUid: string = parser.getUid();
|
|
|
|
const richEvent: CalendarEvent = parser.asRichEvent();
|
2021-06-06 23:10:56 +00:00
|
|
|
|
2021-07-15 01:19:30 +00:00
|
|
|
let success = true;
|
|
|
|
|
2021-06-21 23:15:29 +00:00
|
|
|
const updateResult = credential
|
2021-07-15 01:19:30 +00:00
|
|
|
? await calendars([credential])[0]
|
|
|
|
.updateEvent(uidToUpdate, richEvent)
|
|
|
|
.catch((e) => {
|
|
|
|
log.error("updateEvent failed", e, calEvent);
|
|
|
|
success = false;
|
|
|
|
})
|
2021-06-21 23:15:29 +00:00
|
|
|
: null;
|
2021-06-17 00:44:13 +00:00
|
|
|
|
2021-07-20 18:07:59 +00:00
|
|
|
if (!noMail) {
|
|
|
|
const organizerMail = new EventOrganizerRescheduledMail(calEvent, newUid);
|
|
|
|
const attendeeMail = new EventAttendeeRescheduledMail(calEvent, newUid);
|
2021-06-21 16:15:05 +00:00
|
|
|
try {
|
2021-07-20 18:07:59 +00:00
|
|
|
await organizerMail.sendEmail();
|
2021-06-21 16:15:05 +00:00
|
|
|
} catch (e) {
|
2021-07-20 18:07:59 +00:00
|
|
|
console.error("organizerMail.sendEmail failed", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
|
|
|
try {
|
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("attendeeMail.sendEmail failed", e);
|
|
|
|
}
|
2021-06-21 16:15:05 +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: newUid,
|
2021-06-21 23:15:29 +00:00
|
|
|
updatedEvent: updateResult,
|
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-06-17 00:44:13 +00:00
|
|
|
if (credential) {
|
|
|
|
return calendars([credential])[0].deleteEvent(uid);
|
|
|
|
}
|
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-07-24 20:24:00 +00:00
|
|
|
export { getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, listCalendars };
|