From a202544650f9d591341929f1dfde47809cd4ebe7 Mon Sep 17 00:00:00 2001 From: lwcorp <lwcorp@users.noreply.github.com> Date: Mon, 15 Apr 2024 22:12:21 +0300 Subject: [PATCH 1/3] feat(calendar): Readded multiple groups and added blocked groups 1. Undid the removal of the support for multiple groups (why did you allow this person's PR just so they fix formatting?) 2. Made the multiple groups and get all members functions to use each other to avoid duplicated code 3. Added support for avoiding blocked groups (e.g. external ones) 4. Simplified both functions' code --- .../automations/vacation-calendar/Code.js | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/solutions/automations/vacation-calendar/Code.js b/solutions/automations/vacation-calendar/Code.js index ef0078a20..2ed311f5e 100644 --- a/solutions/automations/vacation-calendar/Code.js +++ b/solutions/automations/vacation-calendar/Code.js @@ -55,16 +55,12 @@ function sync() { maxDate.setMonth(maxDate.getMonth() + MONTHS_IN_ADVANCE); // Determines the time the the script was last run. - let lastRun = PropertiesService.getScriptProperties().getProperty('lastRun'); - lastRun = lastRun ? new Date(lastRun) : null; + //let lastRun = PropertiesService.getScriptProperties().getProperty('lastRun'); + //lastRun = lastRun ? new Date(lastRun) : null; + let lastRun = null; // Gets the list of users in the Google Group. - let users = getAllMembers(GROUP_EMAIL); - if (ONLY_DIRECT_MEMBERS){ - users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers(); - } else if (Array.isArray(GROUP_EMAIL)) { - users = getUsersFromGroups(GROUP_EMAIL); - } + let users = getUsersFromGroups(GROUP_EMAIL); // For each user, finds events having one or more of the keywords in the event // summary in the specified date range. Imports each of those to the team @@ -199,43 +195,47 @@ function formatDateAsRFC3339(date) { /** * Get both direct and indirect members (and delete duplicates). * @param {string} the e-mail address of the group. +* @param {array} the list of already added users. +* @param {array} the list of already added addresses. * @return {object} direct and indirect members. */ -function getAllMembers(groupEmail) { - var group = GroupsApp.getGroupByEmail(groupEmail); - var users = group.getUsers(); - var childGroups = group.getGroups(); - for (var i = 0; i < childGroups.length; i++) { - var childGroup = childGroups[i]; - users = users.concat(getAllMembers(childGroup.getEmail())); +function getAllMembers(groupEmail, userEntities, addresses) { + let group = GroupsApp.getGroupByEmail(groupEmail); + let users = group.getUsers(); + if (!ONLY_DIRECT_MEMBERS) { + try { + let childGroups = group.getGroups(); + for (let i = 0; i < childGroups.length; i++) { + let childGroup = childGroups[i]; + [userEntities, addresses] = getAllMembers(childGroup.getEmail(), userEntities, addresses); + } + } catch (e) { + console.error('Error attempting to pull groups due to %s. Skipping.', + e.toString()); + } } // Remove duplicate members - var uniqueUsers = []; - var userEmails = {}; - for (var i = 0; i < users.length; i++) { - var user = users[i]; - if (!userEmails[user.getEmail()]) { - uniqueUsers.push(user); - userEmails[user.getEmail()] = true; + for (let i = 0; i < users.length; i++) { + let user = users[i]; + if (!addresses.includes(user.getEmail())) { + userEntities.push(user); + addresses.push(user.getEmail()); } } - return uniqueUsers; + return [userEntities, addresses]; } - /** * Get indirect members from multiple groups (and delete duplicates). -* @param {array} the e-mail addresses of multiple groups. +* @param {string or array} the e-mail addresses of multiple groups. * @return {object} indirect members of multiple groups. */ function getUsersFromGroups(groupEmails) { - let users = []; + let users = [], addresses = []; + if (!Array.isArray(groupEmails)) { + groupEmails = [groupEmails]; + } for (let groupEmail of groupEmails) { - let groupUsers = GroupsApp.getGroupByEmail(groupEmail).getUsers(); - for (let user of groupUsers) { - if (!users.some(u => u.getEmail() === user.getEmail())) { - users.push(user); - } - } + [users, addresses] = getAllMembers(groupEmail, users, addresses); } return users; } From e7974d06a41cb17d03f89627f14a41a39a9c85ae Mon Sep 17 00:00:00 2001 From: lwcorp <lwcorp@users.noreply.github.com> Date: Tue, 16 Apr 2024 19:05:17 +0300 Subject: [PATCH 2/3] Fixed spelling --- solutions/automations/vacation-calendar/Code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/automations/vacation-calendar/Code.js b/solutions/automations/vacation-calendar/Code.js index 2ed311f5e..2d8c12ed6 100644 --- a/solutions/automations/vacation-calendar/Code.js +++ b/solutions/automations/vacation-calendar/Code.js @@ -22,7 +22,7 @@ limitations under the License. let TEAM_CALENDAR_ID = 'ENTER_TEAM_CALENDAR_ID_HERE'; // Set the email address of the Google Group that contains everyone in the team. // Ensure the group has less than 500 members to avoid timeouts. -// Change to an array in order to add indirect members frrm multiple groups, for example: +// Change to an array in order to add indirect members from multiple groups, for example: // let GROUP_EMAIL = ['ENTER_GOOGLE_GROUP_EMAIL_HERE', 'ENTER_ANOTHER_GOOGLE_GROUP_EMAIL_HERE']; let GROUP_EMAIL = 'ENTER_GOOGLE_GROUP_EMAIL_HERE'; From 414a643a7257070d98e0a7ab86fbda9e4289abbb Mon Sep 17 00:00:00 2001 From: lwcorp <lwcorp@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:50:40 +0300 Subject: [PATCH 3/3] Prevented script crash for some event has no summary --- solutions/automations/vacation-calendar/Code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/automations/vacation-calendar/Code.js b/solutions/automations/vacation-calendar/Code.js index 2d8c12ed6..8fe906046 100644 --- a/solutions/automations/vacation-calendar/Code.js +++ b/solutions/automations/vacation-calendar/Code.js @@ -167,7 +167,7 @@ function shouldImportEvent(user, keyword, event) { // Filters out events where the keyword did not appear in the summary // (that is, the keyword appeared in a different field, and are thus // is not likely to be relevant). - if (event.summary.toLowerCase().indexOf(keyword) < 0) { + if (!event.summary || event.summary.toLowerCase().indexOf(keyword) < 0) { return false; } if (!event.organizer || event.organizer.email == user.getEmail()) {