diff --git a/solutions/automations/vacation-calendar/Code.js b/solutions/automations/vacation-calendar/Code.js index ef0078a20..8fe906046 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'; @@ -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 @@ -171,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()) { @@ -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; }