UNPKG

@vtex/vtexis-compatibility-layer

Version:

Compatibility layer between intelligent search and VTEX

218 lines (217 loc) 9.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertISProduct = void 0; const convertSKU_1 = __importDefault(require("./convertSKU")); const utils_1 = require("./utils"); const filterVisibleSpecifications = (specificationKeys, visibleSpecifications) => specificationKeys.filter((specificationName) => { if ((visibleSpecifications === null || visibleSpecifications === void 0 ? void 0 : visibleSpecifications[specificationName]) != null) { return visibleSpecifications[specificationName]; } return true; }); const getSpecificationGroups = (product, specificationGroupsMap, specificationsMap) => { var _a; const allSpecificationsGroups = (_a = product.allSpecificationsGroups) !== null && _a !== void 0 ? _a : []; const visibleSpecifications = product.completeSpecifications ? product.completeSpecifications.reduce((acc, specification) => { acc[specification.Name] = specification.IsOnProductDetails; return acc; }, {}) : null; const specifications = allSpecificationsGroups.map((groupName) => { const specificationsFromGroup = specificationGroupsMap[groupName]; const visibleSpecificationsFromGroup = filterVisibleSpecifications(specificationsFromGroup, visibleSpecifications); return { originalName: groupName, name: groupName, specifications: visibleSpecificationsFromGroup.map((name) => { if (!specificationsMap[name]) { return { name, originalName: name, values: [], }; } const { values, labelKey } = specificationsMap[name]; return { originalName: name, name: labelKey, values, }; }), }; }); specifications.push({ originalName: 'allSpecifications', name: 'allSpecifications', specifications: filterVisibleSpecifications(product.allSpecifications, visibleSpecifications).map((name) => { const { values, labelKey } = specificationsMap[name]; return { originalName: name, name: labelKey, values, }; }), }); return specifications; }; const getProperties = (product, specificationsMap) => { var _a; return ((_a = product.allSpecifications) !== null && _a !== void 0 ? _a : []).map((name) => { const { labelKey, values } = specificationsMap[name]; return { name: labelKey, originalName: name, values, }; }); }; const convertISProduct = (product, tradePolicy, indexingType) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; const categories = []; const categoriesIds = []; const categoryTree = []; (_a = product.categoryTrees) === null || _a === void 0 ? void 0 : _a.forEach((category) => { const { categoryIds, categoryNames } = category; categories.push(`/${categoryNames.join('/')}/`); categoriesIds.push(`/${categoryIds.join('/')}/`); categoryTree.push({ id: Number(categoryIds[categoryIds.length - 1]), name: categoryNames[categoryNames.length - 1], href: '', }); }); const skus = ((_b = product.skus) !== null && _b !== void 0 ? _b : []).map(convertSKU_1.default(product, indexingType, tradePolicy)); const specificationGroups = product.specificationGroups ? JSON.parse(product.specificationGroups) : {}; const allSpecificationsGroups = [...Object.keys(specificationGroups)]; const brandId = product.brandId ? Number(product.brandId) : -1; const selectedProperties = product.split && [ { key: product.split.labelKey, value: product.split.labelValue, }, ]; const specificationAttributes = (_d = (_c = product.textAttributes) === null || _c === void 0 ? void 0 : _c.filter((attribute) => attribute.isSku)) !== null && _d !== void 0 ? _d : []; const allSpecifications = ((_e = product.productSpecifications) !== null && _e !== void 0 ? _e : []).concat(Array.from(new Set(specificationAttributes.map((specification) => specification.labelKey)))); const specificationsByKey = specificationAttributes.reduce((specsByKey, spec) => { var _a; // the joinedKey has the format text@@@key@@@labelKey@@@originalKey@@@originalLabelKey // eslint-disable-next-line prefer-destructuring const value = spec.joinedKey.split('@@@')[3]; specsByKey[value] = ((_a = specsByKey[value]) !== null && _a !== void 0 ? _a : []).concat(spec); return specsByKey; }, {}); const specKeys = Object.keys(specificationsByKey); const skuSpecifications = specKeys.map((key) => { // eslint-disable-next-line prefer-destructuring const originalFieldName = specificationsByKey[key][0].joinedKey.split('@@@')[4]; return { field: { name: specificationsByKey[key][0].labelKey, originalName: originalFieldName, }, values: specificationsByKey[key].map((specification) => { return { name: specification.labelValue, originalName: specification.joinedValue.split('@@@')[1], }; }), }; }); const numberAttributes = (_f = product.numberAttributes) !== null && _f !== void 0 ? _f : []; const numberSpecificationsByKey = numberAttributes.reduce((specsByKey, spec) => { var _a; const value = spec.labelKey; specsByKey[value] = ((_a = specsByKey[value]) !== null && _a !== void 0 ? _a : []).concat(spec); return specsByKey; }, {}); const numberSpecKeys = Object.keys(numberSpecificationsByKey); const skuNumberSpecifications = numberSpecKeys .map((key) => { const originalFieldName = numberSpecificationsByKey[key][0].labelKey; const values = numberSpecificationsByKey[key].map((specification) => { return { name: specification.value.toString(), originalName: specification.value.toString(), }; }); values.sort((x, y) => Number(x.name) - Number(y.name)); return { field: { name: originalFieldName.toString(), originalName: originalFieldName.toString(), }, values, }; }) .filter((specification) => allSpecifications.includes(specification.field.name)); const allSkuSpecification = skuSpecifications.concat(skuNumberSpecifications); const convertedProduct = { categories, categoriesIds, productId: product.product, cacheId: `sp-${product.id}`, productName: product.name, productReference: product.reference, linkText: product.link, brand: (_g = product.brand) !== null && _g !== void 0 ? _g : '', brandId, link: product.url, description: product.description, items: skus, priceRange: utils_1.getPriceRange(skus), allSpecifications, categoryId: (_h = product.categoryIds) === null || _h === void 0 ? void 0 : _h.slice(-1)[0], productTitle: (_j = product.productTitle) !== null && _j !== void 0 ? _j : '', metaTagDescription: (_k = product.metaTagDescription) !== null && _k !== void 0 ? _k : '', clusterHighlights: utils_1.objToNameValue('id', 'name', product.clusterHighlights), productClusters: [], searchableClusters: {}, titleTag: (_l = product.productTitle) !== null && _l !== void 0 ? _l : '', Specifications: [], allSpecificationsGroups, specificationGroups, selectedProperties, skuSpecifications: allSkuSpecification, properties: [], categoryTree, rule: product.rule, releaseDate: product.release, }; const specifications = {}; if (product.extraData) { product.extraData.forEach(({ key, value }) => { var _a; (_a = convertedProduct.allSpecifications) === null || _a === void 0 ? void 0 : _a.push(key); specifications[key] = { labelKey: key, values: [value] }; }); } if (product.textAttributes) { allSpecifications.forEach((specification) => { var _a, _b; if (specifications[specification]) { return; } const attributes = product.textAttributes.filter((attribute) => attribute.joinedKey.split('@@@')[4] === specification); const labelKey = (_b = (_a = attributes[0]) === null || _a === void 0 ? void 0 : _a.labelKey) !== null && _b !== void 0 ? _b : specification; specifications[specification] = { labelKey, values: attributes.map(({ labelValue }) => labelValue), }; }); product.textAttributes .filter((attribute) => attribute.labelKey === 'productClusterNames') .forEach((attribute) => { if (attribute.valueId) { convertedProduct.productClusters.push({ id: attribute.valueId, name: attribute.labelValue }); } }); } convertedProduct.properties = getProperties(convertedProduct, specifications); convertedProduct.specificationGroups = getSpecificationGroups(convertedProduct, specificationGroups, specifications); return convertedProduct; }; exports.convertISProduct = convertISProduct;