2022-10-31 22:06:03 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2022-09-01 04:42:53 +00:00
|
|
|
import { calendar_v3, google } from "googleapis";
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2023-01-10 02:01:57 +00:00
|
|
|
import { MeetLocationType } from "@calcom/app-store/locations";
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser";
|
|
|
|
import CalendarService from "@calcom/lib/CalendarService";
|
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import type {
|
|
|
|
Calendar,
|
|
|
|
CalendarEvent,
|
|
|
|
EventBusyDate,
|
|
|
|
IntegrationCalendar,
|
|
|
|
NewCalendarEventType,
|
|
|
|
} from "@calcom/types/Calendar";
|
2022-10-31 22:06:03 +00:00
|
|
|
import { CredentialPayload } from "@calcom/types/Credential";
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-09-01 04:42:53 +00:00
|
|
|
import { getGoogleAppKeys } from "./getGoogleAppKeys";
|
|
|
|
import { googleCredentialSchema } from "./googleCredentialSchema";
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-05-18 15:34:21 +00:00
|
|
|
interface GoogleCalError extends Error {
|
|
|
|
code?: number;
|
|
|
|
}
|
|
|
|
|
2022-01-06 17:28:31 +00:00
|
|
|
export default class GoogleCalendarService implements Calendar {
|
|
|
|
private integrationName = "";
|
2022-09-01 04:42:53 +00:00
|
|
|
private auth: { getToken: () => Promise<MyGoogleAuth> };
|
2022-01-06 22:06:31 +00:00
|
|
|
private log: typeof logger;
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-10-31 22:06:03 +00:00
|
|
|
constructor(credential: CredentialPayload) {
|
2022-03-23 22:00:30 +00:00
|
|
|
this.integrationName = "google_calendar";
|
2022-09-01 04:42:53 +00:00
|
|
|
this.auth = this.googleAuth(credential);
|
2022-01-06 22:06:31 +00:00
|
|
|
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 22:06:03 +00:00
|
|
|
private googleAuth = (credential: CredentialPayload) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const googleCredentials = googleCredentialSchema.parse(credential.key);
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-09-01 04:42:53 +00:00
|
|
|
async function getGoogleAuth() {
|
|
|
|
const { client_id, client_secret, redirect_uris } = await getGoogleAppKeys();
|
|
|
|
const myGoogleAuth = new MyGoogleAuth(client_id, client_secret, redirect_uris[0]);
|
|
|
|
myGoogleAuth.setCredentials(googleCredentials);
|
|
|
|
return myGoogleAuth;
|
|
|
|
}
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-09-01 04:42:53 +00:00
|
|
|
const refreshAccessToken = async (myGoogleAuth: Awaited<ReturnType<typeof getGoogleAuth>>) => {
|
|
|
|
try {
|
|
|
|
const { res } = await myGoogleAuth.refreshToken(googleCredentials.refresh_token);
|
|
|
|
const token = res?.data;
|
|
|
|
googleCredentials.access_token = token.access_token;
|
|
|
|
googleCredentials.expiry_date = token.expiry_date;
|
|
|
|
const key = googleCredentialSchema.parse(googleCredentials);
|
|
|
|
await prisma.credential.update({
|
|
|
|
where: { id: credential.id },
|
|
|
|
data: { key },
|
2022-01-06 17:28:31 +00:00
|
|
|
});
|
2022-09-01 04:42:53 +00:00
|
|
|
myGoogleAuth.setCredentials(googleCredentials);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error("Error refreshing google token", err);
|
|
|
|
}
|
|
|
|
return myGoogleAuth;
|
|
|
|
};
|
2022-01-06 17:28:31 +00:00
|
|
|
return {
|
2022-09-01 04:42:53 +00:00
|
|
|
getToken: async () => {
|
|
|
|
const myGoogleAuth = await getGoogleAuth();
|
|
|
|
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
|
|
|
return !isExpired() ? Promise.resolve(myGoogleAuth) : refreshAccessToken(myGoogleAuth);
|
|
|
|
},
|
2022-01-06 17:28:31 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-28 14:54:31 +00:00
|
|
|
async createEvent(calEventRaw: CalendarEvent): Promise<NewCalendarEventType> {
|
2022-05-02 20:39:35 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const myGoogleAuth = await this.auth.getToken();
|
2022-05-02 20:39:35 +00:00
|
|
|
const payload: calendar_v3.Schema$Event = {
|
|
|
|
summary: calEventRaw.title,
|
|
|
|
description: getRichDescription(calEventRaw),
|
|
|
|
start: {
|
|
|
|
dateTime: calEventRaw.startTime,
|
|
|
|
timeZone: calEventRaw.organizer.timeZone,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
dateTime: calEventRaw.endTime,
|
|
|
|
timeZone: calEventRaw.organizer.timeZone,
|
|
|
|
},
|
2022-05-25 14:29:38 +00:00
|
|
|
attendees: [
|
2022-10-18 19:41:50 +00:00
|
|
|
{
|
|
|
|
...calEventRaw.organizer,
|
|
|
|
id: String(calEventRaw.organizer.id),
|
|
|
|
organizer: true,
|
|
|
|
responseStatus: "accepted",
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line
|
|
|
|
...calEventRaw.attendees.map(({ id, ...rest }) => ({
|
2022-10-25 13:45:43 +00:00
|
|
|
...rest,
|
2022-10-18 19:41:50 +00:00
|
|
|
responseStatus: "accepted",
|
|
|
|
})),
|
2022-05-25 14:29:38 +00:00
|
|
|
],
|
2022-05-02 20:39:35 +00:00
|
|
|
reminders: {
|
|
|
|
useDefault: true,
|
|
|
|
},
|
2022-10-18 19:41:50 +00:00
|
|
|
guestsCanSeeOtherGuests: calEventRaw.seatsShowAttendees,
|
2022-05-02 20:39:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (calEventRaw.location) {
|
|
|
|
payload["location"] = getLocation(calEventRaw);
|
|
|
|
}
|
|
|
|
|
2023-01-10 02:01:57 +00:00
|
|
|
if (calEventRaw.conferenceData && calEventRaw.location === MeetLocationType) {
|
2022-05-02 20:39:35 +00:00
|
|
|
payload["conferenceData"] = calEventRaw.conferenceData;
|
|
|
|
}
|
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
});
|
2022-08-02 18:07:45 +00:00
|
|
|
const selectedCalendar = calEventRaw.destinationCalendar?.externalId
|
|
|
|
? calEventRaw.destinationCalendar.externalId
|
|
|
|
: "primary";
|
2022-05-02 20:39:35 +00:00
|
|
|
calendar.events.insert(
|
|
|
|
{
|
|
|
|
auth: myGoogleAuth,
|
2022-08-02 18:07:45 +00:00
|
|
|
calendarId: selectedCalendar,
|
2022-05-02 20:39:35 +00:00
|
|
|
requestBody: payload,
|
|
|
|
conferenceDataVersion: 1,
|
|
|
|
},
|
2022-08-02 18:07:45 +00:00
|
|
|
function (error, event) {
|
|
|
|
if (error || !event?.data) {
|
|
|
|
console.error("There was an error contacting google calendar service: ", error);
|
|
|
|
return reject(error);
|
2022-05-02 20:39:35 +00:00
|
|
|
}
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-08-02 18:07:45 +00:00
|
|
|
if (event && event.data.id && event.data.hangoutLink) {
|
|
|
|
calendar.events.patch({
|
|
|
|
// Update the same event but this time we know the hangout link
|
|
|
|
calendarId: selectedCalendar,
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
eventId: event.data.id || "",
|
|
|
|
requestBody: {
|
|
|
|
description: getRichDescription({
|
|
|
|
...calEventRaw,
|
|
|
|
additionalInformation: { hangoutLink: event.data.hangoutLink },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-05-02 20:39:35 +00:00
|
|
|
return resolve({
|
|
|
|
uid: "",
|
|
|
|
...event.data,
|
|
|
|
id: event.data.id || "",
|
|
|
|
additionalInfo: {
|
|
|
|
hangoutLink: event.data.hangoutLink || "",
|
|
|
|
},
|
|
|
|
type: "google_calendar",
|
|
|
|
password: "",
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 20:20:09 +00:00
|
|
|
async updateEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<any> {
|
2022-05-02 20:39:35 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const myGoogleAuth = await this.auth.getToken();
|
2022-11-05 18:58:35 +00:00
|
|
|
const eventAttendees = event.attendees.map(({ id, ...rest }) => ({
|
|
|
|
...rest,
|
|
|
|
responseStatus: "accepted",
|
|
|
|
}));
|
2022-05-02 20:39:35 +00:00
|
|
|
const payload: calendar_v3.Schema$Event = {
|
|
|
|
summary: event.title,
|
|
|
|
description: getRichDescription(event),
|
|
|
|
start: {
|
|
|
|
dateTime: event.startTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
dateTime: event.endTime,
|
|
|
|
timeZone: event.organizer.timeZone,
|
|
|
|
},
|
2022-10-18 19:41:50 +00:00
|
|
|
attendees: [
|
|
|
|
{
|
|
|
|
...event.organizer,
|
|
|
|
id: String(event.organizer.id),
|
|
|
|
organizer: true,
|
|
|
|
responseStatus: "accepted",
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line
|
2022-11-05 18:58:35 +00:00
|
|
|
...eventAttendees,
|
2022-10-18 19:41:50 +00:00
|
|
|
],
|
2022-05-02 20:39:35 +00:00
|
|
|
reminders: {
|
|
|
|
useDefault: true,
|
|
|
|
},
|
2022-10-18 19:41:50 +00:00
|
|
|
guestsCanSeeOtherGuests: event.seatsShowAttendees,
|
2022-05-02 20:39:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (event.location) {
|
|
|
|
payload["location"] = getLocation(event);
|
|
|
|
}
|
|
|
|
|
2023-01-10 02:01:57 +00:00
|
|
|
if (event.conferenceData && event.location === MeetLocationType) {
|
2022-08-08 19:38:02 +00:00
|
|
|
payload["conferenceData"] = event.conferenceData;
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2022-08-08 19:38:02 +00:00
|
|
|
|
|
|
|
const selectedCalendar = externalCalendarId
|
|
|
|
? externalCalendarId
|
|
|
|
: event.destinationCalendar?.externalId;
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
calendar.events.update(
|
|
|
|
{
|
2022-01-06 17:28:31 +00:00
|
|
|
auth: myGoogleAuth,
|
2022-08-08 19:38:02 +00:00
|
|
|
calendarId: selectedCalendar,
|
2022-05-02 20:39:35 +00:00
|
|
|
eventId: uid,
|
|
|
|
sendNotifications: true,
|
|
|
|
sendUpdates: "all",
|
|
|
|
requestBody: payload,
|
2022-08-08 19:38:02 +00:00
|
|
|
conferenceDataVersion: 1,
|
2022-05-02 20:39:35 +00:00
|
|
|
},
|
2022-08-08 19:38:02 +00:00
|
|
|
function (err, evt) {
|
2022-05-02 20:39:35 +00:00
|
|
|
if (err) {
|
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
|
|
|
|
|
|
|
return reject(err);
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
2022-08-08 19:38:02 +00:00
|
|
|
|
2023-01-10 02:01:57 +00:00
|
|
|
if (evt && evt.data.id && evt.data.hangoutLink && event.location === MeetLocationType) {
|
2022-08-08 19:38:02 +00:00
|
|
|
calendar.events.patch({
|
|
|
|
// Update the same event but this time we know the hangout link
|
|
|
|
calendarId: selectedCalendar,
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
eventId: evt.data.id || "",
|
|
|
|
requestBody: {
|
|
|
|
description: getRichDescription({
|
|
|
|
...event,
|
|
|
|
additionalInformation: { hangoutLink: evt.data.hangoutLink },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return resolve({
|
|
|
|
uid: "",
|
|
|
|
...evt.data,
|
|
|
|
id: evt.data.id || "",
|
|
|
|
additionalInfo: {
|
|
|
|
hangoutLink: evt.data.hangoutLink || "",
|
|
|
|
},
|
|
|
|
type: "google_calendar",
|
|
|
|
password: "",
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolve(evt?.data);
|
2022-05-02 20:39:35 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 14:52:42 +00:00
|
|
|
async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise<void> {
|
2022-05-02 20:39:35 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const myGoogleAuth = await this.auth.getToken();
|
2022-05-02 20:39:35 +00:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
2022-06-27 14:52:42 +00:00
|
|
|
|
|
|
|
const defaultCalendarId = "primary";
|
|
|
|
const calendarId = externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId;
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
calendar.events.delete(
|
|
|
|
{
|
2022-01-06 17:28:31 +00:00
|
|
|
auth: myGoogleAuth,
|
2022-06-27 14:52:42 +00:00
|
|
|
calendarId: calendarId ? calendarId : defaultCalendarId,
|
2022-05-02 20:39:35 +00:00
|
|
|
eventId: uid,
|
|
|
|
sendNotifications: true,
|
|
|
|
sendUpdates: "all",
|
|
|
|
},
|
2022-05-18 15:34:21 +00:00
|
|
|
function (err: GoogleCalError | null, event) {
|
2022-05-02 20:39:35 +00:00
|
|
|
if (err) {
|
2022-06-27 14:52:42 +00:00
|
|
|
/**
|
|
|
|
* 410 is when an event is already deleted on the Google cal before on cal.com
|
|
|
|
* 404 is when the event is on a different calendar
|
|
|
|
*/
|
|
|
|
if (err.code === 410) return resolve();
|
2022-05-02 20:39:35 +00:00
|
|
|
console.error("There was an error contacting google calendar service: ", err);
|
2022-06-27 14:52:42 +00:00
|
|
|
if (err.code === 404) return resolve();
|
2022-05-02 20:39:35 +00:00
|
|
|
return reject(err);
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
2022-05-02 20:39:35 +00:00
|
|
|
return resolve(event?.data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAvailability(
|
|
|
|
dateFrom: string,
|
|
|
|
dateTo: string,
|
|
|
|
selectedCalendars: IntegrationCalendar[]
|
|
|
|
): Promise<EventBusyDate[]> {
|
2022-05-02 20:39:35 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const myGoogleAuth = await this.auth.getToken();
|
2022-05-02 20:39:35 +00:00
|
|
|
const calendar = google.calendar({
|
|
|
|
version: "v3",
|
|
|
|
auth: myGoogleAuth,
|
|
|
|
});
|
|
|
|
const selectedCalendarIds = selectedCalendars
|
|
|
|
.filter((e) => e.integration === this.integrationName)
|
|
|
|
.map((e) => e.externalId);
|
|
|
|
if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) {
|
|
|
|
// Only calendars of other integrations selected
|
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(selectedCalendarIds.length === 0
|
|
|
|
? calendar.calendarList
|
|
|
|
.list()
|
|
|
|
.then((cals) => cals.data.items?.map((cal) => cal.id).filter(Boolean) || [])
|
|
|
|
: Promise.resolve(selectedCalendarIds)
|
|
|
|
)
|
|
|
|
.then((calsIds) => {
|
|
|
|
calendar.freebusy.query(
|
|
|
|
{
|
|
|
|
requestBody: {
|
|
|
|
timeMin: dateFrom,
|
|
|
|
timeMax: dateTo,
|
|
|
|
items: calsIds.map((id) => ({ id: id })),
|
2022-01-06 17:28:31 +00:00
|
|
|
},
|
2022-05-02 20:39:35 +00:00
|
|
|
},
|
|
|
|
(err, apires) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
if (err) return reject(err);
|
|
|
|
// If there's no calendar we just skip
|
|
|
|
if (!apires?.data.calendars) return resolve([]);
|
|
|
|
const result = Object.values(apires.data.calendars).reduce((c, i) => {
|
|
|
|
i.busy?.forEach((busyTime) => {
|
|
|
|
c.push({
|
|
|
|
start: busyTime.start || "",
|
|
|
|
end: busyTime.end || "",
|
2022-05-02 20:39:35 +00:00
|
|
|
});
|
2022-09-01 04:42:53 +00:00
|
|
|
});
|
|
|
|
return c;
|
|
|
|
}, [] as Prisma.PromiseReturnType<CalendarService["getAvailability"]>);
|
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.log.error("There was an error contacting google calendar service: ", err);
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async listCalendars(): Promise<IntegrationCalendar[]> {
|
2022-05-02 20:39:35 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2022-09-01 04:42:53 +00:00
|
|
|
const myGoogleAuth = await this.auth.getToken();
|
2022-05-02 20:39:35 +00:00
|
|
|
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 ?? "No id",
|
|
|
|
integration: this.integrationName,
|
|
|
|
name: cal.summary ?? "No name",
|
|
|
|
primary: cal.primary ?? false,
|
2022-07-01 20:55:27 +00:00
|
|
|
readOnly: !(cal.accessRole === "reader" || cal.accessRole === "owner") && true,
|
2022-12-20 21:50:20 +00:00
|
|
|
email: cal.id ?? "",
|
2022-05-02 20:39:35 +00:00
|
|
|
};
|
|
|
|
return calendar;
|
|
|
|
}) || []
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err: Error) => {
|
|
|
|
this.log.error("There was an error contacting google calendar service: ", err);
|
2022-01-06 17:28:31 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyGoogleAuth extends google.auth.OAuth2 {
|
|
|
|
constructor(client_id: string, client_secret: string, redirect_uri: string) {
|
|
|
|
super(client_id, client_secret, redirect_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
isTokenExpiring() {
|
|
|
|
return super.isTokenExpiring();
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshToken(token: string | null | undefined) {
|
|
|
|
return super.refreshToken(token);
|
|
|
|
}
|
|
|
|
}
|