import { RepoApi } from '../repo-api' ; import { Logger } from '@alfresco/adf-testing' ; import { Utils } from '../../../../utilities/utils' ; import { SharedlinksApi as AdfSharedlinksApi , SharedLinkEntry , SharedLinkPaging } from '@alfresco/js-api' ; export class SharedLinksApi extends RepoApi { sharedlinksApi = new AdfSharedlinksApi ( this . alfrescoJsApi ) ; constructor ( username ? , password ? ) { super ( username , password ) ; } async shareFileById ( id , expireDate ? ) { try { await this . apiAuth ( ) ; const data = { nodeId : id , expiresAt : expireDate } ; return await this . sharedlinksApi . createSharedLink ( data ) ; } catch ( error ) { this . handleError ( `SharedLinksApi shareFileById : catch : ` , error ) ; return null ; } } async shareFilesByIds ( ids ) { const sharedLinks = [ ] ; try { if ( ids && ids . length > 0 ) { for ( const id of ids ) { const sharedLink = await this . shareFileById ( id ) ; sharedLinks . push ( sharedLink ) ; } } } catch ( error ) { this . handleError ( `SharedLinksApi shareFilesByIds : catch : ` , error ) ; } return sharedLinks ; } async getSharedIdOfNode ( fileId ) { try { const sharedLinksEntries = ( await this . getSharedLinks ( ) ) ?. list . entries ; const found = sharedLinksEntries . find ( ( sharedLink ) => sharedLink . entry . nodeId === fileId ) ; return ( found || { entry : { id : null } } ) . entry . id ; } catch ( error ) { this . handleError ( `SharedLinksApi getSharedIdOfNode : catch : ` , error ) ; return null ; } } async unshareFileById ( fileId ) { try { const sharedId = await this . getSharedIdOfNode ( fileId ) ; return await this . sharedlinksApi . deleteSharedLink ( sharedId ) ; } catch ( error ) { this . handleError ( `SharedLinksApi unshareFileById : catch : ` , error ) ; } } async getSharedLinks ( maxItems = 250 ) { try { await this . apiAuth ( ) ; const opts = { maxItems } ; return await this . sharedlinksApi . listSharedLinks ( opts ) ; } catch ( error ) { this . handleError ( `SharedLinksApi getSharedLinks : catch : ` , error ) ; return null ; } } async getSharedLinksTotalItems ( ) { try { await this . apiAuth ( ) ; const opts = { maxItems : 250 } ; const sharedList = await this . sharedlinksApi . listSharedLinks ( opts ) ; return sharedList . list . entries . length ; } catch ( error ) { this . handleError ( `SharedLinksApi getSharedLinksTotalItems : catch : ` , error ) ; return - 1 ; } } async waitForApi ( data ) { try { const sharedFiles = async ( ) => { const totalItems = await this . getSharedLinksTotalItems ( ) ; if ( totalItems !== data . expect ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ; return await Utils . retryCall ( sharedFiles ) ; } catch ( error ) { Logger . error ( `SharedLinksApi waitForApi :  catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } async waitForFilesToBeShared ( filesIds ) { try { const sharedFile = async ( ) => { const sharedFiles = ( await this . getSharedLinks ( ) ) . list . entries . map ( ( link ) => link . entry . nodeId ) ; const foundItems = filesIds . every ( ( id ) => sharedFiles . includes ( id ) ) ; if ( foundItems ) { return Promise . resolve ( foundItems ) ; } else { return Promise . reject ( foundItems ) ; } } ; return await Utils . retryCall ( sharedFile ) ; } catch ( error ) { Logger . error ( `SharedLinksApi waitForFilesToBeShared :  catch : ${ error }` ) ; Logger . error ( `\tWait timeout reached waiting for files to be shared` ) ; } } async waitForFilesToNotBeShared ( filesIds ) { try { const sharedFile = async ( ) => { const sharedFiles = ( await this . getSharedLinks ( ) ) . list . entries . map ( ( link ) => link . entry . nodeId ) ; const foundItems = filesIds . some ( ( id ) => { return sharedFiles . includes ( id ) ; } ) ; if ( foundItems ) { return Promise . reject ( foundItems ) ; } else { return Promise . resolve ( foundItems ) ; } } ; return await Utils . retryCall ( sharedFile ) ; } catch ( error ) { Logger . error ( `SharedLinksApi waitForFilesToNotBeShared :  catch : ${ error }` ) ; Logger . error ( `\tWait timeout reached waiting for files to no longer be shared` ) ; } } }