hyperpop.js/product.ts

79 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2021-11-15 21:57:36 +00:00
import { Paginacion, paginacionToSearchParams, Paging } from "./common.js";
2021-09-23 00:17:35 +00:00
export interface ProductosQuery {
2021-10-12 22:21:09 +00:00
paginacion?: Paginacion;
2021-09-23 00:17:35 +00:00
/**
* Decide si solo traer los productos habilitados.
*/
onlyEnabled?: boolean;
2021-11-01 20:46:53 +00:00
filter?: string;
2021-09-23 00:17:35 +00:00
}
export interface Producto {
SKUCode: string;
Description: string;
AdditionalDescription: string;
AlternativeCode: string;
BarCode: string;
Commission: number;
Discount: number;
MeasureUnitCode: string;
SalesMeasureUnitCode: string;
SalesEquivalence: number;
MaximumStock: number;
MinimumStock: number;
RestockPoint: number;
Observations: string;
Kit: boolean;
KitValidityDateSince?: string;
KitValidityDateUntil?: string;
UseScale: string;
Scale1: string;
Scale2: string;
BaseArticle: string;
ScaleValue1: string;
ScaleValue2: string;
DescriptionScale1: string;
DescriptionScale2: string;
DescriptionValueScale1: string;
DescriptionValueScale2: string;
Disabled: boolean;
ProductComposition: { ComponentSKUCode: string; Quantity: number }[];
ProductComments: { Line: number; Text: string }[];
}
export interface ProductosResponse {
Paging: Paging;
Data: Producto[];
}
// https://github.com/TangoSoftware/ApiTiendas#art%C3%ADculos
export async function getProductos(
2021-11-15 21:57:36 +00:00
host: string,
2021-09-23 00:17:35 +00:00
token: string,
options: ProductosQuery
): Promise<ProductosResponse> {
let searchParams = new URLSearchParams();
2021-10-12 22:21:09 +00:00
paginacionToSearchParams(options.paginacion, searchParams);
2021-09-23 00:17:35 +00:00
if ("onlyEnabled" in options) {
searchParams.set("onlyEnabled", options.onlyEnabled ? "true" : "false");
}
2021-11-01 20:46:53 +00:00
if (options.filter) {
searchParams.set("filter", options.filter);
}
2021-09-23 00:17:35 +00:00
const res = await fetch(
2021-11-15 21:57:36 +00:00
`${host}/api/Aperture/Product?${searchParams.toString()}`,
2021-09-23 00:17:35 +00:00
{
headers: {
accesstoken: token,
},
}
);
2021-11-01 20:47:07 +00:00
const json = await res.json();
2021-09-23 00:17:35 +00:00
console.debug(json);
2021-11-01 20:47:07 +00:00
if (json.Message) {
throw new Error(`Tango: ${json.Message}`);
}
2021-09-23 00:17:35 +00:00
return json;
}