Fix/office365 api call (#3534)

* WIP fixes for handling office365 api call

* First working version microsoft api call handling

* Remove logs

* Clean up and improve function names

* Clean up

* Remove log

* Fix function missing its correct name

* Fix direct return of statement

* Cleanup

* Simplify with private fetcher

* Update CalendarService.ts

* Update getOfficeAppKeys.ts

Co-authored-by: zomars <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2022-07-27 19:12:42 +00:00
1 parent 7537ea5b91
commit 525ce0d075
9 files changed
+271 -109

No files matched your search

@@ -1,4 +1,4 @@
import { Prisma } from "@prisma/client"; import type { Prisma } from "@prisma/client";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
@@ -0,0 +1,10 @@
import type Zod from "zod";
import getAppKeysFromSlug from "./getAppKeysFromSlug";
export async function getParsedAppKeysFromSlug(slug: string, schema: Zod.Schema) {
const appKeys = await getAppKeysFromSlug(slug);
return schema.parse(appKeys);
}
export default getParsedAppKeysFromSlug;
@@ -3,7 +3,6 @@ import { Credential } from "@prisma/client";
import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser"; import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser";
import { handleErrorsJson, handleErrorsRaw } from "@calcom/lib/errors"; import { handleErrorsJson, handleErrorsRaw } from "@calcom/lib/errors";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger"; import logger from "@calcom/lib/logger";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime"; import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime";
@@ -15,39 +14,52 @@ import type {
NewCalendarEventType, NewCalendarEventType,
} from "@calcom/types/Calendar"; } from "@calcom/types/Calendar";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import { O365AuthCredentials } from "../types/Office365Calendar"; import { O365AuthCredentials } from "../types/Office365Calendar";
import { getOfficeAppKeys } from "./getOfficeAppKeys";
let client_id = ""; interface IRequest {
let client_secret = ""; method: string;
url: string;
id: number;
}
interface ISettledResponse {
id: string;
status: number;
headers: {
"Retry-After": string;
"Content-Type": string;
};
body: Record<string, any>;
}
interface IBatchResponse {
responses: ISettledResponse[];
}
export default class Office365CalendarService implements Calendar { export default class Office365CalendarService implements Calendar {
private url = ""; private url = "";
private integrationName = ""; private integrationName = "";
private log: typeof logger; private log: typeof logger;
auth: Promise<{ getToken: () => Promise<string> }>; private accessToken: string | null = null;
auth: { getToken: () => Promise<string> };
private apiGraphUrl = "https://graph.microsoft.com/v1.0";
constructor(credential: Credential) { constructor(credential: Credential) {
this.integrationName = "office365_calendar"; this.integrationName = "office365_calendar";
this.auth = this.o365Auth(credential).then((t) => t); this.auth = this.o365Auth(credential);
this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] });
} }
async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> { async createEvent(event: CalendarEvent): Promise<NewCalendarEventType> {
try { try {
const accessToken = await (await this.auth).getToken();
const calendarId = event.destinationCalendar?.externalId const calendarId = event.destinationCalendar?.externalId
? `${event.destinationCalendar.externalId}/` ? `${event.destinationCalendar.externalId}/`
: ""; : "";
const response = await fetch(`https://graph.microsoft.com/v1.0/me/calendars/${calendarId}events`, { const response = await this.fetcher(`/me/calendars/${calendarId}events`, {
method: "POST", method: "POST",
headers: {
Authorization: "Bearer " + accessToken,
"Content-Type": "application/json",
},
body: JSON.stringify(this.translateEvent(event)), body: JSON.stringify(this.translateEvent(event)),
}); });
@@ -61,14 +73,8 @@ export default class Office365CalendarService implements Calendar {
async updateEvent(uid: string, event: CalendarEvent): Promise<any> { async updateEvent(uid: string, event: CalendarEvent): Promise<any> {
try { try {
const accessToken = await (await this.auth).getToken(); const response = await this.fetcher(`/me/calendar/events/${uid}`, {
const response = await fetch("https://graph.microsoft.com/v1.0/me/calendar/events/" + uid, {
method: "PATCH", method: "PATCH",
headers: {
Authorization: "Bearer " + accessToken,
"Content-Type": "application/json",
},
body: JSON.stringify(this.translateEvent(event)), body: JSON.stringify(this.translateEvent(event)),
}); });
@@ -82,13 +88,8 @@ export default class Office365CalendarService implements Calendar {
async deleteEvent(uid: string): Promise<void> { async deleteEvent(uid: string): Promise<void> {
try { try {
const accessToken = await (await this.auth).getToken(); const response = await this.fetcher(`/me/calendar/events/${uid}`, {
const response = await fetch("https://graph.microsoft.com/v1.0/me/calendar/events/" + uid, {
method: "DELETE", method: "DELETE",
headers: {
Authorization: "Bearer " + accessToken,
},
}); });
handleErrorsRaw(response); handleErrorsRaw(response);
@@ -110,95 +111,72 @@ export default class Office365CalendarService implements Calendar {
const filter = `?startDateTime=${encodeURIComponent( const filter = `?startDateTime=${encodeURIComponent(
dateFromParsed.toISOString() dateFromParsed.toISOString()
)}&endDateTime=${encodeURIComponent(dateToParsed.toISOString())}`; )}&endDateTime=${encodeURIComponent(dateToParsed.toISOString())}`;
return (await this.auth)
.getToken()
.then(async (accessToken) => {
const selectedCalendarIds = selectedCalendars
.filter((e) => e.integration === this.integrationName)
.map((e) => e.externalId)
.filter(Boolean);
if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) {
// Only calendars of other integrations selected
return Promise.resolve([]);
}
const ids = await (selectedCalendarIds.length === 0 try {
? this.listCalendars().then((cals) => cals.map((e_2) => e_2.externalId).filter(Boolean) || []) const selectedCalendarIds = selectedCalendars
: Promise.resolve(selectedCalendarIds)); .filter((e) => e.integration === this.integrationName)
const requests = ids.map((calendarId, id) => ({ .map((e) => e.externalId)
id, .filter(Boolean);
method: "GET", if (selectedCalendarIds.length === 0 && selectedCalendars.length > 0) {
url: `/me/calendars/${calendarId}/calendarView${filter}`, // Only calendars of other integrations selected
})); return Promise.resolve([]);
const response = await fetch("https://graph.microsoft.com/v1.0/$batch", { }
method: "POST",
headers: { const ids = await (selectedCalendarIds.length === 0
Authorization: "Bearer " + accessToken, ? this.listCalendars().then((cals) => cals.map((e_2) => e_2.externalId).filter(Boolean) || [])
"Content-Type": "application/json", : Promise.resolve(selectedCalendarIds));
}, const requests = ids.map((calendarId, id) => ({
body: JSON.stringify({ requests }), id,
}); method: "GET",
const responseBody = await handleErrorsJson(response); url: `/me/calendars/${calendarId}/calendarView${filter}`,
return responseBody.responses.reduce( }));
(acc: BufferedBusyTime[], subResponse: { body: { value?: any[] } }) => const response = await this.apiGraphBatchCall(requests);
acc.concat( const responseBody = await handleErrorsJson(response);
subResponse.body?.value let responseBatchApi: IBatchResponse = { responses: [] };
? subResponse.body.value if (typeof responseBody === "string") {
.filter((evt) => evt.showAs !== "free" && evt.showAs !== "workingElsewhere") responseBatchApi = this.handleTextJsonResponseWithHtmlInBody(responseBody);
.map((evt) => { }
return { let alreadySuccessResponse = [] as ISettledResponse[];
start: evt.start.dateTime + "Z",
end: evt.end.dateTime + "Z", // Validate if any 429 status Retry-After is present
}; const retryAfter =
}) !!responseBatchApi?.responses && this.findRetryAfterResponse(responseBatchApi.responses);
: []
), if (retryAfter && responseBatchApi.responses) {
[] responseBatchApi = await this.fetchRequestWithRetryAfter(requests, responseBatchApi.responses, 2);
); }
})
.catch((err: unknown) => { // Recursively fetch nextLink responses
console.log(err); alreadySuccessResponse = await this.fetchResponsesWithNextLink(responseBatchApi.responses);
return Promise.reject([]);
}); return alreadySuccessResponse ? this.processBusyTimes(alreadySuccessResponse) : [];
} catch (err) {
console.log(err);
return Promise.reject([]);
}
} }
async listCalendars(): Promise<IntegrationCalendar[]> { async listCalendars(): Promise<IntegrationCalendar[]> {
return (await this.auth).getToken().then((accessToken) => const response = await this.fetcher(`/me/calendars`);
fetch("https://graph.microsoft.com/v1.0/me/calendars", { const responseBody = (await handleErrorsJson(response)) as { value: OfficeCalendar[] };
method: "get", return responseBody.value.map((cal) => {
headers: { const calendar: IntegrationCalendar = {
Authorization: "Bearer " + accessToken, externalId: cal.id ?? "No Id",
"Content-Type": "application/json", integration: this.integrationName,
}, name: cal.name ?? "No calendar name",
}) primary: cal.isDefaultCalendar ?? false,
.then(handleErrorsJson) readOnly: !cal.canEdit && true,
.then((responseBody: { value: OfficeCalendar[] }) => { };
return responseBody.value.map((cal) => { return calendar;
const calendar: IntegrationCalendar = { });
externalId: cal.id ?? "No Id",
integration: this.integrationName,
name: cal.name ?? "No calendar name",
primary: cal.isDefaultCalendar ?? false,
readOnly: !cal.canEdit && true,
};
return calendar;
});
})
);
} }
private o365Auth = async (credential: Credential) => { private o365Auth = (credential: Credential) => {
const appKeys = await getAppKeysFromSlug("office365-calendar");
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
if (!client_id) throw new HttpError({ statusCode: 400, message: "office365 client_id missing." });
if (!client_secret) throw new HttpError({ statusCode: 400, message: "office365 client_secret missing." });
const isExpired = (expiryDate: number) => expiryDate < Math.round(+new Date() / 1000); const isExpired = (expiryDate: number) => expiryDate < Math.round(+new Date() / 1000);
const o365AuthCredentials = credential.key as O365AuthCredentials; const o365AuthCredentials = credential.key as O365AuthCredentials;
const refreshAccessToken = async (refreshToken: string) => { const refreshAccessToken = async (refreshToken: string) => {
const { client_id, client_secret } = await getOfficeAppKeys();
const response = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", { const response = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" }, headers: { "Content-Type": "application/x-www-form-urlencoded" },
@@ -257,4 +235,158 @@ export default class Office365CalendarService implements Calendar {
location: event.location ? { displayName: getLocation(event) } : undefined, location: event.location ? { displayName: getLocation(event) } : undefined,
}; };
}; };
private fetcher = async (endpoint: string, init?: RequestInit | undefined) => {
this.accessToken = await this.auth.getToken();
return fetch(`${this.apiGraphUrl}${endpoint}`, {
method: "get",
headers: {
Authorization: "Bearer " + this.accessToken,
"Content-Type": "application/json",
},
...init,
});
};
private fetchResponsesWithNextLink = async (
settledResponses: ISettledResponse[]
): Promise<ISettledResponse[]> => {
const alreadySuccess = [] as ISettledResponse[];
const newLinkRequest = [] as IRequest[];
settledResponses?.forEach((response) => {
if (response.status === 200 && response.body["@odata.nextLink"] === undefined) {
alreadySuccess.push(response);
} else {
const nextLinkUrl = response.body["@odata.nextLink"]
? response.body["@odata.nextLink"].replace(this.apiGraphUrl, "")
: "";
if (nextLinkUrl) {
// Saving link for later use
newLinkRequest.push({
id: Number(response.id),
method: "GET",
url: nextLinkUrl,
});
}
delete response.body["@odata.nextLink"];
// Pushing success body content
alreadySuccess.push(response);
}
});
if (newLinkRequest.length === 0) {
return alreadySuccess;
}
const newResponse = await this.apiGraphBatchCall(newLinkRequest);
let newResponseBody = await handleErrorsJson(newResponse);
if (typeof newResponseBody === "string") {
newResponseBody = this.handleTextJsonResponseWithHtmlInBody(newResponseBody);
}
// Going recursive to fetch next link
const newSettledResponses = await this.fetchResponsesWithNextLink(newResponseBody.responses);
return [...alreadySuccess, ...newSettledResponses];
};
private fetchRequestWithRetryAfter = async (
originalRequests: IRequest[],
settledPromises: ISettledResponse[],
maxRetries: number,
retryCount = 0
): Promise<IBatchResponse> => {
let retryAfterTimeout = 0;
if (retryCount >= maxRetries) {
return { responses: settledPromises };
}
const alreadySuccessRequest = [] as ISettledResponse[];
const failedRequest = [] as IRequest[];
settledPromises.forEach((item) => {
if (item.status === 200) {
alreadySuccessRequest.push(item);
} else if (item.status === 429) {
const newTimeout = Number(item.headers["Retry-After"]) * 1000 || 0;
retryAfterTimeout = newTimeout > retryAfterTimeout ? newTimeout : retryAfterTimeout;
failedRequest.push(originalRequests[Number(item.id)]);
}
});
if (failedRequest.length === 0) {
return { responses: alreadySuccessRequest };
}
// Await certain time from retry-after header
await new Promise((r) => setTimeout(r, retryAfterTimeout));
const newResponses = await this.apiGraphBatchCall(failedRequest);
let newResponseBody = await handleErrorsJson(newResponses);
if (typeof newResponseBody === "string") {
newResponseBody = this.handleTextJsonResponseWithHtmlInBody(newResponseBody);
}
const retryAfter = !!newResponseBody?.responses && this.findRetryAfterResponse(newResponseBody.responses);
if (retryAfter && newResponseBody.responses) {
newResponseBody = await this.fetchRequestWithRetryAfter(
failedRequest,
newResponseBody.responses,
maxRetries,
retryCount + 1
);
}
return { responses: [...alreadySuccessRequest, ...(newResponseBody?.responses || [])] };
};
private apiGraphBatchCall = async (requests: IRequest[]): Promise<Response> => {
try {
const response = await this.fetcher(`/$batch`, {
method: "POST",
body: JSON.stringify({ requests }),
});
return response;
} catch (error: any) {
throw new Error(error);
}
};
private handleTextJsonResponseWithHtmlInBody = (response: string): IBatchResponse => {
try {
const parsedJson = JSON.parse(response);
return parsedJson;
} catch (error) {
// Looking for html in body
const openTag = '"body":<';
const closeTag = "</html>";
const htmlBeginning = response.indexOf(openTag) + openTag.length - 1;
const htmlEnding = response.indexOf(closeTag) + closeTag.length + 2;
const resultString = `${response.repeat(1).substring(0, htmlBeginning)} ""${response
.repeat(1)
.substring(htmlEnding, response.length)}`;
return JSON.parse(resultString);
}
};
private findRetryAfterResponse = (response: ISettledResponse[]) => {
const foundRetry = response.find((request: ISettledResponse) => request.status === 429);
return !!foundRetry;
};
private processBusyTimes = (responses: ISettledResponse[]) => {
return responses.reduce(
(acc: BufferedBusyTime[], subResponse: { body: { value?: any[]; error?: any[] } }) => {
if (!subResponse.body?.value) return acc;
return acc.concat(
subResponse.body.value
.filter((evt) => evt.showAs !== "free" && evt.showAs !== "workingElsewhere")
.map((evt) => ({
start: evt.start.dateTime + "Z",
end: evt.end.dateTime + "Z",
}))
);
},
[]
);
};
} }
@@ -0,0 +1,12 @@
import { z } from "zod";
import getParsedAppKeysFromSlug from "../../_utils/getParsedAppKeysFromSlug";
const officeAppKeysSchema = z.object({
client_id: z.string(),
client_secret: z.string(),
});
export const getOfficeAppKeys = async () => {
return getParsedAppKeysFromSlug("office365-calendar", officeAppKeysSchema);
};
+3 -1
View File
@@ -100,11 +100,12 @@ const getCachedResults = async (
const passedSelectedCalendars = selectedCalendars.filter((sc) => sc.integration === type); const passedSelectedCalendars = selectedCalendars.filter((sc) => sc.integration === type);
/** We extract external Ids so we don't cache too much */ /** We extract external Ids so we don't cache too much */
const selectedCalendarIds = passedSelectedCalendars.map((sc) => sc.externalId); const selectedCalendarIds = passedSelectedCalendars.map((sc) => sc.externalId);
/** We create a unque hash key based on the input data */ /** We create a unique hash key based on the input data */
const cacheKey = JSON.stringify({ id, selectedCalendarIds, dateFrom, dateTo }); const cacheKey = JSON.stringify({ id, selectedCalendarIds, dateFrom, dateTo });
const cacheHashedKey = createHash("md5").update(cacheKey).digest("hex"); const cacheHashedKey = createHash("md5").update(cacheKey).digest("hex");
/** Check if we already have cached data and return */ /** Check if we already have cached data and return */
const cachedAvailability = cache.get(cacheHashedKey); const cachedAvailability = cache.get(cacheHashedKey);
if (cachedAvailability) { if (cachedAvailability) {
log.debug(`Cache HIT: Calendar Availability for key: ${cacheKey}`); log.debug(`Cache HIT: Calendar Availability for key: ${cacheKey}`);
return cachedAvailability; return cachedAvailability;
@@ -113,6 +114,7 @@ const getCachedResults = async (
/** If we don't then we actually fetch external calendars (which can be very slow) */ /** If we don't then we actually fetch external calendars (which can be very slow) */
const availability = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars); const availability = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars);
/** We save the availability to a few seconds so recurrent calls are nearly instant */ /** We save the availability to a few seconds so recurrent calls are nearly instant */
cache.put(cacheHashedKey, availability, CACHING_TIME); cache.put(cacheHashedKey, availability, CACHING_TIME);
return availability; return availability;
}); });
+1 -1
View File
@@ -47,7 +47,7 @@ export async function getBusyTimes(params: {
logger.debug(`prisma booking get took ${endPrismaBookingGet - startPrismaBookingGet}ms`); logger.debug(`prisma booking get took ${endPrismaBookingGet - startPrismaBookingGet}ms`);
if (credentials.length > 0) { if (credentials.length > 0) {
const calendarBusyTimes = await getBusyCalendarTimes(credentials, startTime, endTime, selectedCalendars); const calendarBusyTimes = await getBusyCalendarTimes(credentials, startTime, endTime, selectedCalendars);
// console.log("calendarBusyTimes", calendarBusyTimes);
busyTimes.push(...calendarBusyTimes); /* busyTimes.push(...calendarBusyTimes); /*
// TODO: Disabled until we can filter Zoom events by date. Also this is adding too much latency. // TODO: Disabled until we can filter Zoom events by date. Also this is adding too much latency.
const videoBusyTimes = (await getBusyVideoTimes(credentials)).filter(notEmpty); const videoBusyTimes = (await getBusyVideoTimes(credentials)).filter(notEmpty);
+1
View File
@@ -151,6 +151,7 @@ export async function getUserAvailability(
); );
const endGetWorkingHours = performance.now(); const endGetWorkingHours = performance.now();
logger.debug(`getWorkingHours took ${endGetWorkingHours - startGetWorkingHours}ms for userId ${userId}`); logger.debug(`getWorkingHours took ${endGetWorkingHours - startGetWorkingHours}ms for userId ${userId}`);
return { return {
busy: bufferedBusyTimes, busy: bufferedBusyTimes,
timeZone, timeZone,
+4
View File
@@ -11,6 +11,9 @@ export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: numb
} }
export function handleErrorsJson(response: Response) { export function handleErrorsJson(response: Response) {
if (response.headers.get("content-encoding") === "gzip") {
return response.text();
}
if (response.status === 204) { if (response.status === 204) {
return new Promise((resolve) => resolve({})); return new Promise((resolve) => resolve({}));
} }
@@ -18,6 +21,7 @@ export function handleErrorsJson(response: Response) {
response.json().then(console.log); response.json().then(console.log);
throw Error(response.statusText); throw Error(response.statusText);
} }
return response.json(); return response.json();
} }
@@ -203,7 +203,7 @@ export async function getSchedule(
}) })
); );
const workingHours = userSchedules.flatMap((s) => s.workingHours); const workingHours = userSchedules?.flatMap((s) => s.workingHours);
const slots: Record<string, Slot[]> = {}; const slots: Record<string, Slot[]> = {};
const availabilityCheckProps = { const availabilityCheckProps = {
@@ -279,6 +279,7 @@ export async function getSchedule(
`checkForAvailability took ${checkForAvailabilityTime}ms and executed ${checkForAvailabilityCount} times` `checkForAvailability took ${checkForAvailabilityTime}ms and executed ${checkForAvailabilityCount} times`
); );
logger.silly(`Available slots: ${JSON.stringify(slots)}`); logger.silly(`Available slots: ${JSON.stringify(slots)}`);
return { return {
slots, slots,
}; };