From 934efde4c34df3381756b8024fecb5796d2525ed Mon Sep 17 00:00:00 2001 From: Nulo Date: Wed, 22 Sep 2021 21:17:35 -0300 Subject: [PATCH] =?UTF-8?q?Implementaci=C3=B3n=20inicial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.ts | 17 +++++++++ demo/index.html | 89 +++++++++++++++++++++++++++++++++++++++++++--- dummy.ts | 15 ++++++++ index.ts | 8 +++-- priceByCustomer.ts | 55 ++++++++++++++++++++++++++++ product.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 7 files changed, 265 insertions(+), 8 deletions(-) create mode 100644 common.ts create mode 100644 dummy.ts create mode 100644 priceByCustomer.ts create mode 100644 product.ts diff --git a/common.ts b/common.ts new file mode 100644 index 0000000..1fec076 --- /dev/null +++ b/common.ts @@ -0,0 +1,17 @@ +// const HOST = 'https://tiendas.axoft.com' +export const HOST = "http://sutty.vm:4001"; + +export interface Paging { + /** + * Si hay más páginas disponibles + */ + MoreData: boolean; + /** + * El número de la página. Empieza desde 1. + */ + PageNumber: number; + /** + * El tamaño de la página. Tiene un límite de 5000. + */ + PageSize: number; +} diff --git a/demo/index.html b/demo/index.html index 8620ab4..9beb92b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,8 +1,89 @@ - - + + hyperpop.js demo + + + + + +
+ +

+
+ +
+ +

+
+ +
+ +

+
diff --git a/dummy.ts b/dummy.ts new file mode 100644 index 0000000..7f1815b --- /dev/null +++ b/dummy.ts @@ -0,0 +1,15 @@ +import { HOST } from "./common.js"; + +export async function dummy(token: string) { + const res = await fetch(`${HOST}/api/Aperture/dummy`, { + method: "POST", + headers: { + accesstoken: token, + }, + }); + const json = await res.json(); + if (!json.isOk) { + console.error(json); + throw new Error("Tango: error desconocido"); + } +} diff --git a/index.ts b/index.ts index 8b8c604..0d6a80c 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,5 @@ -export function prueba() { - console.log("¡Hola desde hyperpop.js!"); -} +// Docs: https://github.com/TangoSoftware/ApiTiendas +export * from "./common.js"; +export * from "./dummy.js"; +export * from "./product.js"; +export * from "./priceByCustomer.js"; diff --git a/priceByCustomer.ts b/priceByCustomer.ts new file mode 100644 index 0000000..9672230 --- /dev/null +++ b/priceByCustomer.ts @@ -0,0 +1,55 @@ +import { HOST, Paging } from "./common.js"; + +export interface PreciosQuery { + paginacion?: { + /** + * El número de la página. Empieza desde 1. + */ + number: number; + /** + * El tamaño de la página. Tiene un límite de 5000. + */ + size: number; + }; +} + +export interface Precio { + SKUCode: string; + CustomerCode: string; + Price: number; + PriceListNumber: number; +} + +export interface PreciosResponse { + Paging: Paging; + Data: Precio[]; +} + +export async function getPricesByCustomer( + token: string, + options: PreciosQuery +): Promise { + let searchParams = new URLSearchParams(); + if (options.paginacion) { + searchParams.set("pageSize", options.paginacion.size.toString()); + searchParams.set("pageNumber", options.paginacion.number.toString()); + } else { + // El máximo, según lo que retorna en 'Paging' + searchParams.set("pageSize", "5000"); + searchParams.set("pageNumber", "1"); + } + const res = await fetch( + `${HOST}/api/Aperture/PriceByCustomer?${searchParams.toString()}`, + { + headers: { + accesstoken: token, + }, + } + ); + const json = await res.json(); + console.debug(json); + if (json.Message) { + throw new Error(`Tango: ${json.Message}`); + } + return json; +} diff --git a/product.ts b/product.ts new file mode 100644 index 0000000..16d0370 --- /dev/null +++ b/product.ts @@ -0,0 +1,86 @@ +import { HOST, Paging } from "./common.js"; + +export interface ProductosQuery { + paginacion?: { + /** + * El número de la página. Empieza desde 1. + */ + number: number; + /** + * El tamaño de la página. Tiene un límite de 5000. + */ + size: number; + }; + /** + * Decide si solo traer los productos habilitados. + */ + onlyEnabled?: boolean; +} + +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( + token: string, + options: ProductosQuery +): Promise { + let searchParams = new URLSearchParams(); + if (options.paginacion) { + searchParams.set("pageSize", options.paginacion.size.toString()); + searchParams.set("pageNumber", options.paginacion.number.toString()); + } else { + // El máximo, según lo que retorna en 'Paging' + searchParams.set("pageSize", "5000"); + searchParams.set("pageNumber", "1"); + } + if ("onlyEnabled" in options) { + searchParams.set("onlyEnabled", options.onlyEnabled ? "true" : "false"); + } + const res = await fetch( + `${HOST}/api/Aperture/Product?${searchParams.toString()}`, + { + headers: { + accesstoken: token, + }, + } + ); + const json: ProductosResponse = await res.json(); + console.debug(json); + return json; +} diff --git a/tsconfig.json b/tsconfig.json index 9c509d4..ce974a8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,9 @@ "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ - "target": "es5", + "target": "ES2015", "module": "es6", + "moduleResolution": "node", "outDir": "./build",