@darkpos/pricing 1.0.54 → 1.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,4 +24,116 @@ describe('Modifier actions', () => {
24
24
  })
25
25
  ).toBe(false);
26
26
  });
27
+
28
+ test('CU-86dve27tq Should not allow adding a modifier more than once to an item if isQuantityMultiplier={false} QTY={2}', () => {
29
+ const order = {
30
+ id: 'ord-123',
31
+ items: [],
32
+ modifiers: [],
33
+ };
34
+ const modifier = {
35
+ _id: 1,
36
+ compute: {
37
+ amount: 10,
38
+ action: 'subtract',
39
+ type: 'fixed',
40
+ },
41
+ };
42
+ order.items.push({
43
+ quantity: 2,
44
+ itemId: '123',
45
+ price: 100,
46
+ modifiers: [],
47
+ });
48
+
49
+ const conditionsBag = [];
50
+
51
+ const updatedOrder = pricingService.order.addItemModifier({
52
+ order,
53
+ modifier,
54
+ itemIndex: 0,
55
+ onConditionsNotMet: bag => {
56
+ bag.forEach(condition => {
57
+ conditionsBag.push(condition);
58
+ });
59
+ },
60
+ });
61
+
62
+ let error = '';
63
+ const updatedOrder2 = pricingService.order.addItemModifier({
64
+ order: { ...updatedOrder },
65
+ modifier,
66
+ itemIndex: 0,
67
+ onConditionsNotMet: bag => {
68
+ bag.forEach(condition => {
69
+ conditionsBag.push(condition);
70
+ });
71
+ },
72
+ onError: errorMessage => {
73
+ error = errorMessage;
74
+ },
75
+ });
76
+
77
+ expect(conditionsBag).toEqual([]);
78
+ expect(error).toEqual('modifier.has.reached.the.maximum.amount.of.applies');
79
+ expect(updatedOrder2).toEqual(updatedOrder);
80
+ });
81
+
82
+ test('CU-86dve27tq Should not allow adding a modifier more than once to an item if isQuantityMultiplier={false} QTY={1}', () => {
83
+ const order = {
84
+ id: 'ord-123',
85
+ items: [],
86
+ modifiers: [],
87
+ };
88
+ const modifier = {
89
+ _id: 1,
90
+ compute: {
91
+ amount: 10,
92
+ action: 'subtract',
93
+ type: 'fixed',
94
+ },
95
+ };
96
+ order.items.push({
97
+ quantity: 1,
98
+ itemId: '123',
99
+ price: 100,
100
+ modifiers: [],
101
+ properties: {
102
+ basePrice: 100,
103
+ },
104
+ });
105
+
106
+ const conditionsBag = [];
107
+
108
+ const updatedOrder = pricingService.order.addItemModifier({
109
+ order,
110
+ modifier,
111
+ itemIndex: 0,
112
+ onConditionsNotMet: bag => {
113
+ bag.forEach(condition => {
114
+ conditionsBag.push(condition);
115
+ });
116
+ },
117
+ });
118
+
119
+ let error = '';
120
+ const updatedOrder2 = pricingService.order.addItemModifier({
121
+ order: { ...updatedOrder },
122
+ modifier,
123
+ itemIndex: 0,
124
+ onConditionsNotMet: bag => {
125
+ bag.forEach(condition => {
126
+ conditionsBag.push(condition);
127
+ });
128
+ },
129
+ onError: errorMessage => {
130
+ error = errorMessage;
131
+ },
132
+ cache: { items: [], modifiers: [] },
133
+ });
134
+
135
+ expect(conditionsBag).toEqual([]);
136
+ expect(error).toEqual('');
137
+ expect(updatedOrder2).toEqual(order);
138
+ });
27
139
  });
@@ -29,7 +29,8 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
29
29
  subTotals._simple = math.mul(price, quantity);
30
30
 
31
31
  const modifiers = [];
32
- const { modifiers: itemModifiers = [] } = item;
32
+ const { modifiers: itemModifiers = [] } =
33
+ actions.removeModifiersByQuantity(item);
33
34
 
34
35
  const validatedModifiers = itemModifiers.map(each =>
35
36
  modifierActions.validate(each, {
package/lib/item/index.js CHANGED
@@ -46,6 +46,7 @@ const getTotalsDifference = require('./getTotalsDifference');
46
46
  const getTotalNeareastDifference = require('./getTotalNeareastDifference');
47
47
  const getNoteTags = require('./getNoteTags');
48
48
  const getBasePrice = require('./getBasePrice');
49
+ const removeModifiersByQuantity = require('./removeModifiersByQuantity');
49
50
 
50
51
  const itemActions = (deps = {}) => {
51
52
  const actions = {};
@@ -105,6 +106,7 @@ const itemActions = (deps = {}) => {
105
106
  getTotalNeareastDifference: getTotalNeareastDifference(innerDeps),
106
107
  getNoteTags: getNoteTags(innerDeps),
107
108
  getBasePrice: getBasePrice(innerDeps),
109
+ removeModifiersByQuantity: removeModifiersByQuantity(innerDeps),
108
110
  });
109
111
 
110
112
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,30 @@
1
+ module.exports = ({ modifierActions }) =>
2
+ function removeModifiersByQuantity(item) {
3
+ if (!item || !Array.isArray(item.modifiers)) {
4
+ return item;
5
+ }
6
+
7
+ const modifierCounts = {};
8
+
9
+ return {
10
+ ...item,
11
+ modifiers: item.modifiers.filter(modifier => {
12
+ if (
13
+ modifierActions.isPaymentModifier(modifier) ||
14
+ modifierActions.isCalculatedPaymentModifier(modifier) ||
15
+ !modifier.modifierId ||
16
+ !modifierActions.isDirect(modifier)
17
+ )
18
+ return true;
19
+
20
+ const count = modifierCounts[modifier.modifierId] || 0;
21
+
22
+ if (count < item.quantity) {
23
+ modifierCounts[modifier.modifierId] = count + 1;
24
+ return true;
25
+ }
26
+
27
+ return false;
28
+ }),
29
+ };
30
+ };
@@ -149,6 +149,7 @@ const isGroup = require('./isGroup');
149
149
  const isOptionsSelectedOverride = require('./isOptionsSelectedOverride');
150
150
  const isFixedSubtract = require('./isFixedSubtract');
151
151
  const isQuantityMultiplier = require('./isQuantityMultiplier');
152
+ const isCompute = require('./isCompute');
152
153
 
153
154
  const modifierActions = (deps = {}) => {
154
155
  const actions = {};
@@ -311,6 +312,7 @@ const modifierActions = (deps = {}) => {
311
312
  isOptionsSelectedOverride: isOptionsSelectedOverride(innerDeps),
312
313
  isFixedSubtract: isFixedSubtract(innerDeps),
313
314
  isQuantityMultiplier: isQuantityMultiplier(innerDeps),
315
+ isCompute: isCompute(innerDeps),
314
316
  });
315
317
 
316
318
  Object.keys(freezedActions).forEach(actionName => {
@@ -0,0 +1,10 @@
1
+ module.exports = () =>
2
+ function isCompute(modifier) {
3
+ if (!modifier || !modifier.compute) return false;
4
+
5
+ return (
6
+ !!modifier.compute.action &&
7
+ !!modifier.compute.type &&
8
+ typeof modifier.compute.amount === 'number'
9
+ );
10
+ };
@@ -177,6 +177,7 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
177
177
  itemIndex,
178
178
  cache,
179
179
  onConditionsNotMet,
180
+ onError,
180
181
  }) {
181
182
  let order = _.cloneDeep(orderProp);
182
183
  if (!order || itemIndex < 0 || !modifier) return order;
@@ -223,6 +224,16 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
223
224
  return order;
224
225
  }
225
226
 
227
+ if (
228
+ contains &&
229
+ modifierActions.isCompute(modifier) &&
230
+ !modifierActions.isQuantityMultiplier(modifier)
231
+ ) {
232
+ if (onError)
233
+ onError('modifier.has.reached.the.maximum.amount.of.applies');
234
+ return order;
235
+ }
236
+
226
237
  item = addModifier({
227
238
  item,
228
239
  modifier,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.54",
3
+ "version": "1.0.55",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -20,6 +20,7 @@
20
20
  "test:split": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/split.test.js",
21
21
  "test:getModifierTags": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item/getModifierTags.test.js",
22
22
  "test:createIndirectModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/createIndirectModifier.test.js",
23
+ "test:conditionsNotMet": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/conditionsNotMet.test.js",
23
24
  "lint": "eslint --quiet lib/"
24
25
  },
25
26
  "publishConfig": {
@@ -43,5 +44,5 @@
43
44
  "supertest": "^6.2.3",
44
45
  "supervisor": "^0.12.0"
45
46
  },
46
- "gitHead": "8bba2c9f52f54528ea2d0dece82406ef9fe69cb3"
47
+ "gitHead": "4341a5b5f555e6148a83ba4dba73bb7c908a02f5"
47
48
  }
@@ -1,27 +0,0 @@
1
- const usePricing = require('../../../../index');
2
-
3
- const orderToStr = require('../order-to-string');
4
-
5
- const orderPartiallyPaid = require('../../partially-paid/order-partially-paid.json');
6
- const orderModifiers = require('../../partially-paid/order-modifiers.json');
7
- const inputItems = require('../../partially-paid/input-items.json');
8
-
9
- const pricingService = usePricing();
10
-
11
- const paymentModifiers = orderModifiers.map(orderMod =>
12
- pricingService.modifier.duplicate({
13
- orderMod,
14
- items: inputItems,
15
- })
16
- );
17
-
18
- let order = { ...orderPartiallyPaid };
19
-
20
- order = pricingService.order.addModifiers({
21
- modifiers: paymentModifiers,
22
- order,
23
- });
24
-
25
- const resultedOrder = pricingService.order.calculate(order);
26
-
27
- orderToStr(resultedOrder);
@@ -1,28 +0,0 @@
1
- const usePricing = require('../../../../index');
2
-
3
- const orderToStr = require('../order-to-string');
4
-
5
- const orderUnpaid = require('../../unpaid/order-not-paid.json');
6
- const orderModifiers = require('../../unpaid/order-modifiers.json');
7
- const inputItems = require('../../unpaid/input-items.json');
8
-
9
- const pricingService = usePricing();
10
-
11
- const paymentModifiers = orderModifiers.map(orderMod =>
12
- pricingService.modifier.duplicate({
13
- ...orderMod,
14
- items: inputItems,
15
- })
16
- );
17
-
18
- let order = { ...orderUnpaid };
19
-
20
- if (paymentModifiers.length > 0)
21
- order = pricingService.order.addModifiers({
22
- modifiers: paymentModifiers,
23
- order,
24
- });
25
-
26
- const resultedOrder = pricingService.order.calculate(order);
27
-
28
- orderToStr(resultedOrder);
@@ -1,18 +0,0 @@
1
- module.exports = function orderToString(order) {
2
- const { total, totalPaid, items } = order;
3
-
4
- console.log({ total, totalPaid });
5
- items.map(item => {
6
- console.log('****');
7
- console.log({
8
- name: item.name,
9
- price: item.price,
10
- total: item.total,
11
- totalPaid: item.totalPaid,
12
- subtotals: item.subTotals,
13
- });
14
- item.modifiers.map(mod => {
15
- console.log('modifier _computed', mod._computed);
16
- });
17
- });
18
- };