I am trying to assign Manager role to multiple members in one call. They're comma separated in the json. It sets for the first user in the comma separated values but it doesn't do for 2nd user. I am not sure where am I going wrong with this. I'm using Google Groups API. Any help would be appreciated as I'm new to using this and callback functions. Below is my JSON:
{
"groupName":"TestGoogle5"
"managerToBeAdded":"test1,test2",
"memberSourceId":"g:gsa",
"groupType":"group"
}
My typescript code:
searchGroup(groupName: string): GaxiosPromise<admin_directory_v1.Schema$Groups> {
console.log("In search group")
return this.googleService.groups.list({
domain: this.googleDomain,
query: `name=${groupName}`
})
}
addManagerToGroup(groupName: string, userId: string, retries: number = this.defaultRetries, delay: number = this.defaultDelay): Promise<any> {
if (userId == "") {
let msg = "Invalid userId"
console.log(msg)
return Promise.resolve(msg)
}
let userIdArray : Array <string> = []
userIdArray= userId.indexOf(",") > -1 ? userId.split(",") : userId.split(" ");
for (let entry of userIdArray) {
return this.searchGroup(groupName).then((result) => {
const groups = result.data.groups ?? []
if (groups.length == 1) {
console.log("In If")
console.log(entry)
const groupKey = groups[0].id ?? ""
return this.googleService.members.insert({
groupKey,
requestBody: {
email: `${entry}@gmail.com`,
role: 'MANAGER',
delivery_settings: 'NONE',
}
}).catch(async (error: GaxiosError) => {
// membership already set, skipping message
if (error.message == "Member already exists.") {
let msg = `${entry} already exists as member of ${groupName}`
return Promise.resolve(msg)
}
return Promise.reject(error)
})
} else if (groups.length == 0) {
return Promise.reject("[UNRESOLVABLE_GROUP_EXCEPTION]: Could not find group with name " + groupName)
} else if (groups.length >= 2) {
return Promise.reject("[MULTIPLE_GROUP_EXCEPTION]: Found multiple groups with name " + groupName + " !")
}
})
}
return Promise.resolve("Manager privilege set")
}
Code that calls addManager
const _handlePrivilegeAdd = (gafeService: GAFEService, shortGroupName: string, userId: string, domain: string): Promise<any> => {
return gafeService.addManagerToGroup(shortGroupName, userId)
.catch((error: any) => {
if (error.toString().includes("[UNRESOLVABLE_GROUP_EXCEPTION]")) {
console.log("attempting to create group:\t" + shortGroupName)
return gafeService
.createGroup(shortGroupName, _shortGroupNameToEmail(shortGroupName, domain), shortGroupName)
.then((groupAddResult: any) => {
//sleep for 5 seconds to let the group create propogate in google
return _later(5000)
})
.then(() => {
return gafeService.addManagerToGroup(shortGroupName, userId).then((results: any) => {
if (results == undefined) {
console.log("Not sure what causes this. Will fail the message.")
return Promise.reject("undefined")
}
})
})
}
})
.then((results: any) => {
if (results == undefined) {
console.log("Not sure what causes this. Will fail the message.")
return Promise.reject("undefined")
}
console.log(results)
const resultMessage = `Successfully set ${userId} as a manager to ${shortGroupName} in GAFE.`
console.log(resultMessage)
return resultMessage
})
}