getCustomers, getPrices y demo TypeScripteada

This commit is contained in:
Cat /dev/Nulo 2021-10-12 19:29:57 -03:00
parent c34dd2d9e5
commit 396a5a5880
5 changed files with 235 additions and 61 deletions

96
customer.ts Normal file
View file

@ -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<CustomerResponse> {
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;
}

76
demo/demo.ts Normal file
View file

@ -0,0 +1,76 @@
import {
dummy,
getProductos,
getPrices,
getPricesByCustomer,
getCustomers,
} from "../index.js";
const tokenInput = document.querySelector<HTMLInputElement>("#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<any>) {
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<any>) {
const formEl = document.querySelector<HTMLFormElement>(selector);
if (!formEl) throw new Error(`No existe el formulario ${selector}`);
const statusEl = formEl.querySelector<HTMLElement>(".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(), {}));

View file

@ -13,7 +13,12 @@
<input id="token-input" name="token" />
<form id="token">
<button>Probar</button>
<button>Probar token</button>
<p class="status"></p>
</form>
<form id="save-token">
<button>Guardar token</button>
<p class="status"></p>
</form>
@ -22,69 +27,19 @@
<p class="status"></p>
</form>
<form id="precios">
<form id="price">
<button>Conseguir precios</button>
<p class="status"></p>
</form>
<script type="module">
import { dummy, getProductos, getPricesByCustomer } from "../build/index.js";
<form id="price-by-customer">
<button>Conseguir precios by customer</button>
<p class="status"></p>
</form>
function token() {
return document.querySelector("#token-input").value;
}
<form id="customer">
<button>Conseguir clientes</button>
<p class="status"></p>
</form>
function clear(el) {
while (el.firstChild) {
el.firstChild.remove();
}
}
function objectToDom(object) {
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.entries(object).length > 0) {
codeEl.append(JSON.stringify(object, null, 2));
} else {
codeEl.append(object);
}
preEl.append(codeEl);
return preEl;
}
async function showResponse(statusEl, promise) {
clear(statusEl);
try {
const response = await promise;
statusEl.append("¡Funcionó!");
if (response) {
statusEl.append(" Respuesta:", objectToDom(response));
}
} catch (error) {
statusEl.append("Hubo un error :(", objectToDom(error));
}
}
const tokenForm = document.querySelector("#token");
tokenForm.addEventListener("submit", async (event) => {
event.preventDefault();
const statusEl = event.target.querySelector(".status");
showResponse(statusEl, dummy(token()));
});
const productosForm = document.querySelector("#productos");
productosForm.addEventListener("submit", async (event) => {
event.preventDefault();
const statusEl = event.target.querySelector(".status");
showResponse(statusEl, getProductos(token(), {}));
});
const preciosForm = document.querySelector("#precios");
preciosForm.addEventListener("submit", async (event) => {
event.preventDefault();
const statusEl = event.target.querySelector(".status");
showResponse(statusEl, getPricesByCustomer(token(), {}));
});
</script>
<script type="module" src="../build/demo/demo.js"></script>

View file

@ -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";

45
price.ts Normal file
View file

@ -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<PriceResponse> {
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;
}