'use strict' ; import BaseModel from '../bases/BaseModel' ; import { eventParser } from '../services' ; import { IEventsResult , IEventData } from 'teambition' ; class EventModel extends BaseModel { public getProjectEventsCollection ( projectId ) { return this . _get < IEventsResult > ( 'events' , projectId ) ; } public setProjectEventsCollection ( projectId , collection ) { let cache = this . getProjectEventsCollection ( projectId ) ; let result = this . prepareEvents ( projectId , collection ) ; if ( ! cache ) { this . _set ( 'events' , projectId , result ) ; } return result ; } public addEvent ( data ) { let collections = this . getProjectEventsCollection ( data . _projectId ) ; let startDate = ( typeof data . startDate === 'object' ) ? data . startDate . toString ( ) . substr ( 0 , 10 ) : data . startDate . substr ( 0 , 10 ) ; if ( collections ) { startDate = data . startDate . substr ( 0 , 10 ) ; let group = collections . data [ startDate ] ; if ( collections . index . indexOf ( data . _id ) === - 1 ) { if ( group ) { group . push ( data ) ; } else { collections . data [ startDate ] = [ data ] ; } collections . raw . push ( data ) ; collections . counter ++ ; collections . index . push ( data . _id ) ; } } } public removeEvent ( projectId , id ) { let $rawData = this . _get < IEventsResult > ( 'events' , projectId ) ; let data = this . _get < IEventData > ( 'event:detail' , id ) ; if ( $rawData ) { let $index = $rawData . index ; let position = $index . indexOf ( id ) ; if ( position !== - 1 ) { $rawData . counter -- ; let startDate = data . startDate . substr ( 0 , 10 ) ; let dataCollection = $rawData . data [ startDate ] ; angular . forEach ( dataCollection , ( event , index ) => { if ( event . _id === id ) { dataCollection . splice ( index , 1 ) ; } } ) ; } } } private prepareEvents ( _projectId , events ) { let results = < IEventsResult > { data : { } , raw : null , index : [ ] , counter : 0 } ; if ( events ) { let raw = [ ] ; let counter = 0 ; angular . forEach ( events , ( event , index ) => { let result = eventParser ( event ) ; let startDate = event . startDate . substr ( 0 , 10 ) ; let eventGroup = results . data [ startDate ] ; if ( eventGroup ) { eventGroup . push ( result ) ; } else { results . data [ startDate ] = [ ] ; results . data [ startDate ] . push ( result ) ; counter ++ ; } result . fetchTime = Date . now ( ) ; raw . push ( result ) ; results . index . push ( event . _id ) ; this . _set ( `event:detail` , event . _id , event ) ; } ) ; results . counter = counter ; results . raw = raw ; return results ; } else { return results ; } } } export default new EventModel ( ) ;