@darkpos/pricing 1.0.43 → 1.0.45

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 (79) hide show
  1. package/__TEST__/item/getItemsModifierDescription.test.js +75 -78
  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/scripts/calculate-partially-paid/index.js +2 -2
  6. package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
  7. package/__TEST__/mocks/unpaid/order-modifiers.json +48 -52
  8. package/__TEST__/modifier/calculate.test.js +1 -0
  9. package/__TEST__/modifier/getMatchTagsModifiers.test.js +72 -18
  10. package/__TEST__/modifier/getModifierIndex.test.js +10 -7
  11. package/__TEST__/modifier/getRecommendedModifiers.test.js +6 -4
  12. package/__TEST__/modifier/hasAttribute.test.js +11 -5
  13. package/__TEST__/modifier/hasMatchTags.test.js +33 -11
  14. package/__TEST__/modifier/sort.test.js +1 -1
  15. package/__TEST__/order/conditionsNotMet.test.js +133 -0
  16. package/__TEST__/order/manualSplit.test.js +104 -0
  17. package/__TEST__/order/order-payment-modifier.test.js +1117 -0
  18. package/__TEST__/order/order.test.js +53 -105
  19. package/__TEST__/order/pickEndDate.test.js +7 -153
  20. package/__TEST__/order/validateConditionsCalculate.test.js +396 -0
  21. package/lib/constants/index.js +1 -1
  22. package/lib/index.js +11 -2
  23. package/lib/item/calculate.js +93 -18
  24. package/lib/item/getAmounts.js +14 -0
  25. package/lib/item/getBalance.js +0 -1
  26. package/lib/item/getItemModifiersDescription.js +1 -0
  27. package/lib/item/getItemsBalance.js +9 -0
  28. package/lib/item/getItemsTotals.js +27 -0
  29. package/lib/item/getTotal.js +3 -1
  30. package/lib/item/hasPaymentMethodType.js +17 -0
  31. package/lib/item/hasPaymentModifierWithPaymentId.js +9 -0
  32. package/lib/item/index.js +15 -8
  33. package/lib/item/removePaymentModifiersByPaymentId.js +13 -0
  34. package/lib/modifier/areConditionsMet.js +61 -0
  35. package/lib/modifier/calculatePaymentDiscountModifier.js +31 -0
  36. package/lib/modifier/calculatePaymentFeeModifier.js +27 -0
  37. package/lib/modifier/calculatePaymentModifier.js +67 -0
  38. package/lib/modifier/createDiscountModifier.js +2 -1
  39. package/lib/modifier/createFeeModifier.js +2 -1
  40. package/lib/modifier/getChildren.js +8 -0
  41. package/lib/modifier/getComputedAmount.js +9 -0
  42. package/lib/modifier/hasPaymentMethodType.js +14 -0
  43. package/lib/modifier/index.js +33 -9
  44. package/lib/modifier/isCalculatedPaymentModifier.js +4 -0
  45. package/lib/modifier/isChild.js +6 -0
  46. package/lib/modifier/isDiscount.js +6 -2
  47. package/lib/modifier/isFee.js +5 -2
  48. package/lib/modifier/isPaymentMethodModifier.js +4 -1
  49. package/lib/modifier/isPaymentTypeModifier.js +4 -1
  50. package/lib/modifier/isPercentage.js +10 -0
  51. package/lib/modifier/isValid.js +12 -0
  52. package/lib/modifier/sort.js +28 -0
  53. package/lib/modifier/validate.js +14 -0
  54. package/lib/modifier/validateDateDaysDiff.js +30 -0
  55. package/lib/modifier/validateInArr.js +12 -0
  56. package/lib/modifier/validateNumberCondition.js +20 -0
  57. package/lib/modifier/validateRequiredModifiers.js +16 -0
  58. package/lib/order/addItemModifier.js +72 -28
  59. package/lib/order/applyPayment.js +61 -0
  60. package/lib/order/calculate.js +45 -22
  61. package/lib/order/getBalance.js +0 -1
  62. package/lib/order/getOrdersBalance.js +8 -0
  63. package/lib/order/index.js +9 -10
  64. package/lib/order/manualSplit.js +34 -0
  65. package/lib/order/manualSplitByQuantity.js +50 -0
  66. package/lib/store/getRecommendedEndDate.js +13 -0
  67. package/lib/store/index.js +25 -0
  68. package/package.json +6 -4
  69. package/lib/item/getBalanceToPay.js +0 -12
  70. package/lib/item/getTotals.js +0 -40
  71. package/lib/item/markModifiersAsLocked.js +0 -11
  72. package/lib/modifier/createPaymentModifier.js +0 -12
  73. package/lib/modifier/findByPaymentMethod.js +0 -10
  74. package/lib/modifier/findByPaymentType.js +0 -10
  75. package/lib/modifier/getLockedModifiers.js +0 -5
  76. package/lib/order/markModifiersAsLocked.js +0 -14
  77. package/lib/order/removeModifiersWithPaymentMethods.js +0 -29
  78. package/lib/order/removeModifiersWithPaymentTypes.js +0 -27
  79. package/lib/{order → store}/pickEndDate.js +2 -2
@@ -9,8 +9,8 @@ const inputItems = require('../../partially-paid/input-items.json');
9
9
  const pricingService = usePricing();
10
10
 
11
11
  const paymentModifiers = orderModifiers.map(orderMod =>
12
- pricingService.modifier.createPaymentModifier({
13
- modifier: orderMod,
12
+ pricingService.modifier.duplicate({
13
+ orderMod,
14
14
  items: inputItems,
15
15
  })
16
16
  );
@@ -9,8 +9,8 @@ const inputItems = require('../../unpaid/input-items.json');
9
9
  const pricingService = usePricing();
10
10
 
11
11
  const paymentModifiers = orderModifiers.map(orderMod =>
12
- pricingService.modifier.createPaymentModifier({
13
- modifier: orderMod,
12
+ pricingService.modifier.duplicate({
13
+ ...orderMod,
14
14
  items: inputItems,
15
15
  })
16
16
  );
@@ -1,53 +1,49 @@
1
1
  [
2
- {
3
- "__typename": "Modifier",
4
- "addModifiers": [],
5
- "delModifiers": [],
6
- "conditions": {
7
- "__typename": "Conditions",
8
- "paymentTypes": [
9
- "cash"
10
- ],
11
- "paymentMethods": [],
12
- "modifiers": [],
13
- "minItemQuantity": 0,
14
- "minItemPieces": 0
15
- },
16
- "compute": {
17
- "__typename": "Compute",
18
- "type": "fixed",
19
- "action": "add",
20
- "amount": 2
21
- },
22
- "_createdAt": "2022-11-08T16:09:49.227Z",
23
- "_updatedAt": "2022-11-28T18:44:16.821Z",
24
- "_id": "636a7f3ec19c040636f3da9a",
25
- "modifierId": null,
26
- "_parentId": null,
27
- "locked": false,
28
- "name": "Cash fee",
29
- "sku": "",
30
- "description": "",
31
- "group": "",
32
- "type": "fee",
33
- "attributes": [],
34
- "color": "",
35
- "backgroundColor": "",
36
- "icon": "",
37
- "url": "",
38
- "tags": [
39
- "default"
40
- ],
41
- "order": 0,
42
- "included": false,
43
- "direct": true,
44
- "hidden": false,
45
- "print": true,
46
- "required": false,
47
- "recommended": false,
48
- "default": false,
49
- "code": "",
50
- "properties": null,
51
- "_computed": null
52
- }
53
- ]
2
+ {
3
+ "__typename": "Modifier",
4
+ "addModifiers": [],
5
+ "delModifiers": [],
6
+ "conditions": {
7
+ "rules": [ {
8
+ "key": "paymentTypes",
9
+ "operand": "$in",
10
+ "value": ["cash"]
11
+ }
12
+ ]
13
+ },
14
+ "compute": {
15
+ "__typename": "Compute",
16
+ "type": "fixed",
17
+ "action": "add",
18
+ "amount": 2
19
+ },
20
+ "_createdAt": "2022-11-08T16:09:49.227Z",
21
+ "_updatedAt": "2022-11-28T18:44:16.821Z",
22
+ "_id": "636a7f3ec19c040636f3da9a",
23
+ "modifierId": null,
24
+ "_parentId": null,
25
+ "locked": false,
26
+ "name": "Cash fee",
27
+ "sku": "",
28
+ "description": "",
29
+ "group": "",
30
+ "type": "fee",
31
+ "attributes": [],
32
+ "color": "",
33
+ "backgroundColor": "",
34
+ "icon": "",
35
+ "url": "",
36
+ "tags": ["default"],
37
+ "order": 0,
38
+ "included": false,
39
+ "direct": true,
40
+ "hidden": false,
41
+ "print": true,
42
+ "required": false,
43
+ "recommended": false,
44
+ "default": false,
45
+ "code": "",
46
+ "properties": null,
47
+ "_computed": null
48
+ }
49
+ ]
@@ -1,6 +1,7 @@
1
1
  const usePricing = require('../../index');
2
2
 
3
3
  const { getComputeModField } = require('../../lib/modifier/utils');
4
+
4
5
  const pricingService = usePricing();
5
6
 
6
7
  describe('Calculate Function', () => {
@@ -10,45 +10,75 @@ describe('GetMatchTagsModifiers Function', () => {
10
10
  const customer = {};
11
11
  let modifiers = [];
12
12
  expect(
13
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
13
+ pricingService.modifier.getMatchTagsModifiers({
14
+ modifiers,
15
+ entity: customer,
16
+ })
14
17
  ).toHaveLength(0);
15
18
  customer.tags = [];
16
19
  modifiers = [{ tags: [] }];
17
20
  expect(
18
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
21
+ pricingService.modifier.getMatchTagsModifiers({
22
+ modifiers,
23
+ entity: customer,
24
+ })
19
25
  ).toHaveLength(1);
20
26
  customer.tags = ['all'];
21
27
  expect(
22
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
28
+ pricingService.modifier.getMatchTagsModifiers({
29
+ modifiers,
30
+ entity: customer,
31
+ })
23
32
  ).toHaveLength(1);
24
33
  modifiers = [{ tags: ['test'] }];
25
34
  expect(
26
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
35
+ pricingService.modifier.getMatchTagsModifiers({
36
+ modifiers,
37
+ entity: customer,
38
+ })
27
39
  ).toHaveLength(0);
28
40
  modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
29
41
  expect(
30
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
42
+ pricingService.modifier.getMatchTagsModifiers({
43
+ modifiers,
44
+ entity: customer,
45
+ })
31
46
  ).toHaveLength(1);
32
47
  modifiers = [{ tags: ['all'] }, { tags: ['test'] }, { tags: [] }];
33
48
  expect(
34
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
49
+ pricingService.modifier.getMatchTagsModifiers({
50
+ modifiers,
51
+ entity: customer,
52
+ })
35
53
  ).toHaveLength(2);
36
54
  customer.tags = ['test'];
37
55
  modifiers = [{ tags: [] }];
38
56
  expect(
39
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
57
+ pricingService.modifier.getMatchTagsModifiers({
58
+ modifiers,
59
+ entity: customer,
60
+ })
40
61
  ).toHaveLength(0);
41
62
  modifiers = [{ tags: ['test'] }];
42
63
  expect(
43
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
64
+ pricingService.modifier.getMatchTagsModifiers({
65
+ modifiers,
66
+ entity: customer,
67
+ })
44
68
  ).toHaveLength(1);
45
69
  modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
46
70
  expect(
47
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
71
+ pricingService.modifier.getMatchTagsModifiers({
72
+ modifiers,
73
+ entity: customer,
74
+ })
48
75
  ).toHaveLength(1);
49
76
  modifiers = [{ tags: ['all'] }, { tags: ['test'] }, { tags: [] }];
50
77
  expect(
51
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
78
+ pricingService.modifier.getMatchTagsModifiers({
79
+ modifiers,
80
+ entity: customer,
81
+ })
52
82
  ).toHaveLength(1);
53
83
  modifiers = [
54
84
  { tags: ['all'] },
@@ -57,7 +87,10 @@ describe('GetMatchTagsModifiers Function', () => {
57
87
  { tags: ['Test'] },
58
88
  ];
59
89
  expect(
60
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
90
+ pricingService.modifier.getMatchTagsModifiers({
91
+ modifiers,
92
+ entity: customer,
93
+ })
61
94
  ).toHaveLength(1);
62
95
  modifiers = [
63
96
  { tags: ['all'] },
@@ -67,15 +100,24 @@ describe('GetMatchTagsModifiers Function', () => {
67
100
  { tags: ['test', 'Test'] },
68
101
  ];
69
102
  expect(
70
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
103
+ pricingService.modifier.getMatchTagsModifiers({
104
+ modifiers,
105
+ entity: customer,
106
+ })
71
107
  ).toHaveLength(2);
72
108
  modifiers = [{ tags: ['all'] }];
73
109
  expect(
74
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
110
+ pricingService.modifier.getMatchTagsModifiers({
111
+ modifiers,
112
+ entity: customer,
113
+ })
75
114
  ).toHaveLength(0);
76
115
  modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
77
116
  expect(
78
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
117
+ pricingService.modifier.getMatchTagsModifiers({
118
+ modifiers,
119
+ entity: customer,
120
+ })
79
121
  ).toHaveLength(1);
80
122
  modifiers = [
81
123
  { tags: ['all'] },
@@ -84,11 +126,17 @@ describe('GetMatchTagsModifiers Function', () => {
84
126
  {},
85
127
  ];
86
128
  expect(
87
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
129
+ pricingService.modifier.getMatchTagsModifiers({
130
+ modifiers,
131
+ entity: customer,
132
+ })
88
133
  ).toHaveLength(2);
89
134
  customer.tags = ['all'];
90
135
  expect(
91
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
136
+ pricingService.modifier.getMatchTagsModifiers({
137
+ modifiers,
138
+ entity: customer,
139
+ })
92
140
  ).toHaveLength(2);
93
141
  modifiers = [
94
142
  { tags: ['all'] },
@@ -99,11 +147,17 @@ describe('GetMatchTagsModifiers Function', () => {
99
147
  {},
100
148
  ];
101
149
  expect(
102
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
150
+ pricingService.modifier.getMatchTagsModifiers({
151
+ modifiers,
152
+ entity: customer,
153
+ })
103
154
  ).toHaveLength(5);
104
155
  customer.tags = undefined;
105
156
  expect(
106
- pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
157
+ pricingService.modifier.getMatchTagsModifiers({
158
+ modifiers,
159
+ entity: customer,
160
+ })
107
161
  ).toHaveLength(5);
108
162
  });
109
163
  });
@@ -12,7 +12,10 @@ describe('GetModifierIndex Function', () => {
12
12
  ).toBe(-1);
13
13
  const order = { modifiers: [{ modifierId: '123' }] };
14
14
  expect(
15
- pricingService.modifier.getModifierIndex({ entity: order, modifier: undefined })
15
+ pricingService.modifier.getModifierIndex({
16
+ entity: order,
17
+ modifier: undefined,
18
+ })
16
19
  ).toBe(-1);
17
20
  const modifier = { _id: '123' };
18
21
  expect(
@@ -29,12 +32,12 @@ describe('GetModifierIndex Function', () => {
29
32
  })
30
33
  ).toBe(-1);
31
34
  order.modifiers = [{ modifierId: '123' }, { modifierId: '234' }];
32
- expect(pricingService.modifier.getModifierIndex({ entity: order, modifier })).toBe(
33
- 1
34
- );
35
+ expect(
36
+ pricingService.modifier.getModifierIndex({ entity: order, modifier })
37
+ ).toBe(1);
35
38
  modifier._id = '123';
36
- expect(pricingService.modifier.getModifierIndex({ entity: order, modifier })).toBe(
37
- 0
38
- );
39
+ expect(
40
+ pricingService.modifier.getModifierIndex({ entity: order, modifier })
41
+ ).toBe(0);
39
42
  });
40
43
  });
@@ -5,13 +5,15 @@ const pricingService = usePricing();
5
5
  describe('GetRecommendedModifiers Function', () => {
6
6
  test('usecases', () => {
7
7
  const modifier = { modifier: { recommended: true } };
8
- expect(pricingService.modifier.getRecommendedModifiers([modifier])).toHaveLength(1);
8
+ expect(
9
+ pricingService.modifier.getRecommendedModifiers([modifier])
10
+ ).toHaveLength(1);
9
11
 
10
12
  const modifier2 = {
11
13
  modifier: { modifier: { _id: 1 } },
12
14
  };
13
- expect(pricingService.modifier.getRecommendedModifiers([modifier2])).toHaveLength(
14
- 0
15
- );
15
+ expect(
16
+ pricingService.modifier.getRecommendedModifiers([modifier2])
17
+ ).toHaveLength(0);
16
18
  });
17
19
  });
@@ -7,17 +7,23 @@ describe('Has Modifiers Function', () => {
7
7
  expect(pricingService.modifier.hasAttribute()).toBe(false);
8
8
  expect(pricingService.modifier.hasAttribute(undefined, 'test')).toBe(false);
9
9
  expect(pricingService.modifier.hasAttribute({}, undefined)).toBe(false);
10
- expect(pricingService.modifier.hasAttribute({ attributes: [] }, undefined)).toBe(
11
- false
12
- );
13
10
  expect(
14
- pricingService.modifier.hasAttribute({ attributes: [undefined] }, undefined)
11
+ pricingService.modifier.hasAttribute({ attributes: [] }, undefined)
12
+ ).toBe(false);
13
+ expect(
14
+ pricingService.modifier.hasAttribute(
15
+ { attributes: [undefined] },
16
+ undefined
17
+ )
15
18
  ).toBe(false);
16
19
  expect(
17
20
  pricingService.modifier.hasAttribute({ attributes: ['test'] }, undefined)
18
21
  ).toBe(false);
19
22
  expect(
20
- pricingService.modifier.hasAttribute({ attributes: ['undefined'] }, undefined)
23
+ pricingService.modifier.hasAttribute(
24
+ { attributes: ['undefined'] },
25
+ undefined
26
+ )
21
27
  ).toBe(false);
22
28
  expect(
23
29
  pricingService.modifier.hasAttribute(
@@ -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
  });
@@ -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
+ });