UNPKG

@vtex/vtexis-compatibility-layer

Version:

Compatibility layer between intelligent search and VTEX

162 lines (161 loc) 6.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const getVariations = (sku, product) => { return sku.attributes.map((attribute) => { const translatedAttribute = product.textAttributes.find(({ joinedKey, joinedValue }) => { return joinedKey.split('@@@')[2] === attribute.key && joinedValue.split('@@@')[1] === attribute.value; }); if (!translatedAttribute) { return { name: attribute.key, values: [attribute.value] }; } return { name: translatedAttribute.labelKey, values: [translatedAttribute.labelValue], }; }); }; const buildCommertialOffer = (price, oldPrice, spotPrice, stock, teasers, installment, tax) => { const installments = installment ? [ { Value: installment.value, InterestRate: 0, TotalValuePlusInterestRate: price, NumberOfInstallments: installment.count, Name: '', PaymentSystemName: '', PaymentSystemGroupName: '', }, ] : []; const availableQuantity = stock && stock > 0 ? 10000 : 0; return { DeliverySlaSamplesPerRegion: {}, DeliverySlaSamples: [], AvailableQuantity: availableQuantity, discountHighlights: [], Installments: installments, Price: price, ListPrice: oldPrice, spotPrice, taxPercentage: (tax !== null && tax !== void 0 ? tax : 0) / price, PriceWithoutDiscount: oldPrice, Tax: tax !== null && tax !== void 0 ? tax : 0, GiftSkuIds: [], BuyTogether: [], ItemMetadataAttachment: [], RewardValue: 0, PriceValidUntil: '', GetInfoErrorMessage: null, CacheVersionUsedToCallCheckout: '', teasers, }; }; const getSellersIndexedByApi = (product, sku, tradePolicy) => { var _a; const selectedPolicy = tradePolicy ? sku.policies.find((policy) => policy.id === tradePolicy) : sku.policies[0]; const biggySellers = (_a = selectedPolicy === null || selectedPolicy === void 0 ? void 0 : selectedPolicy.sellers) !== null && _a !== void 0 ? _a : []; return biggySellers.map((seller) => { var _a, _b, _c, _d, _e, _f, _g; const price = (_a = utils_1.getFirstNonNullable([seller.price, sku.price, product.price])) !== null && _a !== void 0 ? _a : 0; const oldPrice = (_b = utils_1.getFirstNonNullable([seller.oldPrice, sku.oldPrice, product.oldPrice])) !== null && _b !== void 0 ? _b : 0; // if spot price does not exist, it means that there is no spot price promotion, aka spotPrice == price const spotPrice = (_c = utils_1.getFirstNonNullable([seller.spotPrice, sku.spotPrice, product.spotPrice])) !== null && _c !== void 0 ? _c : price; const installment = (_d = seller.installment) !== null && _d !== void 0 ? _d : product.installment; const stock = (_e = utils_1.getFirstNonNullable([seller.stock, sku.stock, product.stock])) !== null && _e !== void 0 ? _e : 0; const teasers = (_f = seller.teasers) !== null && _f !== void 0 ? _f : []; const commertialOffer = buildCommertialOffer(price, oldPrice, spotPrice, stock, teasers, installment, seller.tax); return { sellerId: seller.id, sellerName: seller.name, addToCartLink: '', sellerDefault: (_g = seller.default) !== null && _g !== void 0 ? _g : false, commertialOffer, }; }); }; const getSellersIndexedByXML = (product) => { var _a; const { price, oldPrice, installment, stock } = product; // if spot price does not exist, it means that there is no spot price promotion, aka spotPrice == price const spotPrice = (_a = product.spotPrice) !== null && _a !== void 0 ? _a : price; const commertialOffer = buildCommertialOffer(price, oldPrice, spotPrice, stock, [], installment, product.tax); return [ { sellerId: '1', sellerName: '', addToCartLink: '', sellerDefault: false, commertialOffer, }, ]; }; const toVtexAssets = (image) => image.replace('vteximg.com.br', 'vtexassets.com').replace('http://', 'https://'); const elasticImageToSearchImage = (image, imageId) => { var _a, _b; return { imageId, cacheId: imageId, imageTag: '', imageLabel: (_a = image.name) !== null && _a !== void 0 ? _a : '', imageText: (_b = image.name) !== null && _b !== void 0 ? _b : '', imageUrl: toVtexAssets(image.value), }; }; const getImageId = (imageUrl) => { var _a; const baseUrlRegex = new RegExp(/.+ids\/(\d+)/); return baseUrlRegex.test(imageUrl) ? (_a = baseUrlRegex.exec(imageUrl)) === null || _a === void 0 ? void 0 : _a[1] : undefined; }; const convertImages = (images, indexingType) => { const vtexImages = []; if (indexingType && indexingType === 'XML') { const [selectedImage] = images; const imageId = getImageId(selectedImage.value); return imageId ? [elasticImageToSearchImage(selectedImage, imageId)] : []; } images.forEach((image) => { const imageId = getImageId(image.value); imageId ? vtexImages.push(elasticImageToSearchImage(image, imageId)) : []; }); return vtexImages; }; const convertSKU = (product, indexingType, tradePolicy) => (sku) => { var _a, _b, _c, _d, _e, _f, _g; const images = convertImages((_a = sku.images) !== null && _a !== void 0 ? _a : product.images, indexingType); const sellers = indexingType === 'XML' ? getSellersIndexedByXML(product) : getSellersIndexedByApi(product, sku, tradePolicy); const variations = getVariations(sku, product); const attributes = (_b = sku.catalogAttributes) !== null && _b !== void 0 ? _b : []; const item = { sellers, images, itemId: sku.id, name: sku.name, nameComplete: sku.nameComplete, complementName: (_c = sku.complementName) !== null && _c !== void 0 ? _c : '', referenceId: [ { Key: 'RefId', Value: sku.reference, }, ], measurementUnit: (_d = sku.measurementUnit) !== null && _d !== void 0 ? _d : product.measurementUnit, unitMultiplier: (_e = sku.unitMultiplier) !== null && _e !== void 0 ? _e : product.unitMultiplier, variations, ean: (_f = sku.ean) !== null && _f !== void 0 ? _f : '', modalType: '', videos: (_g = sku.videos) !== null && _g !== void 0 ? _g : [], attachments: [], isKit: false, attributes, }; variations.forEach((variation) => { var _a; item[variation.name] = (_a = variation.values) !== null && _a !== void 0 ? _a : []; }); return item; }; exports.default = convertSKU;