import { HttpClient , HttpErrorResponse , HttpResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Router } from '@angular/router' ; import { Observable , of , BehaviorSubject } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { environment } from '../../environments/environment' ; import { Event } from '../event' ; import { ScenarioEvent } from 'app/scenario-event' ; /**
 * Event service, gets event, updates products, makes http calls dealing with
 * the specific event
 */ @ Injectable ( ) export class EventService { event$ = new BehaviorSubject < Event > ( new Event ( null ) ) ; product$ = new BehaviorSubject < any > ( null ) ; productCode : string ; productSource : string ; productType : string ; constructor ( public http , public router ) { } param eventid getEvent ( eventid ) { const url = this . getEventDetailsUrl ( eventid ) ; if ( this . event$ . value . id !== eventid ) { this . setEvent ( new Event ( null ) ) ; } this . http . get < HttpResponse < any > > ( url ) . pipe ( catchError ( this . handleError ( eventid ) ) ) . subscribe ( response => { if ( response . type === 'Error' && response . status === 409 ) { this . getDeletedEvent ( eventid ) ; return ; } else if ( response . type === 'Error' && response . status === 404 ) { this . getUnknownEvent ( eventid ) ; return ; } if ( response ) { const isScenario = this . isScenarioEvent ( response ) ; if ( isScenario ) { this . setEvent ( new ScenarioEvent ( response ) ) ; } else { this . setEvent ( new Event ( response ) ) ; } } } ) ; } param eventid getEventDetailsUrl ( eventid ) { let url ; url = environment . scenario ? environment . SCENARIO_SERVICE . replace ( '{eventid}' , eventid ) : environment . EVENT_SERVICE . replace ( '{eventid}' , eventid ) ; return url ; } param type param source param code getProduct ( type , source ? , code ? ) { this . productType = type ; this . productSource = source ; this . productCode = code ; this . updateProduct ( ) ; } param eventid getUnknownEvent ( eventid ) { this . router . navigate ( [ 'unknown' ] , { skipLocationChange : true } ) ; } param event returns isScenarioEvent ( event ) { let hasScenario = false ; if ( event && event . properties && event . properties . products ) { Object . keys ( event . properties . products ) . some ( key => { if ( key . includes ( 'scenario' ) ) { hasScenario = true ; return true ; } } ) ; } return hasScenario ; } param eventid private getDeletedEvent ( eventid ) { const url = `${ environment . DELETED_EVENT_SERVICE }&eventid=${ eventid }` ; this . http . get < any > ( url ) . pipe ( catchError ( this . handleError ( eventid ) ) ) . subscribe ( response => { this . setEvent ( new Event ( response ) ) ; } ) ; } param eventid returns { any } private handleError ( eventid ) { return ( error ) : Observable < any > => { return of ( { id : eventid , message : error . message , status : error . status , type : 'Error' } ) ; } ; } param event private setEvent ( event ) { this . event$ . next ( event ) ; this . updateProduct ( ) ; } return private updateProduct ( ) { const event = this . event$ . value ; const product = event . getProduct ( this . productType , this . productSource , this . productCode ) ; event . product = product ; this . product$ . next ( product ) ; } }