import { IRequestOptions } from "@esri/arcgis-rest-request" ; import { removeInitiativeGroup } from "./groups" ; import { getInitiative } from "./get" ; import { detachSiteFromInitiative } from "./detach-site" ; import { getItem , removeItem , unprotectItem , getSelf , IUserItemOptions } from "@esri/arcgis-rest-portal" ; import { getProp , createId } from "@esri/hub-common" ; export param { string } id param { IRequestOptions } requestOptions returns { Promise < any > } export function removeInitiative ( id , requestOptions ) { const state = { id } as any ; const processId = createId ( "remove-" ) ; const startTS = new Date ( ) . getTime ( ) ; return Promise . all ( [ getInitiative ( id , requestOptions ) , getSelf ( requestOptions ) ] ) . then ( async ( results ) => { const model = results [ 0 ] ; const portal = results [ 1 ] ; const siteId = model . item . properties . siteId ; if ( siteId ) { try { await getItem ( siteId , requestOptions ) ; state . hasSite = true ; state . siteId = siteId ; } catch ( e ) { state . hasSite = false ; } } else { state . hasSite = false ; } state . initiativeOwner = model . item . owner ; state . collaborationGroupId = getProp ( portal , "properties.openData.settings.groupId" ) ; const prms = [ ] as any [ ] ; [ "collaborationGroupId" , "contentGroupId" , "followersGroupId" ] . forEach ( prop => { if ( model . item . properties [ prop ] ) { prms . push ( removeInitiativeGroup ( model . item . properties [ prop ] , requestOptions ) . catch ( ( ) => Promise . resolve ( true ) ) ) ; } } ) ; if ( model . item . protected ) { const opts = { id , ... requestOptions } as IUserItemOptions ; prms . push ( unprotectItem ( opts ) ) ; } return Promise . all ( prms ) ; } ) . then ( ( ) => { const prms = [ ] ; const opts = { id , owner : state . initiativeOwner , ... requestOptions } as IUserItemOptions ; prms . push ( removeItem ( opts ) ) ; if ( state . hasSite ) { prms . push ( detachSiteFromInitiative ( state . siteId , state . collaborationGroupId , requestOptions ) ) ; } return Promise . all ( prms ) ; } ) . then ( ( ) => { return { success : true } ; } ) ; }