From 86490e01781c3a08bdeb4c9a81081571192dd1a3 Mon Sep 17 00:00:00 2001 From: w33kly <117058340+w33kly@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:44:37 -0500 Subject: [PATCH 1/4] Implement Recursive Pagination for getEvents maxResults can be changed to further optimize efficiency & reduce spamming --- src/utils/api/calls.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/utils/api/calls.js b/src/utils/api/calls.js index 971fd797..3b36d31f 100644 --- a/src/utils/api/calls.js +++ b/src/utils/api/calls.js @@ -36,13 +36,14 @@ 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 = []; - data.items +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) => { renameKey(obj.start, 'dateTime', 'date'); @@ -54,9 +55,12 @@ 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 getEventsRecursive(data.nextPageToken, allEvents); + } else { + return _.sortBy(allEvents, (o) => moment(o.start)); + } }; exports.checkRecaptcha = async function checkRecaptcha(recaptchaToken) { From d2370eb74f4dff673eb4b06a3111346d001e63da Mon Sep 17 00:00:00 2001 From: w33kly <117058340+w33kly@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:07:07 -0500 Subject: [PATCH 2/4] Update calls.js --- src/utils/api/calls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/api/calls.js b/src/utils/api/calls.js index 3b36d31f..bae21e8e 100644 --- a/src/utils/api/calls.js +++ b/src/utils/api/calls.js @@ -57,7 +57,7 @@ exports.getEvents = async function getEvents(pageToken = null, allEvents = []) { }); allEvents.push(...events); if (data.nextPageToken) { - return getEventsRecursive(data.nextPageToken, allEvents); + return getEvents(data.nextPageToken, allEvents); } else { return _.sortBy(allEvents, (o) => moment(o.start)); } From 7d989e1a1ed5091c32e02694f4923cfe35a4186a Mon Sep 17 00:00:00 2001 From: w33kly <117058340+w33kly@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:24:39 -0500 Subject: [PATCH 3/4] Update calls.js --- src/utils/api/calls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/api/calls.js b/src/utils/api/calls.js index bae21e8e..60772962 100644 --- a/src/utils/api/calls.js +++ b/src/utils/api/calls.js @@ -59,7 +59,7 @@ exports.getEvents = async function getEvents(pageToken = null, allEvents = []) { if (data.nextPageToken) { return getEvents(data.nextPageToken, allEvents); } else { - return _.sortBy(allEvents, (o) => moment(o.start)); + return { "events": _.sortBy(allEvents, (o) => moment(o.start)) }; } }; From b42cf6e79bac302ffeb12e76a53ad1f6bd044da2 Mon Sep 17 00:00:00 2001 From: w33kly <117058340+w33kly@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:17:24 -0500 Subject: [PATCH 4/4] Update calls.js --- src/utils/api/calls.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/utils/api/calls.js b/src/utils/api/calls.js index 60772962..c60fd0c1 100644 --- a/src/utils/api/calls.js +++ b/src/utils/api/calls.js @@ -37,13 +37,13 @@ exports.sendEmail = async function sendEmail(toEmail, email, subject, content) { }; 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 + 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) => { renameKey(obj.start, 'dateTime', 'date'); @@ -56,11 +56,10 @@ exports.getEvents = async function getEvents(pageToken = null, allEvents = []) { }); }); allEvents.push(...events); - if (data.nextPageToken) { - return getEvents(data.nextPageToken, allEvents); - } else { - return { "events": _.sortBy(allEvents, (o) => moment(o.start)) }; - } + if (data.nextPageToken) { + return getEvents(data.nextPageToken, allEvents); + } + return { events: _.sortBy(allEvents, (o) => moment(o.start)) }; }; exports.checkRecaptcha = async function checkRecaptcha(recaptchaToken) {