ikea-availability-checker
Version:
ikea product in-store availability checker and product search
81 lines (80 loc) • 2.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.daysUntil = daysUntil;
exports.availabilityColor = availabilityColor;
exports.createStockInfoReportTable = createStockInfoReportTable;
const ansis_1 = require("ansis");
const cli_table3_1 = __importDefault(require("cli-table3"));
const ingka_1 = require("../../lib/ingka");
function daysUntil(to = new Date()) {
return Math.floor((to.getTime() - new Date().getTime()) / 60 / 60 / 24 / 1000);
}
function availabilityColor(val) {
if (val >= 5) {
return (0, ansis_1.green)(String(val));
}
else if (val >= 1) {
return (0, ansis_1.yellow)(String(val));
}
else {
return (0, ansis_1.red)(String(val || "0"));
}
}
function probabilityColor(val) {
switch (val) {
case ingka_1.PRODUCT_AVAILABILITY.HIGH_IN_STOCK:
return (0, ansis_1.green)(val);
case ingka_1.PRODUCT_AVAILABILITY.LOW_IN_STOCK:
return (0, ansis_1.yellow)(val);
case ingka_1.PRODUCT_AVAILABILITY.OUT_OF_STOCK:
return (0, ansis_1.red)(val);
default:
return (0, ansis_1.white)(String(val || ""));
}
}
function createStockInfoReportTable(data) {
const table = new cli_table3_1.default({
head: [
"date",
"product",
"countryCode",
"country",
"storeId (buCode)",
"store",
"stock",
"probability",
"restockDate",
],
colAligns: [
"left",
"left",
"left",
"left",
"left",
"left",
"left",
"right",
"right",
],
});
// transform the data to the array of objects
table.push(...data.map((stockInfo) => [
stockInfo.createdAt ? stockInfo.createdAt.toISOString() : "",
stockInfo.productId,
stockInfo.store.countryCode,
stockInfo.store.country,
stockInfo.store.buCode,
stockInfo.store.name,
availabilityColor(stockInfo.stock),
probabilityColor(stockInfo.probability),
stockInfo.restockDate
? `in ${daysUntil(stockInfo.restockDate)}d (${stockInfo.restockDate
.toISOString()
.substring(0, 10)})`
: "",
]));
return table;
}