Skip to content

Commit

Permalink
Merge pull request #297 from rossvc/master
Browse files Browse the repository at this point in the history
Implement Recursive Pagination for getEvents
  • Loading branch information
bnleft authored Oct 10, 2023
2 parents 946fe0e + b42cf6e commit cfac2f6
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/utils/api/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ exports.sendEmail = async function sendEmail(toEmail, email, subject, content) {
);
};

exports.getEvents = async function getEvents() {
const { data } = await axios.get(
`https://www.googleapis.com/calendar/v3/calendars/${CALENDAR_ID}/events?key=${CALENDAR_API_KEY}`
);

let events = [];
exports.getEvents = async function getEvents(pageToken = null, allEvents = []) {
let url = `https://www.googleapis.com/calendar/v3/calendars/${CALENDAR_ID}/events?maxResults=50&key=${CALENDAR_API_KEY}`;
if (pageToken) {
url += `&pageToken=${pageToken}`;
}
const { data } = await axios.get(url);
const events = [];
data.items
.filter((obj) => obj?.start?.date || obj?.start?.dateTime)
.forEach((obj) => {
Expand All @@ -54,9 +55,11 @@ exports.getEvents = async function getEvents() {
desc: obj?.description ? obj.description : 'TBD',
});
});

events = _.sortBy(events, (o) => moment(o.start));
return { events };
allEvents.push(...events);
if (data.nextPageToken) {
return getEvents(data.nextPageToken, allEvents);
}
return { events: _.sortBy(allEvents, (o) => moment(o.start)) };
};

exports.checkRecaptcha = async function checkRecaptcha(recaptchaToken) {
Expand Down

0 comments on commit cfac2f6

Please sign in to comment.