import { getUser } from "@esri/arcgis-rest-portal" ; import { request } from "@esri/arcgis-rest-request" ; import { IUser } from "@esri/arcgis-rest-types" ; import { IHubRequestOptions , getPortalUrl , Logger , batch } from "@esri/hub-common" ; param { string [ ] } usernames param { object } requestOptions export function getMembers ( usernames , requestOptions ) { return requestOptions . authentication ? authenticatedGetMembers ( usernames , requestOptions ) : unauthenticatedGetMembers ( usernames , requestOptions ) ; } param { Array } usernames param { object } requestOptions function authenticatedGetMembers ( usernames , requestOptions ) { const urlPath = `${ getPortalUrl ( requestOptions ) }/sharing/rest/community/users` ; const chunkSize = 100 ; const chunkedUsernames = [ ] ; for ( let i = 0 ; i < usernames . length ; i += chunkSize ) { chunkedUsernames . push ( usernames . slice ( i , i + chunkSize ) ) ; } const chunkedOptions = chunkedUsernames . map ( chunk => { const q = chunk . map ( username => `username:${ username }` ) . join ( " OR " ) ; return { urlPath , requestOptions : { params : { q , num : chunk . length } , ... requestOptions } } ; } ) ; return batch ( chunkedOptions , batchMemberRequest ) . then ( batchedMembers => { return batchedMembers . reduce ( ( flat , toFlatten ) => { return flat . concat ( toFlatten ) ; } , [ ] ) ; } ) ; } param { Array } usernames param { object } requestOptions function unauthenticatedGetMembers ( usernames , requestOptions ) { return Promise . all ( usernames . map ( username => { return getUser ( { username , ... requestOptions } ) . then ( response => { if ( response . firstName || response . lastName || response . fullName ) { return response ; } } ) . catch ( e => { Logger . error ( `Error fetching user, ${ username }, from AGO user endpoint, ${ e }` ) ; return null ; } ) ; } ) ) . then ( members => members . filter ( Boolean ) ) ; } interface IBatchMemberRequestOptions { urlPath : "string" ; requestOptions : IHubRequestOptions ; } param options function batchMemberRequest ( options ) { return request ( options . urlPath , options . requestOptions ) . then ( response => response . results ) . catch ( e => { Logger . error ( `Error fetching members from AGO user search endpoint: ${ e }` ) ; return [ ] ; } ) ; }