I'm trying to search for all users in certain groups and just get their email address. Then take those users, combine them and remove duplicates as same user can be in both groups. Lastly I need to check if the user is suspended and create a new array with only users not suspended(false).
Eventually this list of users will be used to make API calls for another application and then write to Google sheets, but for now I'm just trying to get the final list of users.
What I have so far works but seems redundant. Is there a better way to do this? Also not positive how to create the final array after the loop?
function getSpecificUser(user) {
const userDetails = AdminDirectory.Members.list(user);
return userDetails
}
function userSorting() {
let groupList = getSpecificUser('[email protected]').members;
let groupList2 = getSpecificUser('[email protected]').members;
let groupList3 = getSpecificUser('[email protected]').members;
let merged = [...groupList,...groupList2,...groupList3]//tried ...new Set in front but not iterable error
let mergedMap = merged.map(i => i.email);
let noDups = [...new Set(mergedMap)];
for (i in noDups) {
if (AdminDirectory.Users.get(noDups[i]).suspended === false){
Logger.log(noDups[i])//need to put final array here?
}
}
}
CodePudding user response:
You can use a Map to index unique members by member.email
and remove the repetitive getSpecificUser
calls by using a loop over an array of the group emails.
The final result can then be obtained by filtering the Map.values()
by each user's suspended
property, to either isolate the suspended or active members.
function getSpecificUser(user) {
const userDetails = AdminDirectory.Members.list(user);
return userDetails
}
function userSorting() {
const userEmails = ['[email protected]', '[email protected]', '[email protected]'];
const uniqueMembers = new Map();
for (const email of userEmails) {
const { members } = getSpecificUser(email);
for (const member of members) {
uniqueMembers.set(member.email, member)
}
}
const activeMembers = [...uniqueMembers.values()]
.filter(({ email }) => !AdminDirectory.Users.get(email).suspended);
const suspendedMembers = [...uniqueMembers.values()]
.filter(({ email }) => AdminDirectory.Users.get(email).suspended);
}
Note: Don't use for...in on arrays, rather use for...of for iterating array elements directly or for (const i of [].keys()){...}
(Array#keys()) for iterating the indexes of the array.
CodePudding user response:
This would iterate only once over the list of emails. Or twice, considering Array.from()
.
function getActiveEmails(...mailingGroups) {
const uniqueEmails = new Set();
for (let i = 0, len = mailingGroups.length; i < len; i ) {
const members = AdminDirectory.Members.list(mailingGroups[i])?.members || [];
for (let j = 0, lenJ = members.length; j < lenJ; j ) {
const member = members[j];
if (!AdminDirectory.Users.get(member.email)?.suspended) {
uniqueEmails.add(member.email);
}
}
}
return Array.from(uniqueEmails);
}
const activeEmails = getActiveEmails("[email protected]", "[email protected]", "[email protected]");
console.log(activeEmails);
Note that a user can participate in groups using different aliases. If you want to make sure that a user (not email) is only present once in the list, or if that other API call you mention requires the primary email of the user, you could use something like the following to resolve aliases:
function getActiveEmails(...mailingGroups) {
const uniqueEmails = new Set();
for (let i = 0, len = mailingGroups.length; i < len; i ) {
const members = AdminDirectory.Members.list(mailingGroups[i])?.members || [];
for (let j = 0, lenJ = members.length; j < lenJ; j ) {
const member = members[j];
const user = AdminDirectory.Users.get(member.email);
if (!user) continue;
if (!user.suspended) {
// always use the user's primary email
uniqueEmails.add(user.primaryEmail);
}
}
}
return Array.from(uniqueEmails);
}
const activeEmails = getActiveEmails("[email protected]", "[email protected]", "[email protected]");
console.log(activeEmails);