2023-05-25 13:54:28 +02:00
|
|
|
const axios = require("axios");
|
2023-05-28 19:48:06 +02:00
|
|
|
const utils = require("./utils");
|
2023-05-25 13:54:28 +02:00
|
|
|
|
2023-05-27 22:27:09 +02:00
|
|
|
const conversions = {
|
2023-06-01 08:25:04 +02:00
|
|
|
'cm': { unit: 'cm', factor: 1 },
|
|
|
|
'dag': { unit: 'g', factor: 10 },
|
|
|
|
'dl': { unit: 'ml', factor: 10 },
|
|
|
|
'grm': { unit: 'g', factor: 1 },
|
|
|
|
'kgm': { unit: 'g', factor: 1000 },
|
|
|
|
'ltr': { unit: 'ml', factor: 1000 },
|
|
|
|
'mlt': { unit: 'ml', factor: 1 },
|
|
|
|
'mtr': { unit: 'm', factor: 1 },
|
|
|
|
'stk': { unit: 'stk', factor: 1 },
|
|
|
|
'stk.': { unit: 'stk', factor: 1 },
|
|
|
|
'g': { unit: 'g', factor: 1 },
|
|
|
|
'anw': { unit: 'stk', factor: 1 },
|
|
|
|
'l': { unit: 'ml', factor: 1000 },
|
|
|
|
'm': { unit: 'cm', factor: 100 },
|
|
|
|
'ml': { unit: 'ml', factor: 1 },
|
|
|
|
'kg': { unit: 'g', factor: 1000 },
|
|
|
|
'paar': { unit: 'stk', factor: 1 },
|
|
|
|
'stück': { unit: 'stk', factor: 1 },
|
|
|
|
'bl.': { unit: 'stk', factor: 1 },
|
|
|
|
'pkg': { unit: 'stk', factor: 1 },
|
|
|
|
'gr': { unit: 'g', factor: 1 },
|
|
|
|
'er': { unit: 'stk', factor: 1 },
|
2023-05-27 22:27:09 +02:00
|
|
|
};
|
|
|
|
|
2023-05-25 13:54:28 +02:00
|
|
|
exports.getCanonical = function(item, today) {
|
2023-05-27 22:27:09 +02:00
|
|
|
let quantity = item.prices[0].presentationPrice.measurementUnit.quantity
|
2023-06-01 08:25:04 +02:00
|
|
|
let unit = item.prices[0].presentationPrice.measurementUnit.unitCode.toLowerCase()
|
|
|
|
if(['xro', 'h87', 'hlt'].indexOf(unit)!=-1) {
|
|
|
|
const q = utils.parseUnitAndQuantityAtEnd(item.mixins.productCustomAttributes.packagingUnit)
|
|
|
|
quantity = q[0] ?? quantity;
|
|
|
|
unit = q[1];
|
|
|
|
}
|
|
|
|
if (!(unit in conversions)) {
|
|
|
|
unit = 'stk';
|
|
|
|
}
|
2023-05-28 18:30:02 +02:00
|
|
|
const isWeighted = (item.mixins.productCustomAttributes?.packagingDescription ?? "").startsWith("Gewichtsware");
|
2023-05-28 19:48:06 +02:00
|
|
|
return utils.convertUnit({
|
2023-05-25 13:54:28 +02:00
|
|
|
id: item.code,
|
2023-05-28 19:10:08 +02:00
|
|
|
name: item.name[0],
|
2023-05-28 18:30:02 +02:00
|
|
|
isWeighted,
|
|
|
|
price: isWeighted ? item.prices[0].effectiveAmount : item.prices[0].presentationPrice.effectiveAmount,
|
2023-05-25 13:54:28 +02:00
|
|
|
priceHistory: [{ date: today, price: item.prices[0].presentationPrice.effectiveAmount }],
|
2023-05-27 22:27:09 +02:00
|
|
|
unit,
|
|
|
|
quantity,
|
2023-05-26 18:12:29 +02:00
|
|
|
bio: item.mixins.mpreisAttributes.properties?.includes('BIO'),
|
2023-05-28 22:31:12 +02:00
|
|
|
url: `https://www.mpreis.at/shop/p/${item.code}`,
|
2023-05-28 19:48:06 +02:00
|
|
|
}, conversions, 'mpreis');
|
2023-05-25 13:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetchData = async function() {
|
|
|
|
const MPREIS_URL = `https://ax2ixv4hll-dsn.algolia.net/1/indexes/prod_mpreis_8450/browse?X-Algolia-API-Key=NmJlMTI0NjY1NGU4MDUwYTRlMmYzYWFjOWFlY2U4MGFkNGZjMDY2NmNjNjQzNWY3OWJlNDY4OTY0ZjEwOTEwYWZpbHRlcnM9cHVibGlzaGVk&X-Algolia-Application-Id=AX2IXV4HLL&X-Algolia-Agent=Vue.js`
|
|
|
|
let mpreisItems = [];
|
|
|
|
let res = (await axios.get(MPREIS_URL)).data;
|
|
|
|
mpreisItems = mpreisItems.concat(res.hits);
|
|
|
|
cursor = res.cursor;
|
|
|
|
while (cursor) {
|
|
|
|
res = (await axios.get(MPREIS_URL + `&cursor=${cursor}`)).data;
|
|
|
|
mpreisItems = mpreisItems.concat(res.hits);
|
|
|
|
cursor = res.cursor;
|
|
|
|
}
|
|
|
|
return mpreisItems;
|
2023-05-28 22:31:12 +02:00
|
|
|
}
|