import { Component , OnInit } from '@angular/core' ; import { DateService } from '../../../../services/date.service' ; import { UserService } from 'carey-user' ; import { Deal } from '../../models/deal' ; import { DealCriteria } from '../../models/deal-criteria' ; import { DealService } from '../../service/deal.service' ; const maxResults = 6 ; const maxDays = 60 ; @ Component ( { selector : 'app-future-pipeline' , templateUrl : './future-pipeline.component.html' , styleUrls : [ './future-pipeline.component.css' ] } ) export class FuturePipelineComponent implements OnInit { options : any ; map : Map < string , number > = new Map < string , number > ( ) ; pageTitle : string = 'Upcoming Deal Closures' ; pageSubtitle : string = 'Next ' + maxDays + ' Days' ; constructor ( private dealService , private userService , private dateService ) { } ngOnInit ( ) { this . loadDeals ( ) ; } private getDealCriteria ( ) { let criteria = new DealCriteria ( ) ; criteria . userId = "6014081e221e1b534a8aa432" ; criteria . orderType = "ASC" ; criteria . maxResults = maxResults ; criteria . minDate = Date . now ( ) ; criteria . maxDate = this . dateService . getDaysForwardAsNumber ( maxDays ) ; return criteria ; } private loadDeals ( ) { let criteria = this . getDealCriteria ( ) ; this . dealService . fetchDealsByCriteria ( criteria ) . subscribe ( ( deals ) => this . handleDealsResponse ( deals ) , ( err ) => this . handleError ( err ) ) ; } private handleDealsResponse ( deals ) { console . log ( deals ) ; deals . forEach ( deal => { this . addDealValue ( deal ) ; } ) ; console . log ( this . map ) ; this . setOptions ( ) ; } private addDealValue ( deal ) { let date = this . dateService . getShortDateDisplay ( deal . expectedClosureDate ) ; let value = this . dealService . getAmount ( deal ) ; if ( this . map . get ( date ) ) { let currentVal = this . map . get ( date ) ; let newVal = currentVal + value ; this . map . set ( date , newVal ) ; } else { this . map . set ( date , value ) ; } } private setOptions ( ) { this . options = { grid : { left : '15%' } , xAxis : { type : 'category' , data : Array . from ( this . map . keys ( ) ) } , yAxis : { type : 'value' , axisLabel : { formatter : function ( value ) { return '$' + ( value / 1000 ) + 'K' ; } } } , series : [ { data : Array . from ( this . map . values ( ) ) , type : 'bar' } ] } ; } private handleError ( err ) { console . error ( err ) ; } }