import { IValidatedEvent } from '../../../../plugins/event_log/server' ; import { getUrlPrefix } from '.' ; import { FtrProviderContext } from '../ftr_provider_context' ; interface GreaterThanEqualCondition { gte : number ; } interface EqualCondition { equal : number ; } function isEqualConsition ( condition ) { return Number . isInteger ( ( condition as EqualCondition ) . equal ) ; } interface GetEventLogParams { getService : FtrProviderContext [ 'getService' ] ; spaceId : string ; type : string ; id : string ; provider : string ; actions : Map < string , { gte : number ; } | { equal : number ; } > ; filter ? : string ; } export async function getEventLog ( params ) { const { getService , spaceId , type , id , provider , actions } = params ; const supertest = getService ( 'supertest' ) ; const spacePrefix = getUrlPrefix ( spaceId ) ; const url = `${ spacePrefix }/api/event_log/${ type }/${ id }/_find?per_page=5000${ params . filter ? `&filter=${ params . filter }` : '' }` ; const { body : result } = await supertest . get ( url ) . expect ( 200 ) ; if ( ! result . total ) { throw new Error ( 'no events found yet' ) ; } const events = ( result . data as IValidatedEvent [ ] ) . filter ( ( event ) => event ?. event ?. provider === provider ) . filter ( ( event ) => event ?. event ?. action ) . filter ( ( event ) => actions . has ( event ?. event ?. action ! ) ) ; const foundActions = events . map ( ( event ) => event ?. event ?. action ) . reduce ( ( actionsSum , action ) => { if ( action ) { actionsSum . set ( action , 1 + ( actionsSum . get ( action ) ?? 0 ) ) ; } return actionsSum ; } , new Map < string , number > ( ) ) ; for ( const [ action , condition ] of actions . entries ( ) ) { if ( ! ( foundActions . has ( action ) && ( isEqualConsition ( condition ) ? foundActions . get ( action ) ! === condition . equal : foundActions . get ( action ) ! >= condition . gte ) ) ) { throw new Error ( `insufficient events found with action "${ action }" (${ foundActions . get ( action ) ?? 0 } must be ${ isEqualConsition ( condition ) ? `equal to ${ condition . equal }` : `greater than or equal to ${ condition . gte }` })` ) ; } } return events ; }