@vtex/vtexis-compatibility-layer
Version:
Compatibility layer between intelligent search and VTEX
60 lines (59 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTranslationInfo = exports.dateToTicks = exports.getPriceRange = exports.getFirstNonNullable = exports.objToNameValue = exports.distinct = void 0;
const ramda_1 = require("ramda");
function distinct(value, index, self) {
return self.indexOf(value) === index;
}
exports.distinct = distinct;
const objToNameValue = (keyName, valueName, record) => {
if (!record) {
return [];
}
return Object.keys(record).reduce((acc, key) => {
const value = record[key];
if (typeof value === 'string') {
acc.push({ [keyName]: key, [valueName]: value });
}
return acc;
}, []);
};
exports.objToNameValue = objToNameValue;
const getMaxAndMinForAttribute = (offers, attribute) => {
return offers.reduce((acc, currentOffer) => {
const highPrice = currentOffer[attribute] > acc.highPrice ? currentOffer[attribute] : acc.highPrice;
const lowPrice = currentOffer[attribute] < acc.lowPrice && currentOffer[attribute] > 0 ? currentOffer[attribute] : acc.lowPrice;
return { highPrice, lowPrice };
}, { highPrice: 0, lowPrice: Infinity });
};
const getFirstNonNullable = (arr) => {
return arr.find((el) => el !== null && typeof el !== 'undefined');
};
exports.getFirstNonNullable = getFirstNonNullable;
const isSellerAvailable = (seller) => ramda_1.pathOr(0, ['commertialOffer', 'AvailableQuantity'], seller) > 0;
const getPriceRange = (searchItems) => {
const offers = searchItems.reduce((acc, currentItem) => {
for (const seller of currentItem.sellers) {
if (isSellerAvailable(seller)) {
acc.push(seller.commertialOffer);
}
}
return acc;
}, []);
return {
sellingPrice: getMaxAndMinForAttribute(offers, 'Price'),
listPrice: getMaxAndMinForAttribute(offers, 'ListPrice'),
};
};
exports.getPriceRange = getPriceRange;
const TICKS_AT_EPOCH = 621355968000000000;
const dateToTicks = (dateTime) => (new Date(dateTime)).getTime() * 10000 + TICKS_AT_EPOCH;
exports.dateToTicks = dateToTicks;
const getTranslationInfo = (info, translationInfo, infoId) => {
if (!translationInfo || !translationInfo.length) {
return undefined;
}
const item = translationInfo.find((value) => infoId ? value.field === info && value.context === infoId : value.field === info);
return item ? item.translation : undefined;
};
exports.getTranslationInfo = getTranslationInfo;