import { RepoApi } from '../repo-api' ; import { ApiUtil , Logger } from '@alfresco/adf-testing' ; import { Utils } from '../../../../utilities/utils' ; import { SearchApi as AdfSearchApi } from '@alfresco/js-api' ; export class SearchApi extends RepoApi { searchApi = new AdfSearchApi ( this . alfrescoJsApi ) ; constructor ( username ? , password ? ) { super ( username , password ) ; } async queryRecentFiles ( username ) { const data = { query : { query : '*' , language : 'afts' } , filterQueries : [ { query : `cm:modified:[NOW/DAY-30DAYS TO NOW/DAY+1DAY]` } , { query : `cm:modifier:${ username } OR cm:creator:${ username }` } , { query : `TYPE:"content" AND -TYPE:"app:filelink" AND -TYPE:"fm:post"` } ] } ; try { await this . apiAuth ( ) ; return this . searchApi . search ( data ) ; } catch ( error ) { this . handleError ( `SearchApi queryRecentFiles : catch : ` , error ) ; return null ; } } async getTotalItems ( username ) { try { return ( await this . queryRecentFiles ( username ) ) . list . pagination . totalItems ; } catch ( error ) { this . handleError ( `SearchApi getTotalItems : catch : ` , error ) ; return - 1 ; } } async queryNodesNames ( searchTerm ) { const data = { query : { query : `cm:name:\"${ searchTerm }*\"` , language : 'afts' } , filterQueries : [ { query : `+TYPE:'cm:folder' OR +TYPE:'cm:content'` } ] } ; try { await this . apiAuth ( ) ; return this . searchApi . search ( data ) ; } catch ( error ) { this . handleError ( `SearchApi queryNodesNames : catch : ` , error ) ; return null ; } } async getSearchByTermTotalItems ( searchTerm ) { try { return ( await this . queryNodesNames ( searchTerm ) ) . list . pagination . totalItems ; } catch ( error ) { this . handleError ( `SearchApi getSearchByTermTotalItems : catch : ` , error ) ; return - 1 ; } } async queryNodesExactNames ( searchTerm ) { const data = { query : { query : `cm:name:\"${ searchTerm }\"` , language : 'afts' } , filterQueries : [ { query : `+TYPE:'cm:folder' OR +TYPE:'cm:content'` } ] } ; try { await this . apiAuth ( ) ; return this . searchApi . search ( data ) ; } catch ( error ) { this . handleError ( `SearchApi queryNodesExactNames : catch : ` , error ) ; return null ; } } async waitForApi ( username , data ) { try { const recentFiles = async ( ) => { const totalItems = await this . getTotalItems ( username ) ; if ( totalItems !== data . expect ) { return Promise . reject ( totalItems ) ; } else { return Promise . resolve ( totalItems ) ; } } ; return await Utils . retryCall ( recentFiles ) ; } catch ( error ) { Logger . error ( `SearchApi waitForApi : catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } async waitForNodes ( searchTerm , data ) { const predicate = ( totalItems ) => totalItems === data . expect ; const apiCall = async ( ) => { try { return await this . getSearchByTermTotalItems ( searchTerm ) ; } catch ( error ) { return 0 ; } } ; try { await ApiUtil . waitForApi ( apiCall , predicate , 30 , 2500 ) ; } catch ( error ) { Logger . error ( `SearchApi waitForNodes : catch : ` ) ; Logger . error ( `\tExpected: ${ data . expect } items, but found ${ error }` ) ; } } }