import { Injectable } from "@angular/core"; import { ShippingService } from "../services/shippingService"; import { Address, Api, IRslt, UPSAddress, UPSCreateShipment, UPSShippingRequest, UPSShippingResult, UPSTrackRslt, UPSValidationResult, Utility, RateResult, UPSRate, UPSRateDTO, UPSRateResultDTO } from "../../public-api"; import { ShippingDetail } from "../../tff-models/src/models/shippings/shipping.detail"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class ShippingState { shippingDetails:Array = [] redirectUrl: string = '' get upsUrl() { return 'https://wwwcie.ups.com/security/v1/oauth/authorize?client_id=jeeOOVXueSAy3nqc2BCIv2mBbBoXDXi1YuN7aDF8KeySTOiE&redirect_uri=' + this.redirectUrl + '&response_type=code' } hasToken:boolean = false; constructor(private readonly ups: ShippingService, private readonly utility:Utility, private readonly api:Api) { if (api.location === 'local') { this.redirectUrl = 'http://localhost:4200/shipping-auth' } else if (api.location = 'stage') { this.redirectUrl = 'https://stage.thefriendlyfacilitator.com/shipping-auth' } else if (api.location = 'prod') { this.redirectUrl = 'https://thefriendlyfacilitator.com/shipping-auth' } } getByID(ids: string[], done:any, err:any){ const self = this; this.ups.getByID(ids).subscribe({ next(n) { const rslt = n as IRslt; self.utility.update(self.shippingDetails, rslt.results) if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) // return this.api.post(this.route + '/details/ids',ids) } post(detail: ShippingDetail, done:any, err:any){ const self = this; this.ups.post(detail).subscribe({ next(n) { const rslt = n as IRslt; // self.utility.update(self.shippingDetails, rslt.results) if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) // return this.api.post(this.route + '/details', track) } put(detail: ShippingDetail, done:any, err:any){ const self = this; this.ups.put(detail).subscribe({ next(n) { const rslt = n as IRslt; // self.utility.update(self.shippingDetails, rslt.results) if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) // return this.api.put(this.route + '/details', track) } checkToken(done: any, err: any) { const self = this; return this.ups.checkToken().subscribe({ next(n) { self.hasToken = n.results; done(n.results) }, error(e) { err(e) }, complete() { } }) } createToken(code: string, done: any, err: any) { const self = this; return this.ups.getToken(code, this.redirectUrl).subscribe({ next(n) { self.hasToken = n.results; done(n.results) }, error(e) { err(e) }, complete() { } }) } convertAddressToUPS(model: Address): UPSAddress { const upsAddress = new UPSAddress(); upsAddress.AddressLine = [model.street]; upsAddress.City = model.city; upsAddress.StateProvinceCode = model.state; upsAddress.CountryCode = 'US'; upsAddress.PostalCode = model.zip; return upsAddress; } validateAddress(model: Address, done: any, err: any) { this.ups.validateAddress(model).subscribe({ next(n) { const rslt = n as IRslt; if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) } shipPackage(model: UPSCreateShipment, done: any, err: any) { this.ups.shipPackage(model).subscribe({ next(n) { const rslt = n as IRslt; if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) } getRate(model: UPSRateDTO, done: any, err: any) { this.ups.getRate(model).subscribe({ next(n) { const rslt = n as IRslt; if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) } getTracking(model: string, done: any, err: any) { this.ups.getTracking(model).subscribe({ next(n) { const rslt = n as IRslt; if (rslt) { done(rslt.results) } }, error(e) { err(e) }, complete() { } }) } }