import { getModelFromOptions , IModel , includes , getProp , serializeModel , mapBy , ensureUniqueString , slugify , maybePush , failSafe , shareItemToGroups , deepSet } from "@esri/hub-common" ; import { updateItem , IItem } from "@esri/arcgis-rest-portal" ; import { UserSession } from "@esri/arcgis-rest-auth" ; import { isSite } from "./is-site" ; param { ILinkPageAndSiteRequestOptions } linkRequestOptions export function linkSiteAndPage ( linkRequestOptions ) { let shareGroups = [ ] ; const promises = [ ] ; let pageModel ; let siteModel ; const requestOptions = { authentication : linkRequestOptions . authentication } ; return Promise . all ( [ getModelFromOptions ( "page" , linkRequestOptions ) , getModelFromOptions ( "site" , linkRequestOptions ) ] ) . then ( models => { [ pageModel , siteModel ] = models ; if ( ! siteModel . isMissing && ! pageModel . isMissing ) { if ( ! isSite ( siteModel . item ) || ! includes ( [ "Hub Page" , "Site Page" ] , pageModel . item . type ) ) { return Promise . resolve ( [ ] ) ; } const siteEntry = { id : siteModel . item . id , title : siteModel . item . title } ; if ( ! getProp ( pageModel , "data.values.sites" ) ) { deepSet ( pageModel , "data.values.sites" , [ ] ) ; } const sites = getProp ( pageModel , "data.values.sites" ) ; const hasSiteAlready = includes ( sites . map ( ( p ) => p . id ) , siteEntry . id ) ; if ( ! hasSiteAlready ) { pageModel . data . values . sites . push ( siteEntry ) ; const opts = Object . assign ( { item : serializeModel ( pageModel ) } , requestOptions ) ; promises . push ( updateItem ( opts ) ) ; } const pageEntry = { id : pageModel . item . id , title : pageModel . item . title } ; if ( ! getProp ( siteModel , "data.values.pages" ) ) { deepSet ( siteModel , "data.values.pages" , [ ] ) ; } const pages = getProp ( siteModel , "data.values.pages" ) ; const hasPageAlready = includes ( pages . map ( ( p ) => p . id ) , pageEntry . id ) ; if ( ! hasPageAlready ) { const slugs = mapBy ( "slug" , pages ) ; pageEntry . slug = linkRequestOptions . pageSlug || ensureUniqueString ( slugs , slugify ( pageEntry . title ) ) ; siteModel . data . values . pages . push ( pageEntry ) ; const opts = Object . assign ( { item : serializeModel ( siteModel ) } , requestOptions ) ; promises . push ( updateItem ( opts ) ) ; } shareGroups = maybePush ( getProp ( siteModel , "item.properties.collaborationGroupId" ) , shareGroups ) ; shareGroups = maybePush ( getProp ( siteModel , "item.properties.contentGroupId" ) , shareGroups ) ; const failSafeShare = failSafe ( shareItemToGroups ) ; promises . push ( failSafeShare ( pageEntry . id , shareGroups , requestOptions ) ) ; return Promise . all ( promises ) ; } else { let msg = `The Page item (${ pageModel . item . id }) is inaccessible.` ; if ( siteModel . isMissing ) { if ( pageModel . isMissing ) { msg = `Both the Page item (${ pageModel . item . id }) and the Site item (${ siteModel . item . id }) are inaccssible` ; } else { msg = `The Site item (${ siteModel . item . id }) is inaccessible.` ; } } throw new Error ( `Linking Failed: ${ msg }` ) ; } } ) . then ( ( ) => { return { pageModel , siteModel } ; } ) . catch ( ( err ) => { throw Error ( `Error occured linking site ${ siteModel . item . id } with ${ pageModel . item . id }: ${ err }` ) ; } ) ; }