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 { APViewModel } from "../Interfaces/APViewModel"; import { DyCommon } from "../Common"; @Injectable() export class APResolver 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): APViewModel | Observable | Promise { console.log(route); var promise = new Promise((fres, rej) => { if (route.params["id"] == undefined) { DyCommon.newGuid().then((id) => { var ap: APViewModel = { ap: { vendor: {}, paymentTerm: {}, id: id, accountsPayableAllocations: [] } }; // ContactResolver.setupContact(cont); fres(ap); }) } this.http.get(`/bbapi/object/ap/${route.params["id"]}`).takeUntil(this.ngUnsubscribe).subscribe(res => { var mod: APViewModel = res; APResolver.checkDefaults(mod); //ContactResolver.setupContact(mod); // res.contact.recordTypeId = res.contact.recordTypeId.split(","); fres(mod) }); }) return promise; } public static checkDefaults = (mod: APViewModel) => { if (mod.ap.vendor == undefined) { mod.ap.vendor = {}; } if (mod.ap.paymentTerm == undefined) { mod.ap.paymentTerm = {}; } if (mod.ap.accountsPayableAllocations == undefined) { mod.ap.accountsPayableAllocations = []; } mod.ap.accountsPayableAllocations.forEach((item) => { if (item.amount != undefined) { item.amount = item.amount.toString(); } if (item.location == null) { item.location = {}; } if (item.chartOfAccount == null) { item.chartOfAccount = {}; } }) } }