Files
cally/calendar-integration/google-calendar-utils.js
2024-10-31 19:57:06 +01:00

56 lines
1.4 KiB
JavaScript

export async function fetchGoogleCalendarEvents(token, email, familyId, startDate, endDate) {
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}&time_max=${endDate}`,
{
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) {
if (start.dateTime) {
const stringDate = start.dateTime;
startDateTime = new Date(stringDate);
} else {
const stringDate = start.date;
startDateTime = new Date(stringDate);
isAllDay = true;
}
}
const end = item.end;
let endDateTime;
if (end !== undefined) {
if (end.dateTime) {
const stringDate = end.dateTime;
endDateTime = new Date(stringDate);
} else {
const stringDate = end.date;
endDateTime = new Date(stringDate);
}
}
const googleEvent = {
id: item.id,
title: item.summary ?? "",
startDate: startDateTime,
endDate: endDateTime,
allDay: isAllDay,
familyId,
email
};
googleEvents.push(googleEvent);
});
return {googleEvents, success: response.ok};
}