import { ArcGISRequestError } from "@esri/arcgis-rest-request" ; import { IUser } from "@esri/arcgis-rest-types" ; import { autoAddUsers , getProp } from "@esri/hub-common" ; import { IAddOrInviteContext , IAddOrInviteResponse } from "../types" ; import { autoAddUsersAsAdmins } from "./auto-add-users-as-admins" ; import { processEmailUsers } from "./process-email-users" ; private export param { IAddOrInviteContext } context param { string } userType param { boolean } shouldEmail return { IAddOrInviteResponse } export async function processAutoAddUsers ( context , userType , shouldEmail = false ) { const users = getProp ( context , userType ) ; let autoAddResponse ; let emailResponse ; let notAdded = [ ] ; let errors = [ ] ; const { addUserAsGroupAdmin } = context ; if ( addUserAsGroupAdmin ) { autoAddResponse = await autoAddUsersAsAdmins ( getProp ( context , "groupId" ) , users , getProp ( context , "primaryRO" ) ) ; } else { autoAddResponse = await autoAddUsers ( getProp ( context , "groupId" ) , users , getProp ( context , "primaryRO" ) ) ; } if ( autoAddResponse . notAdded ) { notAdded = notAdded . concat ( autoAddResponse . notAdded ) ; } if ( autoAddResponse . errors ) { errors = errors . concat ( autoAddResponse . errors ) ; } if ( shouldEmail ) { emailResponse = await processEmailUsers ( context ) ; if ( emailResponse . errors && emailResponse . errors . length > 0 ) { errors = errors . concat ( emailResponse . errors ) ; } } return { users : users . map ( ( u ) => u . username ) , notAdded , errors , notEmailed : emailResponse ?. notEmailed || [ ] , notInvited : [ ] , } ; }