import { IModel , IHubRequestOptions , serializeModel , cloneObject , interpolateItemId , uploadResourcesFromUrl , getProp } from "@esri/hub-common" ; import { ensureRequiredSiteProperties } from "./ensure-required-site-properties" ; import { createItem , protectItem , updateItem , shareItemWithGroup } from "@esri/arcgis-rest-portal" ; import { registerSiteAsApplication } from "./register-site-as-application" ; import { _addSiteDomains } from "./_add-site-domains" ; import { updateInitiativeSiteId } from "@esri/hub-initiatives" ; param { Object } model param { Object } options param { IHubRequestOptions } hubRequestOptions export function createSite ( model , options , hubRequestOptions ) { model = ensureRequiredSiteProperties ( model , hubRequestOptions . authentication . username , hubRequestOptions . isPortal ) ; return createItem ( { item : serializeModel ( model ) , owner : model . item . owner , authentication : hubRequestOptions . authentication } ) . then ( createResponse => { model . item . id = createResponse . id ; return protectItem ( { id : model . item . id , owner : model . item . owner , authentication : hubRequestOptions . authentication } ) ; } ) . then ( protectResponse => { return registerSiteAsApplication ( model , hubRequestOptions ) ; } ) . then ( appRegistrationResponse => { model . data . values . clientId = appRegistrationResponse . client_id ; const dcatConfig = cloneObject ( model . data . values . dcatConfig ) ; delete model . data . values . dcatConfig ; model = interpolateItemId ( model ) ; if ( dcatConfig ) { model . data . values . dcatConfig = dcatConfig ; } return updateItem ( { item : serializeModel ( model ) , authentication : hubRequestOptions . authentication } ) ; } ) . then ( updateResponse => { return _addSiteDomains ( model , hubRequestOptions ) ; } ) . then ( domainResponses => { return uploadResourcesFromUrl ( model , options . assets || [ ] , hubRequestOptions ) ; } ) . then ( uploadResponses => { let sharePrms = Promise . resolve ( { success : true } ) ; const collabGroupId = getProp ( model , "item.properties.collaborationGroupId" ) ; if ( collabGroupId ) { sharePrms = shareItemWithGroup ( { id : model . item . id , groupId : collabGroupId , authentication : hubRequestOptions . authentication , confirmItemControl : true } ) ; } return sharePrms ; } ) . then ( resp => { const initiativeItemId = getProp ( model , "item.properties.parentInitiativeId" ) ; if ( initiativeItemId ) { return updateInitiativeSiteId ( initiativeItemId , model . item . id , hubRequestOptions ) ; } else { return Promise . resolve ( true ) ; } } ) . then ( resp => { return model ; } ) . catch ( err => { throw Error ( `site-utils::createSite - Error creating site ${ JSON . stringify ( err ) }` ) ; } ) ; }