Skip to content

Commit

Permalink
chore(products-promotion): add config enable products in promotions #17
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Jun 20, 2024
1 parent c6ebaa4 commit c5f5981
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
6 changes: 6 additions & 0 deletions functions/ecom.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 40 additions & 1 deletion functions/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const ecomUtils = require('@ecomplus/utils')
const ecomClient = require('@ecomplus/client')

const validateDateRange = rule => {
// filter campaings by date
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
17 changes: 5 additions & 12 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions functions/routes/ecom/modules/apply-discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c5f5981

Please sign in to comment.