@darkpos/pricing 1.0.5 → 1.0.8

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 (242) hide show
  1. package/.eslintrc +8 -10
  2. package/__TEST__/item.test.js +137 -99
  3. package/__TEST__/modifier/calculate.test.js +73 -0
  4. package/__TEST__/modifier/getAvailablePromotionsOrSubscriptions.test.js +81 -0
  5. package/__TEST__/modifier/getMatchTagsModifiers.test.js +109 -0
  6. package/__TEST__/modifier/getModifierIndex.test.js +40 -0
  7. package/__TEST__/modifier/getRecommendedModifiers.test.js +17 -0
  8. package/__TEST__/modifier/hasAllTags.test.js +25 -0
  9. package/__TEST__/modifier/hasAttribute.test.js +47 -0
  10. package/__TEST__/modifier/hasMatchTags.test.js +56 -0
  11. package/__TEST__/modifier/hasNoTags.test.js +21 -0
  12. package/__TEST__/modifier/mocks/toggleModifier_no1.json +13440 -0
  13. package/__TEST__/modifier/mocks/toggleModifier_no2.json +13438 -0
  14. package/__TEST__/modifier/mocks/toggleModifier_no3.json +13598 -0
  15. package/__TEST__/modifier/sort.test.js +41 -0
  16. package/__TEST__/order.test.js +50 -37
  17. package/index.js +1 -25
  18. package/lib/constants/Modifier/Attributes.js +9 -0
  19. package/lib/constants/Modifier/Compute.js +15 -0
  20. package/lib/constants/Modifier/Types.js +7 -0
  21. package/lib/constants/Modifier/index.js +9 -0
  22. package/lib/constants/Status.js +39 -0
  23. package/lib/constants/index.js +7 -0
  24. package/lib/constants/status.js +39 -0
  25. package/lib/index.js +38 -0
  26. package/lib/item/addIndirectModifier.js +34 -17
  27. package/lib/item/calculate.js +41 -19
  28. package/lib/item/create.js +35 -0
  29. package/lib/item/filterByDirectModifiers.js +5 -6
  30. package/lib/item/findOriginalItem.js +8 -0
  31. package/lib/item/getBalanceToPay.js +12 -0
  32. package/lib/item/getDepartmentModifiers.js +5 -0
  33. package/lib/item/getDiscountModifiers.js +7 -0
  34. package/lib/item/getItemModifiers.js +16 -0
  35. package/lib/item/getItemModifiersDescription.js +22 -0
  36. package/lib/item/getItemPrice.js +57 -0
  37. package/lib/item/getItemsTotalPaid.js +9 -0
  38. package/lib/item/getItemsTotalPieces.js +7 -0
  39. package/lib/item/getNotIncludedModifiers.js +11 -0
  40. package/lib/item/getParentItem.js +5 -0
  41. package/lib/item/getPendingIndex.js +5 -0
  42. package/lib/item/getTotal.js +8 -0
  43. package/lib/item/getTotalPieces.js +7 -0
  44. package/lib/item/getTotals.js +25 -0
  45. package/lib/item/hasCreateSubscription.js +7 -0
  46. package/lib/item/hasModifier.js +15 -0
  47. package/lib/item/hasModifierWithValue.js +11 -0
  48. package/lib/item/hasModifiers.js +9 -0
  49. package/lib/item/hasSubscription.js +5 -0
  50. package/lib/item/index.js +80 -10
  51. package/lib/item/isParentIncluded.js +4 -0
  52. package/lib/item/isRelatedItem.js +4 -0
  53. package/lib/item/isSelected.js +13 -0
  54. package/lib/item/openRecommendationDialog.js +4 -0
  55. package/lib/item/removeModifier.js +84 -0
  56. package/lib/item/removeModifierById.js +8 -0
  57. package/lib/item/removeModifiers.js +14 -0
  58. package/lib/modifier/addFile.js +19 -0
  59. package/lib/modifier/addImage.js +18 -0
  60. package/lib/modifier/calculate.js +43 -0
  61. package/lib/modifier/contains.js +5 -0
  62. package/lib/modifier/convertMenuModifiers.js +5 -0
  63. package/lib/modifier/create.js +44 -0
  64. package/lib/modifier/createAmountOverrideModifier.js +20 -0
  65. package/lib/modifier/createCreditModifier.js +32 -0
  66. package/lib/modifier/createDiscountModifier.js +25 -0
  67. package/lib/modifier/createGroupValuesModifier.js +29 -0
  68. package/lib/modifier/createIndirectModifier.js +42 -0
  69. package/lib/modifier/createSubscriptionModifier.js +19 -0
  70. package/lib/modifier/displayAsCard.js +9 -0
  71. package/lib/modifier/displayAsChips.js +9 -0
  72. package/lib/modifier/displayAsList.js +9 -0
  73. package/lib/modifier/duplicate.js +12 -0
  74. package/lib/modifier/enableAutoPopup.js +9 -0
  75. package/lib/modifier/filterByRequiredModifiers.js +4 -0
  76. package/lib/modifier/findById.js +7 -0
  77. package/lib/modifier/findByPaymentMethod.js +10 -0
  78. package/lib/modifier/findByPaymentType.js +10 -0
  79. package/lib/modifier/getAvailablePromotions.js +5 -0
  80. package/lib/modifier/getAvailablePromotionsOrSubscriptions.js +15 -0
  81. package/lib/modifier/getAvailableSubscriptions.js +5 -0
  82. package/lib/modifier/getByAttribute.js +6 -0
  83. package/lib/modifier/getCreditModifier.js +6 -0
  84. package/lib/modifier/getDefaultModifiers.js +4 -0
  85. package/lib/modifier/getDepartmentMaxItems.js +10 -0
  86. package/lib/modifier/getDepartmentModifiers.js +11 -0
  87. package/lib/modifier/getDescription.js +6 -0
  88. package/lib/modifier/getDescriptions.js +9 -0
  89. package/lib/modifier/getDirectModifiers.js +5 -0
  90. package/lib/modifier/getDiscountModifiers.js +7 -0
  91. package/lib/modifier/getEntityModifiers.js +11 -0
  92. package/lib/modifier/getEntityPromotions.js +11 -0
  93. package/lib/modifier/getExtendedTags.js +14 -0
  94. package/lib/modifier/getFiles.js +5 -0
  95. package/lib/modifier/getGroupModifiers.js +9 -0
  96. package/lib/modifier/getGroupOfItemsModifiers.js +4 -0
  97. package/lib/modifier/getGroupRelatedModifiers.js +26 -0
  98. package/lib/modifier/getImages.js +5 -0
  99. package/lib/modifier/getInheritedModifiers.js +30 -0
  100. package/lib/modifier/getItemModifiers.js +39 -0
  101. package/lib/modifier/getLaundryModifiers.js +9 -0
  102. package/lib/modifier/getMatchTagsModifiers.js +9 -0
  103. package/lib/modifier/getModifierIndex.js +14 -0
  104. package/lib/modifier/getPreferences.js +24 -0
  105. package/lib/modifier/getPromotionModifiers.js +5 -0
  106. package/lib/modifier/getPromptMessage.js +6 -0
  107. package/lib/modifier/getProperty.js +11 -0
  108. package/lib/modifier/getRecommendedModifiers.js +7 -0
  109. package/lib/modifier/getRelatedModifiers.js +10 -0
  110. package/lib/modifier/getRequiredModifiers.js +4 -0
  111. package/lib/modifier/getSelectedValues.js +7 -0
  112. package/lib/modifier/getService.js +5 -0
  113. package/lib/modifier/getSplittedModifiers.js +30 -0
  114. package/lib/modifier/getStarchModifier.js +9 -0
  115. package/lib/modifier/getSubscriptionItem.js +12 -0
  116. package/lib/modifier/getSubscriptionModifiers.js +5 -0
  117. package/lib/modifier/getSuggestion.js +9 -0
  118. package/lib/modifier/getUnhiddenModifiers.js +9 -0
  119. package/lib/modifier/getUsingCount.js +11 -0
  120. package/lib/modifier/hasAddModifiers.js +5 -0
  121. package/lib/modifier/hasAllTag.js +12 -0
  122. package/lib/modifier/hasAttribute.js +8 -0
  123. package/lib/modifier/hasAttributes.js +19 -0
  124. package/lib/modifier/hasCreateSubscription.js +10 -0
  125. package/lib/modifier/hasDisplayPrompt.js +9 -0
  126. package/lib/modifier/hasMatchTags.js +27 -0
  127. package/lib/modifier/hasModifier.js +12 -0
  128. package/lib/modifier/hasNoTags.js +9 -0
  129. package/lib/modifier/hasRelatedItems.js +8 -0
  130. package/lib/modifier/includePiecesInQuantity.js +10 -0
  131. package/lib/modifier/includesInGroup.js +9 -0
  132. package/lib/modifier/index.js +242 -0
  133. package/lib/modifier/isAmountOverride.js +7 -0
  134. package/lib/modifier/isAvailablePromotion.js +23 -0
  135. package/lib/modifier/isAvailableSubscription.js +28 -0
  136. package/lib/modifier/isCompact.js +12 -0
  137. package/lib/modifier/isCredit.js +6 -0
  138. package/lib/modifier/isCustomerTagsExtend.js +9 -0
  139. package/lib/modifier/isDefault.js +4 -0
  140. package/lib/modifier/isDepartment.js +6 -0
  141. package/lib/modifier/isDirect.js +6 -0
  142. package/lib/modifier/isDiscount.js +6 -0
  143. package/lib/modifier/isEditable.js +10 -0
  144. package/lib/modifier/isGratuity.js +6 -0
  145. package/lib/modifier/isGroupOfItems.js +12 -0
  146. package/lib/modifier/isGroupOfModifiers.js +12 -0
  147. package/lib/modifier/isGroupOfValues.js +13 -0
  148. package/lib/modifier/isHidden.js +4 -0
  149. package/lib/modifier/isIncluded.js +4 -0
  150. package/lib/modifier/isManual.js +9 -0
  151. package/lib/modifier/isMultiplier.js +11 -0
  152. package/lib/modifier/isOverride.js +6 -0
  153. package/lib/modifier/isPaymentMethods.js +8 -0
  154. package/lib/modifier/isPaymentTypes.js +8 -0
  155. package/lib/modifier/isPreferences.js +8 -0
  156. package/lib/modifier/isPriceOverride.js +7 -0
  157. package/lib/modifier/isPromotion.js +6 -0
  158. package/lib/modifier/isQuantityOverride.js +7 -0
  159. package/lib/modifier/isRemoveModifier.js +4 -0
  160. package/lib/modifier/isRequired.js +4 -0
  161. package/lib/modifier/isRequiredAndOverride.js +4 -0
  162. package/lib/modifier/isService.js +9 -0
  163. package/lib/modifier/isSingleValue.js +9 -0
  164. package/lib/modifier/isSubscription.js +6 -0
  165. package/lib/modifier/isTrackUsageSubscription.js +5 -0
  166. package/lib/modifier/isValueSelected.js +7 -0
  167. package/lib/modifier/purifyModifiers.js +15 -0
  168. package/lib/modifier/sort.js +35 -0
  169. package/lib/order/addCreditModifier.js +27 -0
  170. package/lib/order/addDiscountModifier.js +9 -0
  171. package/lib/order/addItem.js +292 -0
  172. package/lib/order/addItemModifier.js +214 -0
  173. package/lib/order/addModifier.js +9 -0
  174. package/lib/order/addModifierToAllItems.js +16 -0
  175. package/lib/order/addNote.js +25 -0
  176. package/lib/order/addTable.js +11 -0
  177. package/lib/order/calculate.js +51 -16
  178. package/lib/order/calculateDue.js +15 -0
  179. package/lib/order/calculateOrdersDue.js +26 -0
  180. package/lib/order/canSplit.js +4 -0
  181. package/lib/order/changeEndDate.js +15 -0
  182. package/lib/order/changeLocation.js +20 -0
  183. package/lib/order/changeStartDate.js +14 -0
  184. package/lib/order/combineSubOrders.js +14 -0
  185. package/lib/order/containsSerial.js +5 -0
  186. package/lib/order/create.js +67 -0
  187. package/lib/order/createParent.js +9 -0
  188. package/lib/order/duplicateItem.js +16 -0
  189. package/lib/order/findItemIndex.js +6 -0
  190. package/lib/order/getAppliedCredit.js +5 -0
  191. package/lib/order/getBalance.js +9 -0
  192. package/lib/order/getCustomer.js +4 -0
  193. package/lib/order/getCustomerStoreCredit.js +14 -0
  194. package/lib/order/getCustomerSubscriptions.js +8 -0
  195. package/lib/order/getCustomerSubscriptionsByItem.js +14 -0
  196. package/lib/order/getDepartmentModifiers.js +9 -0
  197. package/lib/order/getEndDate.js +4 -0
  198. package/lib/order/getItemByItemId.js +6 -0
  199. package/lib/order/getItemIndex.js +9 -0
  200. package/lib/order/getItemsToPay.js +17 -0
  201. package/lib/order/getMenuItemsByCustomer.js +19 -0
  202. package/lib/order/getNumberOfItems.js +10 -0
  203. package/lib/order/getOrdersPieces.js +10 -0
  204. package/lib/order/getPendingItemIndex.js +5 -0
  205. package/lib/order/getRelatedItems.js +11 -0
  206. package/lib/order/getSameItems.js +19 -0
  207. package/lib/order/getScheduleByCustomer.js +20 -0
  208. package/lib/order/getScreenCustomerNotes.js +12 -0
  209. package/lib/order/getSelectedItem.js +6 -0
  210. package/lib/order/getStartDate.js +4 -0
  211. package/lib/order/getTip.js +10 -0
  212. package/lib/order/getTotalPieces.js +17 -0
  213. package/lib/order/getTotals.js +28 -0
  214. package/lib/order/hasAggregate.js +20 -0
  215. package/lib/order/hasItem.js +7 -0
  216. package/lib/order/hasRemainingSubscription.js +67 -0
  217. package/lib/order/hold.js +12 -0
  218. package/lib/order/index.js +163 -5
  219. package/lib/order/isDetailed.js +4 -0
  220. package/lib/order/isHold.js +4 -0
  221. package/lib/order/isNew.js +4 -0
  222. package/lib/order/isOpen.js +4 -0
  223. package/lib/order/isParent.js +7 -0
  224. package/lib/order/merge.js +96 -0
  225. package/lib/order/open.js +11 -0
  226. package/lib/order/pickEndDate.js +70 -0
  227. package/lib/order/removeApplyModifier.js +24 -0
  228. package/lib/order/removeDiscountModifier.js +16 -0
  229. package/lib/order/removeItem.js +55 -0
  230. package/lib/order/removeItemByIndex.js +5 -0
  231. package/lib/order/removeItemModifier.js +69 -0
  232. package/lib/order/removeModifier.js +21 -0
  233. package/lib/order/removeModifierByAttribute.js +23 -0
  234. package/lib/order/removeModifiersWithPaymentMethods.js +29 -0
  235. package/lib/order/removeModifiersWithPaymentTypes.js +27 -0
  236. package/lib/order/setCustomer.js +50 -0
  237. package/lib/order/splitByDepartments.js +151 -0
  238. package/lib/order/toggleModifier.js +9 -0
  239. package/lib/order/updateItem.js +8 -0
  240. package/lib/order/updateItemQuantity.js +35 -0
  241. package/lib/order/void.js +11 -0
  242. package/package.json +15 -18
package/.eslintrc CHANGED
@@ -7,18 +7,16 @@
7
7
  "plugins": ["prettier", "jest"],
8
8
  "rules": {
9
9
  "no-debugger": 0,
10
- "import/no-extraneous-dependencies": 0,
11
10
  "no-underscore-dangle": 0,
12
- "import/no-dynamic-require": 0,
13
11
  "no-console": 0,
14
12
  "global-require": 0,
15
- "prettier/prettier": [
16
- "error",
17
- {
18
- "trailingComma": "es5",
19
- "singleQuote": true,
20
- "printWidth": 100
21
- }
22
- ]
13
+ "prettier/prettier": ["error", {
14
+ "singleQuote": true,
15
+ "trailingComma": "es5",
16
+ "bracketSpacing": true,
17
+ "bracketSameLine": false,
18
+ "arrowParens": "avoid",
19
+ "parser": "flow"
20
+ }]
23
21
  }
24
22
  }
@@ -6,25 +6,37 @@ describe('Item actions', () => {
6
6
  test('Get calculated Item: included: false && direct: false', () => {
7
7
  const modifiers = [
8
8
  {
9
- amount: 20,
10
- type: 'percentage',
11
- action: 'add',
9
+ compute: {
10
+ amount: 20,
11
+ type: 'percentage',
12
+ action: 'add',
13
+ },
12
14
  name: 'modifier1',
13
- category: 'discount',
15
+ type: 'discount',
16
+ direct: false,
17
+ included: false,
14
18
  },
15
19
  {
16
- amount: 20,
17
- type: 'percentage',
18
- action: 'subtract',
20
+ compute: {
21
+ amount: 20,
22
+ type: 'percentage',
23
+ action: 'subtract',
24
+ },
19
25
  name: 'modifier2',
20
- category: 'discount',
26
+ type: 'discount',
27
+ direct: false,
28
+ included: false,
21
29
  },
22
30
  {
23
- amount: 2,
24
- type: 'fixed',
25
- action: 'subtract',
31
+ compute: {
32
+ amount: 2,
33
+ type: 'fixed',
34
+ action: 'subtract',
35
+ },
26
36
  name: 'modifier3',
27
- category: 'discount',
37
+ type: 'discount',
38
+ direct: false,
39
+ included: false,
28
40
  },
29
41
  ];
30
42
  const orderItem = { price: 30, quantity: 2, modifiers };
@@ -34,8 +46,8 @@ describe('Item actions', () => {
34
46
  discount: -4,
35
47
  _included: 0,
36
48
  _xincluded: -4,
37
- _direct: -4,
38
- _xdirect: 0,
49
+ _direct: 0,
50
+ _xdirect: -4,
39
51
  _simple: 60,
40
52
  _actual: 60,
41
53
  });
@@ -43,28 +55,33 @@ describe('Item actions', () => {
43
55
  test('Get calculated Item: included: false && direct: true', () => {
44
56
  const modifiers = [
45
57
  {
46
- amount: 2,
47
- type: 'fixed',
48
- action: 'subtract',
49
- properties: {
50
- direct: false,
51
- },
52
- category: 'discount',
58
+ compute: { amount: 2, type: 'fixed', action: 'subtract' },
59
+ type: 'discount',
53
60
  name: 'modifier1',
61
+ direct: true,
62
+ included: false,
54
63
  },
55
64
  {
56
- amount: 20,
57
- type: 'percentage',
58
- action: 'subtract',
65
+ compute: {
66
+ amount: 20,
67
+ type: 'percentage',
68
+ action: 'subtract',
69
+ },
59
70
  name: 'modifier2',
60
- category: 'discount',
71
+ type: 'discount',
72
+ direct: true,
73
+ included: false,
61
74
  },
62
75
  {
63
- amount: 2,
64
- type: 'fixed',
65
- action: 'add',
76
+ compute: {
77
+ amount: 2,
78
+ type: 'fixed',
79
+ action: 'add',
80
+ },
66
81
  name: 'modifier3',
67
- category: 'tax',
82
+ type: 'tax',
83
+ direct: true,
84
+ included: false,
68
85
  },
69
86
  ];
70
87
  const orderItem = { price: 30, quantity: 2, modifiers };
@@ -75,8 +92,8 @@ describe('Item actions', () => {
75
92
  tax: 4,
76
93
  _included: 0,
77
94
  _xincluded: -12,
78
- _direct: -8,
79
- _xdirect: -4,
95
+ _direct: -12,
96
+ _xdirect: 0,
80
97
  _simple: 60,
81
98
  _actual: 60,
82
99
  });
@@ -84,57 +101,63 @@ describe('Item actions', () => {
84
101
  test('Get calculated Item: included: true && direct: true', () => {
85
102
  const modifiers = [
86
103
  {
87
- amount: 2,
88
- type: 'fixed',
89
- action: 'subtract',
90
- name: 'modifier1',
91
- category: 'discount',
92
- properties: {
93
- direct: false,
104
+ compute: {
105
+ amount: 2,
106
+ type: 'fixed',
107
+ action: 'subtract',
94
108
  },
109
+ name: 'modifier1',
110
+ type: 'discount',
111
+ direct: true,
112
+ included: true,
95
113
  },
96
114
  {
97
- amount: 20,
98
- type: 'percentage',
99
- action: 'subtract',
115
+ compute: {
116
+ amount: 20,
117
+ type: 'percentage',
118
+ action: 'subtract',
119
+ },
100
120
  name: 'modifier2',
101
- category: 'discount',
121
+ type: 'discount',
122
+ direct: true,
123
+ included: true,
102
124
  },
103
125
  {
104
- amount: 2,
105
- type: 'fixed',
106
- action: 'add',
107
- name: 'modifier3',
108
- category: 'upcharge',
109
- properties: {
110
- included: true,
126
+ compute: {
127
+ amount: 2,
128
+ type: 'fixed',
129
+ action: 'add',
111
130
  },
131
+ name: 'modifier3',
132
+ type: 'upcharge',
133
+ direct: true,
134
+ included: true,
112
135
  },
113
136
  ];
114
137
  const orderItem = { price: 30, quantity: 2, modifiers };
115
138
  const newItem = pricingService.item.calculate(orderItem);
116
139
  expect(newItem).toHaveProperty('total', 48);
117
140
  expect(newItem).toHaveProperty('subTotals', {
118
- discount: -16,
119
- _included: 4,
120
- _xincluded: -16,
121
- _direct: -8,
122
- _xdirect: -4,
141
+ _included: -12,
142
+ _xincluded: 0,
143
+ _direct: -12,
144
+ _xdirect: 0,
123
145
  _simple: 60,
124
- _actual: 64,
146
+ _actual: 48,
125
147
  });
126
148
  });
127
149
  test('Get calculated Item: included: true, sample 1', () => {
128
150
  const modifiers = [
129
151
  {
130
- amount: 2,
131
- type: 'fixed',
132
- action: 'add',
133
- name: 'modifier1',
134
- category: 'upcharge',
135
- properties: {
136
- included: true,
152
+ compute: {
153
+ amount: 2,
154
+ type: 'fixed',
155
+ action: 'add',
137
156
  },
157
+ name: 'modifier1',
158
+ type: 'upcharge',
159
+ included: true,
160
+ direct: true,
138
161
  },
139
162
  ];
140
163
  const orderItem = { price: 2, quantity: 1, modifiers };
@@ -152,96 +175,111 @@ describe('Item actions', () => {
152
175
  test('Get calculated Item: included: true, sample 2', () => {
153
176
  const modifiers = [
154
177
  {
155
- amount: 2,
156
- type: 'fixed',
157
- action: 'add',
178
+ compute: { amount: 2, type: 'fixed', action: 'add' },
158
179
  name: 'modifier1',
159
- category: 'upcharge',
180
+ type: 'upcharge',
181
+ included: true,
182
+ direct: true,
160
183
  properties: {
161
- included: true,
162
184
  sort: 1,
163
185
  },
164
186
  },
165
187
  {
166
- amount: 10,
167
- type: 'percentage',
168
- action: 'add',
188
+ compute: {
189
+ amount: 10,
190
+ type: 'percentage',
191
+ action: 'add',
192
+ },
169
193
  name: 'modifier2',
170
- category: 'tax',
194
+ type: 'tax',
171
195
  properties: {
172
196
  sort: 2,
173
197
  },
198
+ included: true,
199
+ direct: true,
174
200
  },
175
201
  ];
176
202
  const orderItem = { price: 2, quantity: 1, modifiers };
177
203
  const newItem = pricingService.item.calculate(orderItem);
178
204
  expect(newItem).toHaveProperty('total', 4.4);
179
205
  expect(newItem).toHaveProperty('subTotals', {
180
- _included: 2,
181
- _xincluded: 0.4,
206
+ _included: 2.4,
207
+ _xincluded: 0,
182
208
  _direct: 2.4,
183
209
  _xdirect: 0,
184
210
  _simple: 2,
185
- _actual: 4,
186
- tax: 0.4,
211
+ _actual: 4.4,
187
212
  });
188
213
  });
189
214
  test('Get calculated Item: included: true, sample 3', () => {
190
215
  const modifiers = [
191
216
  {
192
- amount: 2,
193
- type: 'fixed',
194
- action: 'subtract',
217
+ compute: {
218
+ amount: 2,
219
+ type: 'fixed',
220
+ action: 'subtract',
221
+ },
195
222
  name: 'modifier1',
196
- category: 'discount',
223
+ type: 'discount',
197
224
  properties: {
198
225
  sort: 1,
199
226
  },
227
+ included: true,
228
+ direct: true,
200
229
  },
201
230
  {
202
- amount: 2,
203
- type: 'fixed',
204
- action: 'add',
231
+ compute: {
232
+ amount: 2,
233
+ type: 'fixed',
234
+ action: 'add',
235
+ },
205
236
  name: 'modifier1',
206
- category: 'upcharge',
237
+ type: 'upcharge',
238
+ included: true,
239
+ direct: true,
207
240
  properties: {
208
- included: true,
209
241
  sort: 1,
210
242
  },
211
243
  },
212
244
  {
213
- amount: 10,
214
- type: 'percentage',
215
- action: 'add',
245
+ compute: {
246
+ amount: 10,
247
+ type: 'percentage',
248
+ action: 'add',
249
+ },
216
250
  name: 'modifier2',
217
- category: 'tax',
251
+ type: 'tax',
218
252
  properties: {
219
253
  sort: 2,
220
254
  },
255
+ included: true,
256
+ direct: true,
221
257
  },
222
258
  {
223
- amount: 6,
224
- type: 'percentage',
225
- action: 'add',
259
+ compute: {
260
+ amount: 6,
261
+ type: 'percentage',
262
+ action: 'add',
263
+ },
226
264
  name: 'modifier3',
227
- category: 'tax',
265
+ type: 'tax',
228
266
  properties: {
229
267
  sort: 2,
230
268
  },
269
+ included: true,
270
+ direct: true,
231
271
  },
232
272
  ];
233
273
  const orderItem = { price: 2, quantity: 1, modifiers };
234
274
  const newItem = pricingService.item.calculate(orderItem);
235
- expect(newItem).toHaveProperty('total', 2.64);
275
+ expect(newItem).toHaveProperty('total', 2.32);
236
276
  expect(newItem).toHaveProperty('subTotals', {
237
- _included: 2,
238
- _xincluded: -1.36,
239
- _direct: 0.64,
240
277
  _xdirect: 0,
241
278
  _simple: 2,
242
- _actual: 4,
243
- tax: 0.64,
244
- discount: -2,
279
+ _actual: 2.32,
280
+ _direct: 0.32,
281
+ _included: 0.32,
282
+ _xincluded: 0,
245
283
  });
246
284
  });
247
285
  });
@@ -0,0 +1,73 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('Calculate Function', () => {
6
+ test('calculate with fix amount and add action', () => {
7
+ const modifier = {
8
+ compute: {
9
+ type: 'fixed',
10
+ action: 'add',
11
+ amount: 10,
12
+ },
13
+ name: 'someModifier',
14
+ };
15
+ const orderItem = { price: 30, quantity: 1 };
16
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
17
+ expect(computedModifier._computed).toMatchObject({
18
+ amount: 10,
19
+ description: 'someModifier ($10.00)',
20
+ });
21
+ });
22
+
23
+ test('calculate with fix amount and subtract action', () => {
24
+ const modifier = {
25
+ compute: {
26
+ type: 'fixed',
27
+ action: 'subtract',
28
+ amount: 10,
29
+ },
30
+ name: 'someModifier',
31
+ };
32
+ const orderItem = { price: 30, quantity: 1 };
33
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
34
+ expect(computedModifier._computed).toMatchObject({
35
+ amount: -10,
36
+ description: 'someModifier (-$10.00)',
37
+ });
38
+ });
39
+
40
+ test('calculate with percentage amount and subtract action', () => {
41
+ const modifier = {
42
+ compute: {
43
+ amount: 20,
44
+ type: 'percentage',
45
+ action: 'subtract',
46
+ },
47
+ name: 'someModifier',
48
+ };
49
+ const orderItem = { price: 30, quantity: 1 };
50
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
51
+ expect(computedModifier._computed).toMatchObject({
52
+ amount: -6,
53
+ description: 'someModifier (-$6.00)',
54
+ });
55
+ });
56
+
57
+ test('calculate with percentage amount and add action', () => {
58
+ const modifier = {
59
+ compute: {
60
+ amount: 20,
61
+ type: 'percentage',
62
+ action: 'add',
63
+ },
64
+ name: 'someModifier',
65
+ };
66
+ const orderItem = { price: 30, quantity: 1 };
67
+ const computedModifier = pricingService.modifier.calculate(modifier, orderItem);
68
+ expect(computedModifier._computed).toMatchObject({
69
+ amount: 6,
70
+ description: 'someModifier ($6.00)',
71
+ });
72
+ });
73
+ });
@@ -0,0 +1,81 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('GetAvailablePromotionsOrSubscriptions Function', () => {
6
+ test('usecases', () => {
7
+ const today = new Date();
8
+ expect(
9
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions()
10
+ ).toStrictEqual([]);
11
+
12
+ const modifier = { attributes: ['promotion'] };
13
+ expect(
14
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
15
+ ).toStrictEqual([modifier]);
16
+
17
+ modifier.properties = {
18
+ promotion: { numberOfUsesLimit: 4, numberOfUses: 3 },
19
+ };
20
+ expect(
21
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
22
+ ).toStrictEqual([modifier]);
23
+
24
+ modifier.properties.promotion.numberOfUses = 4;
25
+ expect(
26
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
27
+ ).toStrictEqual([]);
28
+
29
+ modifier.properties.promotion.numberOfUses = 5;
30
+ expect(
31
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
32
+ ).toStrictEqual([]);
33
+
34
+ modifier.properties = {
35
+ promotion: { dateLimit: { from: today.toISOString(), to: '' } },
36
+ };
37
+ expect(
38
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
39
+ ).toStrictEqual([modifier]);
40
+
41
+ const tomorrow = new Date(today);
42
+ tomorrow.setDate(tomorrow.getDate() + 1);
43
+ modifier.properties.promotion.dateLimit.from = tomorrow.toISOString();
44
+ expect(
45
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
46
+ ).toStrictEqual([]);
47
+
48
+ const yesterday = new Date(today);
49
+ yesterday.setDate(yesterday.getDate() - 1);
50
+ modifier.properties.promotion.dateLimit.from = yesterday.toISOString();
51
+ expect(
52
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
53
+ ).toStrictEqual([modifier]);
54
+
55
+ modifier.properties.promotion.dateLimit.to = yesterday.toISOString();
56
+ expect(
57
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
58
+ ).toStrictEqual([]);
59
+
60
+ modifier.properties.promotion.dateLimit.to = today.toISOString();
61
+ expect(
62
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
63
+ ).toStrictEqual([modifier]);
64
+
65
+ modifier.properties.promotion.dateLimit.to = tomorrow.toISOString();
66
+ expect(
67
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
68
+ ).toStrictEqual([modifier]);
69
+
70
+ modifier.attributes = ['subscription'];
71
+ modifier.properties.subscription = modifier.properties.promotion;
72
+ expect(
73
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
74
+ ).toStrictEqual([modifier]);
75
+
76
+ delete modifier.properties;
77
+ expect(
78
+ pricingService.modifier.getAvailablePromotionsOrSubscriptions([modifier])
79
+ ).toStrictEqual([]);
80
+ });
81
+ });
@@ -0,0 +1,109 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('GetMatchTagsModifiers Function', () => {
6
+ test('usecases', () => {
7
+ expect(
8
+ pricingService.modifier.getMatchTagsModifiers({ modifiers: [] })
9
+ ).toHaveLength(0);
10
+ const customer = {};
11
+ let modifiers = [];
12
+ expect(
13
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
14
+ ).toHaveLength(0);
15
+ customer.tags = [];
16
+ modifiers = [{ tags: [] }];
17
+ expect(
18
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
19
+ ).toHaveLength(1);
20
+ customer.tags = ['all'];
21
+ expect(
22
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
23
+ ).toHaveLength(1);
24
+ modifiers = [{ tags: ['test'] }];
25
+ expect(
26
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
27
+ ).toHaveLength(0);
28
+ modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
29
+ expect(
30
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
31
+ ).toHaveLength(1);
32
+ modifiers = [{ tags: ['all'] }, { tags: ['test'] }, { tags: [] }];
33
+ expect(
34
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
35
+ ).toHaveLength(2);
36
+ customer.tags = ['test'];
37
+ modifiers = [{ tags: [] }];
38
+ expect(
39
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
40
+ ).toHaveLength(0);
41
+ modifiers = [{ tags: ['test'] }];
42
+ expect(
43
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
44
+ ).toHaveLength(1);
45
+ modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
46
+ expect(
47
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
48
+ ).toHaveLength(1);
49
+ modifiers = [{ tags: ['all'] }, { tags: ['test'] }, { tags: [] }];
50
+ expect(
51
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
52
+ ).toHaveLength(1);
53
+ modifiers = [
54
+ { tags: ['all'] },
55
+ { tags: ['test'] },
56
+ { tags: [] },
57
+ { tags: ['Test'] },
58
+ ];
59
+ expect(
60
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
61
+ ).toHaveLength(1);
62
+ modifiers = [
63
+ { tags: ['all'] },
64
+ { tags: ['test'] },
65
+ { tags: [] },
66
+ { tags: ['Test'] },
67
+ { tags: ['test', 'Test'] },
68
+ ];
69
+ expect(
70
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
71
+ ).toHaveLength(2);
72
+ modifiers = [{ tags: ['all'] }];
73
+ expect(
74
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
75
+ ).toHaveLength(0);
76
+ modifiers = [{ tags: ['all'] }, { tags: ['test'] }];
77
+ expect(
78
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
79
+ ).toHaveLength(1);
80
+ modifiers = [
81
+ { tags: ['all'] },
82
+ { tags: ['test'] },
83
+ { tags: ['test'], attributes: ['hidden'] },
84
+ {},
85
+ ];
86
+ expect(
87
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
88
+ ).toHaveLength(2);
89
+ customer.tags = ['all'];
90
+ expect(
91
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
92
+ ).toHaveLength(2);
93
+ modifiers = [
94
+ { tags: ['all'] },
95
+ { tags: ['test'] },
96
+ { tags: ['all'], attributes: ['hidden'] },
97
+ { tags: ['all'], attributes: ['default'] },
98
+ { tags: ['all'], attributes: ['required'] },
99
+ {},
100
+ ];
101
+ expect(
102
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
103
+ ).toHaveLength(5);
104
+ customer.tags = undefined;
105
+ expect(
106
+ pricingService.modifier.getMatchTagsModifiers({ modifiers, entity: customer })
107
+ ).toHaveLength(5);
108
+ });
109
+ });
@@ -0,0 +1,40 @@
1
+ const usePricing = require('../../index');
2
+
3
+ const pricingService = usePricing();
4
+
5
+ describe('GetModifierIndex Function', () => {
6
+ test('usecases', () => {
7
+ expect(
8
+ pricingService.modifier.getModifierIndex({
9
+ entity: undefined,
10
+ modifier: undefined,
11
+ })
12
+ ).toBe(-1);
13
+ const order = { modifiers: [{ modifierId: '123' }] };
14
+ expect(
15
+ pricingService.modifier.getModifierIndex({ entity: order, modifier: undefined })
16
+ ).toBe(-1);
17
+ const modifier = { _id: '123' };
18
+ expect(
19
+ pricingService.modifier.getModifierIndex({
20
+ entity: order,
21
+ modifier,
22
+ })
23
+ ).toBe(0);
24
+ modifier._id = '234';
25
+ expect(
26
+ pricingService.modifier.getModifierIndex({
27
+ entity: order,
28
+ modifier,
29
+ })
30
+ ).toBe(-1);
31
+ order.modifiers = [{ modifierId: '123' }, { modifierId: '234' }];
32
+ expect(pricingService.modifier.getModifierIndex({ entity: order, modifier })).toBe(
33
+ 1
34
+ );
35
+ modifier._id = '123';
36
+ expect(pricingService.modifier.getModifierIndex({ entity: order, modifier })).toBe(
37
+ 0
38
+ );
39
+ });
40
+ });