diff --git a/calendar-integration/google-calendar-utils.js b/calendar-integration/google-calendar-utils.js new file mode 100644 index 0000000..0b7bbd6 --- /dev/null +++ b/calendar-integration/google-calendar-utils.js @@ -0,0 +1,66 @@ +export async function fetchGoogleCalendarEvents(token) { + + const response = await fetch( + 'https://www.googleapis.com/calendar/v3/calendars/primary/events', + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + + const data = await response.json(); + const googleEvents = []; + data.items?.forEach((item) => { + + let isAllDay = false; + const start = item.start; + let startDateTime; + if (start !== undefined) { + + const timezone = start.timeZone; + if (start.dateTime) { + const stringDate = start.dateTime; + startDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone}); + } else { + const stringDate = start.date; + startDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone}); + isAllDay = true; + } + } + + const end = item.end; + let endDateTime; + if (end !== undefined) { + const timezone = end.timeZone; + if (end.dateTime) { + const stringDate = end.dateTime; + endDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone}); + } else { + const stringDate = end.date; + endDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone}); + } + } + + const googleEvent = { + id: item.id, + title: item.summary, + startDate: startDateTime, + endDate: endDateTime, + allDay: isAllDay + }; + googleEvents.push(googleEvent); + }); + + return googleEvents; +} + +const options = { + year: "numeric", + month: "long", + day: "numeric", + hour: "numeric", + minute: "numeric", + second: "numeric", + timeZoneName: "short" +}; \ No newline at end of file