import { BoosterConfig , EventEnvelope , Logger , UUID } from '@boostercloud/framework-types' ; export type EventsStreamingCallback = ( entityName : string , entityID : UUID , eventEnvelopes : Array < EventEnvelope > , config : BoosterConfig ) => Promise < void > ; type EnvelopesPerEntity = Record < string , Array < EventEnvelope > > ; export class RawEventsParser { public static async streamPerEntityEvents ( logger , config , rawEvents , callbackFn ) { const eventEnvelopesPerEntity = config . provider . events . rawToEnvelopes ( rawEvents ) . filter ( isEventKind ) . reduce ( groupByEntity , { } ) ; for ( const entityEnvelopes of Object . values ( eventEnvelopesPerEntity ) ) { const { entityTypeName , entityID } = entityEnvelopes [ 0 ] ; logger . debug ( `[RawEventsParser#streamPerEntityEvents]: Streaming the following events for entity '${ entityTypeName }' and ID '${ entityID }':` , entityEnvelopes ) ; await callbackFn ( entityTypeName , entityID , entityEnvelopes , config ) ; } } } function isEventKind ( envelope ) { return envelope . kind == 'event' ; } function groupByEntity ( envelopesPerEntity , envelope ) { const entityKey = `${ envelope . entityTypeName }-${ envelope . entityID }` ; if ( ! envelopesPerEntity [ entityKey ] ) { envelopesPerEntity [ entityKey ] = [ ] ; } envelopesPerEntity [ entityKey ] . push ( envelope ) ; return envelopesPerEntity ; }