import { request , IRequestOptions , ArcGISRequestError } from "@esri/arcgis-rest-request" ; import { getPortalUrl } from "../util/get-portal-url" ; import { chunk } from "../util/array" ; export interface IInviteGroupUsersOptions extends IRequestOptions { /**
     * Group ID
     */ id : string ; /**
     * An array of usernames to be added to the group as group members
     */ users : string [ ] ; /**
     * What role the users should be invited as ('group_member' | 'group_admin')
     */ role : string ; /**
     * Expiration date on the invitation can be set for one day, three days, one week, or two weeks, in minutes.
     */ expiration : number ; } export interface IInviteGroupUsersResult { /**
     * Whether the operation was successful
     */ success : boolean ; /**
     * An array of request errors
     */ errors ? : ArcGISRequestError [ ] ; } param { IInviteGroupUsersOptions } options returns { Promise < IAddGroupUsersResult > } export function inviteGroupUsers ( options ) { const id = options . id ; const url = `${ getPortalUrl ( options ) }/community/groups/${ id }/invite` ; const batches = _generateBatchRequests ( options ) ; const promises = batches . map ( batch => _sendSafeRequest ( url , batch ) ) ; return Promise . all ( promises ) . then ( _combineResults ) ; } private function _generateBatchRequests ( options ) { const userBatches = chunk < string > ( options . users , 25 ) ; return userBatches . map ( users => _generateRequestOptions ( users , options ) ) ; } private function _generateRequestOptions ( users , baseOptions ) { const requestOptions = Object . assign ( { } , baseOptions ) ; requestOptions . params = { ... requestOptions . params , users , role : requestOptions . role , expiration : requestOptions . expiration , } ; return requestOptions ; } private function _sendSafeRequest ( url , requestOptions ) { return request ( url , requestOptions ) . catch ( error => ( { errors : [ error ] } ) ) ; } private function _combineResults ( responses ) { const success = responses . every ( res => res . success ) ; const errors = responses . reduce ( ( collection , res ) => collection . concat ( res . errors || [ ] ) , [ ] ) ; const combined = { success } ; if ( errors . length > 0 ) { combined . errors = errors ; } return combined ; }