import { Component , OnInit , ViewEncapsulation } from '@angular/core' ; import { switchMap } from 'rxjs/operators' ; import { ActivatedRoute , ParamMap , Router } from '@angular/router' ; import { AlertService } from 'carey-alert' ; import { HttpErrorResponse } from '@angular/common/http' ; import { Deal } from '../models/deal' ; import { BreadcrumbService } from '../../../ui/breadcrumb/breadcrumb.service' ; import { DealService } from '../service/deal.service' ; import { DateService } from '../../../services/date.service' ; import { DealStage } from '../models/deal-stage' ; import { ContactService } from '../../contacts/services/contact.service' ; import { ActivitySearchCriteria } from '../../activities/models/activity-search-criteria' ; @ Component ( { selector : 'app-view-deal' , encapsulation : ViewEncapsulation . None , templateUrl : './view-deal.component.html' , styleUrls : [ './view-deal.component.css' ] } ) export class ViewDealComponent implements OnInit { loading : boolean = true ; deal : Deal ; pageTitle : string = 'View Deal' ; dealAmount : number = 0 ; closureDate : string = '' ; prohibitedEdit : boolean = false ; saving : boolean = false ; wonButtonColor : string = 'green' ; lostButtonColor : string = 'red' ; recentActivitiesCriteria : ActivitySearchCriteria = new ActivitySearchCriteria ( ) ; recentActivitiesTitle : string = "Recent Activities" ; constructor ( private route , private alertService , private router , private dealService , private breadcrumbService , private dateService , private contactService ) { } ngOnInit ( ) { this . loadDeal ( ) ; } private setCriteria ( ) { this . setRecentActivitiesCriteria ( ) ; this . loading = false ; } private setRecentActivitiesCriteria ( ) { this . recentActivitiesCriteria . dealId = this . deal . id ; } private loadDeal ( ) { let deal$ = this . route . queryParamMap . pipe ( switchMap ( ( params ) => this . dealService . fetchDealById ( params . get ( 'dealId' ) ) ) ) ; deal$ . subscribe ( ( deal ) => this . handleDealResponse ( deal ) , err => this . handleDealError ( err ) ) ; } private handleDealResponse ( deal ) { this . deal = deal ; this . pageTitle = this . deal . name ; this . breadcrumbService . updateBreadcrumb ( 'View ' + this . pageTitle ) ; this . loadSupplementalData ( ) ; } private loadSupplementalData ( ) { this . closureDate = this . dateService . getShortDateDisplay ( this . deal . expectedClosureDate ) ; this . dealAmount = this . dealService . getAmount ( this . deal ) ; this . setCriteria ( ) ; } private handleDealError ( err ) { this . loading = false ; console . error ( err ) ; let alertMessage = 'Problem loading deal' ; if ( err instanceof HttpErrorResponse ) { if ( err . status ) { if ( err . status == 404 ) { alertMessage = 'Deal with that ID does not exist' ; } } } this . alertService . error ( alertMessage ) ; } editDeal ( ) { let route = '/deals/edit-deal' ; this . router . navigate ( [ route ] , { queryParams : { dealId : this . deal . id } } ) ; } saveDeal ( stage ) { this . saving = true ; this . dealService . fetchDealStagesBySalesType ( 'INBOUND' ) . subscribe ( ( stages ) => this . handleDealStagesResponse ( stages , stage ) , ( err ) => this . handleError ( err ) ) ; } private handleDealStagesResponse ( stages , newStageName ) { let newStage = stages . find ( st => st . name === newStageName ) ; if ( newStage ) { this . deal . stage = newStage ; this . dealService . updateDeal ( this . deal ) . subscribe ( ( deal ) => this . handleDealUpdateResponse ( deal ) , ( err ) => this . handleError ( err ) ) ; if ( this . deal . stage . name == 'Won' ) { this . contactService . convertContactToCustomer ( this . deal . contact . id ) ; } } else { this . alertService . error ( "Problem finding stage by name!" ) ; } } private handleDealUpdateResponse ( deal ) { this . saving = false ; } private handleError ( err ) { console . error ( err ) ; this . alertService . error ( "Something went wrong. Please call support." ) ; this . saving = false ; } }