fallback argument in convertUnit

Fixes #70
This commit is contained in:
Matthias Hochsteger 2023-06-05 14:19:38 +02:00
parent e714d720b0
commit 505b3c75b3
4 changed files with 22 additions and 16 deletions

View File

@ -200,6 +200,7 @@ exports.updateData = async function (dataDir, done) {
try { try {
const storeItems = await stores[store].fetchData(); const storeItems = await stores[store].fetchData();
writeJSON(`${dataDir}/${store}-${today}.json`, storeItems, FILE_COMPRESSOR); writeJSON(`${dataDir}/${store}-${today}.json`, storeItems, FILE_COMPRESSOR);
// const storeItems = readJSON(`${dataDir}/${store}-${today}.json.${FILE_COMPRESSOR}`);
const storeItemsCanonical = getCanonicalFor(store, storeItems, today); const storeItemsCanonical = getCanonicalFor(store, storeItems, today);
console.log(`Fetched ${store.toUpperCase()} data, took ${(performance.now() - start) / 1000} seconds`); console.log(`Fetched ${store.toUpperCase()} data, took ${(performance.now() - start) / 1000} seconds`);
resolve(storeItemsCanonical); resolve(storeItemsCanonical);

View File

@ -12,11 +12,6 @@ exports.getCanonical = function (item, today) {
// try to read quantity and unit from product name // try to read quantity and unit from product name
const name = item.ProductName; const name = item.ProductName;
let [quantity, unit] = utils.parseUnitAndQuantityAtEnd(name); let [quantity, unit] = utils.parseUnitAndQuantityAtEnd(name);
if (units[unit] === undefined) {
// fallback: use given quantity and unit (including packaging)
quantity = item.Unit;
unit = item.UnitType;
}
return utils.convertUnit( return utils.convertUnit(
{ {
@ -31,7 +26,11 @@ exports.getCanonical = function (item, today) {
url: `${item.CategorySEOName}/${item.SEOName}`, url: `${item.CategorySEOName}/${item.SEOName}`,
}, },
units, units,
"hofer" "hofer",
{
quantity: item.Unit,
unit: item.UnitType,
}
); );
}; };

View File

@ -28,13 +28,13 @@ exports.getCanonical = function (item, today) {
quantity = q[0]; quantity = q[0];
unit = q[1]; unit = q[1];
} }
if (!(unit in units) && !(unit in utils.globalUnits)) {
// use price per unit to calculate quantity (less accurate) let [unitPrice, unit_] = item.masterValues["price-per-unit"].split("/");
let [unitPrice, unit_] = item.masterValues["price-per-unit"].split("/"); unitPrice = parseFloat(unitPrice.replace("€", ""));
unitPrice = parseFloat(unitPrice.replace("€", "")); const fallback = {
quantity = parseFloat((price / unitPrice).toFixed(3)); quantity: parseFloat((price / unitPrice).toFixed(3)),
unit = unit_.toLowerCase(); unit: unit_.toLowerCase(),
} };
return utils.convertUnit( return utils.convertUnit(
{ {
@ -50,7 +50,8 @@ exports.getCanonical = function (item, today) {
url: item.masterValues.url, url: item.masterValues.url,
}, },
units, units,
"spar" "spar",
fallback
); );
}; };

View File

@ -24,7 +24,7 @@ exports.globalUnits = {
wg: { unit: "wg", factor: 1 }, wg: { unit: "wg", factor: 1 },
}; };
exports.convertUnit = function (item, units, store) { exports.convertUnit = function (item, units, store, fallback) {
if (typeof item.quantity == "string") item.quantity = parseFloat(item.quantity.replace(",", ".")); if (typeof item.quantity == "string") item.quantity = parseFloat(item.quantity.replace(",", "."));
let unit = item.unit; let unit = item.unit;
@ -32,7 +32,12 @@ exports.convertUnit = function (item, units, store) {
const conv = unit in exports.globalUnits ? exports.globalUnits[unit] : units[unit]; const conv = unit in exports.globalUnits ? exports.globalUnits[unit] : units[unit];
if (conv === undefined) { if (conv === undefined) {
console.error(`Unknown unit in ${store}: '${unit}' in item ${item.name}`); if (fallback) {
item.quantity = fallback.quantity;
item.unit = fallback.unit;
} else {
console.error(`Unknown unit in ${store}: '${unit}' in item ${item.name}`);
}
return item; return item;
} }