import { RepoApi } from '../repo-api' ; import { Logger } from '@alfresco/adf-testing' ; import { SiteBody , SiteMemberRoleBody , SiteMemberBody , SiteEntry , SiteMembershipRequestEntry , SitesApi as AdfSiteApi , SiteMemberEntry } from '@alfresco/js-api' ; import { SITE_VISIBILITY , SITE_ROLES } from '../../../../configs' ; import { Utils } from '../../../../utilities/utils' ; export class SitesApi extends RepoApi { sitesApi = new AdfSiteApi ( this . alfrescoJsApi ) ; constructor ( username ? , password ? ) { super ( username , password ) ; } async getSite ( siteId ) { try { await this . apiAuth ( ) ; return await this . sitesApi . getSite ( siteId ) ; } catch ( error ) { this . handleError ( `SitesApi getSite : catch : ` , error ) ; return null ; } } async getSites ( ) { try { await this . apiAuth ( ) ; return await this . sitesApi . listSiteMembershipsForPerson ( this . username ) ; } catch ( error ) { this . handleError ( `SitesApi getSites : catch : ` , error ) ; return null ; } } async getSitesTotalItems ( ) { try { await this . apiAuth ( ) ; return ( await this . sitesApi . listSiteMembershipsForPerson ( this . username ) ) . list . pagination . totalItems ; } catch ( error ) { this . handleError ( `SitesApi getSitesTotalItems : catch : ` , error ) ; return - 1 ; } } async getDocLibId ( siteId ) { try { await this . apiAuth ( ) ; return ( await this . sitesApi . listSiteContainers ( siteId ) ) . list . entries [ 0 ] . entry . id ; } catch ( error ) { this . handleError ( `SitesApi getDocLibId : catch : ` , error ) ; return null ; } } async getVisibility ( siteId ) { try { const site = await this . getSite ( siteId ) ; return site . entry . visibility ; } catch ( error ) { this . handleError ( `SitesApi getVisibility : catch : ` , error ) ; return null ; } } async getDescription ( siteId ) { try { const site = await this . getSite ( siteId ) ; return site . entry . description ; } catch ( error ) { this . handleError ( `SitesApi getDescription : catch : ` , error ) ; return null ; } } async getTitle ( siteId ) { try { const site = await this . getSite ( siteId ) ; return site . entry . title ; } catch ( error ) { this . handleError ( `SitesApi getTitle : catch : ` , error ) ; return null ; } } async createSite ( title , visibility ? , description ? , siteId ? ) { const site = { title , visibility : visibility || SITE_VISIBILITY . PUBLIC , description : description , id : siteId || title } as SiteBody ; try { await this . apiAuth ( ) ; return await this . sitesApi . createSite ( site ) ; } catch ( error ) { this . handleError ( `SitesApi createSite : catch : ` , error ) ; return null ; } } async createSitePrivate ( title , description ? , siteId ? ) { return this . createSite ( title , SITE_VISIBILITY . PRIVATE , description , siteId ) ; } async createSiteModerated ( title , description ? , siteId ? ) { return this . createSite ( title , SITE_VISIBILITY . MODERATED , description , siteId ) ; } async createSites ( siteNames , visibility ? ) { const sites = [ ] ; try { if ( siteNames && siteNames . length > 0 ) { for ( const siteName of siteNames ) { const site = await this . createSite ( siteName , visibility ) ; sites . push ( site ) ; } } } catch ( error ) { this . handleError ( `SitesApi createSites : catch : ` , error ) ; } return sites ; } async createSitesPrivate ( siteNames ) { return this . createSites ( siteNames , SITE_VISIBILITY . PRIVATE ) ; } async deleteSite ( siteId , permanent = true ) { try { await this . apiAuth ( ) ; return await this . sitesApi . deleteSite ( siteId , { permanent } ) ; } catch ( error ) { this . handleError ( `SitesApi deleteSite : catch : ` , error ) ; } } async deleteSites ( siteIds , permanent = true ) { try { if ( siteIds && siteIds . length > 0 ) { await this . apiAuth ( ) ; for ( const siteId of siteIds ) { await this . sitesApi . deleteSite ( siteId , { permanent } ) ; } } } catch ( error ) { this . handleError ( `SitesApi deleteSites : catch : ` , error ) ; } } async deleteAllUserSites ( permanent = true ) { try { const siteIds = ( await this . getSites ( ) ) . list . entries . map ( ( entries ) => entries . entry . id ) ; return await siteIds . reduce ( async ( previous , current ) => { await previous ; return this . deleteSite ( current , permanent ) ; } , Promise . resolve ( ) ) ; } catch ( error ) { this . handleError ( `SitesApi deleteAllUserSites : catch : ` , error ) ; } } async updateSiteMember ( siteId , userId , role ) { const siteRole = { role : role } as SiteMemberRoleBody ; try { await this . apiAuth ( ) ; return await this . sitesApi . updateSiteMembership ( siteId , userId , siteRole ) ; } catch ( error ) { this . handleError ( `SitesApi updateSiteMember : catch : ` , error ) ; return null ; } } async addSiteMember ( siteId , userId , role ) { const memberBody = { id : userId , role : role } as SiteMemberBody ; try { await this . apiAuth ( ) ; return await this . sitesApi . createSiteMembership ( siteId , memberBody ) ; } catch ( error ) { if ( error . status === 409 ) { return this . updateSiteMember ( siteId , userId , role ) ; } else { this . handleError ( `SitesApi addSiteMember : catch : ` , error ) ; return null ; } } } async addSiteConsumer ( siteId , userId ) { return this . addSiteMember ( siteId , userId , SITE_ROLES . SITE_CONSUMER . ROLE ) ; } async addSiteContributor ( siteId , userId ) { return this . addSiteMember ( siteId , userId , SITE_ROLES . SITE_CONTRIBUTOR . ROLE ) ; } async addSiteCollaborator ( siteId , userId ) { return this . addSiteMember ( siteId , userId , SITE_ROLES . SITE_COLLABORATOR . ROLE ) ; } async addSiteManager ( siteId , userId ) { return this . addSiteMember ( siteId , userId , SITE_ROLES . SITE_MANAGER . ROLE ) ; } async deleteSiteMember ( siteId , userId ) { try { await this . apiAuth ( ) ; return await this . sitesApi . deleteSiteMembership ( siteId , userId ) ; } catch ( error ) { this . handleError ( `SitesApi deleteSiteMember : catch : ` , error ) ; } } async requestToJoin ( siteId ) { const body = { id : siteId } ; try { await this . apiAuth ( ) ; return await this . sitesApi . createSiteMembershipRequestForPerson ( '-me-' , body ) ; } catch ( error ) { this . handleError ( `SitesApi requestToJoin : catch : ` , error ) ; return null ; } } async hasMembershipRequest ( siteId ) { try { await this . apiAuth ( ) ; const requests = ( await this . sitesApi . getSiteMembershipRequests ( '-me-' ) ) . list . entries . map ( ( e ) => e . entry . id ) ; return requests . includes ( siteId ) ; } catch ( error ) { this . handleError ( `SitesApi hasMembershipRequest : catch : ` , error ) ; return null ; } } async waitForSitesToBeCreated ( sitesIds ) { try { const site = async ( ) => { const sitesList = ( await this . getSites ( ) ) . list . entries . map ( ( link ) => link . entry . id ) ; const foundItems = sitesIds . every ( ( id ) => sitesList . includes ( id ) ) ; if ( foundItems ) { return Promise . resolve ( foundItems ) ; } else { return Promise . reject ( foundItems ) ; } } ; return await Utils . retryCall ( site ) ; } catch ( error ) { Logger . error ( `SitesApi waitForSitesToBeCreated :  catch : ${ error }` ) ; Logger . error ( `\tWait timeout reached waiting for sites to be created` ) ; } } async waitForApi ( data ) { try { const sites = async ( ) => { const totalItems = await this . getSitesTotalItems ( ) ; if ( totalItems !== data . expect ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ; return await Utils . retryCall ( sites ) ; } catch ( error ) { Logger . error ( `SitesApi waitForApi : catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } }