import { RepoApi } from '../repo-api' ; import { Logger } from '@alfresco/adf-testing' ; import { Utils } from '../../../../utilities/utils' ; import { QueriesApi as AdfQueriesApi } from '@alfresco/js-api' ; export class QueriesApi extends RepoApi { queriesApi = new AdfQueriesApi ( this . alfrescoJsApi ) ; constructor ( username ? , password ? ) { super ( username , password ) ; } async findSites ( searchTerm ) { const data = { term : searchTerm , fields : [ 'title' ] } ; try { await this . apiAuth ( ) ; return this . queriesApi . findSites ( searchTerm , data ) ; } catch ( error ) { this . handleError ( `QueriesApi findSites : catch : ` , error ) ; return null ; } } async findSitesTotalItems ( searchTerm ) { try { return ( await this . findSites ( searchTerm ) ) . list . pagination . totalItems ; } catch ( error ) { this . handleError ( `QueriesApi findSitesTotalItems : catch :` , error ) ; return - 1 ; } } async findNodes ( searchTerm ) { const data = { term : searchTerm , fields : [ 'name' ] } ; try { await this . apiAuth ( ) ; return this . queriesApi . findNodes ( searchTerm , data ) ; } catch ( error ) { this . handleError ( `QueriesApi findNodes : catch : ` , error ) ; return null ; } } async waitForSites ( searchTerm , data ) { try { const sites = async ( ) => { const totalItems = await this . findSitesTotalItems ( searchTerm ) ; if ( totalItems !== data . expect ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ; return await Utils . retryCall ( sites ) ; } catch ( error ) { Logger . error ( `QueriesApi waitForSites : catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } async waitForFilesAndFolders ( searchTerm , data ) { try { const nodes = async ( ) => { const totalItems = ( await this . findNodes ( searchTerm ) ) . list . pagination . totalItems ; if ( totalItems !== data . expect ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ; return await Utils . retryCall ( nodes ) ; } catch ( error ) { Logger . error ( `QueriesApi waitForFilesAndFolders : catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } }