import { HttpClient } from '@angular/common/http'; import { inject, Injectable } from '@angular/core'; import { catchError, map, Observable, of } from 'rxjs'; import { Region } from '../../models/region'; import { State } from '../../models/state'; import { environment } from '../../../../environments/environment'; import { DeliveryType } from '../../models/delivery-type'; import { FeeType } from '../../models/fee-type'; import { TimeZone } from '../../models/timezone'; import { BondSurety } from '../../models/bond-surety'; import { CargoPolicy } from '../../models/cargo-policy'; import { CargoSurety } from '../../models/cargo-surety'; import { CarnetStatus } from '../../models/carnet-status'; import { Country } from '../../models/country'; import { UserService } from './user.service'; import { DeliveryMethod } from '../../models/delivery-method'; import { FormOfSecurity } from '../../models/formofsecurity'; import { PaymentType } from '../../models/payment-type'; import { UnitOfMeasure } from '../../models/unitofmeasure'; import { ExtensionReason } from '../../models/extension-reason'; @Injectable({ providedIn: 'root' }) export class CommonService { private apiUrl = environment.apiUrl; private apiDb = environment.apiDb; private http = inject(HttpClient); private userService = inject(UserService); getCountries(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=002&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getStates(country: string): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=001&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, countryValue: item.ADDLPARAMVALUE1, })).filter((state) => state.countryValue === country) // Filter by country value ), catchError((error) => { console.error('Error fetching states:', error); return of([]); }) ); } getRegions(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetRegions`).pipe( map((response) => response.map((item) => ({ id: item.REGIONID, region: item.REGION, regionname: item.REGIONNAME })) ) ); } getDeliveryTypes(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=006&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, daysToDelivery: item.ADDLPARAMVALUE1, cutOffTime: item.ADDLPARAMVALUE2, })) ) ); } getTimezones(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=010&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getFeeTypes(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=009&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getBondSuretys(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=003&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getCargoPolicies(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=004&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getCargoSuretys(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=005&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE })) ) ); } getCarnetStatuses(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=011&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, color: item.ADDLPARAMVALUE1, })) ) ); } getUnitOfMeasures(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=013&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, })) ) ); } getDeliveryMethods(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=007&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, })) ) ); } getPaymentTypes(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=008&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, })) ) ); } getFormOfSecurities(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=016&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, isGov: item.ADDLPARAMVALUE1 === 'GOV', })) ) ); } getExtensionReasons(): Observable { return this.http.get(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=008&P_SPID=${this.userService.getUserSpid()}`).pipe( map((response) => response.map((item) => ({ name: item.PARAMDESC, id: item.PARAMID, value: item.PARAMVALUE, })) ) ); } formatUSDate(datetime: Date): string { const date = new Date(datetime); const month = String(date.getUTCMonth() + 1).padStart(2, '0'); const day = String(date.getUTCDate()).padStart(2, '0'); const year = date.getUTCFullYear(); return `${month}/${day}/${year}`; } }