import { createGroup , protectGroup , searchGroups , removeGroup , unprotectGroup , IUserGroupOptions } from "@esri/arcgis-rest-portal" ; import { IRequestOptions } from "@esri/arcgis-rest-request" ; export param { string } name param { string } description param { * } options param { IRequestOptions } requestOptions returns { Promise < IInitiativeModel > } export function createInitiativeGroup ( name , description , options , requestOptions ) { const group = { title : name , description , access : "org" , sortField : "title" , sortOrder : "asc" } as any ; if ( options . isOpenData ) { group . isOpenData = true ; group . tags = [ "Hub Initiative Group" , "Open Data" ] ; group . access = "public" ; } if ( options . isSharedEditing ) { group . capabilities = "updateitemcontrol" ; group . _edit_privacy = "on" ; group . _edit_contributors = "on" ; group . tags = [ "Hub Initiative Group" , "initiativeCollaborationGroup" ] ; } const createOpts = { group , ... requestOptions } ; let groupId ; return createGroup ( createOpts ) . then ( ( result ) => { groupId = result . group . id ; const protectOpts = { id : groupId , ... requestOptions } as IUserGroupOptions ; return protectGroup ( protectOpts ) ; } ) . then ( ( ) => { return groupId ; } ) ; } export param { string } id param { IRequestOptions } requestOptions returns { Promise < any > } export function removeInitiativeGroup ( id , requestOptions ) { const opts = { id , ... requestOptions } as IUserGroupOptions ; return unprotectGroup ( opts ) . then ( ( ) => { return removeGroup ( opts ) ; } , ex => { if ( ex . messageCode === "COM_0003" && ex . code === 400 ) { return Promise . resolve ( { success : true } ) ; } else { throw ex ; } } ) ; } export param { string } title param { string } orgId param { IRequestOptions } requestOptions returns { Promise < boolean > } export function checkGroupExists ( title , orgId , requestOptions ) { const options = { q : `("${ title }") AND orgid: ${ orgId }` , ... requestOptions } ; return searchGroups ( options ) . then ( ( response ) => { const result = { exists : false } as any ; if ( response . total > 0 ) { result . exists = true ; result . group = response . results [ 0 ] ; } return result ; } ) ; } export param { string } title param { string } orgId param { number } step param { IRequestOptions } requestOptions returns { Promise < string > } export function getUniqueGroupName ( title , orgId , step , requestOptions ) { let proposedName = title ; if ( step ) { proposedName = `${ title } - ${ step }` ; } return checkGroupExists ( proposedName , orgId , requestOptions ) . then ( result => { if ( result . exists ) { step = step + 1 ; return getUniqueGroupName ( title , orgId , step , requestOptions ) ; } else { return proposedName ; } } ) ; } export function isSharedEditingGroup ( group ) { return ! ! ( group . capabilities . indexOf ( "updateitemcontrol" ) > - 1 ) ; }