diff --git a/customer.ts b/customer.ts new file mode 100644 index 0000000..6d7fd9d --- /dev/null +++ b/customer.ts @@ -0,0 +1,96 @@ +import { + HOST, + Paginacion, + paginacionToSearchParams, + Paging, +} from "./common.js"; + +export interface CustomerQuery { + paginacion?: Paginacion; +} + +export interface Customer { + Code: string; + BusinessName: string; + TradeName: string; + Address: string; + PostalCode: string; + City: string; + ProvinceCode: string; + TradeAddress: string; + PhoneNumbers: string; + Email: string; + MobilePhoneNumber: string; + WebPage: string; + IvaCategoryCode: string; + DocumentType: string; + DocumentNumber: string; + PriceListNumber: number; + Discount: number; + Observations: string; + DisabledDate: string; + SellerCode: string; + CreditQuota: number; + LocalAccountBalance: number; + ForeignAccountBalance: number; + ForeignCurrencyClause: boolean; + CreditQuotaCurrencyCode: string; + UpdateDatetime?: null; + LastUpdateUtc?: string; + ShippingAddresses: ShippingAddress[]; + CustomerComments: CustomerComment[]; + SaleConditionCode: number; + TransportCode: string; +} + +export interface CustomerComment { + Line: number; + Text: string; +} + +export interface ShippingAddress { + Code: string; + Address: string; + ProvinceCode: string; + City: string; + PostalCode: string; + PhoneNumber1: string; + PhoneNumber2: string; + DefaultAddress: string; + Enabled: string; + DeliveryHours: string; + DeliversMonday: string; + DeliversTuesday: string; + DeliversWednesday: string; + DeliversThursday: string; + DeliversFriday: string; + DeliversSaturday: string; + DeliversSunday: string; +} + +export interface CustomerResponse { + Paging: Paging; + Data: Customer[]; +} + +export async function getCustomers( + token: string, + options: CustomerQuery +): Promise { + let searchParams = new URLSearchParams(); + paginacionToSearchParams(options.paginacion, searchParams); + const res = await fetch( + `${HOST}/api/Aperture/Customer?${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/demo/demo.ts b/demo/demo.ts new file mode 100644 index 0000000..02de6a3 --- /dev/null +++ b/demo/demo.ts @@ -0,0 +1,76 @@ +import { + dummy, + getProductos, + getPrices, + getPricesByCustomer, + getCustomers, +} from "../index.js"; + +const tokenInput = document.querySelector("#token-input")!; + +if (localStorage.token) { + tokenInput.value = localStorage.token; +} + +function token() { + return tokenInput.value; +} + +function clear(el: Node) { + while (el.firstChild) { + el.firstChild.remove(); + } +} + +function objectToDom(object: any) { + const preEl = document.createElement("pre"); + const codeEl = document.createElement("code"); + if (typeof object === "string") { + codeEl.append(JSON.stringify(JSON.parse(object), null, 2)); + } else if (Object.keys(object).length > 0) { + codeEl.append(JSON.stringify(object, null, 2)); + } else { + codeEl.append(object); + } + preEl.append(codeEl); + return preEl; +} + +async function showResponse(statusEl: Element | null, promise: Promise) { + if (statusEl) clear(statusEl); + try { + const response = await promise; + if (statusEl) { + statusEl.append("¡Funcionó!"); + if (response) { + statusEl.append(" Respuesta:", objectToDom(response)); + } + } else alert(`¡Funcionó!${response ? ` Respuesta: ${response}` : ""}`); + } catch (error) { + if (statusEl) statusEl.append("Hubo un error :(", objectToDom(error)); + else alert(`Hubo un error: ${error}`); + } +} + +function setupForm(selector: string, listener: () => Promise) { + const formEl = document.querySelector(selector); + if (!formEl) throw new Error(`No existe el formulario ${selector}`); + const statusEl = formEl.querySelector(".status"); + if (!statusEl) + console.error( + `El formulario ${selector} no tiene un .status, voy a reportar los errores con alert()` + ); + formEl.addEventListener("submit", async (event) => { + event.preventDefault(); + showResponse(statusEl, listener()); + }); +} + +setupForm("#token", () => dummy(token())); +setupForm("#save-token", async () => { + localStorage.token = token(); +}); +setupForm("#productos", () => getProductos(token(), {})); +setupForm("#price", () => getPrices(token(), {})); +setupForm("#price-by-customer", () => getPricesByCustomer(token(), {})); +setupForm("#customer", () => getCustomers(token(), {})); diff --git a/demo/index.html b/demo/index.html index 7300564..3d10c88 100644 --- a/demo/index.html +++ b/demo/index.html @@ -13,7 +13,12 @@
- + +

+
+ +
+

@@ -22,69 +27,19 @@

-
+

- + diff --git a/index.ts b/index.ts index 0d6a80c..2bcca5b 100644 --- a/index.ts +++ b/index.ts @@ -2,4 +2,6 @@ export * from "./common.js"; export * from "./dummy.js"; export * from "./product.js"; +export * from "./price.js"; export * from "./priceByCustomer.js"; +export * from "./customer.js"; diff --git a/price.ts b/price.ts new file mode 100644 index 0000000..4f85c8d --- /dev/null +++ b/price.ts @@ -0,0 +1,45 @@ +import { + HOST, + Paginacion, + paginacionToSearchParams, + Paging, +} from "./common.js"; + +export interface PriceQuery { + paginacion?: Paginacion; +} + +export interface Price { + PriceListNumber: number; + SKUCode: string; + Price: number; + ValidityDateSince: string; + ValidityDateUntil: string; +} + +export interface PriceResponse { + Paging: Paging; + Data: Price[]; +} + +export async function getPrices( + token: string, + options: PriceQuery +): Promise { + let searchParams = new URLSearchParams(); + paginacionToSearchParams(options.paginacion, searchParams); + const res = await fetch( + `${HOST}/api/Aperture/Price?${searchParams.toString()}`, + { + headers: { + accesstoken: token, + }, + } + ); + const json = await res.json(); + console.debug(json); + if (json.Message) { + throw new Error(`Tango: ${json.Message}`); + } + return json; +}