client-app/src/app/core/services/common/common.service.ts

230 lines
7.4 KiB
TypeScript
Raw Normal View History

2025-07-02 22:40:35 -03:00
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 { UnitOfMeasure } from '../../models/unitofmeasure';
2025-07-14 09:59:26 -03:00
import { DeliveryMethod } from '../../models/delivery-method';
import { PaymentType } from '../../models/payment-type';
2025-07-28 13:56:04 -03:00
import { FormOfSecurity } from '../../models/formofsecurity';
2025-08-02 22:32:20 -03:00
import { UserService } from './user.service';
2025-08-16 20:37:38 -03:00
import { ExtensionReason } from '../../models/extension-reason';
2025-07-02 22:40:35 -03:00
@Injectable({
providedIn: 'root'
})
export class CommonService {
private apiUrl = environment.apiUrl;
private apiDb = environment.apiDb;
private http = inject(HttpClient);
2025-08-02 22:32:20 -03:00
private userService = inject(UserService);
2025-07-02 22:40:35 -03:00
2025-08-02 22:32:20 -03:00
getCountries(): Observable<Country[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=002&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getStates(country: string): Observable<State[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=001&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
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<Region[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetRegions`).pipe(
map((response) =>
response.map((item) => ({
id: item.REGIONID,
region: item.REGION,
regionname: item.REGIONNAME
}))
)
);
}
2025-08-02 22:32:20 -03:00
getDeliveryTypes(): Observable<DeliveryType[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=006&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
2025-07-15 22:24:24 -03:00
value: item.PARAMVALUE,
daysToDelivery: item.ADDLPARAMVALUE1,
cutOffTime: item.ADDLPARAMVALUE2,
2025-07-02 22:40:35 -03:00
}))
)
);
}
2025-08-02 22:32:20 -03:00
getTimezones(): Observable<TimeZone[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=010&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getFeeTypes(): Observable<FeeType[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=009&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getBondSuretys(): Observable<BondSurety[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=003&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getCargoPolicies(): Observable<CargoPolicy[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=004&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getCargoSuretys(): Observable<CargoSurety[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=005&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE
}))
)
);
}
2025-08-02 22:32:20 -03:00
getCarnetStatuses(): Observable<CarnetStatus[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=011&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-02 22:40:35 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE,
color: item.ADDLPARAMVALUE1,
}))
)
);
}
2025-08-02 22:32:20 -03:00
getUnitOfMeasures(): Observable<UnitOfMeasure[]> {
return this.http.get<any[]>(`${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,
2025-07-14 09:59:26 -03:00
}))
)
);
}
2025-08-02 22:32:20 -03:00
getDeliveryMethods(): Observable<DeliveryMethod[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=007&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-14 09:59:26 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE,
}))
)
);
}
2025-08-02 22:32:20 -03:00
getPaymentTypes(): Observable<PaymentType[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=008&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-14 09:59:26 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE,
}))
)
);
}
2025-08-02 22:32:20 -03:00
getFormOfSecurities(): Observable<FormOfSecurity[]> {
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetParamValues?P_PARAMTYPE=016&P_SPID=${this.userService.getUserSpid()}`).pipe(
2025-07-28 13:56:04 -03:00
map((response) =>
response.map((item) => ({
name: item.PARAMDESC,
id: item.PARAMID,
value: item.PARAMVALUE,
isGov: item.ADDLPARAMVALUE1 === 'GOV',
}))
)
);
}
2025-08-16 20:37:38 -03:00
getExtensionReasons(): Observable<ExtensionReason[]> {
return this.http.get<any[]>(`${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,
}))
)
);
}
2025-07-02 22:40:35 -03:00
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}`;
}
}