Minimized msgraph calls while event listing by batching
This commit is contained in:
		
							parent
							
								
									90d6f8faee
								
							
						
					
					
						commit
						496fcdfabc
					
				
					 1 changed files with 32 additions and 22 deletions
				
			
		|  | @ -225,28 +225,38 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => { | |||
|               ? listCalendars().then((cals) => cals.map((e) => e.externalId)) | ||||
|               : Promise.resolve(selectedCalendarIds).then((x) => x) | ||||
|           ).then((ids: string[]) => { | ||||
|             const urls = ids.map( | ||||
|               (calendarId) => | ||||
|                 "https://graph.microsoft.com/v1.0/me/calendars/" + calendarId + "/events" + filter | ||||
|             ); | ||||
|             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", | ||||
|                     })) | ||||
|                   ) | ||||
|               ) | ||||
|             ).then((results) => results.reduce((acc, events) => acc.concat(events), [])); | ||||
|             const requests = ids.map((calendarId, id) => ({ | ||||
|               id, | ||||
|               method: "GET", | ||||
|               headers: { | ||||
|                 Prefer: 'outlook.timezone="Etc/GMT"', | ||||
|               }, | ||||
|               url: `/me/calendars/${calendarId}/events${filter}`, | ||||
|             })); | ||||
| 
 | ||||
|             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", | ||||
|                         }; | ||||
|                       }) | ||||
|                     ), | ||||
|                   [] | ||||
|                 ) | ||||
|               ); | ||||
|           }); | ||||
|         }) | ||||
|         .catch((err) => { | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Alex van Andel
						Alex van Andel