import { IModel , IUpdateSiteOptions , deepSet , getProp , mergeObjects , serializeModel , getSiteById , } from "@esri/hub-common" ; import { SITE_UI_VERSION } from "./site-ui-version" ; import { _ensurePortalDomainKeyword } from "./_ensure-portal-domain-keyword" ; import { updateItem , IUpdateItemResponse } from "@esri/arcgis-rest-portal" ; param { Object } model param { IUpdateSiteOptions } updateSiteOptions export function updateSite ( model , updateSiteOptions ) { const allowList = updateSiteOptions . allowList || [ ] ; const { updateVersions = true } = updateSiteOptions ; deepSet ( model , "data.values.uiVersion" , SITE_UI_VERSION ) ; deepSet ( model , "data.values.updatedAt" , new Date ( ) . toISOString ( ) ) ; deepSet ( model , "data.values.updatedBy" , updateSiteOptions . authentication . username ) ; if ( allowList . length ) { allowList . push ( "data.values.updatedAt" ) ; allowList . push ( "data.values.updatedBy" ) ; if ( updateVersions ) { allowList . push ( "data.values.uiVersion" ) ; allowList . push ( "item.properties.schemaVersion" ) ; } } if ( updateSiteOptions . isPortal ) { model . item . typeKeywords = _ensurePortalDomainKeyword ( getProp ( model , "data.values.subdomain" ) , model . item . typeKeywords ) ; if ( allowList . length ) { allowList . push ( "item.typeKeywords" ) ; } } let agoModelPromise ; if ( allowList . length ) { agoModelPromise = getSiteById ( model . item . id , updateSiteOptions ) ; } else { agoModelPromise = Promise . resolve ( model ) ; } return agoModelPromise . then ( ( agoModel ) => { if ( allowList . length ) { model = mergeObjects ( model , agoModel , allowList ) ; } return updateItem ( { item : serializeModel ( model ) , authentication : updateSiteOptions . authentication , params : { clearEmptyFields : true } , } ) ; } ) . catch ( ( err ) => { throw Error ( `updateSite: Error updating site: ${ err }` ) ; } ) ; }