Merge pull request #343 from emrysal/feature/minimize-o365-calls-batching-list-events

Minimized msgraph calls while event listing by batching
This commit is contained in:
Bailey Pumfleet 2021-07-26 12:56:08 +01:00 committed by GitHub
commit 5df4fe413e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -223,28 +223,38 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
? listCalendars().then((cals) => cals.map((e) => e.externalId)) ? listCalendars().then((cals) => cals.map((e) => e.externalId))
: Promise.resolve(selectedCalendarIds).then((x) => x) : Promise.resolve(selectedCalendarIds).then((x) => x)
).then((ids: string[]) => { ).then((ids: string[]) => {
const urls = ids.map( const requests = ids.map((calendarId, id) => ({
(calendarId) => id,
"https://graph.microsoft.com/v1.0/me/calendars/" + calendarId + "/events" + filter method: "GET",
); headers: {
return Promise.all( Prefer: 'outlook.timezone="Etc/GMT"',
urls.map((url) => },
fetch(url, { url: `/me/calendars/${calendarId}/events${filter}`,
method: "get", }));
headers: {
Authorization: "Bearer " + accessToken, return fetch("https://graph.microsoft.com/v1.0/$batch", {
Prefer: 'outlook.timezone="Etc/GMT"', method: "POST",
}, headers: {
}) Authorization: "Bearer " + accessToken,
.then(handleErrorsJson) "Content-Type": "application/json",
.then((responseBody) => },
responseBody.value.map((evt) => ({ body: JSON.stringify({ requests }),
start: evt.start.dateTime + "Z", })
end: evt.end.dateTime + "Z", .then(handleErrorsJson)
})) .then((responseBody) =>
) responseBody.responses.reduce(
) (acc, subResponse) =>
).then((results) => results.reduce((acc, events) => acc.concat(events), [])); acc.concat(
subResponse.body.value.map((evt) => {
return {
start: evt.start.dateTime + "Z",
end: evt.end.dateTime + "Z",
};
})
),
[]
)
);
}); });
}) })
.catch((err) => { .catch((err) => {