import { Event } from '../event.model' ; import { SubFilter } from '../filters/subFilter.model' ; import { AmountFilter } from '../filters/amountFilter.model' ; export class Filter { isActive : boolean ; bumpIsActive : boolean ; bumpThreshold : number ; subscriptionFilter : SubFilter ; amountFilter : AmountFilter ; constructor ( ) { this . isActive = true ; this . bumpIsActive = false ; this . bumpThreshold = 300000 ; this . subscriptionFilter = null ; this . amountFilter = null ; } public runFilters ( eventList ) { let filteredEvents = eventList ; if ( this . isActive ) { if ( this . bumpIsActive ) { filteredEvents = this . bumpToTopByTime ( filteredEvents ) ; } if ( this . subscriptionFilter !== null ) { if ( this . subscriptionFilter . filterByMonths ) { filteredEvents = this . subscriptionFilter . byMonths ( filteredEvents ) ; } if ( this . subscriptionFilter . filterBySubPlan ) { filteredEvents = this . subscriptionFilter . bySubPlan ( filteredEvents ) ; } } if ( this . amountFilter !== null ) { if ( this . amountFilter . filterByAmount ) { filteredEvents = this . amountFilter . byAmount ( filteredEvents ) ; } } } filteredEvents . forEach ( ( event , index ) => { filteredEvents [ index ] . duration = event . getDuration ( ) ; } ) ; return filteredEvents ; } public enableAllFilters ( ) { if ( this . subscriptionFilter !== null ) { this . subscriptionFilter . filterByMonths = true ; this . subscriptionFilter . filterBySubPlan = true ; } if ( this . amountFilter !== null ) { this . amountFilter . filterByAmount = true ; } } private bumpToTopByTime ( eventList ) { const tempList = Array < Event > ( ) ; eventList . forEach ( ( event , index ) => { if ( ! event . didRead ) { const currentMs = new Date ( ) . getTime ( ) ; const eventMs = new Date ( event . timestamp ) . getTime ( ) ; const difference = currentMs - eventMs ; if ( difference >= this . bumpThreshold && ! event . didBump ) { tempList . unshift ( event ) ; event . didBump = true ; } else { tempList . push ( event ) ; } } else { tempList . push ( event ) ; } } ) ; return tempList ; } }