import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/Router" import { Injectable,Inject, OnDestroy } from "@angular/core"; import { HttpClient } from "@angular/Common/http"; import { Observable, Subject } from "rxjs"; import { ContactViewModel } from "../Interfaces/ContactViewModel"; import { DyCommon } from "../Common"; @Injectable() export class ContactResolver implements Resolve,OnDestroy{ private http: HttpClient; constructor(@Inject(HttpClient) _http: HttpClient) { this.http = _http; } private ngUnsubscribe: Subject = new Subject(); ngOnDestroy(): any { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); } resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): ContactViewModel | Observable | Promise { console.log(route); var promise = new Promise((fres, rej) => { if (route.params["id"] == undefined) { DyCommon.newGuid().then((id) => { var cont = { contact: { id: id, active: true, contactType:'Individual'} }; ContactResolver.setupContact(cont); fres(cont); }) } this.http.get(`bbapi/object/Contact/${route.params["id"]}`).takeUntil(this.ngUnsubscribe).subscribe(res => { var mod: ContactViewModel = res; ContactResolver.setupContact(mod); // res.contact.recordTypeId = res.contact.recordTypeId.split(","); fres(mod) }); }) return promise; } public static setupContact(cvm: ContactViewModel) { if (cvm.contact.recordTypeId == undefined) { cvm.contact.recordTypeId = ""; } if (cvm.employee == undefined) { cvm.employee = {contactId: cvm.contact.id}; } if (cvm.vendor == undefined) { cvm.vendor = { contactId: cvm.contact.id }; } if (cvm.salesAssociate == undefined) { cvm.salesAssociate = { contactId: cvm.contact.id }; } if (cvm.subContractor == undefined) { cvm.subContractor = { contactId: cvm.contact.id }; } if (cvm.contact.addresses == undefined) { cvm.contact.addresses = []; } if (cvm.contact.phones == undefined) { cvm.contact.phones = []; } } }