Created
December 10, 2020 15:51
-
-
Save brainysmurf/9d8e1dc262862272095218b9f329be80 to your computer and use it in GitHub Desktop.
Resolve aliases given email in appscripts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function resolveAlias_(email) { | |
var resp, ret; | |
try { | |
resp = AdminDirectory.Users.Aliases.list(email); | |
} catch(e) { | |
if (e.message === 'Resource Not Found: userKey') { | |
return null; | |
} | |
return null; | |
} | |
if (resp.aliases) { | |
var primaries; | |
primaries = resp.aliases.reduce( | |
function (acc, aliasRecord) { | |
if (acc.indexOf(aliasRecord.primaryEmail) === -1) acc.push(aliasRecord.primaryEmail); | |
return acc; | |
}, [] | |
); | |
if (primaries.length > 1) throw Error("More than one primary email!"); | |
ret = primaries[0]; | |
} else { | |
ret = email; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment