import { Logger } from '@alfresco/adf-testing' ; import { AlfrescoApi , Comment , CommentsApi , NodesApi , TrashcanApi , SitesApi , SharedlinksApi } from '@alfresco/js-api' ; import { browser } from 'protractor' ; import { Utils } from './utils' ; export class UserActions { protected readonly alfrescoApi : AlfrescoApi ; readonly commentsApi : CommentsApi ; readonly nodesApi : NodesApi ; readonly trashcanApi : TrashcanApi ; readonly sitesApi : SitesApi ; readonly sharedLinksApi : SharedlinksApi ; protected username : string ; protected password : string ; constructor ( ) { this . alfrescoApi = new AlfrescoApi ( ) ; this . alfrescoApi . setConfig ( browser . params . config ) ; this . commentsApi = new CommentsApi ( this . alfrescoApi ) ; this . nodesApi = new NodesApi ( this . alfrescoApi ) ; this . trashcanApi = new TrashcanApi ( this . alfrescoApi ) ; this . sitesApi = new SitesApi ( this . alfrescoApi ) ; this . sharedLinksApi = new SharedlinksApi ( this . alfrescoApi ) ; } async login ( username , password ) { this . username = username || this . username ; this . password = password || this . password ; try { return this . alfrescoApi . login ( this . username , this . password ) ; } catch ( error ) { this . handleError ( 'User Actions - login failed : ' , error ) ; } } async logout ( ) { try { await this . alfrescoApi . login ( this . username , this . password ) ; return this . alfrescoApi . logout ( ) ; } catch ( error ) { this . handleError ( 'User Actions - logout failed : ' , error ) ; } } async createComment ( nodeId , content ) { try { const comment = await this . commentsApi . createComment ( nodeId , { content } ) ; return comment ?. entry ; } catch ( error ) { this . handleError ( 'User Actions - createComment failed : ' , error ) ; return null ; } } param nodeIds param permanent async deleteNodes ( nodeIds , permanent = true ) { try { for ( const nodeId of nodeIds ) { await this . nodesApi . deleteNode ( nodeId , { permanent } ) ; } } catch ( error ) { this . handleError ( 'User Actions - deleteNodes failed : ' , error ) ; } } /**
     * Empties the trashcan. Uses multiple batches 1000 nodes each.
     */ async emptyTrashcan ( ) { try { const nodes = await this . trashcanApi . listDeletedNodes ( { maxItems : 1000 } ) ; if ( nodes ?. list ?. entries && nodes ?. list ?. entries ?. length > 0 ) { const ids = nodes . list . entries . map ( ( entries ) => entries . entry . id ) ; for ( const nodeId of ids ) { await this . trashcanApi . deleteDeletedNode ( nodeId ) ; } await this . emptyTrashcan ( ) ; } } catch ( error ) { this . handleError ( 'User Actions - emptyTrashcan failed : ' , error ) ; } } /**
     * Returns the amount of deleted nodes in the trashcan.
     * TODO: limited to 1000 items only, needs improvements.
     */ async getTrashcanSize ( ) { try { const response = await this . trashcanApi . listDeletedNodes ( { maxItems : 1000 } ) ; return response ?. list ?. pagination ?. totalItems || 0 ; } catch ( error ) { this . handleError ( 'User Actions - getTrashcanSize failed : ' , error ) ; return - 1 ; } } param expectedSize async waitForTrashcanSize ( expectedSize ) { try { return Utils . retryCall ( async ( ) => { const totalItems = await this . getTrashcanSize ( ) ; if ( totalItems !== expectedSize ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ) ; } catch ( error ) { this . handleError ( 'User Actions - waitForTrashcanSize failed : ' , error ) ; return - 1 ; } } param nodeIds async unlockNodes ( nodeIds ) { try { for ( const nodeId of nodeIds ) { await this . nodesApi . unlockNode ( nodeId ) ; } } catch ( error ) { this . handleError ( 'User Actions - unlockNodes failed : ' , error ) ; } } param siteIds param permanent async deleteSites ( siteIds , permanent = true ) { try { if ( siteIds && siteIds . length > 0 ) { for ( const siteId of siteIds ) { await this . sitesApi . deleteSite ( siteId , { permanent } ) ; } } } catch ( error ) { this . handleError ( 'User Actions - deleteSites failed : ' , error ) ; } } param nodeIds param expiresAt async shareNodes ( nodeIds , expiresAt ? ) { try { for ( const nodeId of nodeIds ) { await this . sharedLinksApi . createSharedLink ( { nodeId , expiresAt } ) ; } } catch ( error ) { this . handleError ( 'User Actions - shareNodes failed : ' , error ) ; } } protected handleError ( message , response ) { Logger . error ( `\n--- ${ message } error :` ) ; Logger . error ( '\t>>> username: ' , this . username ) ; Logger . error ( '\t>>> JSON: ' , JSON . stringify ( browser . params . config ) ) ; if ( response . status && response . response ) { try { Logger . error ( '\t>>> Status: ' , response . status ) ; Logger . error ( '\t>>> Text: ' , response . response . text ) ; Logger . error ( '\t>>> Method: ' , response . response . error . method ) ; Logger . error ( '\t>>> Path: ' , response . response . error . path ) ; } catch { Logger . error ( '\t>>> ' , response ) ; } } else Logger . error ( '\t>>> ' , response ) ; } }