@darkpos/pricing 1.0.41 → 1.0.44

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.
Files changed (51) hide show
  1. package/__TEST__/item/getItemsModifierDescription.test.js +148 -0
  2. package/__TEST__/mocks/addItemMock.js +18729 -19357
  3. package/__TEST__/mocks/partially-paid/order-modifiers.json +48 -51
  4. package/__TEST__/mocks/partially-paid/order-partially-paid.json +893 -860
  5. package/__TEST__/mocks/unpaid/order-modifiers.json +48 -52
  6. package/__TEST__/modifier/calculate.test.js +1 -0
  7. package/__TEST__/modifier/getGroupedModifierLabels.test.js +95 -0
  8. package/__TEST__/modifier/getGroupedModifiers.test.js +65 -0
  9. package/__TEST__/modifier/getMatchTagsModifiers.test.js +72 -18
  10. package/__TEST__/modifier/getModifierIndex.test.js +10 -7
  11. package/__TEST__/modifier/getNotesToModifierTags.test.js +78 -0
  12. package/__TEST__/modifier/getRecommendedModifiers.test.js +6 -4
  13. package/__TEST__/modifier/hasAttribute.test.js +11 -5
  14. package/__TEST__/modifier/hasMatchTags.test.js +33 -11
  15. package/__TEST__/modifier/isOptionsOverride.test.js +46 -0
  16. package/__TEST__/modifier/sort.test.js +1 -1
  17. package/__TEST__/order/conditionsNotMet.test.js +133 -0
  18. package/__TEST__/order/order.test.js +7 -5
  19. package/__TEST__/order/pickEndDate.test.js +4 -4
  20. package/__TEST__/order/validateConditionsCalculate.test.js +397 -0
  21. package/lib/constants/index.js +1 -1
  22. package/lib/index.js +11 -2
  23. package/lib/item/calculate.js +27 -10
  24. package/lib/item/getItemModifiersDescription.js +40 -7
  25. package/lib/item/markModifiersAsLocked.js +3 -1
  26. package/lib/modifier/areConditionsMet.js +71 -0
  27. package/lib/modifier/getGroupedModifierLabels.js +10 -0
  28. package/lib/modifier/getGroupedModifiers.js +21 -0
  29. package/lib/modifier/getNotesToModifierTags.js +21 -0
  30. package/lib/modifier/index.js +22 -5
  31. package/lib/modifier/isOptionsOverride.js +9 -0
  32. package/lib/modifier/isPaymentMethodModifier.js +1 -1
  33. package/lib/modifier/isPaymentTypeModifier.js +1 -1
  34. package/lib/modifier/isValid.js +12 -0
  35. package/lib/modifier/validate.js +14 -0
  36. package/lib/modifier/validateDateDaysDiff.js +30 -0
  37. package/lib/modifier/validateInArr.js +12 -0
  38. package/lib/modifier/validateNumberCondition.js +20 -0
  39. package/lib/modifier/validateRequiredModifiers.js +16 -0
  40. package/lib/order/addItemModifier.js +72 -28
  41. package/lib/order/calculate.js +12 -7
  42. package/lib/order/index.js +0 -2
  43. package/lib/order/removeModifiersWithPaymentMethods.js +1 -2
  44. package/lib/order/removeModifiersWithPaymentTypes.js +1 -2
  45. package/lib/store/getRecommendedEndDate.js +13 -0
  46. package/lib/store/index.js +25 -0
  47. package/lib/store/pickEndDate.js +62 -0
  48. package/package.json +3 -3
  49. package/lib/modifier/findByPaymentMethod.js +0 -10
  50. package/lib/modifier/findByPaymentType.js +0 -10
  51. package/lib/order/pickEndDate.js +0 -65
@@ -8,49 +8,71 @@ describe('Has Match Tags Function', () => {
8
8
  const customer = {};
9
9
  const modifier = {};
10
10
  expect(pricingService.modifier.hasMatchTags(customer)).toBe(true);
11
- expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(true);
11
+ expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(
12
+ true
13
+ );
12
14
  customer.tags = [];
13
15
  modifier.tags = [];
14
16
  expect(pricingService.modifier.hasMatchTags(customer)).toBe(true);
15
- expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(true);
17
+ expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(
18
+ true
19
+ );
16
20
  customer.tags = ['all'];
17
21
  modifier.tags = ['all'];
18
22
  expect(pricingService.modifier.hasMatchTags(customer)).toBe(true);
19
- expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(true);
23
+ expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(
24
+ true
25
+ );
20
26
  customer.tags = ['All'];
21
27
  modifier.tags = ['All'];
22
28
  expect(pricingService.modifier.hasMatchTags(customer)).toBe(true);
23
- expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(true);
29
+ expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(
30
+ true
31
+ );
24
32
  customer.tags = ['test'];
25
33
  modifier.tags = ['test'];
26
34
  expect(pricingService.modifier.hasMatchTags(customer)).toBe(false);
27
- expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(false);
35
+ expect(pricingService.modifier.hasMatchTags(undefined, modifier)).toBe(
36
+ false
37
+ );
28
38
  customer.tags = ['test'];
29
39
  modifier.tags = ['all'];
30
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
40
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
41
+ false
42
+ );
31
43
  customer.tags = ['all'];
32
44
  modifier.tags = ['test'];
33
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
45
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
46
+ false
47
+ );
34
48
  customer.tags = ['all'];
35
49
  modifier.tags = ['all'];
36
50
  expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(true);
37
51
  customer.tags = ['test'];
38
52
  modifier.tags = ['test2'];
39
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
53
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
54
+ false
55
+ );
40
56
  customer.tags = ['test'];
41
57
  modifier.tags = ['test'];
42
58
  expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(true);
43
59
  customer.tags = ['test11', 'test12', 'test13', 'test14'];
44
60
  modifier.tags = ['test'];
45
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
61
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
62
+ false
63
+ );
46
64
  customer.tags = ['test11', 'test12', 'test13', 'test14'];
47
65
  modifier.tags = ['test21', 'test22', 'test23', 'test24'];
48
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
66
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
67
+ false
68
+ );
49
69
  customer.tags = ['test11', 'test12', 'test13', 'test14', 'match'];
50
70
  modifier.tags = ['test21', 'test22', 'test23', 'test24', 'match'];
51
71
  expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(true);
52
72
  customer.tags = ['test11', 'test12', 'test13', 'test14', 'Match'];
53
73
  modifier.tags = ['test21', 'test22', 'test23', 'test24', 'match'];
54
- expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(false);
74
+ expect(pricingService.modifier.hasMatchTags(customer, modifier)).toBe(
75
+ false
76
+ );
55
77
  });
56
78
  });
@@ -0,0 +1,46 @@
1
+ const isOptionsOverrideFunction = require('../../lib/modifier/isOptionsOverride');
2
+
3
+ describe('isOptionsOverride Function', () => {
4
+ const isOptionsOverride = isOptionsOverrideFunction();
5
+
6
+ test('should return false when no modifier is provided', () => {
7
+ const result = isOptionsOverride(null);
8
+ expect(result).toBe(false);
9
+ });
10
+
11
+ test('should return false when modifier does not have properties', () => {
12
+ const modifier = { someKey: 'someValue' };
13
+ const result = isOptionsOverride(modifier);
14
+ expect(result).toBe(false);
15
+ });
16
+
17
+ test('should return false when modifier properties do not have override', () => {
18
+ const modifier = { properties: {} };
19
+ const result = isOptionsOverride(modifier);
20
+ expect(result).toBe(false);
21
+ });
22
+
23
+ test('should return false when override has no options', () => {
24
+ const modifier = {
25
+ properties: {
26
+ override: {
27
+ options: [],
28
+ },
29
+ },
30
+ };
31
+ const result = isOptionsOverride(modifier);
32
+ expect(result).toBe(false);
33
+ });
34
+
35
+ test('should return true when override has options', () => {
36
+ const modifier = {
37
+ properties: {
38
+ override: {
39
+ options: ['option1', 'option2'],
40
+ },
41
+ },
42
+ };
43
+ const result = isOptionsOverride(modifier);
44
+ expect(result).toBe(true);
45
+ });
46
+ });
@@ -35,7 +35,7 @@ describe('Sort Function', () => {
35
35
  },
36
36
  ];
37
37
  const sortedModifiers = pricingService.modifier.sort(modifiers);
38
- const ids = sortedModifiers.map((mob) => mob._id);
38
+ const ids = sortedModifiers.map(mob => mob._id);
39
39
  expect(ids).toMatchObject([1, 3, 2]);
40
40
  });
41
41
  });
@@ -0,0 +1,133 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('Conditions not met for the item', () => {
6
+ test('#1: Item pieces condition is not met, errors bag populated', () => {
7
+ const order = {
8
+ id: 'ord-123',
9
+ items: [],
10
+ modifiers: [],
11
+ };
12
+ const item = { pieces: 1, itemId: '123', price: 100, modifiers: [] };
13
+ order.items.push(item);
14
+ const modifier = {
15
+ conditions: {
16
+ rules: [
17
+ {
18
+ key: 'itemPieces',
19
+ value: 5,
20
+ operand: '$gt',
21
+ },
22
+ ],
23
+ },
24
+ };
25
+ const conditionsBag = [];
26
+ const { addItemModifier } = pricingService.order;
27
+ addItemModifier({
28
+ order,
29
+ modifier,
30
+ itemIndex: 0,
31
+ onConditionsNotMet: bag => {
32
+ bag.forEach(condition => {
33
+ conditionsBag.push(condition);
34
+ });
35
+ },
36
+ });
37
+ expect(conditionsBag).toEqual([
38
+ {
39
+ current: 1,
40
+ name: '$gt.itemPieces',
41
+ value: 5,
42
+ },
43
+ ]);
44
+ });
45
+ // Write two more tests for the other conditions
46
+ test('#2: Item quantity condition is not met, errors bag populated', () => {
47
+ const order = {
48
+ id: 'ord-123',
49
+ items: [],
50
+ modifiers: [],
51
+ };
52
+ const item = { quantity: 1, itemId: '123', price: 100, modifiers: [] };
53
+ order.items.push(item);
54
+ const modifier = {
55
+ conditions: {
56
+ rules: [
57
+ {
58
+ key: 'itemQuantity',
59
+ value: 5,
60
+ operand: '$gt',
61
+ },
62
+ ],
63
+ },
64
+ };
65
+ const conditionsBag = [];
66
+ const { addItemModifier } = pricingService.order;
67
+ addItemModifier({
68
+ order,
69
+ modifier,
70
+ itemIndex: 0,
71
+ onConditionsNotMet: bag => {
72
+ bag.forEach(condition => {
73
+ conditionsBag.push(condition);
74
+ });
75
+ },
76
+ });
77
+ expect(conditionsBag).toEqual([
78
+ {
79
+ current: 1,
80
+ name: '$gt.itemQuantity',
81
+ value: 5,
82
+ },
83
+ ]);
84
+ });
85
+ test('#3: Required modifiers condition is not met, errors bag populated', () => {
86
+ const order = {
87
+ id: 'ord-123',
88
+ items: [],
89
+ modifiers: [],
90
+ };
91
+ const item = {
92
+ quantity: 1,
93
+ itemId: '123',
94
+ price: 100,
95
+ modifiers: [],
96
+ };
97
+ order.items.push(item);
98
+ const modifier = {
99
+ conditions: {
100
+ rules: [
101
+ {
102
+ key: 'modifiers',
103
+ value: [{ modifierId: 'mod-1' }],
104
+ operand: '$in',
105
+ },
106
+ ],
107
+ },
108
+ };
109
+ const conditionsBag = [];
110
+ const { addItemModifier } = pricingService.order;
111
+ addItemModifier({
112
+ order,
113
+ modifier,
114
+ itemIndex: 0,
115
+ onConditionsNotMet: bag => {
116
+ bag.forEach(condition => {
117
+ conditionsBag.push(condition);
118
+ });
119
+ },
120
+ });
121
+ expect(conditionsBag).toEqual([
122
+ {
123
+ current: [],
124
+ name: '$in.modifiers',
125
+ value: [
126
+ {
127
+ modifierId: 'mod-1',
128
+ },
129
+ ],
130
+ },
131
+ ]);
132
+ });
133
+ });
@@ -119,7 +119,9 @@ describe('Order actions', () => {
119
119
 
120
120
  const order = { items: [item1, item2], modifiers: [modifier1] };
121
121
 
122
- const newOrder = pricingService.order.calculate(order);
122
+ const newOrder = pricingService.order.calculate(order, {
123
+ paymentMethod: 'cash',
124
+ });
123
125
  expect(newOrder).toHaveProperty('total', 68);
124
126
  expect(newOrder).toHaveProperty('subTotal', 70);
125
127
  expect(newOrder).toHaveProperty('subTotals', {
@@ -1603,8 +1605,8 @@ describe('Order actions', () => {
1603
1605
  modifiers: paymentModifiers,
1604
1606
  order,
1605
1607
  });
1606
-
1607
- const newOrder = pricingService.order.calculate(order);
1608
+ const paymentMethod = { type: 'cash' };
1609
+ const newOrder = pricingService.order.calculate(order, { paymentMethod });
1608
1610
 
1609
1611
  expect(newOrder).toHaveProperty('total', 34.45);
1610
1612
  expect(newOrder).toHaveProperty('subTotal', 42.45);
@@ -1631,8 +1633,8 @@ describe('Order actions', () => {
1631
1633
  modifiers: paymentModifiers,
1632
1634
  order,
1633
1635
  });
1634
-
1635
- const newOrder = pricingService.order.calculate(order);
1636
+ const paymentMethod = { type: 'cash' };
1637
+ const newOrder = pricingService.order.calculate(order, { paymentMethod });
1636
1638
 
1637
1639
  expect(newOrder).toHaveProperty('total', 36.45);
1638
1640
  expect(newOrder).toHaveProperty('subTotal', 42.45);
@@ -1,6 +1,6 @@
1
1
  const moment = require('moment-timezone');
2
2
  const _ = require('lodash');
3
- const pickEndDateFunction = require('../../lib/order/pickEndDate');
3
+ const pickEndDateFunction = require('../../lib/store/pickEndDate');
4
4
 
5
5
  describe('pickEndDate function', () => {
6
6
  test('Get calculated Order, one item', () => {
@@ -56,8 +56,8 @@ describe('pickEndDate function', () => {
56
56
 
57
57
  const expectedDate = now
58
58
  .clone()
59
- .add(2, 'days')
60
- .set({ hour: 17, minute: 0, second: 0 })
59
+ .add(3, 'days')
60
+ .set({ hour: 0, minute: 0, second: 0 })
61
61
  .utc()
62
62
  .format();
63
63
 
@@ -113,7 +113,7 @@ describe('pickEndDate function', () => {
113
113
  const expectedDate = now
114
114
  .clone()
115
115
  .add(2, 'days') // Skips the closed Wednesday, goes to Thursday
116
- .set({ hour: 17, minute: 0, second: 0 })
116
+ .set({ hour: 0, minute: 0, second: 0 })
117
117
  .utc()
118
118
  .format();
119
119