@medusajs/promotion 0.0.5-snapshot-20240815071705 → 0.0.5-snapshot-20240821142206
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/services/promotion-module.d.ts.map +1 -1
- package/dist/services/promotion-module.js +1 -1
- package/dist/utils/compute-actions/buy-get.d.ts.map +1 -1
- package/dist/utils/compute-actions/buy-get.js +8 -5
- package/dist/utils/compute-actions/line-items.d.ts.map +1 -1
- package/dist/utils/compute-actions/line-items.js +1 -1
- package/dist/utils/compute-actions/shipping-methods.d.ts.map +1 -1
- package/dist/utils/compute-actions/shipping-methods.js +3 -2
- package/dist/utils/validations/promotion-rule.d.ts +2 -2
- package/dist/utils/validations/promotion-rule.d.ts.map +1 -1
- package/dist/utils/validations/promotion-rule.js +22 -2
- package/package.json +5 -5
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"promotion-module.d.ts","sourceRoot":"","sources":["../../src/services/promotion-module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,OAAO,EACP,GAAG,EACH,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACf,MAAM,iBAAiB,CAAA;AAoBxB,OAAO,EACL,iBAAiB,EACjB,QAAQ,EACR,cAAc,EACd,SAAS,EACT,aAAa,EACb,kBAAkB,EACnB,MAAM,SAAS,CAAA;AAChB,OAAO,EACL,0BAA0B,EAU3B,MAAM,QAAQ,CAAA;AAWf,KAAK,oBAAoB,GAAG;IAC1B,cAAc,EAAE,GAAG,CAAC,iBAAiB,CAAA;IACrC,gBAAgB,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;IAC7D,wBAAwB,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;IACrE,oBAAoB,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;IACjE,yBAAyB,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;IACtE,eAAe,EAAE,eAAe,CAAC,sBAAsB,C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AA;KAAE,CAAC;cAOb,6BAA6B,CAC3C,IAAI,EAAE,cAAc,CAAC,0BAA0B,EAC9B,aAAa,GAAE,OAAY;CAiC/C"}
|
@@ -182,7 +182,7 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
182
182
|
if (!applicationMethod) {
|
183
183
|
continue;
|
184
184
|
}
|
185
|
-
const isPromotionApplicable = (0, _utils_1.areRulesValidForContext)(promotionRules, applicationContext);
|
185
|
+
const isPromotionApplicable = (0, _utils_1.areRulesValidForContext)(promotionRules, applicationContext, utils_1.ApplicationMethodTargetType.ORDER);
|
186
186
|
if (!isPromotionApplicable) {
|
187
187
|
continue;
|
188
188
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"buy-get.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/buy-get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EACL,2BAA2B,EAM5B,MAAM,iBAAiB,CAAA;AAKxB,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,YAAY,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,KAAK,CAAC,EACpF,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GACjD,cAAc,CAAC,cAAc,EAAE,
|
1
|
+
{"version":3,"file":"buy-get.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/buy-get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EACL,2BAA2B,EAM5B,MAAM,iBAAiB,CAAA;AAKxB,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,YAAY,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,KAAK,CAAC,EACpF,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GACjD,cAAc,CAAC,cAAc,EAAE,CA+FjC;AAED,wBAAgB,gBAAgB,CAAC,CAAC,KAAA,EAAE,CAAC,KAAA,cAWpC"}
|
@@ -18,7 +18,7 @@ function getComputedActionsForBuyGet(promotion, itemsContext, methodIdPromoValue
|
|
18
18
|
return [];
|
19
19
|
}
|
20
20
|
const validQuantity = utils_1.MathBN.sum(...itemsContext
|
21
|
-
.filter((item) => (0, validations_1.areRulesValidForContext)(buyRules, item))
|
21
|
+
.filter((item) => (0, validations_1.areRulesValidForContext)(buyRules, item, utils_1.ApplicationMethodTargetType.ITEMS))
|
22
22
|
.map((item) => item.quantity));
|
23
23
|
if (!buyRulesMinQuantity ||
|
24
24
|
!applyToQuantity ||
|
@@ -26,18 +26,21 @@ function getComputedActionsForBuyGet(promotion, itemsContext, methodIdPromoValue
|
|
26
26
|
return [];
|
27
27
|
}
|
28
28
|
const validItemsForTargetRules = itemsContext
|
29
|
-
.filter((item) => (0, validations_1.areRulesValidForContext)(targetRules, item))
|
29
|
+
.filter((item) => (0, validations_1.areRulesValidForContext)(targetRules, item, utils_1.ApplicationMethodTargetType.ITEMS))
|
30
30
|
.filter((item) => (0, utils_1.isPresent)(item.subtotal) && (0, utils_1.isPresent)(item.quantity))
|
31
31
|
.sort((a, b) => {
|
32
|
-
const
|
33
|
-
const
|
32
|
+
const divA = utils_1.MathBN.eq(a.quantity, 0) ? 1 : a.quantity;
|
33
|
+
const divB = utils_1.MathBN.eq(b.quantity, 0) ? 1 : b.quantity;
|
34
|
+
const aPrice = utils_1.MathBN.div(a.subtotal, divA);
|
35
|
+
const bPrice = utils_1.MathBN.div(b.subtotal, divB);
|
34
36
|
return utils_1.MathBN.lt(bPrice, aPrice) ? -1 : 1;
|
35
37
|
});
|
36
38
|
let remainingQtyToApply = utils_1.MathBN.convert(applyToQuantity);
|
37
39
|
for (const method of validItemsForTargetRules) {
|
38
40
|
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0;
|
39
41
|
const multiplier = utils_1.MathBN.min(method.quantity, remainingQtyToApply);
|
40
|
-
const
|
42
|
+
const div = utils_1.MathBN.eq(method.quantity, 0) ? 1 : method.quantity;
|
43
|
+
const amount = utils_1.MathBN.mult(utils_1.MathBN.div(method.subtotal, div), multiplier);
|
41
44
|
const newRemainingQtyToApply = utils_1.MathBN.sub(remainingQtyToApply, multiplier);
|
42
45
|
if (utils_1.MathBN.lt(newRemainingQtyToApply, 0) || utils_1.MathBN.lte(amount, 0)) {
|
43
46
|
break;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"line-items.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/line-items.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EAEjC,cAAc,EACf,MAAM,iBAAiB,CAAA;AACxB,OAAO,
|
1
|
+
{"version":3,"file":"line-items.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/line-items.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EAEjC,cAAc,EACf,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAML,2BAA2B,IAAI,UAAU,EAE1C,MAAM,iBAAiB,CAAA;AAgBxB,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,KAAK,EAAE,cAAc,CAAC,oBAAoB,CAAC,UAAU,CAAC,KAAK,CAAC,EAC5D,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EACzC,kBAAkB,CAAC,EAAE,iCAAiC,GACrD,cAAc,CAAC,cAAc,EAAE,CASjC;AAED,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,eAAe,EAAE,cAAc,CAAC,oBAAoB,CAAC,UAAU,CAAC,gBAAgB,CAAC,EACjF,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACxC,cAAc,CAAC,cAAc,EAAE,CAIjC;AAED,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,sBAAsB,EAAE,cAAc,CAAC,oBAAoB,EAC3D,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACzC,cAAc,CAAC,cAAc,EAAE,CAOjC"}
|
@@ -84,7 +84,7 @@ function getValidItemsForPromotion(items, promotion) {
|
|
84
84
|
return (items?.filter((item) => {
|
85
85
|
const isSubtotalPresent = "subtotal" in item;
|
86
86
|
const isQuantityPresent = "quantity" in item;
|
87
|
-
const isPromotionApplicableToItem = (0, validations_1.areRulesValidForContext)(promotion?.application_method?.target_rules, item);
|
87
|
+
const isPromotionApplicableToItem = (0, validations_1.areRulesValidForContext)(promotion?.application_method?.target_rules, item, utils_1.ApplicationMethodTargetType.ITEMS);
|
88
88
|
return (isPromotionApplicableToItem &&
|
89
89
|
(isQuantityPresent || isTargetShippingMethod) &&
|
90
90
|
isSubtotalPresent);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"shipping-methods.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/shipping-methods.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EAEL,2BAA2B,EAK5B,MAAM,iBAAiB,CAAA;AAIxB,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,gCAAgC,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,gBAAgB,CAAC,EACnH,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACzC,cAAc,CAAC,cAAc,EAAE,
|
1
|
+
{"version":3,"file":"shipping-methods.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/shipping-methods.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EAEL,2BAA2B,EAK5B,MAAM,iBAAiB,CAAA;AAIxB,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,gCAAgC,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,gBAAgB,CAAC,EACnH,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACzC,cAAc,CAAC,cAAc,EAAE,CA8BjC;AAED,wBAAgB,+BAA+B,CAC7C,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,eAAe,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,gBAAgB,CAAC,EAClG,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GACjD,cAAc,CAAC,cAAc,EAAE,CAyHjC"}
|
@@ -10,7 +10,7 @@ function getComputedActionsForShippingMethods(promotion, shippingMethodApplicati
|
|
10
10
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `"shipping_methods" should be present as an array in the context for computeActions`);
|
11
11
|
}
|
12
12
|
for (const shippingMethodContext of shippingMethodApplicationContext) {
|
13
|
-
const isPromotionApplicableToItem = (0, validations_1.areRulesValidForContext)(promotion.application_method?.target_rules, shippingMethodContext);
|
13
|
+
const isPromotionApplicableToItem = (0, validations_1.areRulesValidForContext)(promotion.application_method?.target_rules, shippingMethodContext, utils_1.ApplicationMethodTargetType.SHIPPING_METHODS);
|
14
14
|
if (!isPromotionApplicableToItem) {
|
15
15
|
continue;
|
16
16
|
}
|
@@ -67,7 +67,8 @@ function applyPromotionToShippingMethods(promotion, shippingMethods, methodIdPro
|
|
67
67
|
const promotionValue = applicationMethod?.value ?? 0;
|
68
68
|
const applicableTotal = method.subtotal;
|
69
69
|
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0;
|
70
|
-
|
70
|
+
const div = utils_1.MathBN.eq(totalApplicableValue, 0) ? 1 : totalApplicableValue;
|
71
|
+
let applicablePromotionValue = utils_1.MathBN.sub(utils_1.MathBN.mult(utils_1.MathBN.div(applicableTotal, div), promotionValue), appliedPromoValue);
|
71
72
|
if (applicationMethod?.type === utils_1.ApplicationMethodType.PERCENTAGE) {
|
72
73
|
applicablePromotionValue = utils_1.MathBN.sub(utils_1.MathBN.mult(utils_1.MathBN.div(promotionValue, 100), applicableTotal), appliedPromoValue);
|
73
74
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { PromotionRuleDTO } from "@medusajs/types";
|
1
|
+
import { ApplicationMethodTargetTypeValues, PromotionRuleDTO } from "@medusajs/types";
|
2
2
|
import { CreatePromotionRuleDTO } from "../../types";
|
3
3
|
export declare function validatePromotionRuleAttributes(promotionRulesData: CreatePromotionRuleDTO[]): void;
|
4
|
-
export declare function areRulesValidForContext(rules: PromotionRuleDTO[], context: Record<string, any
|
4
|
+
export declare function areRulesValidForContext(rules: PromotionRuleDTO[], context: Record<string, any>, contextScope: ApplicationMethodTargetTypeValues): boolean;
|
5
5
|
export declare function evaluateRuleValueCondition(ruleValues: string[], operator: string, ruleValuesToCheck: string[] | string): boolean;
|
6
6
|
//# sourceMappingURL=promotion-rule.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"promotion-rule.d.ts","sourceRoot":"","sources":["../../../src/utils/validations/promotion-rule.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"promotion-rule.d.ts","sourceRoot":"","sources":["../../../src/utils/validations/promotion-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,gBAAgB,EAEjB,MAAM,iBAAiB,CAAA;AASxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,QAAQ,CAAA;AAE/C,wBAAgB,+BAA+B,CAC7C,kBAAkB,EAAE,sBAAsB,EAAE,QAiC7C;AAED,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,gBAAgB,EAAE,EACzB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5B,YAAY,EAAE,iCAAiC,GAC9C,OAAO,CAmBT;AAkCD,wBAAgB,0BAA0B,CACxC,UAAU,EAAE,MAAM,EAAE,EACpB,QAAQ,EAAE,MAAM,EAChB,iBAAiB,EAAE,MAAM,EAAE,GAAG,MAAM,WAiCrC"}
|
@@ -26,17 +26,37 @@ function validatePromotionRuleAttributes(promotionRulesData) {
|
|
26
26
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, errors.join(", "));
|
27
27
|
}
|
28
28
|
exports.validatePromotionRuleAttributes = validatePromotionRuleAttributes;
|
29
|
-
function areRulesValidForContext(rules, context) {
|
29
|
+
function areRulesValidForContext(rules, context, contextScope) {
|
30
30
|
return rules.every((rule) => {
|
31
31
|
const validRuleValues = rule.values?.map((ruleValue) => ruleValue.value);
|
32
32
|
if (!rule.attribute) {
|
33
33
|
return false;
|
34
34
|
}
|
35
|
-
const valuesToCheck = (0, utils_1.pickValueFromObject)(rule.attribute, context);
|
35
|
+
const valuesToCheck = (0, utils_1.pickValueFromObject)(fetchRuleAttributeForContext(rule.attribute, contextScope), context);
|
36
36
|
return evaluateRuleValueCondition(validRuleValues.filter(utils_1.isString), rule.operator, valuesToCheck);
|
37
37
|
});
|
38
38
|
}
|
39
39
|
exports.areRulesValidForContext = areRulesValidForContext;
|
40
|
+
/*
|
41
|
+
The context here can either be either:
|
42
|
+
- a cart context
|
43
|
+
- an item context under a cart
|
44
|
+
- a shipping method context under a cart
|
45
|
+
|
46
|
+
The rule's attributes are set from the perspective of the cart context. For example: items.product.id
|
47
|
+
|
48
|
+
When the context here is item or shipping_method, we need to drop the "items."" or "shipping_method."
|
49
|
+
from the rule attribute string to accurate pick the values from the context.
|
50
|
+
*/
|
51
|
+
function fetchRuleAttributeForContext(ruleAttribute, contextScope) {
|
52
|
+
if (contextScope === utils_1.ApplicationMethodTargetType.ITEMS) {
|
53
|
+
ruleAttribute = ruleAttribute.replace(`${utils_1.ApplicationMethodTargetType.ITEMS}.`, "");
|
54
|
+
}
|
55
|
+
if (contextScope === utils_1.ApplicationMethodTargetType.SHIPPING_METHODS) {
|
56
|
+
ruleAttribute = ruleAttribute.replace(`${utils_1.ApplicationMethodTargetType.SHIPPING_METHODS}.`, "");
|
57
|
+
}
|
58
|
+
return ruleAttribute;
|
59
|
+
}
|
40
60
|
function evaluateRuleValueCondition(ruleValues, operator, ruleValuesToCheck) {
|
41
61
|
if (!Array.isArray(ruleValuesToCheck)) {
|
42
62
|
ruleValuesToCheck = [ruleValuesToCheck];
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/promotion",
|
3
|
-
"version": "0.0.5-snapshot-
|
3
|
+
"version": "0.0.5-snapshot-20240821142206",
|
4
4
|
"description": "Medusa Promotion module",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -34,12 +34,12 @@
|
|
34
34
|
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
|
35
35
|
},
|
36
36
|
"devDependencies": {
|
37
|
-
"@medusajs/modules-sdk": "1.13.0-snapshot-
|
38
|
-
"@medusajs/types": "1.12.0-snapshot-
|
37
|
+
"@medusajs/modules-sdk": "1.13.0-snapshot-20240821142206",
|
38
|
+
"@medusajs/types": "1.12.0-snapshot-20240821142206",
|
39
39
|
"@mikro-orm/cli": "5.9.7",
|
40
40
|
"cross-env": "^5.2.1",
|
41
41
|
"jest": "^29.7.0",
|
42
|
-
"medusa-test-utils": "1.1.45-snapshot-
|
42
|
+
"medusa-test-utils": "1.1.45-snapshot-20240821142206",
|
43
43
|
"rimraf": "^3.0.2",
|
44
44
|
"ts-jest": "^29.1.1",
|
45
45
|
"ts-node": "^10.9.1",
|
@@ -47,7 +47,7 @@
|
|
47
47
|
"typescript": "^5.1.6"
|
48
48
|
},
|
49
49
|
"dependencies": {
|
50
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
50
|
+
"@medusajs/utils": "1.12.0-snapshot-20240821142206",
|
51
51
|
"@mikro-orm/core": "5.9.7",
|
52
52
|
"@mikro-orm/migrations": "5.9.7",
|
53
53
|
"@mikro-orm/postgresql": "5.9.7",
|