import { Component, Inject, Input } from "@angular/core"; import { Http, Headers, Response, URLSearchParams, RequestOptions } from "@angular/http"; import {Observable} from "rxjs/Rx"; import "rxjs/add/operator/map"; import { MaterialType } from "./MaterialType"; import { UnitType, Unit } from "./Unit"; import { MinimalShippingUnit } from "./MinimalShippingUnit"; import { IAngularSettings, IComponentServiceSettings } from "./AngularSettings"; declare var AngularSettings: IAngularSettings; export class ComponentService { @Input() settings: IComponentServiceSettings; private getMaterialTypesUrl = ""; private http: Http; constructor(@Inject(Http) http: Http) { this.http = http; this.settings = AngularSettings.ComponentServiceSettings; } getMaterialTypes(unitId: number): Observable { const search = new URLSearchParams(); search.set("culture", this.settings.Culture); search.set("unitId", unitId.toString()); const options = new RequestOptions({ search: search, headers: this.headers }); return this.http.get(this.settings.UnitProfileServiceUrl + this.settings.GetMaterialTypesUrl, options) .map((res: Response) => res.json() as MaterialType[]); } getUnits(departmentId: number, types: UnitType[]): Observable { const search = new URLSearchParams(types.map(t => 'types=' + t).join('&')); search.set("departmentId", departmentId.toString()); const options = new RequestOptions({ search: search, headers: this.headers }); return this.http.get(this.settings.UnitProfileServiceUrl + this.settings.GetUnitsUrl, options) .map((res: Response) => res.json() as Unit[]); } getMinimalShippingUnits(unitId: number, materialTypeId: number, asOfUtc: string, unitOfMeasure: number): Observable { const search = new URLSearchParams(); search.set("unitId", unitId.toString()); search.set("materialTypeId", materialTypeId.toString()); search.set("asOfUtc", asOfUtc); search.set("unitOfMeasure", unitOfMeasure.toString()); search.set("culture", this.settings.Culture); const options = new RequestOptions({ search: search, headers: this.headers }); return this.http.get(this.settings.UnitProfileServiceUrl + this.settings.GetMinimalShippingUnitsUrl, options) .map((res: Response) => res.json() as MinimalShippingUnit[]); } get headers() { return new Headers({ 'Content-Type': "application/json;charset=utf-8", UserId: this.settings.Ticket.UserId, UserRole: this.settings.Ticket.UserRole, AuthToken: this.settings.Ticket.AuthToken, SiteId: this.settings.Ticket.SiteId }); } }