From c5f59817654914e958ec05bce26409de157247cb Mon Sep 17 00:00:00 2001 From: Wisley Alves Date: Thu, 20 Jun 2024 10:44:36 -0300 Subject: [PATCH] chore(products-promotion): add config enable products in promotions #17 --- functions/ecom.config.js | 6 +++ functions/lib/helpers.js | 41 ++++++++++++++++++- functions/package-lock.json | 17 +++----- functions/package.json | 1 + .../routes/ecom/modules/apply-discount.js | 4 +- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/functions/ecom.config.js b/functions/ecom.config.js index f18c3a3..52edfa0 100644 --- a/functions/ecom.config.js +++ b/functions/ecom.config.js @@ -309,6 +309,12 @@ const app = { "title": "Desconto por domínio", "description": "Caso trabalhe com multilojas, o domínio irá filtrar o desconto pelo domínio" }, + "enable_products_promotion": { + "type": "boolean", + "default": true, + "title": "Habilitar Cupom para produtos em promoção", + "description": "Se desabilitado, o cupom não será aplicado caso qualquer item do carrinho esteja em promoção." + }, "category_ids": { "title": "Lista de categorias da campanha", "description": "Se preenchido, desconto será ativo se algum produto estiver no carrinho pertence a essa categoria", diff --git a/functions/lib/helpers.js b/functions/lib/helpers.js index c52bc0c..af7a7a3 100644 --- a/functions/lib/helpers.js +++ b/functions/lib/helpers.js @@ -1,6 +1,7 @@ 'use strict' const ecomUtils = require('@ecomplus/utils') +const ecomClient = require('@ecomplus/client') const validateDateRange = rule => { // filter campaings by date @@ -62,13 +63,50 @@ const checkOpenPromotion = rule => { (!Array.isArray(rule.customer_ids) || !rule.customer_ids.length) } -const getValidDiscountRules = (discountRules, params, items) => { +const havePromotion = async (storeId, items) => { + const skus = items.map(item => item.sku) + return await ecomClient.search({ + storeId, + url: '/items.json', + data: { + size: skus.length, + query: { + bool: { + must: { + terms: { skus } + } + } + } + } + }).then(({ data }) => { + let isPromotion = false + data?.hits?.hits?.forEach(({ _id, _source }) => { + const variation = _source.variations?.find(({ sku }) => skus.includes(sku)) + let item + if (skus.includes(_source.sku)) { + item = { _id, ..._source } + } else if (variation) { + item = { _id, ..._source, sku: variation.sku } + } + if (item && ecomUtils.onPromotion(item)) { + isPromotion = true + } + }) + return isPromotion + }) + .catch(() => false) +} + +const getValidDiscountRules = (storeId, discountRules, params, items) => { if (Array.isArray(discountRules) && discountRules.length) { // validate rules objects return discountRules.filter(rule => { if (!rule || !validateCustomerId(rule, params)) { return false } + if (rule.enable_products_promotion === false && Array.isArray(params.items) && havePromotion(storeId, params.items)) { + return false + } if ((Array.isArray(rule.product_ids) || (Array.isArray(rule.category_ids))) && Array.isArray(items)) { const checkProductId = item => { if (!(rule.product_ids && rule.product_ids.length) && Array.isArray(rule.category_ids) && rule.category_ids.length) { @@ -127,6 +165,7 @@ const getValidDiscountRules = (discountRules, params, items) => { const enableCategoryProductsOnly = rule.enable_category_products_only let value = 0 params.items.forEach(item => { + console.log('>> sku ', item.sku) const haveCategory = item.categories?.find(category => categoryIds.includes(category._id)) const price = ecomUtils.price(item) if (price > 0 && ((enableCategoryProductsOnly && haveCategory) || !enableCategoryProductsOnly)) { diff --git a/functions/package-lock.json b/functions/package-lock.json index d66c74a..36996c7 100644 --- a/functions/package-lock.json +++ b/functions/package-lock.json @@ -7,6 +7,7 @@ "name": "functions", "dependencies": { "@ecomplus/application-sdk": "^22.0.0-firestore.1.15.7", + "@ecomplus/client": "^2.3.1", "@google-cloud/firestore": "^7.5.0", "express": "^4.19.2", "firebase-admin": "^11.11.1", @@ -40,11 +41,11 @@ } }, "node_modules/@ecomplus/client": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/@ecomplus/client/-/client-2.2.1.tgz", - "integrity": "sha512-Hhq1MgzSlTWJ0Y/PZc5YkPhOmDsuIdrcrX8erzdBlrKDdApXoKD2pxC+KtlU2En141rFwwWzYdjJCp683nKFEA==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@ecomplus/client/-/client-2.3.1.tgz", + "integrity": "sha512-Nb9F8QZUxje9q3gS/UNFFaAILvsYS+EYN0Ambf3LC6IXAiTJ661GzPssQBJQ7r9A+sGeTTC/J0iC9rOJkYAhQw==", "dependencies": { - "axios": "^0.21.1" + "axios": "^0.27.2 || ^1.1.2" }, "optionalDependencies": { "core-js": "3.x" @@ -53,14 +54,6 @@ "@ecomplus/utils": "1.x" } }, - "node_modules/@ecomplus/client/node_modules/axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "dependencies": { - "follow-redirects": "^1.14.0" - } - }, "node_modules/@ecomplus/utils": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/@ecomplus/utils/-/utils-1.4.1.tgz", diff --git a/functions/package.json b/functions/package.json index 75ab2d1..e2a33d0 100644 --- a/functions/package.json +++ b/functions/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@ecomplus/application-sdk": "^22.0.0-firestore.1.15.7", + "@ecomplus/client": "^2.3.1", "@google-cloud/firestore": "^7.5.0", "express": "^4.19.2", "firebase-admin": "^11.11.1", diff --git a/functions/routes/ecom/modules/apply-discount.js b/functions/routes/ecom/modules/apply-discount.js index d11318e..984ee18 100644 --- a/functions/routes/ecom/modules/apply-discount.js +++ b/functions/routes/ecom/modules/apply-discount.js @@ -260,7 +260,7 @@ exports.post = ({ appSdk, admin }, req, res) => { return kitDiscount }) } - const kitDiscounts = getValidDiscountRules(config.product_kit_discounts, params, params.items) + const kitDiscounts = getValidDiscountRules(storeId, config.product_kit_discounts, params, params.items) .sort((a, b) => { if (!Array.isArray(a.product_ids) || !a.product_ids.length) { if (Array.isArray(b.product_ids) && b.product_ids.length) { @@ -466,7 +466,7 @@ exports.post = ({ appSdk, admin }, req, res) => { } }) - const discountRules = getValidDiscountRules(config.discount_rules, params) + const discountRules = getValidDiscountRules(storeId, config.discount_rules, params) if (discountRules.length) { const { discountRule, discountMatchEnum } = matchDiscountRule(discountRules, params) if (discountRule) {