hyperpop.js/priceByCustomer.ts

60 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-10-12 22:21:09 +00:00
import {
QueryCustomer,
queryCustomerToSearchParams,
2021-10-12 22:21:09 +00:00
Paginacion,
paginacionToSearchParams,
Paging,
} from "./common.js";
2021-09-23 00:17:35 +00:00
2021-10-12 22:21:09 +00:00
export interface PriceByCustomerQuery {
paginacion?: Paginacion;
2021-10-12 22:57:24 +00:00
/// Filtro por el código del cliente ("Code" en Customer.)
filter?: string;
2021-11-01 20:48:57 +00:00
SKUCode?: string;
customer?: QueryCustomer;
2021-09-23 00:17:35 +00:00
}
export interface Precio {
SKUCode: string;
CustomerCode: string;
Price: number;
PriceListNumber: number;
}
export interface PreciosResponse {
Paging: Paging;
Data: Precio[];
}
export async function getPricesByCustomer(
2021-11-15 21:57:36 +00:00
host: string,
2021-09-23 00:17:35 +00:00
token: string,
2021-10-12 22:21:09 +00:00
options: PriceByCustomerQuery
2021-09-23 00:17:35 +00:00
): Promise<PreciosResponse> {
let searchParams = new URLSearchParams();
2021-10-12 22:21:09 +00:00
paginacionToSearchParams(options.paginacion, searchParams);
2021-10-12 22:57:24 +00:00
if (options.filter) {
searchParams.set("filter", options.filter);
}
2021-11-01 20:48:57 +00:00
if (options.SKUCode) {
searchParams.set("SKUCode", options.SKUCode);
}
if (options.customer) {
queryCustomerToSearchParams(options.customer, searchParams);
}
2021-09-23 00:17:35 +00:00
const res = await fetch(
2021-11-15 21:57:36 +00:00
`${host}/api/Aperture/PriceByCustomer?${searchParams.toString()}`,
2021-09-23 00:17:35 +00:00
{
headers: {
accesstoken: token,
},
}
);
const json = await res.json();
console.debug(json);
if (json.Message) {
throw new Error(`Tango: ${json.Message}`);
}
return json;
}