@open-tender/utils 0.4.34 → 0.4.36

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 (246) hide show
  1. package/LICENSE.md +21 -21
  2. package/README.md +2 -2
  3. package/dist/.DS_Store +0 -0
  4. package/dist/cjs/__tests__/utils/helpers.test.d.ts +1 -1
  5. package/dist/cjs/__tests__/utils/helpers.test.js +96 -96
  6. package/dist/cjs/__tests__/utils/theme.test.d.ts +1 -1
  7. package/dist/cjs/__tests__/utils/theme.test.js +37 -37
  8. package/dist/cjs/hooks/index.d.ts +35 -35
  9. package/dist/cjs/hooks/index.js +38 -38
  10. package/dist/cjs/hooks/types.d.ts +4 -4
  11. package/dist/cjs/hooks/types.js +2 -2
  12. package/dist/cjs/hooks/useAccordion.d.ts +22 -22
  13. package/dist/cjs/hooks/useAccordion.js +56 -56
  14. package/dist/cjs/hooks/useAddressForm.d.ts +12 -12
  15. package/dist/cjs/hooks/useAddressForm.js +83 -83
  16. package/dist/cjs/hooks/useAllergenForm.d.ts +10 -10
  17. package/dist/cjs/hooks/useAllergenForm.js +58 -58
  18. package/dist/cjs/hooks/useBuilder.d.ts +14 -14
  19. package/dist/cjs/hooks/useBuilder.js +144 -144
  20. package/dist/cjs/hooks/useCartGuestForm.d.ts +12 -12
  21. package/dist/cjs/hooks/useCartGuestForm.js +58 -58
  22. package/dist/cjs/hooks/useCartPoints.d.ts +13 -13
  23. package/dist/cjs/hooks/useCartPoints.js +74 -74
  24. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -1
  25. package/dist/cjs/hooks/useCreditCard.d.ts +13 -13
  26. package/dist/cjs/hooks/useCreditCard.js +63 -63
  27. package/dist/cjs/hooks/useCreditCardForm.d.ts +10 -10
  28. package/dist/cjs/hooks/useCreditCardForm.js +71 -71
  29. package/dist/cjs/hooks/useCurbside.d.ts +9 -9
  30. package/dist/cjs/hooks/useCurbside.js +39 -39
  31. package/dist/cjs/hooks/useDineInForm.d.ts +11 -11
  32. package/dist/cjs/hooks/useDineInForm.js +56 -56
  33. package/dist/cjs/hooks/useDonationForm.d.ts +28 -28
  34. package/dist/cjs/hooks/useDonationForm.js +172 -172
  35. package/dist/cjs/hooks/useFirstTimes.d.ts +18 -18
  36. package/dist/cjs/hooks/useFirstTimes.js +81 -81
  37. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +14 -14
  38. package/dist/cjs/hooks/useGiftCardAssignForm.js +57 -57
  39. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  40. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +50 -50
  41. package/dist/cjs/hooks/useGiftCardForm.d.ts +12 -12
  42. package/dist/cjs/hooks/useGiftCardForm.js +74 -74
  43. package/dist/cjs/hooks/useGiftCardsForm.d.ts +31 -31
  44. package/dist/cjs/hooks/useGiftCardsForm.js +195 -195
  45. package/dist/cjs/hooks/useGuestCheckoutForm.d.ts +21 -21
  46. package/dist/cjs/hooks/useGuestCheckoutForm.js +89 -89
  47. package/dist/cjs/hooks/useGuestForm.d.ts +12 -12
  48. package/dist/cjs/hooks/useGuestForm.js +60 -60
  49. package/dist/cjs/hooks/useImage.d.ts +5 -5
  50. package/dist/cjs/hooks/useImage.js +33 -33
  51. package/dist/cjs/hooks/useLoginForm.d.ts +12 -12
  52. package/dist/cjs/hooks/useLoginForm.js +67 -67
  53. package/dist/cjs/hooks/useMenuItem.d.ts +57 -57
  54. package/dist/cjs/hooks/useMenuItem.js +67 -67
  55. package/dist/cjs/hooks/useModifier.d.ts +53 -53
  56. package/dist/cjs/hooks/useModifier.js +72 -72
  57. package/dist/cjs/hooks/useModifierGroup.d.ts +15 -15
  58. package/dist/cjs/hooks/useModifierGroup.js +17 -17
  59. package/dist/cjs/hooks/useOneTimePasscodeForm.d.ts +16 -16
  60. package/dist/cjs/hooks/useOneTimePasscodeForm.js +87 -87
  61. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +11 -11
  62. package/dist/cjs/hooks/useOrderFulfillmentForm.js +65 -65
  63. package/dist/cjs/hooks/useOrderItem.d.ts +48 -48
  64. package/dist/cjs/hooks/useOrderItem.js +54 -54
  65. package/dist/cjs/hooks/useOrderRating.d.ts +10 -10
  66. package/dist/cjs/hooks/useOrderRating.js +34 -34
  67. package/dist/cjs/hooks/useOrderRatingForm.d.ts +11 -11
  68. package/dist/cjs/hooks/useOrderRatingForm.js +35 -35
  69. package/dist/cjs/hooks/useProfileForm.d.ts +15 -15
  70. package/dist/cjs/hooks/useProfileForm.js +116 -116
  71. package/dist/cjs/hooks/useResetPasswordForm.d.ts +12 -12
  72. package/dist/cjs/hooks/useResetPasswordForm.js +77 -77
  73. package/dist/cjs/hooks/useSendResetForm.d.ts +14 -14
  74. package/dist/cjs/hooks/useSendResetForm.js +57 -57
  75. package/dist/cjs/hooks/useSignUpForm.d.ts +20 -20
  76. package/dist/cjs/hooks/useSignUpForm.js +151 -152
  77. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -23
  78. package/dist/cjs/hooks/useSignUpGuestForm.js +113 -124
  79. package/dist/cjs/hooks/useSigninForm.d.ts +12 -12
  80. package/dist/cjs/hooks/useSigninForm.js +66 -66
  81. package/dist/cjs/index.d.ts +2 -2
  82. package/dist/cjs/index.js +5 -5
  83. package/dist/cjs/utils/cards.d.ts +20 -20
  84. package/dist/cjs/utils/cards.js +155 -155
  85. package/dist/cjs/utils/cart.d.ts +202 -202
  86. package/dist/cjs/utils/cart.js +909 -909
  87. package/dist/cjs/utils/checkout.d.ts +55 -55
  88. package/dist/cjs/utils/checkout.js +125 -125
  89. package/dist/cjs/utils/constants.d.ts +43 -43
  90. package/dist/cjs/utils/constants.js +58 -58
  91. package/dist/cjs/utils/customer.d.ts +4 -4
  92. package/dist/cjs/utils/customer.js +34 -34
  93. package/dist/cjs/utils/datetimes.d.ts +99 -99
  94. package/dist/cjs/utils/datetimes.js +601 -601
  95. package/dist/cjs/utils/errors.d.ts +51 -51
  96. package/dist/cjs/utils/errors.js +191 -191
  97. package/dist/cjs/utils/favorites.d.ts +10 -10
  98. package/dist/cjs/utils/favorites.js +119 -119
  99. package/dist/cjs/utils/googleMaps.d.ts +14 -14
  100. package/dist/cjs/utils/googleMaps.js +30 -30
  101. package/dist/cjs/utils/helpers.d.ts +63 -63
  102. package/dist/cjs/utils/helpers.js +244 -244
  103. package/dist/cjs/utils/index.d.ts +19 -19
  104. package/dist/cjs/utils/index.js +23 -23
  105. package/dist/cjs/utils/keypad.d.ts +9 -9
  106. package/dist/cjs/utils/keypad.js +105 -105
  107. package/dist/cjs/utils/maps.d.ts +226 -226
  108. package/dist/cjs/utils/maps.js +334 -334
  109. package/dist/cjs/utils/menu.d.ts +117 -117
  110. package/dist/cjs/utils/menu.js +82 -82
  111. package/dist/cjs/utils/orders.d.ts +21 -21
  112. package/dist/cjs/utils/orders.js +202 -202
  113. package/dist/cjs/utils/pizzaBuilder.d.ts +10 -10
  114. package/dist/cjs/utils/pizzaBuilder.js +43 -43
  115. package/dist/cjs/utils/refunds.d.ts +35 -35
  116. package/dist/cjs/utils/refunds.js +98 -98
  117. package/dist/cjs/utils/revenueCenters.d.ts +11 -11
  118. package/dist/cjs/utils/revenueCenters.js +125 -125
  119. package/dist/cjs/utils/rewards.d.ts +4 -4
  120. package/dist/cjs/utils/rewards.js +106 -106
  121. package/dist/cjs/utils/theme.d.ts +27 -27
  122. package/dist/cjs/utils/theme.js +266 -266
  123. package/dist/cjs/utils/version.d.ts +4 -4
  124. package/dist/cjs/utils/version.js +38 -38
  125. package/dist/esm/__tests__/utils/helpers.test.d.ts +1 -1
  126. package/dist/esm/__tests__/utils/helpers.test.js +94 -94
  127. package/dist/esm/__tests__/utils/theme.test.d.ts +1 -1
  128. package/dist/esm/__tests__/utils/theme.test.js +35 -35
  129. package/dist/esm/hooks/index.d.ts +35 -35
  130. package/dist/esm/hooks/index.js +35 -35
  131. package/dist/esm/hooks/types.d.ts +4 -4
  132. package/dist/esm/hooks/types.js +1 -1
  133. package/dist/esm/hooks/useAccordion.d.ts +22 -22
  134. package/dist/esm/hooks/useAccordion.js +52 -52
  135. package/dist/esm/hooks/useAddressForm.d.ts +12 -12
  136. package/dist/esm/hooks/useAddressForm.js +79 -79
  137. package/dist/esm/hooks/useAllergenForm.d.ts +10 -10
  138. package/dist/esm/hooks/useAllergenForm.js +54 -54
  139. package/dist/esm/hooks/useBuilder.d.ts +14 -14
  140. package/dist/esm/hooks/useBuilder.js +140 -140
  141. package/dist/esm/hooks/useCartGuestForm.d.ts +12 -12
  142. package/dist/esm/hooks/useCartGuestForm.js +54 -54
  143. package/dist/esm/hooks/useCartPoints.d.ts +13 -13
  144. package/dist/esm/hooks/useCartPoints.js +70 -70
  145. package/dist/esm/hooks/useCommunicationPreferences.js +1 -1
  146. package/dist/esm/hooks/useCreditCard.d.ts +13 -13
  147. package/dist/esm/hooks/useCreditCard.js +59 -59
  148. package/dist/esm/hooks/useCreditCardForm.d.ts +10 -10
  149. package/dist/esm/hooks/useCreditCardForm.js +67 -67
  150. package/dist/esm/hooks/useCurbside.d.ts +9 -9
  151. package/dist/esm/hooks/useCurbside.js +35 -35
  152. package/dist/esm/hooks/useDineInForm.d.ts +11 -11
  153. package/dist/esm/hooks/useDineInForm.js +52 -52
  154. package/dist/esm/hooks/useDonationForm.d.ts +28 -28
  155. package/dist/esm/hooks/useDonationForm.js +168 -168
  156. package/dist/esm/hooks/useFirstTimes.d.ts +18 -18
  157. package/dist/esm/hooks/useFirstTimes.js +77 -77
  158. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +14 -14
  159. package/dist/esm/hooks/useGiftCardAssignForm.js +53 -53
  160. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  161. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +46 -46
  162. package/dist/esm/hooks/useGiftCardForm.d.ts +12 -12
  163. package/dist/esm/hooks/useGiftCardForm.js +70 -70
  164. package/dist/esm/hooks/useGiftCardsForm.d.ts +31 -31
  165. package/dist/esm/hooks/useGiftCardsForm.js +191 -191
  166. package/dist/esm/hooks/useGuestCheckoutForm.d.ts +21 -21
  167. package/dist/esm/hooks/useGuestCheckoutForm.js +85 -85
  168. package/dist/esm/hooks/useGuestForm.d.ts +12 -12
  169. package/dist/esm/hooks/useGuestForm.js +56 -56
  170. package/dist/esm/hooks/useImage.d.ts +5 -5
  171. package/dist/esm/hooks/useImage.js +29 -29
  172. package/dist/esm/hooks/useLoginForm.d.ts +12 -12
  173. package/dist/esm/hooks/useLoginForm.js +63 -63
  174. package/dist/esm/hooks/useMenuItem.d.ts +57 -57
  175. package/dist/esm/hooks/useMenuItem.js +63 -63
  176. package/dist/esm/hooks/useModifier.d.ts +53 -53
  177. package/dist/esm/hooks/useModifier.js +68 -68
  178. package/dist/esm/hooks/useModifierGroup.d.ts +15 -15
  179. package/dist/esm/hooks/useModifierGroup.js +13 -13
  180. package/dist/esm/hooks/useOneTimePasscodeForm.d.ts +16 -16
  181. package/dist/esm/hooks/useOneTimePasscodeForm.js +83 -83
  182. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +11 -11
  183. package/dist/esm/hooks/useOrderFulfillmentForm.js +61 -61
  184. package/dist/esm/hooks/useOrderItem.d.ts +48 -48
  185. package/dist/esm/hooks/useOrderItem.js +50 -50
  186. package/dist/esm/hooks/useOrderRating.d.ts +10 -10
  187. package/dist/esm/hooks/useOrderRating.js +30 -30
  188. package/dist/esm/hooks/useOrderRatingForm.d.ts +11 -11
  189. package/dist/esm/hooks/useOrderRatingForm.js +31 -31
  190. package/dist/esm/hooks/useProfileForm.d.ts +15 -15
  191. package/dist/esm/hooks/useProfileForm.js +112 -112
  192. package/dist/esm/hooks/useResetPasswordForm.d.ts +12 -12
  193. package/dist/esm/hooks/useResetPasswordForm.js +73 -73
  194. package/dist/esm/hooks/useSendResetForm.d.ts +14 -14
  195. package/dist/esm/hooks/useSendResetForm.js +53 -53
  196. package/dist/esm/hooks/useSignUpForm.d.ts +20 -20
  197. package/dist/esm/hooks/useSignUpForm.js +147 -148
  198. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -23
  199. package/dist/esm/hooks/useSignUpGuestForm.js +109 -120
  200. package/dist/esm/hooks/useSigninForm.d.ts +12 -12
  201. package/dist/esm/hooks/useSigninForm.js +62 -62
  202. package/dist/esm/index.d.ts +2 -2
  203. package/dist/esm/index.js +2 -2
  204. package/dist/esm/utils/cards.d.ts +20 -20
  205. package/dist/esm/utils/cards.js +146 -146
  206. package/dist/esm/utils/cart.d.ts +202 -202
  207. package/dist/esm/utils/cart.js +868 -868
  208. package/dist/esm/utils/checkout.d.ts +55 -55
  209. package/dist/esm/utils/checkout.js +117 -117
  210. package/dist/esm/utils/constants.d.ts +43 -43
  211. package/dist/esm/utils/constants.js +55 -55
  212. package/dist/esm/utils/customer.d.ts +4 -4
  213. package/dist/esm/utils/customer.js +29 -29
  214. package/dist/esm/utils/datetimes.d.ts +99 -99
  215. package/dist/esm/utils/datetimes.js +531 -531
  216. package/dist/esm/utils/errors.d.ts +51 -51
  217. package/dist/esm/utils/errors.js +180 -180
  218. package/dist/esm/utils/favorites.d.ts +10 -10
  219. package/dist/esm/utils/favorites.js +109 -109
  220. package/dist/esm/utils/googleMaps.d.ts +14 -14
  221. package/dist/esm/utils/googleMaps.js +26 -26
  222. package/dist/esm/utils/helpers.d.ts +63 -63
  223. package/dist/esm/utils/helpers.js +205 -205
  224. package/dist/esm/utils/index.d.ts +19 -19
  225. package/dist/esm/utils/index.js +20 -20
  226. package/dist/esm/utils/keypad.d.ts +9 -9
  227. package/dist/esm/utils/keypad.js +97 -97
  228. package/dist/esm/utils/maps.d.ts +226 -226
  229. package/dist/esm/utils/maps.js +316 -316
  230. package/dist/esm/utils/menu.d.ts +117 -117
  231. package/dist/esm/utils/menu.js +74 -74
  232. package/dist/esm/utils/orders.d.ts +21 -21
  233. package/dist/esm/utils/orders.js +185 -185
  234. package/dist/esm/utils/pizzaBuilder.d.ts +10 -10
  235. package/dist/esm/utils/pizzaBuilder.js +38 -38
  236. package/dist/esm/utils/refunds.d.ts +35 -35
  237. package/dist/esm/utils/refunds.js +90 -90
  238. package/dist/esm/utils/revenueCenters.d.ts +11 -11
  239. package/dist/esm/utils/revenueCenters.js +115 -115
  240. package/dist/esm/utils/rewards.d.ts +4 -4
  241. package/dist/esm/utils/rewards.js +100 -100
  242. package/dist/esm/utils/theme.d.ts +27 -27
  243. package/dist/esm/utils/theme.js +254 -254
  244. package/dist/esm/utils/version.d.ts +4 -4
  245. package/dist/esm/utils/version.js +34 -34
  246. package/package.json +69 -69
@@ -1,909 +1,909 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeDeviceType = exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.compareCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItemsForCart = exports.makeUpsellItemIds = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNamesForGroups = exports.makeModifierNames = exports.getItemOptions = void 0;
4
- const types_1 = require("@open-tender/types");
5
- const helpers_1 = require("./helpers");
6
- const favorites_1 = require("./favorites");
7
- // TODO: refactor for nested mods (if needed)
8
- const getItemOptions = (item) => {
9
- if (!item.groups.length)
10
- return [];
11
- const options = item.groups
12
- .map(group => group.options.filter(option => option.quantity > 0))
13
- .reduce((arr, i) => [...arr, ...i], []);
14
- return options;
15
- };
16
- exports.getItemOptions = getItemOptions;
17
- const makeModifierNames = (item) => {
18
- return item.groups
19
- .map(group => group.options.filter(option => option.quantity > 0))
20
- .reduce((arr, i) => [...arr, ...i], [])
21
- .map(i => {
22
- const optionNames = (0, exports.makeModifierNames)(i);
23
- const name = optionNames ? `${i.name} (${optionNames})` : i.name;
24
- return Object.assign(Object.assign({}, i), { name });
25
- })
26
- .map(option => option.name)
27
- .join(', ');
28
- };
29
- exports.makeModifierNames = makeModifierNames;
30
- const makeModifierNamesForGroups = (groups) => {
31
- return groups
32
- .map(group => group.options.filter(option => option.quantity > 0))
33
- .reduce((arr, i) => [...arr, ...i], [])
34
- .map(i => {
35
- const optionNames = (0, exports.makeModifierNamesForGroups)(i.groups);
36
- const name = optionNames ? `${i.name} (${optionNames})` : i.name;
37
- return Object.assign(Object.assign({}, i), { name });
38
- })
39
- .map(option => option.name)
40
- .join(', ');
41
- };
42
- exports.makeModifierNamesForGroups = makeModifierNamesForGroups;
43
- // export const makeModifierNames = <T extends BaseItem>(item: T): string => {
44
- // return makeModifierNamesForGroups(item.groups)
45
- // }
46
- const hasGroupsBelowMin = (option, groups) => {
47
- if (!groups || !groups.length)
48
- return false;
49
- if (option && option.quantity === 0)
50
- return false;
51
- const childGroupsBelow = groups.filter(g => {
52
- const optionsBelowMin = g.options.filter(o => (0, exports.hasGroupsBelowMin)(o, o.groups));
53
- return optionsBelowMin.length > 0;
54
- });
55
- if (childGroupsBelow.length > 0)
56
- return true;
57
- return groups.filter(g => ((g === null || g === void 0 ? void 0 : g.quantity) || 0) < g.min).length > 0;
58
- };
59
- exports.hasGroupsBelowMin = hasGroupsBelowMin;
60
- const makeModifierGroups = (groups) => {
61
- if (!groups || !groups.length)
62
- return [[], []];
63
- const otherGroups = groups.filter(g => !g.isSize);
64
- if (otherGroups.length)
65
- return [groups, []];
66
- const sizeGroup = groups.find(g => g.isSize);
67
- if (!sizeGroup)
68
- return [[], []];
69
- const sizeGroupNested = sizeGroup.options.filter(o => o.groups && o.groups.length);
70
- if (!sizeGroupNested || !sizeGroupNested.length)
71
- return [[], []];
72
- const selectedOption = sizeGroup.options.find(o => o.quantity > 0);
73
- return selectedOption
74
- ? [selectedOption.groups, [[sizeGroup.id, selectedOption.id]]]
75
- : [[], []];
76
- };
77
- exports.makeModifierGroups = makeModifierGroups;
78
- const makeUpsellItems = (itemIds, itemLookup) => {
79
- return itemIds
80
- .map(id => itemLookup[id])
81
- .filter(i => i)
82
- .map(i => (0, exports.makeOrderItem)(i))
83
- .filter(i => !i.isSoldOut)
84
- .filter(i => {
85
- const belowMin = i.groups.filter(g => !g.isSize && (g.quantity || 0) < g.min);
86
- return belowMin.length === 0;
87
- });
88
- };
89
- exports.makeUpsellItems = makeUpsellItems;
90
- const makeUpsellItemIds = (item, cartIds, soldOut) => {
91
- const upsellItems = item ? item.upsellItems : null;
92
- return upsellItems
93
- ? upsellItems.filter(id => !cartIds.includes(id) && !soldOut.includes(id))
94
- : [];
95
- };
96
- exports.makeUpsellItemIds = makeUpsellItemIds;
97
- const makeUpsellItemsForCart = (categories, cartIds, soldOut) => {
98
- const itemLookup = (0, favorites_1.makeMenuItemLookup)(categories);
99
- const menuItems = cartIds.map(id => itemLookup[id]).filter(Boolean);
100
- const upsellItemIds = menuItems.reduce((arr, i) => [...arr, ...i.upsell_items], []);
101
- const uniqueIds = Array.from(new Set(upsellItemIds));
102
- const itemIds = uniqueIds.filter(id => !cartIds.includes(id) && !soldOut.includes(id));
103
- return (0, exports.makeUpsellItems)(itemIds, itemLookup);
104
- };
105
- exports.makeUpsellItemsForCart = makeUpsellItemsForCart;
106
- const nutritionKeys = [
107
- 'calories',
108
- 'cholesterol',
109
- 'dietary_fiber',
110
- 'protein',
111
- 'saturated_fat',
112
- 'sodium',
113
- 'sugars',
114
- 'total_carbs',
115
- 'total_fat',
116
- 'trans_fat'
117
- ];
118
- const calcNutrition = (item) => {
119
- const { nutritionalInfo, groups } = item;
120
- let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
121
- const options = groups.reduce((arr, g) => {
122
- return [...arr, ...g.options.filter(o => o.quantity > 0)];
123
- }, []);
124
- options.forEach(option => {
125
- const optionNutrition = (0, exports.calcNutrition)(option);
126
- nutritionKeys.forEach(key => {
127
- const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
128
- const optionVal = optionPer * option.quantity;
129
- const itemVal = nutrition[key] || 0;
130
- nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
131
- });
132
- });
133
- return nutrition;
134
- };
135
- exports.calcNutrition = calcNutrition;
136
- const calcNutritionalInfo = (item) => {
137
- const nutrition = (0, exports.calcNutrition)(item);
138
- const { serving_size = '0.00' } = item.nutritionalInfo || {};
139
- return Object.assign(Object.assign({}, nutrition), { serving_size });
140
- };
141
- exports.calcNutritionalInfo = calcNutritionalInfo;
142
- const calcPrices = (item) => {
143
- const groups = item.groups.map(g => {
144
- let groupQuantity = 0;
145
- const optionsWithPrices = g.options.map((o, index) => {
146
- return Object.assign(Object.assign({}, (0, exports.calcPrices)(o)), { index });
147
- });
148
- const sortedOptions = optionsWithPrices.sort((a, b) => {
149
- const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
150
- const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
151
- return pricePerA - pricePerB;
152
- });
153
- const pricedOptions = sortedOptions.map(o => {
154
- const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
155
- const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
156
- const includedRemaining = Math.max(g.included - groupQuantity, 0);
157
- const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
158
- const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals: o.totalCals || 0 });
159
- groupQuantity += o.quantity || 0;
160
- return option;
161
- });
162
- const options = pricedOptions.sort((a, b) => a.index - b.index);
163
- return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
164
- });
165
- const optionsPrice = groups.reduce((t, g) => {
166
- return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
167
- }, 0.0);
168
- const totalPrice = item.quantity * (item.price + optionsPrice);
169
- const optionsPoints = groups.reduce((t, g) => {
170
- return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
171
- }, 0.0);
172
- const totalPoints = item.points
173
- ? item.quantity * (item.points + optionsPoints)
174
- : null;
175
- const optionsCals = groups.reduce((t, g) => {
176
- return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
177
- }, 0.0);
178
- const totalCals = item.cals || item.cals === 0
179
- ? item.quantity * (item.cals + optionsCals)
180
- : null;
181
- const nonSizeGroups = groups ? groups.filter(i => !i.isSize) : null;
182
- const currentOptions = nonSizeGroups
183
- ? (0, exports.getItemOptions)(Object.assign(Object.assign({}, item), { groups: nonSizeGroups }))
184
- : [];
185
- const modifiersAllergens = currentOptions.reduce((t, o) => {
186
- return [...t, ...o.allergens];
187
- }, []);
188
- const totalAllergens = [...new Set(item.allergens.concat(modifiersAllergens))];
189
- return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals, totalAllergens });
190
- };
191
- exports.calcPrices = calcPrices;
192
- const checkSoldOut = (id, suspendUntil, soldOut = []) => {
193
- if (soldOut.includes(id))
194
- return true;
195
- return suspendUntil ? suspendUntil > Date.now() / 1000 : false;
196
- };
197
- exports.checkSoldOut = checkSoldOut;
198
- const makeOrderItemGroups = (optionGroups, isEdit, soldOut = []) => {
199
- if (!optionGroups)
200
- return [];
201
- const groups = optionGroups.map(g => {
202
- const options = g.option_items.map(o => {
203
- var _a;
204
- const isSoldOut = (0, exports.checkSoldOut)(o.id, o.suspend_until, soldOut);
205
- const quantity = isSoldOut
206
- ? 0
207
- : o.opt_is_default && !isEdit
208
- ? o.min_quantity || 1
209
- : 0;
210
- const cals = ((_a = o.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories)
211
- ? parseInt(o.nutritional_info.calories.toString())
212
- : null;
213
- const groups = makeOrderItemGroups(o.option_groups, isEdit, soldOut);
214
- const option = {
215
- allergens: (0, helpers_1.convertStringToArray)(o.allergens),
216
- allergens_list: o.allergens_list || [],
217
- cals: cals,
218
- description: o.description,
219
- groups: groups,
220
- id: o.id,
221
- imageUrl: o.small_image_url,
222
- increment: o.increment,
223
- ingredients: o.ingredients,
224
- isDefault: o.opt_is_default,
225
- isSoldOut: isSoldOut,
226
- max: isSoldOut ? 0 : o.max_quantity,
227
- min: o.min_quantity,
228
- name: o.name,
229
- nutritionalInfo: o.nutritional_info,
230
- points: o.points || 0,
231
- price: parseFloat(o.price),
232
- quantity: quantity,
233
- shorthand: o.shorthand,
234
- shortDescription: o.short_description,
235
- shortName: o.short_name,
236
- slug: o.slug,
237
- tags: (0, helpers_1.convertStringToArray)(o.tags),
238
- tags_list: o.tags_list || [],
239
- totalAllergens: [],
240
- totalCals: null,
241
- totalPoints: null,
242
- totalPrice: null
243
- };
244
- return option;
245
- });
246
- const group = {
247
- description: g.description,
248
- id: g.id,
249
- imageUrl: g.small_image_url,
250
- included: g.included_options,
251
- isPizza: !!g.is_pizza,
252
- isSize: !!g.is_size,
253
- max: g.max_options,
254
- min: g.min_options,
255
- name: g.name,
256
- options: options,
257
- quantity: options.reduce((t, o) => t + o.quantity, 0)
258
- };
259
- return group;
260
- });
261
- return groups;
262
- };
263
- const makeOrderItem = (item, isEdit, soldOut = [], simpleItem, hasPoints = false) => {
264
- var _a;
265
- const groups = makeOrderItemGroups(item.option_groups, isEdit, soldOut);
266
- const cals = ((_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories)
267
- ? parseInt(item.nutritional_info.calories.toString())
268
- : null;
269
- const isSoldOut = (0, exports.checkSoldOut)(item.id, item.suspend_until, soldOut);
270
- const orderItem = {
271
- allergens: (0, helpers_1.convertStringToArray)(item.allergens),
272
- allergens_list: item.allergens_list || [],
273
- cals: cals,
274
- cartGuestId: simpleItem ? simpleItem.cart_guest_id || null : null,
275
- customerId: simpleItem ? simpleItem.customer_id || null : null,
276
- description: item.description,
277
- groups: groups,
278
- id: item.id,
279
- imageUrl: item.large_image_url,
280
- increment: item.increment,
281
- // index: null,
282
- ingredients: item.ingredients,
283
- isSoldOut: isSoldOut,
284
- madeFor: simpleItem ? simpleItem.made_for || null : null,
285
- max: item.max_quantity,
286
- min: item.min_quantity,
287
- name: item.name,
288
- notes: simpleItem ? simpleItem.notes || null : null,
289
- nutritionalInfo: item.nutritional_info,
290
- points: hasPoints ? item.points || null : null,
291
- price: parseFloat(item.price),
292
- quantity: item.min_quantity || 1 * item.increment,
293
- shorthand: item.shorthand,
294
- shortDescription: item.short_description,
295
- shortName: item.short_name,
296
- similarItems: item.similar_items,
297
- slug: item.slug,
298
- tags: (0, helpers_1.convertStringToArray)(item.tags),
299
- tags_list: item.tags_list || [],
300
- totalAllergens: [],
301
- totalCals: null,
302
- totalPoints: null,
303
- totalPrice: null,
304
- upsellItems: item.upsell_items
305
- };
306
- const pricedItem = (0, exports.calcPrices)(orderItem);
307
- return pricedItem;
308
- };
309
- exports.makeOrderItem = makeOrderItem;
310
- const calcCartCounts = (cart) => {
311
- return cart.reduce((obj, item) => {
312
- const newCount = (obj[item.id.toString()] || 0) + item.quantity;
313
- return Object.assign(Object.assign({}, obj), { [item.id]: newCount });
314
- }, {});
315
- };
316
- exports.calcCartCounts = calcCartCounts;
317
- const makeSimpleGroupsLookup = (groups) => {
318
- if (!groups || !groups.length)
319
- return null;
320
- return groups.reduce((grpObj, g) => {
321
- const options = g.options.reduce((optObj, o) => {
322
- optObj[o.id] = {
323
- quantity: o.quantity,
324
- groupsLookup: makeSimpleGroupsLookup(o.groups)
325
- };
326
- return optObj;
327
- }, {});
328
- grpObj[g.id] = { options };
329
- return grpObj;
330
- }, {});
331
- };
332
- const rehydrateGroups = (groups, groupsLookup) => {
333
- if (!groupsLookup)
334
- return groups;
335
- groups.forEach(group => {
336
- const simpleGroup = groupsLookup[group.id];
337
- if (simpleGroup) {
338
- group.options.forEach(option => {
339
- const simpleOption = simpleGroup.options[option.id];
340
- if (simpleOption) {
341
- option.quantity = simpleOption.quantity;
342
- option.groups = rehydrateGroups(option.groups, simpleOption.groupsLookup);
343
- }
344
- });
345
- }
346
- });
347
- return groups;
348
- };
349
- const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = []) => {
350
- const orderItem = (0, exports.makeOrderItem)(menuItem, true, soldOut, simpleCartItem);
351
- orderItem.quantity = simpleCartItem.quantity || 1;
352
- const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
353
- if (groupsLookup) {
354
- orderItem.groups = rehydrateGroups(orderItem.groups, groupsLookup);
355
- }
356
- const pricedItem = (0, exports.calcPrices)(orderItem);
357
- return pricedItem;
358
- };
359
- exports.rehydrateOrderItem = rehydrateOrderItem;
360
- const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
361
- const orderItems = [];
362
- simpleCartItems.forEach(item => {
363
- const menuItem = menuItems.find(i => i.id === item.id);
364
- if (menuItem) {
365
- const orderItem = (0, exports.rehydrateOrderItem)(menuItem, item, soldOut);
366
- orderItems.push(orderItem);
367
- }
368
- });
369
- const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
370
- const cartCounts = (0, exports.calcCartCounts)(cart);
371
- return { cart, cartCounts };
372
- };
373
- exports.rehydrateCart = rehydrateCart;
374
- // TODO: maybe refactor
375
- const rehydrateDetails = (details) => {
376
- return {
377
- cart_id: details === null || details === void 0 ? void 0 : details.cart_id,
378
- eating_utensils: details === null || details === void 0 ? void 0 : details.eating_utensils,
379
- notes: details === null || details === void 0 ? void 0 : details.notes,
380
- person_count: details === null || details === void 0 ? void 0 : details.person_count,
381
- serving_utensils: details === null || details === void 0 ? void 0 : details.serving_utensils,
382
- tax_exempt_id: details === null || details === void 0 ? void 0 : details.tax_exempt_id
383
- };
384
- };
385
- // TODO: maybe refactor
386
- const rehydrateCustomer = (customer) => {
387
- return {
388
- customer_id: customer.customer_id,
389
- first_name: customer.first_name,
390
- last_name: customer.last_name,
391
- email: customer.email,
392
- phone: customer.phone,
393
- company: customer.company || ''
394
- };
395
- };
396
- // TODO: maybe refactor
397
- const rehydrateAddress = (address) => {
398
- if (!address)
399
- return {};
400
- return {
401
- unit: address.unit,
402
- company: address.company,
403
- contact: address.contact,
404
- phone: address.phone
405
- };
406
- };
407
- const rehydrateSurcharges = (surcharges) => {
408
- return (surcharges === null || surcharges === void 0 ? void 0 : surcharges.filter(i => i.is_optional).map(i => ({ id: i.id }))) || [];
409
- };
410
- const rehydrateDiscounts = (discounts) => {
411
- return ((discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => !i.is_promo_code).map(i => ({ id: i.id, ext_id: i.ext_id }))) || []);
412
- };
413
- const rehydratePromoCodes = (discounts) => {
414
- return (discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => i.is_promo_code).map(i => i.name)) || [];
415
- };
416
- const rehydrateTenders = (tenders) => {
417
- const other = tenders === null || tenders === void 0 ? void 0 : tenders.map(i => {
418
- if ((0, types_1.isCreditTender)(i)) {
419
- return Object.assign(Object.assign({}, i), i.credit_card);
420
- }
421
- else if ((0, types_1.isHouseAccountTender)(i)) {
422
- return Object.assign(Object.assign({}, i), i.house_account);
423
- }
424
- else {
425
- return i;
426
- }
427
- });
428
- return other;
429
- };
430
- const rehydrateCheckoutForm = (order) => {
431
- const form = {
432
- details: rehydrateDetails(order.details),
433
- customer: order.customer && (0, types_1.isOrderCustomer)(order.customer)
434
- ? rehydrateCustomer(order.customer)
435
- : {},
436
- address: rehydrateAddress(order.address),
437
- surcharges: rehydrateSurcharges(order.surcharges),
438
- discounts: rehydrateDiscounts(order.discounts),
439
- promoCodes: rehydratePromoCodes(order.discounts),
440
- tenders: rehydrateTenders(order.tenders),
441
- tip: order.totals.tip
442
- };
443
- return form;
444
- };
445
- exports.rehydrateCheckoutForm = rehydrateCheckoutForm;
446
- // group orders
447
- const makeGuestLookup = (cartGuests) => {
448
- return cartGuests.reduce((obj, i) => i.cart_guest_id ? Object.assign(Object.assign({}, obj), { [i.cart_guest_id]: i }) : Object.assign({}, obj), {});
449
- };
450
- exports.makeGuestLookup = makeGuestLookup;
451
- const compareCarts = (cart) => {
452
- if (!cart)
453
- return null;
454
- const customerCart = cart.filter(i => !i.cartGuestId);
455
- return customerCart.length < cart.length ? customerCart : null;
456
- };
457
- exports.compareCarts = compareCarts;
458
- const combineCarts = (cart, guestCart, cartOwner, cartGuests) => {
459
- const guestLookup = (0, exports.makeGuestLookup)(cartGuests);
460
- const withGuestNames = guestCart
461
- .filter(i => i.cartGuestId && guestLookup[i.cartGuestId])
462
- .map(i => {
463
- const guest = i.cartGuestId ? guestLookup[i.cartGuestId] : null;
464
- const madeFor = guest ? `${guest === null || guest === void 0 ? void 0 : guest.first_name} ${guest === null || guest === void 0 ? void 0 : guest.last_name}` : null;
465
- return Object.assign(Object.assign({}, i), { madeFor });
466
- });
467
- const withOwnerName = cart.map(i => {
468
- return Object.assign(Object.assign({}, i), { customerId: cartOwner.customer_id, madeFor: `${cartOwner.first_name} ${cartOwner.last_name}` });
469
- });
470
- return [...withOwnerName, ...withGuestNames];
471
- };
472
- exports.combineCarts = combineCarts;
473
- // display items from past orders
474
- const makeItemImageUrl = (images) => {
475
- const imageMap = images
476
- .filter(i => i.url)
477
- .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.type]: i.url })), {});
478
- return (imageMap.SMALL_IMAGE || imageMap.LARGE_IMAGE || imageMap.APP_IMAGE || null);
479
- };
480
- exports.makeItemImageUrl = makeItemImageUrl;
481
- const makeDisplayItemGroups = (optionGroups) => {
482
- if (!optionGroups || !optionGroups.length)
483
- return [];
484
- return optionGroups.map(g => {
485
- var _a;
486
- const options = (_a = g.options) === null || _a === void 0 ? void 0 : _a.map(o => (0, exports.makeDisplayItem)(o));
487
- return Object.assign(Object.assign({}, g), { options });
488
- });
489
- };
490
- exports.makeDisplayItemGroups = makeDisplayItemGroups;
491
- const makeDisplayItem = (item, isOption = false) => {
492
- var _a;
493
- const cals = item.nutritional_info ? (_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories : null;
494
- let displayItem = {
495
- allergens: item.allergens,
496
- cals: cals,
497
- description: item.description,
498
- groups: (0, exports.makeDisplayItemGroups)(item.groups),
499
- imageUrl: (0, exports.makeItemImageUrl)(item.images),
500
- id: item.id,
501
- name: item.name,
502
- nutritionalInfo: item.nutritional_info,
503
- quantity: item.quantity,
504
- points: null,
505
- price: item.price,
506
- shortDescription: item.short_description,
507
- tags: item.tags,
508
- totalPrice: item.price_total
509
- };
510
- if (!isOption) {
511
- const itemDetails = {
512
- madeFor: item.made_for,
513
- notes: item.notes,
514
- signature: (0, exports.makeItemSignature)(displayItem)
515
- };
516
- displayItem = Object.assign(Object.assign({}, displayItem), itemDetails);
517
- }
518
- return displayItem;
519
- };
520
- exports.makeDisplayItem = makeDisplayItem;
521
- const makeDisplayItems = (orders) => {
522
- const items = orders.reduce((items, order) => [...items, ...order.cart], []);
523
- return items.map(i => (0, exports.makeDisplayItem)(i));
524
- };
525
- exports.makeDisplayItems = makeDisplayItems;
526
- const makeUniqueDisplayItems = (orders) => {
527
- const items = (0, exports.makeDisplayItems)(orders);
528
- const unique = [];
529
- const signatures = [];
530
- items.forEach(item => {
531
- var _a;
532
- if (signatures.includes((_a = item.signature) !== null && _a !== void 0 ? _a : ''))
533
- return;
534
- item.signature && signatures.push(item.signature);
535
- unique.push(item);
536
- });
537
- return unique;
538
- };
539
- exports.makeUniqueDisplayItems = makeUniqueDisplayItems;
540
- const makeFavoritesLookup = (favorites) => {
541
- if (!favorites)
542
- return {};
543
- return favorites
544
- .filter(i => i.cart)
545
- .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [(0, exports.makeItemSignature)(i.cart)]: i.favorite_id })), {});
546
- };
547
- exports.makeFavoritesLookup = makeFavoritesLookup;
548
- const makeCartItemOptionIds = (groups) => {
549
- if (!groups || !groups.length)
550
- return [];
551
- return groups
552
- .reduce((arr, group) => {
553
- const ids = group.options
554
- .filter(o => o.quantity > 0)
555
- .reduce((optionsArr, o) => {
556
- const optionArr = new Array(o.quantity);
557
- optionArr.fill(o.id);
558
- const nestedIds = makeCartItemOptionIds(o.groups);
559
- return [...optionsArr, ...optionArr, ...nestedIds];
560
- }, []);
561
- return [...arr, ...ids];
562
- }, [])
563
- .sort((a, b) => a - b);
564
- };
565
- const makeItemSignature = (item) => {
566
- const optionIds = makeCartItemOptionIds(item.groups);
567
- return [item.id, ...optionIds].join('.');
568
- };
569
- exports.makeItemSignature = makeItemSignature;
570
- const makeCartItemSignature = (item) => {
571
- const optionIds = makeCartItemOptionIds(item.groups);
572
- const itemIds = [item.id, ...optionIds].join('.');
573
- const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
574
- return signature;
575
- };
576
- exports.makeCartItemSignature = makeCartItemSignature;
577
- const addItem = (cart, item) => {
578
- if (typeof item.index === 'undefined') {
579
- const itemSignature = (0, exports.makeCartItemSignature)(item);
580
- const itemSignatures = cart.map(i => (Object.assign({ signature: (0, exports.makeCartItemSignature)(i) }, i)));
581
- const match = itemSignatures.find(i => i.signature === itemSignature);
582
- if (match && match.index !== undefined) {
583
- const quantity = item.quantity + match.quantity;
584
- const newItem = Object.assign(Object.assign({}, item), { quantity });
585
- const updated = (0, exports.calcPrices)(newItem);
586
- cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
587
- }
588
- else {
589
- cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
590
- }
591
- }
592
- else {
593
- cart[item.index] = item;
594
- }
595
- const cartCounts = (0, exports.calcCartCounts)(cart);
596
- return { cart, cartCounts };
597
- };
598
- exports.addItem = addItem;
599
- const getItemRealIndex = (cart, index) => cart.findIndex(item => item.index === index);
600
- const removeItem = (cart, index) => {
601
- const realIndex = getItemRealIndex(cart, index);
602
- if (realIndex > -1) {
603
- cart.splice(realIndex, 1);
604
- cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
605
- }
606
- const cartCounts = (0, exports.calcCartCounts)(cart);
607
- return { cart, cartCounts };
608
- };
609
- exports.removeItem = removeItem;
610
- const incrementItem = (cart, index) => {
611
- const realIndex = getItemRealIndex(cart, index);
612
- if (realIndex > -1) {
613
- const item = cart[realIndex];
614
- if (item.max === 0 || item.quantity < item.max) {
615
- let newQuantity = item.quantity + item.increment;
616
- newQuantity =
617
- item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
618
- const newItem = (0, exports.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
619
- cart[realIndex] = newItem;
620
- }
621
- }
622
- const cartCounts = (0, exports.calcCartCounts)(cart);
623
- return { cart, cartCounts };
624
- };
625
- exports.incrementItem = incrementItem;
626
- const decrementItem = (cart, index) => {
627
- const realIndex = getItemRealIndex(cart, index);
628
- if (realIndex > -1) {
629
- const item = cart[realIndex];
630
- const newQuantity = Math.max(item.quantity - item.increment, 0);
631
- if (newQuantity === 0 || newQuantity < item.min) {
632
- cart.splice(realIndex, 1);
633
- cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
634
- }
635
- else {
636
- const newItem = (0, exports.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
637
- cart[realIndex] = newItem;
638
- }
639
- }
640
- const cartCounts = (0, exports.calcCartCounts)(cart);
641
- return { cart, cartCounts };
642
- };
643
- exports.decrementItem = decrementItem;
644
- const makeSimpleGroups = (groups) => {
645
- return groups.map(g => {
646
- const options = g.options
647
- .filter(o => o.quantity !== 0)
648
- .map(o => ({
649
- id: o.id,
650
- quantity: o.quantity,
651
- groups: makeSimpleGroups(o.groups || [])
652
- }));
653
- return { id: g.id, options: options };
654
- });
655
- };
656
- const makeSimpleCart = (cart) => {
657
- const simpleCart = cart.map(i => {
658
- return {
659
- id: i.id,
660
- quantity: i.quantity,
661
- groups: makeSimpleGroups(i.groups),
662
- made_for: i.madeFor || i.made_for || '',
663
- notes: i.notes || ''
664
- };
665
- });
666
- return simpleCart;
667
- };
668
- exports.makeSimpleCart = makeSimpleCart;
669
- /* cart validation */
670
- // TODO: refactor for nested modifiers
671
- // convert menu items to order items for validation but only include
672
- // items that are already in the cart to save wasted effort
673
- const makeItemLookup = (categories, itemIds) => {
674
- const itemLookup = {};
675
- categories.forEach(category => {
676
- category.items.forEach(item => {
677
- if (itemIds.includes(item.id)) {
678
- itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
679
- }
680
- });
681
- category.children.forEach(child => {
682
- child.items.forEach(item => {
683
- if (itemIds.includes(item.id)) {
684
- itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
685
- }
686
- });
687
- });
688
- });
689
- return itemLookup;
690
- };
691
- // TODO: refactor for nested modifiers
692
- const makeGroupsLookup = (item) => {
693
- return item.groups.reduce((grpObj, i) => {
694
- const options = i.options.reduce((optObj, o) => {
695
- if (o.id) {
696
- optObj[o.id] = o;
697
- }
698
- return optObj;
699
- }, {});
700
- grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
701
- return grpObj;
702
- }, {});
703
- };
704
- // TODO: refactor for nested modifiers
705
- const validateCart = (cart, categories, soldOut) => {
706
- const itemIds = cart.map(item => item.id);
707
- const itemLookup = makeItemLookup(categories, itemIds);
708
- let errors = null;
709
- const missingItems = [];
710
- const invalidItems = [];
711
- let newCart = cart.map(oldItem => {
712
- var _a, _b, _c, _d;
713
- const newItem = itemLookup[oldItem.id];
714
- if (!newItem || soldOut.includes(oldItem.id)) {
715
- missingItems.push(oldItem);
716
- return null;
717
- }
718
- let missingGroups = [];
719
- const invalidGroups = [];
720
- const missingOptions = [];
721
- const invalidOptions = [];
722
- const newGroups = makeGroupsLookup(newItem);
723
- // check if any required groups for the new item are missing from the old item
724
- const newRequiredGroups = Object.values(newGroups).filter(i => i.min > 0);
725
- const oldGroupIds = (_a = oldItem.groups) === null || _a === void 0 ? void 0 : _a.map(i => i.id);
726
- missingGroups = newRequiredGroups.filter(i => !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id)));
727
- const newEmptyGroups = Object.values(newGroups)
728
- .filter((i) => {
729
- return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
730
- })
731
- .map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
732
- const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map((group) => {
733
- const optionCount = group.options.reduce((t, i) => (t += i.quantity), 0);
734
- const newGroup = newGroups[group.id];
735
- // check if option group is still part of the item
736
- if (!newGroup) {
737
- if (optionCount > 0) {
738
- invalidGroups.push(group);
739
- return group;
740
- }
741
- else {
742
- return null;
743
- }
744
- }
745
- else {
746
- const updatedOptions = group.options.map((option) => {
747
- const newOption = newGroup.options[option.id];
748
- // check to see if any OLD options aren't avaiilable on the current menu
749
- // or if any of the options are currently sold out
750
- if (!newOption ||
751
- (option.quantity > 0 && soldOut.includes(option.id))) {
752
- missingOptions.push(option);
753
- return option;
754
- }
755
- else {
756
- // check to see if option quantity is valid relative to option min & max
757
- // ignore the option increment setting for the time being
758
- if ((newOption.max && option.quantity > newOption.max) ||
759
- (newOption.min && option.quantity < newOption.min)) {
760
- invalidOptions.push(option);
761
- return option;
762
- }
763
- else {
764
- // update the old option price to the new option price
765
- const updatedOption = Object.assign(Object.assign({}, option), { price: newOption.price });
766
- return updatedOption;
767
- }
768
- }
769
- });
770
- // check to see if the option count is greater than the max (if one exists)
771
- // or less than the min (if one exists)
772
- if ((newGroup.max && optionCount > newGroup.max) ||
773
- (newGroup.min && optionCount < newGroup.min)) {
774
- invalidGroups.push(group);
775
- }
776
- const updatedGroup = Object.assign(Object.assign({}, group), { options: updatedOptions });
777
- return updatedGroup;
778
- }
779
- });
780
- if (missingGroups.length ||
781
- invalidGroups.length ||
782
- missingOptions.length ||
783
- invalidOptions.length) {
784
- const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
785
- invalidGroups,
786
- missingOptions,
787
- invalidOptions });
788
- invalidItems.push(invalidItem);
789
- return null;
790
- }
791
- else {
792
- // check to see if this item's quantity is below the min or
793
- // above the max from the current menu
794
- if ((newItem.max && ((_c = oldItem.quantity) !== null && _c !== void 0 ? _c : 0) > newItem.max) ||
795
- (newItem.min && ((_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0) < newItem.min)) {
796
- const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
797
- invalidGroups,
798
- missingOptions,
799
- invalidOptions });
800
- invalidItems.push(invalidItem);
801
- return null;
802
- }
803
- const nonNullGroups = updatedGroups === null || updatedGroups === void 0 ? void 0 : updatedGroups.filter(i => i !== null);
804
- const allNewGroups = [...(nonNullGroups !== null && nonNullGroups !== void 0 ? nonNullGroups : []), ...newEmptyGroups];
805
- const updatedItem = Object.assign(Object.assign({}, oldItem), { groups: allNewGroups });
806
- // update the old item price to the new item price
807
- updatedItem.price = newItem.price;
808
- const pricedItem = (0, exports.calcPrices)(updatedItem);
809
- return pricedItem;
810
- }
811
- });
812
- newCart = newCart.filter(i => i !== null);
813
- if (missingItems.length || invalidItems.length) {
814
- errors = { missingItems, invalidItems };
815
- }
816
- return { newCart: newCart, errors };
817
- };
818
- exports.validateCart = validateCart;
819
- /* order submission */
820
- const getDefaultTip = (config) => {
821
- if (!config || !config.gratuity || !config.gratuity.has_tip)
822
- return null;
823
- return config.gratuity.default ? config.gratuity.default.amount : null;
824
- };
825
- exports.getDefaultTip = getDefaultTip;
826
- const prepareOrder = (data) => {
827
- const order = {
828
- revenue_center_id: data.revenueCenterId,
829
- service_type: data.serviceType,
830
- requested_at: data.requestedAt,
831
- cart: (0, exports.makeSimpleCart)(data.cart)
832
- };
833
- if (data.customer)
834
- order.customer = data.customer;
835
- if (data.details) {
836
- const details = Object.assign({}, data.details);
837
- // person_count must be submitted as integer
838
- if (details.person_count)
839
- details.person_count = parseInt(`${details.person_count}`) || null;
840
- if (data.deviceType)
841
- details.device_type = data.deviceType;
842
- if (data.table || data.prepType) {
843
- const prepType = data.prepType
844
- ? data.prepType
845
- : data.table
846
- ? 'EAT_HERE'
847
- : 'TAKE_OUT';
848
- details.notes_internal =
849
- prepType === 'EAT_HERE'
850
- ? data.table
851
- ? `DINE IN -- TABLE ${data.table}`
852
- : 'DINE IN'
853
- : 'TAKE OUT';
854
- }
855
- order.details = Object.assign({}, details);
856
- }
857
- if (data.surcharges)
858
- order.surcharges = data.surcharges;
859
- if (data.discounts)
860
- order.discounts = data.discounts;
861
- if (data.discountsInternal)
862
- order.discounts_internal = data.discountsInternal;
863
- if (data.promoCodes)
864
- order.promo_codes = data.promoCodes;
865
- if (data.points)
866
- order.points = data.points;
867
- if (data.tip)
868
- order.tip = data.tip;
869
- if (data.deposit)
870
- order.deposit = data.deposit;
871
- if (data.tenders)
872
- order.tenders = data.tenders;
873
- if (data.address && data.serviceType === 'DELIVERY') {
874
- order.address = data.address;
875
- }
876
- else {
877
- order.address = null;
878
- }
879
- if (data.orderId)
880
- order.order_id = data.orderId;
881
- if (data.cartId)
882
- order.cart_id = data.cartId;
883
- if (data.save)
884
- order.save = data.save;
885
- return order;
886
- };
887
- exports.prepareOrder = prepareOrder;
888
- const checkAmountRemaining = (total, tenders) => {
889
- if (!tenders)
890
- return total ? parseFloat(total) : 0.0;
891
- let remaining = parseFloat(total) -
892
- tenders.reduce((t, i) => { var _a; return (t += parseFloat((_a = i.amount) !== null && _a !== void 0 ? _a : '0')); }, 0.0);
893
- remaining = Math.round((remaining + Number.EPSILON) * 100) / 100;
894
- return Object.is(remaining, -0) ? 0.0 : remaining;
895
- };
896
- exports.checkAmountRemaining = checkAmountRemaining;
897
- const makeDeviceType = (deviceType) => {
898
- switch (deviceType) {
899
- case 'tablet':
900
- return 'TABLET';
901
- case 'mobile':
902
- return 'MOBILE';
903
- case 'browser':
904
- return 'DESKTOP';
905
- default:
906
- return 'DESKTOP';
907
- }
908
- };
909
- exports.makeDeviceType = makeDeviceType;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeDeviceType = exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.compareCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItemsForCart = exports.makeUpsellItemIds = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNamesForGroups = exports.makeModifierNames = exports.getItemOptions = void 0;
4
+ const types_1 = require("@open-tender/types");
5
+ const helpers_1 = require("./helpers");
6
+ const favorites_1 = require("./favorites");
7
+ // TODO: refactor for nested mods (if needed)
8
+ const getItemOptions = (item) => {
9
+ if (!item.groups.length)
10
+ return [];
11
+ const options = item.groups
12
+ .map(group => group.options.filter(option => option.quantity > 0))
13
+ .reduce((arr, i) => [...arr, ...i], []);
14
+ return options;
15
+ };
16
+ exports.getItemOptions = getItemOptions;
17
+ const makeModifierNames = (item) => {
18
+ return item.groups
19
+ .map(group => group.options.filter(option => option.quantity > 0))
20
+ .reduce((arr, i) => [...arr, ...i], [])
21
+ .map(i => {
22
+ const optionNames = (0, exports.makeModifierNames)(i);
23
+ const name = optionNames ? `${i.name} (${optionNames})` : i.name;
24
+ return Object.assign(Object.assign({}, i), { name });
25
+ })
26
+ .map(option => option.name)
27
+ .join(', ');
28
+ };
29
+ exports.makeModifierNames = makeModifierNames;
30
+ const makeModifierNamesForGroups = (groups) => {
31
+ return groups
32
+ .map(group => group.options.filter(option => option.quantity > 0))
33
+ .reduce((arr, i) => [...arr, ...i], [])
34
+ .map(i => {
35
+ const optionNames = (0, exports.makeModifierNamesForGroups)(i.groups);
36
+ const name = optionNames ? `${i.name} (${optionNames})` : i.name;
37
+ return Object.assign(Object.assign({}, i), { name });
38
+ })
39
+ .map(option => option.name)
40
+ .join(', ');
41
+ };
42
+ exports.makeModifierNamesForGroups = makeModifierNamesForGroups;
43
+ // export const makeModifierNames = <T extends BaseItem>(item: T): string => {
44
+ // return makeModifierNamesForGroups(item.groups)
45
+ // }
46
+ const hasGroupsBelowMin = (option, groups) => {
47
+ if (!groups || !groups.length)
48
+ return false;
49
+ if (option && option.quantity === 0)
50
+ return false;
51
+ const childGroupsBelow = groups.filter(g => {
52
+ const optionsBelowMin = g.options.filter(o => (0, exports.hasGroupsBelowMin)(o, o.groups));
53
+ return optionsBelowMin.length > 0;
54
+ });
55
+ if (childGroupsBelow.length > 0)
56
+ return true;
57
+ return groups.filter(g => ((g === null || g === void 0 ? void 0 : g.quantity) || 0) < g.min).length > 0;
58
+ };
59
+ exports.hasGroupsBelowMin = hasGroupsBelowMin;
60
+ const makeModifierGroups = (groups) => {
61
+ if (!groups || !groups.length)
62
+ return [[], []];
63
+ const otherGroups = groups.filter(g => !g.isSize);
64
+ if (otherGroups.length)
65
+ return [groups, []];
66
+ const sizeGroup = groups.find(g => g.isSize);
67
+ if (!sizeGroup)
68
+ return [[], []];
69
+ const sizeGroupNested = sizeGroup.options.filter(o => o.groups && o.groups.length);
70
+ if (!sizeGroupNested || !sizeGroupNested.length)
71
+ return [[], []];
72
+ const selectedOption = sizeGroup.options.find(o => o.quantity > 0);
73
+ return selectedOption
74
+ ? [selectedOption.groups, [[sizeGroup.id, selectedOption.id]]]
75
+ : [[], []];
76
+ };
77
+ exports.makeModifierGroups = makeModifierGroups;
78
+ const makeUpsellItems = (itemIds, itemLookup) => {
79
+ return itemIds
80
+ .map(id => itemLookup[id])
81
+ .filter(i => i)
82
+ .map(i => (0, exports.makeOrderItem)(i))
83
+ .filter(i => !i.isSoldOut)
84
+ .filter(i => {
85
+ const belowMin = i.groups.filter(g => !g.isSize && (g.quantity || 0) < g.min);
86
+ return belowMin.length === 0;
87
+ });
88
+ };
89
+ exports.makeUpsellItems = makeUpsellItems;
90
+ const makeUpsellItemIds = (item, cartIds, soldOut) => {
91
+ const upsellItems = item ? item.upsellItems : null;
92
+ return upsellItems
93
+ ? upsellItems.filter(id => !cartIds.includes(id) && !soldOut.includes(id))
94
+ : [];
95
+ };
96
+ exports.makeUpsellItemIds = makeUpsellItemIds;
97
+ const makeUpsellItemsForCart = (categories, cartIds, soldOut) => {
98
+ const itemLookup = (0, favorites_1.makeMenuItemLookup)(categories);
99
+ const menuItems = cartIds.map(id => itemLookup[id]).filter(Boolean);
100
+ const upsellItemIds = menuItems.reduce((arr, i) => [...arr, ...i.upsell_items], []);
101
+ const uniqueIds = Array.from(new Set(upsellItemIds));
102
+ const itemIds = uniqueIds.filter(id => !cartIds.includes(id) && !soldOut.includes(id));
103
+ return (0, exports.makeUpsellItems)(itemIds, itemLookup);
104
+ };
105
+ exports.makeUpsellItemsForCart = makeUpsellItemsForCart;
106
+ const nutritionKeys = [
107
+ 'calories',
108
+ 'cholesterol',
109
+ 'dietary_fiber',
110
+ 'protein',
111
+ 'saturated_fat',
112
+ 'sodium',
113
+ 'sugars',
114
+ 'total_carbs',
115
+ 'total_fat',
116
+ 'trans_fat'
117
+ ];
118
+ const calcNutrition = (item) => {
119
+ const { nutritionalInfo, groups } = item;
120
+ let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
121
+ const options = groups.reduce((arr, g) => {
122
+ return [...arr, ...g.options.filter(o => o.quantity > 0)];
123
+ }, []);
124
+ options.forEach(option => {
125
+ const optionNutrition = (0, exports.calcNutrition)(option);
126
+ nutritionKeys.forEach(key => {
127
+ const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
128
+ const optionVal = optionPer * option.quantity;
129
+ const itemVal = nutrition[key] || 0;
130
+ nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
131
+ });
132
+ });
133
+ return nutrition;
134
+ };
135
+ exports.calcNutrition = calcNutrition;
136
+ const calcNutritionalInfo = (item) => {
137
+ const nutrition = (0, exports.calcNutrition)(item);
138
+ const { serving_size = '0.00' } = item.nutritionalInfo || {};
139
+ return Object.assign(Object.assign({}, nutrition), { serving_size });
140
+ };
141
+ exports.calcNutritionalInfo = calcNutritionalInfo;
142
+ const calcPrices = (item) => {
143
+ const groups = item.groups.map(g => {
144
+ let groupQuantity = 0;
145
+ const optionsWithPrices = g.options.map((o, index) => {
146
+ return Object.assign(Object.assign({}, (0, exports.calcPrices)(o)), { index });
147
+ });
148
+ const sortedOptions = optionsWithPrices.sort((a, b) => {
149
+ const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
150
+ const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
151
+ return pricePerA - pricePerB;
152
+ });
153
+ const pricedOptions = sortedOptions.map(o => {
154
+ const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
155
+ const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
156
+ const includedRemaining = Math.max(g.included - groupQuantity, 0);
157
+ const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
158
+ const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals: o.totalCals || 0 });
159
+ groupQuantity += o.quantity || 0;
160
+ return option;
161
+ });
162
+ const options = pricedOptions.sort((a, b) => a.index - b.index);
163
+ return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
164
+ });
165
+ const optionsPrice = groups.reduce((t, g) => {
166
+ return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
167
+ }, 0.0);
168
+ const totalPrice = item.quantity * (item.price + optionsPrice);
169
+ const optionsPoints = groups.reduce((t, g) => {
170
+ return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
171
+ }, 0.0);
172
+ const totalPoints = item.points
173
+ ? item.quantity * (item.points + optionsPoints)
174
+ : null;
175
+ const optionsCals = groups.reduce((t, g) => {
176
+ return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
177
+ }, 0.0);
178
+ const totalCals = item.cals || item.cals === 0
179
+ ? item.quantity * (item.cals + optionsCals)
180
+ : null;
181
+ const nonSizeGroups = groups ? groups.filter(i => !i.isSize) : null;
182
+ const currentOptions = nonSizeGroups
183
+ ? (0, exports.getItemOptions)(Object.assign(Object.assign({}, item), { groups: nonSizeGroups }))
184
+ : [];
185
+ const modifiersAllergens = currentOptions.reduce((t, o) => {
186
+ return [...t, ...o.allergens];
187
+ }, []);
188
+ const totalAllergens = [...new Set(item.allergens.concat(modifiersAllergens))];
189
+ return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals, totalAllergens });
190
+ };
191
+ exports.calcPrices = calcPrices;
192
+ const checkSoldOut = (id, suspendUntil, soldOut = []) => {
193
+ if (soldOut.includes(id))
194
+ return true;
195
+ return suspendUntil ? suspendUntil > Date.now() / 1000 : false;
196
+ };
197
+ exports.checkSoldOut = checkSoldOut;
198
+ const makeOrderItemGroups = (optionGroups, isEdit, soldOut = []) => {
199
+ if (!optionGroups)
200
+ return [];
201
+ const groups = optionGroups.map(g => {
202
+ const options = g.option_items.map(o => {
203
+ var _a;
204
+ const isSoldOut = (0, exports.checkSoldOut)(o.id, o.suspend_until, soldOut);
205
+ const quantity = isSoldOut
206
+ ? 0
207
+ : o.opt_is_default && !isEdit
208
+ ? o.min_quantity || 1
209
+ : 0;
210
+ const cals = ((_a = o.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories)
211
+ ? parseInt(o.nutritional_info.calories.toString())
212
+ : null;
213
+ const groups = makeOrderItemGroups(o.option_groups, isEdit, soldOut);
214
+ const option = {
215
+ allergens: (0, helpers_1.convertStringToArray)(o.allergens),
216
+ allergens_list: o.allergens_list || [],
217
+ cals: cals,
218
+ description: o.description,
219
+ groups: groups,
220
+ id: o.id,
221
+ imageUrl: o.small_image_url,
222
+ increment: o.increment,
223
+ ingredients: o.ingredients,
224
+ isDefault: o.opt_is_default,
225
+ isSoldOut: isSoldOut,
226
+ max: isSoldOut ? 0 : o.max_quantity,
227
+ min: o.min_quantity,
228
+ name: o.name,
229
+ nutritionalInfo: o.nutritional_info,
230
+ points: o.points || 0,
231
+ price: parseFloat(o.price),
232
+ quantity: quantity,
233
+ shorthand: o.shorthand,
234
+ shortDescription: o.short_description,
235
+ shortName: o.short_name,
236
+ slug: o.slug,
237
+ tags: (0, helpers_1.convertStringToArray)(o.tags),
238
+ tags_list: o.tags_list || [],
239
+ totalAllergens: [],
240
+ totalCals: null,
241
+ totalPoints: null,
242
+ totalPrice: null
243
+ };
244
+ return option;
245
+ });
246
+ const group = {
247
+ description: g.description,
248
+ id: g.id,
249
+ imageUrl: g.small_image_url,
250
+ included: g.included_options,
251
+ isPizza: !!g.is_pizza,
252
+ isSize: !!g.is_size,
253
+ max: g.max_options,
254
+ min: g.min_options,
255
+ name: g.name,
256
+ options: options,
257
+ quantity: options.reduce((t, o) => t + o.quantity, 0)
258
+ };
259
+ return group;
260
+ });
261
+ return groups;
262
+ };
263
+ const makeOrderItem = (item, isEdit, soldOut = [], simpleItem, hasPoints = false) => {
264
+ var _a;
265
+ const groups = makeOrderItemGroups(item.option_groups, isEdit, soldOut);
266
+ const cals = ((_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories)
267
+ ? parseInt(item.nutritional_info.calories.toString())
268
+ : null;
269
+ const isSoldOut = (0, exports.checkSoldOut)(item.id, item.suspend_until, soldOut);
270
+ const orderItem = {
271
+ allergens: (0, helpers_1.convertStringToArray)(item.allergens),
272
+ allergens_list: item.allergens_list || [],
273
+ cals: cals,
274
+ cartGuestId: simpleItem ? simpleItem.cart_guest_id || null : null,
275
+ customerId: simpleItem ? simpleItem.customer_id || null : null,
276
+ description: item.description,
277
+ groups: groups,
278
+ id: item.id,
279
+ imageUrl: item.large_image_url,
280
+ increment: item.increment,
281
+ // index: null,
282
+ ingredients: item.ingredients,
283
+ isSoldOut: isSoldOut,
284
+ madeFor: simpleItem ? simpleItem.made_for || null : null,
285
+ max: item.max_quantity,
286
+ min: item.min_quantity,
287
+ name: item.name,
288
+ notes: simpleItem ? simpleItem.notes || null : null,
289
+ nutritionalInfo: item.nutritional_info,
290
+ points: hasPoints ? item.points || null : null,
291
+ price: parseFloat(item.price),
292
+ quantity: item.min_quantity || 1 * item.increment,
293
+ shorthand: item.shorthand,
294
+ shortDescription: item.short_description,
295
+ shortName: item.short_name,
296
+ similarItems: item.similar_items,
297
+ slug: item.slug,
298
+ tags: (0, helpers_1.convertStringToArray)(item.tags),
299
+ tags_list: item.tags_list || [],
300
+ totalAllergens: [],
301
+ totalCals: null,
302
+ totalPoints: null,
303
+ totalPrice: null,
304
+ upsellItems: item.upsell_items
305
+ };
306
+ const pricedItem = (0, exports.calcPrices)(orderItem);
307
+ return pricedItem;
308
+ };
309
+ exports.makeOrderItem = makeOrderItem;
310
+ const calcCartCounts = (cart) => {
311
+ return cart.reduce((obj, item) => {
312
+ const newCount = (obj[item.id.toString()] || 0) + item.quantity;
313
+ return Object.assign(Object.assign({}, obj), { [item.id]: newCount });
314
+ }, {});
315
+ };
316
+ exports.calcCartCounts = calcCartCounts;
317
+ const makeSimpleGroupsLookup = (groups) => {
318
+ if (!groups || !groups.length)
319
+ return null;
320
+ return groups.reduce((grpObj, g) => {
321
+ const options = g.options.reduce((optObj, o) => {
322
+ optObj[o.id] = {
323
+ quantity: o.quantity,
324
+ groupsLookup: makeSimpleGroupsLookup(o.groups)
325
+ };
326
+ return optObj;
327
+ }, {});
328
+ grpObj[g.id] = { options };
329
+ return grpObj;
330
+ }, {});
331
+ };
332
+ const rehydrateGroups = (groups, groupsLookup) => {
333
+ if (!groupsLookup)
334
+ return groups;
335
+ groups.forEach(group => {
336
+ const simpleGroup = groupsLookup[group.id];
337
+ if (simpleGroup) {
338
+ group.options.forEach(option => {
339
+ const simpleOption = simpleGroup.options[option.id];
340
+ if (simpleOption) {
341
+ option.quantity = simpleOption.quantity;
342
+ option.groups = rehydrateGroups(option.groups, simpleOption.groupsLookup);
343
+ }
344
+ });
345
+ }
346
+ });
347
+ return groups;
348
+ };
349
+ const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = []) => {
350
+ const orderItem = (0, exports.makeOrderItem)(menuItem, true, soldOut, simpleCartItem);
351
+ orderItem.quantity = simpleCartItem.quantity || 1;
352
+ const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
353
+ if (groupsLookup) {
354
+ orderItem.groups = rehydrateGroups(orderItem.groups, groupsLookup);
355
+ }
356
+ const pricedItem = (0, exports.calcPrices)(orderItem);
357
+ return pricedItem;
358
+ };
359
+ exports.rehydrateOrderItem = rehydrateOrderItem;
360
+ const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
361
+ const orderItems = [];
362
+ simpleCartItems.forEach(item => {
363
+ const menuItem = menuItems.find(i => i.id === item.id);
364
+ if (menuItem) {
365
+ const orderItem = (0, exports.rehydrateOrderItem)(menuItem, item, soldOut);
366
+ orderItems.push(orderItem);
367
+ }
368
+ });
369
+ const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
370
+ const cartCounts = (0, exports.calcCartCounts)(cart);
371
+ return { cart, cartCounts };
372
+ };
373
+ exports.rehydrateCart = rehydrateCart;
374
+ // TODO: maybe refactor
375
+ const rehydrateDetails = (details) => {
376
+ return {
377
+ cart_id: details === null || details === void 0 ? void 0 : details.cart_id,
378
+ eating_utensils: details === null || details === void 0 ? void 0 : details.eating_utensils,
379
+ notes: details === null || details === void 0 ? void 0 : details.notes,
380
+ person_count: details === null || details === void 0 ? void 0 : details.person_count,
381
+ serving_utensils: details === null || details === void 0 ? void 0 : details.serving_utensils,
382
+ tax_exempt_id: details === null || details === void 0 ? void 0 : details.tax_exempt_id
383
+ };
384
+ };
385
+ // TODO: maybe refactor
386
+ const rehydrateCustomer = (customer) => {
387
+ return {
388
+ customer_id: customer.customer_id,
389
+ first_name: customer.first_name,
390
+ last_name: customer.last_name,
391
+ email: customer.email,
392
+ phone: customer.phone,
393
+ company: customer.company || ''
394
+ };
395
+ };
396
+ // TODO: maybe refactor
397
+ const rehydrateAddress = (address) => {
398
+ if (!address)
399
+ return {};
400
+ return {
401
+ unit: address.unit,
402
+ company: address.company,
403
+ contact: address.contact,
404
+ phone: address.phone
405
+ };
406
+ };
407
+ const rehydrateSurcharges = (surcharges) => {
408
+ return (surcharges === null || surcharges === void 0 ? void 0 : surcharges.filter(i => i.is_optional).map(i => ({ id: i.id }))) || [];
409
+ };
410
+ const rehydrateDiscounts = (discounts) => {
411
+ return ((discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => !i.is_promo_code).map(i => ({ id: i.id, ext_id: i.ext_id }))) || []);
412
+ };
413
+ const rehydratePromoCodes = (discounts) => {
414
+ return (discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => i.is_promo_code).map(i => i.name)) || [];
415
+ };
416
+ const rehydrateTenders = (tenders) => {
417
+ const other = tenders === null || tenders === void 0 ? void 0 : tenders.map(i => {
418
+ if ((0, types_1.isCreditTender)(i)) {
419
+ return Object.assign(Object.assign({}, i), i.credit_card);
420
+ }
421
+ else if ((0, types_1.isHouseAccountTender)(i)) {
422
+ return Object.assign(Object.assign({}, i), i.house_account);
423
+ }
424
+ else {
425
+ return i;
426
+ }
427
+ });
428
+ return other;
429
+ };
430
+ const rehydrateCheckoutForm = (order) => {
431
+ const form = {
432
+ details: rehydrateDetails(order.details),
433
+ customer: order.customer && (0, types_1.isOrderCustomer)(order.customer)
434
+ ? rehydrateCustomer(order.customer)
435
+ : {},
436
+ address: rehydrateAddress(order.address),
437
+ surcharges: rehydrateSurcharges(order.surcharges),
438
+ discounts: rehydrateDiscounts(order.discounts),
439
+ promoCodes: rehydratePromoCodes(order.discounts),
440
+ tenders: rehydrateTenders(order.tenders),
441
+ tip: order.totals.tip
442
+ };
443
+ return form;
444
+ };
445
+ exports.rehydrateCheckoutForm = rehydrateCheckoutForm;
446
+ // group orders
447
+ const makeGuestLookup = (cartGuests) => {
448
+ return cartGuests.reduce((obj, i) => i.cart_guest_id ? Object.assign(Object.assign({}, obj), { [i.cart_guest_id]: i }) : Object.assign({}, obj), {});
449
+ };
450
+ exports.makeGuestLookup = makeGuestLookup;
451
+ const compareCarts = (cart) => {
452
+ if (!cart)
453
+ return null;
454
+ const customerCart = cart.filter(i => !i.cartGuestId);
455
+ return customerCart.length < cart.length ? customerCart : null;
456
+ };
457
+ exports.compareCarts = compareCarts;
458
+ const combineCarts = (cart, guestCart, cartOwner, cartGuests) => {
459
+ const guestLookup = (0, exports.makeGuestLookup)(cartGuests);
460
+ const withGuestNames = guestCart
461
+ .filter(i => i.cartGuestId && guestLookup[i.cartGuestId])
462
+ .map(i => {
463
+ const guest = i.cartGuestId ? guestLookup[i.cartGuestId] : null;
464
+ const madeFor = guest ? `${guest === null || guest === void 0 ? void 0 : guest.first_name} ${guest === null || guest === void 0 ? void 0 : guest.last_name}` : null;
465
+ return Object.assign(Object.assign({}, i), { madeFor });
466
+ });
467
+ const withOwnerName = cart.map(i => {
468
+ return Object.assign(Object.assign({}, i), { customerId: cartOwner.customer_id, madeFor: `${cartOwner.first_name} ${cartOwner.last_name}` });
469
+ });
470
+ return [...withOwnerName, ...withGuestNames];
471
+ };
472
+ exports.combineCarts = combineCarts;
473
+ // display items from past orders
474
+ const makeItemImageUrl = (images) => {
475
+ const imageMap = images
476
+ .filter(i => i.url)
477
+ .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.type]: i.url })), {});
478
+ return (imageMap.SMALL_IMAGE || imageMap.LARGE_IMAGE || imageMap.APP_IMAGE || null);
479
+ };
480
+ exports.makeItemImageUrl = makeItemImageUrl;
481
+ const makeDisplayItemGroups = (optionGroups) => {
482
+ if (!optionGroups || !optionGroups.length)
483
+ return [];
484
+ return optionGroups.map(g => {
485
+ var _a;
486
+ const options = (_a = g.options) === null || _a === void 0 ? void 0 : _a.map(o => (0, exports.makeDisplayItem)(o));
487
+ return Object.assign(Object.assign({}, g), { options });
488
+ });
489
+ };
490
+ exports.makeDisplayItemGroups = makeDisplayItemGroups;
491
+ const makeDisplayItem = (item, isOption = false) => {
492
+ var _a;
493
+ const cals = item.nutritional_info ? (_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories : null;
494
+ let displayItem = {
495
+ allergens: item.allergens,
496
+ cals: cals,
497
+ description: item.description,
498
+ groups: (0, exports.makeDisplayItemGroups)(item.groups),
499
+ imageUrl: (0, exports.makeItemImageUrl)(item.images),
500
+ id: item.id,
501
+ name: item.name,
502
+ nutritionalInfo: item.nutritional_info,
503
+ quantity: item.quantity,
504
+ points: null,
505
+ price: item.price,
506
+ shortDescription: item.short_description,
507
+ tags: item.tags,
508
+ totalPrice: item.price_total
509
+ };
510
+ if (!isOption) {
511
+ const itemDetails = {
512
+ madeFor: item.made_for,
513
+ notes: item.notes,
514
+ signature: (0, exports.makeItemSignature)(displayItem)
515
+ };
516
+ displayItem = Object.assign(Object.assign({}, displayItem), itemDetails);
517
+ }
518
+ return displayItem;
519
+ };
520
+ exports.makeDisplayItem = makeDisplayItem;
521
+ const makeDisplayItems = (orders) => {
522
+ const items = orders.reduce((items, order) => [...items, ...order.cart], []);
523
+ return items.map(i => (0, exports.makeDisplayItem)(i));
524
+ };
525
+ exports.makeDisplayItems = makeDisplayItems;
526
+ const makeUniqueDisplayItems = (orders) => {
527
+ const items = (0, exports.makeDisplayItems)(orders);
528
+ const unique = [];
529
+ const signatures = [];
530
+ items.forEach(item => {
531
+ var _a;
532
+ if (signatures.includes((_a = item.signature) !== null && _a !== void 0 ? _a : ''))
533
+ return;
534
+ item.signature && signatures.push(item.signature);
535
+ unique.push(item);
536
+ });
537
+ return unique;
538
+ };
539
+ exports.makeUniqueDisplayItems = makeUniqueDisplayItems;
540
+ const makeFavoritesLookup = (favorites) => {
541
+ if (!favorites)
542
+ return {};
543
+ return favorites
544
+ .filter(i => i.cart)
545
+ .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [(0, exports.makeItemSignature)(i.cart)]: i.favorite_id })), {});
546
+ };
547
+ exports.makeFavoritesLookup = makeFavoritesLookup;
548
+ const makeCartItemOptionIds = (groups) => {
549
+ if (!groups || !groups.length)
550
+ return [];
551
+ return groups
552
+ .reduce((arr, group) => {
553
+ const ids = group.options
554
+ .filter(o => o.quantity > 0)
555
+ .reduce((optionsArr, o) => {
556
+ const optionArr = new Array(o.quantity);
557
+ optionArr.fill(o.id);
558
+ const nestedIds = makeCartItemOptionIds(o.groups);
559
+ return [...optionsArr, ...optionArr, ...nestedIds];
560
+ }, []);
561
+ return [...arr, ...ids];
562
+ }, [])
563
+ .sort((a, b) => a - b);
564
+ };
565
+ const makeItemSignature = (item) => {
566
+ const optionIds = makeCartItemOptionIds(item.groups);
567
+ return [item.id, ...optionIds].join('.');
568
+ };
569
+ exports.makeItemSignature = makeItemSignature;
570
+ const makeCartItemSignature = (item) => {
571
+ const optionIds = makeCartItemOptionIds(item.groups);
572
+ const itemIds = [item.id, ...optionIds].join('.');
573
+ const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
574
+ return signature;
575
+ };
576
+ exports.makeCartItemSignature = makeCartItemSignature;
577
+ const addItem = (cart, item) => {
578
+ if (typeof item.index === 'undefined') {
579
+ const itemSignature = (0, exports.makeCartItemSignature)(item);
580
+ const itemSignatures = cart.map(i => (Object.assign({ signature: (0, exports.makeCartItemSignature)(i) }, i)));
581
+ const match = itemSignatures.find(i => i.signature === itemSignature);
582
+ if (match && match.index !== undefined) {
583
+ const quantity = item.quantity + match.quantity;
584
+ const newItem = Object.assign(Object.assign({}, item), { quantity });
585
+ const updated = (0, exports.calcPrices)(newItem);
586
+ cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
587
+ }
588
+ else {
589
+ cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
590
+ }
591
+ }
592
+ else {
593
+ cart[item.index] = item;
594
+ }
595
+ const cartCounts = (0, exports.calcCartCounts)(cart);
596
+ return { cart, cartCounts };
597
+ };
598
+ exports.addItem = addItem;
599
+ const getItemRealIndex = (cart, index) => cart.findIndex(item => item.index === index);
600
+ const removeItem = (cart, index) => {
601
+ const realIndex = getItemRealIndex(cart, index);
602
+ if (realIndex > -1) {
603
+ cart.splice(realIndex, 1);
604
+ cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
605
+ }
606
+ const cartCounts = (0, exports.calcCartCounts)(cart);
607
+ return { cart, cartCounts };
608
+ };
609
+ exports.removeItem = removeItem;
610
+ const incrementItem = (cart, index) => {
611
+ const realIndex = getItemRealIndex(cart, index);
612
+ if (realIndex > -1) {
613
+ const item = cart[realIndex];
614
+ if (item.max === 0 || item.quantity < item.max) {
615
+ let newQuantity = item.quantity + item.increment;
616
+ newQuantity =
617
+ item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
618
+ const newItem = (0, exports.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
619
+ cart[realIndex] = newItem;
620
+ }
621
+ }
622
+ const cartCounts = (0, exports.calcCartCounts)(cart);
623
+ return { cart, cartCounts };
624
+ };
625
+ exports.incrementItem = incrementItem;
626
+ const decrementItem = (cart, index) => {
627
+ const realIndex = getItemRealIndex(cart, index);
628
+ if (realIndex > -1) {
629
+ const item = cart[realIndex];
630
+ const newQuantity = Math.max(item.quantity - item.increment, 0);
631
+ if (newQuantity === 0 || newQuantity < item.min) {
632
+ cart.splice(realIndex, 1);
633
+ cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
634
+ }
635
+ else {
636
+ const newItem = (0, exports.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
637
+ cart[realIndex] = newItem;
638
+ }
639
+ }
640
+ const cartCounts = (0, exports.calcCartCounts)(cart);
641
+ return { cart, cartCounts };
642
+ };
643
+ exports.decrementItem = decrementItem;
644
+ const makeSimpleGroups = (groups) => {
645
+ return groups.map(g => {
646
+ const options = g.options
647
+ .filter(o => o.quantity !== 0)
648
+ .map(o => ({
649
+ id: o.id,
650
+ quantity: o.quantity,
651
+ groups: makeSimpleGroups(o.groups || [])
652
+ }));
653
+ return { id: g.id, options: options };
654
+ });
655
+ };
656
+ const makeSimpleCart = (cart) => {
657
+ const simpleCart = cart.map(i => {
658
+ return {
659
+ id: i.id,
660
+ quantity: i.quantity,
661
+ groups: makeSimpleGroups(i.groups),
662
+ made_for: i.madeFor || i.made_for || '',
663
+ notes: i.notes || ''
664
+ };
665
+ });
666
+ return simpleCart;
667
+ };
668
+ exports.makeSimpleCart = makeSimpleCart;
669
+ /* cart validation */
670
+ // TODO: refactor for nested modifiers
671
+ // convert menu items to order items for validation but only include
672
+ // items that are already in the cart to save wasted effort
673
+ const makeItemLookup = (categories, itemIds) => {
674
+ const itemLookup = {};
675
+ categories.forEach(category => {
676
+ category.items.forEach(item => {
677
+ if (itemIds.includes(item.id)) {
678
+ itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
679
+ }
680
+ });
681
+ category.children.forEach(child => {
682
+ child.items.forEach(item => {
683
+ if (itemIds.includes(item.id)) {
684
+ itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
685
+ }
686
+ });
687
+ });
688
+ });
689
+ return itemLookup;
690
+ };
691
+ // TODO: refactor for nested modifiers
692
+ const makeGroupsLookup = (item) => {
693
+ return item.groups.reduce((grpObj, i) => {
694
+ const options = i.options.reduce((optObj, o) => {
695
+ if (o.id) {
696
+ optObj[o.id] = o;
697
+ }
698
+ return optObj;
699
+ }, {});
700
+ grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
701
+ return grpObj;
702
+ }, {});
703
+ };
704
+ // TODO: refactor for nested modifiers
705
+ const validateCart = (cart, categories, soldOut) => {
706
+ const itemIds = cart.map(item => item.id);
707
+ const itemLookup = makeItemLookup(categories, itemIds);
708
+ let errors = null;
709
+ const missingItems = [];
710
+ const invalidItems = [];
711
+ let newCart = cart.map(oldItem => {
712
+ var _a, _b, _c, _d;
713
+ const newItem = itemLookup[oldItem.id];
714
+ if (!newItem || soldOut.includes(oldItem.id)) {
715
+ missingItems.push(oldItem);
716
+ return null;
717
+ }
718
+ let missingGroups = [];
719
+ const invalidGroups = [];
720
+ const missingOptions = [];
721
+ const invalidOptions = [];
722
+ const newGroups = makeGroupsLookup(newItem);
723
+ // check if any required groups for the new item are missing from the old item
724
+ const newRequiredGroups = Object.values(newGroups).filter(i => i.min > 0);
725
+ const oldGroupIds = (_a = oldItem.groups) === null || _a === void 0 ? void 0 : _a.map(i => i.id);
726
+ missingGroups = newRequiredGroups.filter(i => !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id)));
727
+ const newEmptyGroups = Object.values(newGroups)
728
+ .filter((i) => {
729
+ return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
730
+ })
731
+ .map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
732
+ const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map((group) => {
733
+ const optionCount = group.options.reduce((t, i) => (t += i.quantity), 0);
734
+ const newGroup = newGroups[group.id];
735
+ // check if option group is still part of the item
736
+ if (!newGroup) {
737
+ if (optionCount > 0) {
738
+ invalidGroups.push(group);
739
+ return group;
740
+ }
741
+ else {
742
+ return null;
743
+ }
744
+ }
745
+ else {
746
+ const updatedOptions = group.options.map((option) => {
747
+ const newOption = newGroup.options[option.id];
748
+ // check to see if any OLD options aren't avaiilable on the current menu
749
+ // or if any of the options are currently sold out
750
+ if (!newOption ||
751
+ (option.quantity > 0 && soldOut.includes(option.id))) {
752
+ missingOptions.push(option);
753
+ return option;
754
+ }
755
+ else {
756
+ // check to see if option quantity is valid relative to option min & max
757
+ // ignore the option increment setting for the time being
758
+ if ((newOption.max && option.quantity > newOption.max) ||
759
+ (newOption.min && option.quantity < newOption.min)) {
760
+ invalidOptions.push(option);
761
+ return option;
762
+ }
763
+ else {
764
+ // update the old option price to the new option price
765
+ const updatedOption = Object.assign(Object.assign({}, option), { price: newOption.price });
766
+ return updatedOption;
767
+ }
768
+ }
769
+ });
770
+ // check to see if the option count is greater than the max (if one exists)
771
+ // or less than the min (if one exists)
772
+ if ((newGroup.max && optionCount > newGroup.max) ||
773
+ (newGroup.min && optionCount < newGroup.min)) {
774
+ invalidGroups.push(group);
775
+ }
776
+ const updatedGroup = Object.assign(Object.assign({}, group), { options: updatedOptions });
777
+ return updatedGroup;
778
+ }
779
+ });
780
+ if (missingGroups.length ||
781
+ invalidGroups.length ||
782
+ missingOptions.length ||
783
+ invalidOptions.length) {
784
+ const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
785
+ invalidGroups,
786
+ missingOptions,
787
+ invalidOptions });
788
+ invalidItems.push(invalidItem);
789
+ return null;
790
+ }
791
+ else {
792
+ // check to see if this item's quantity is below the min or
793
+ // above the max from the current menu
794
+ if ((newItem.max && ((_c = oldItem.quantity) !== null && _c !== void 0 ? _c : 0) > newItem.max) ||
795
+ (newItem.min && ((_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0) < newItem.min)) {
796
+ const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
797
+ invalidGroups,
798
+ missingOptions,
799
+ invalidOptions });
800
+ invalidItems.push(invalidItem);
801
+ return null;
802
+ }
803
+ const nonNullGroups = updatedGroups === null || updatedGroups === void 0 ? void 0 : updatedGroups.filter(i => i !== null);
804
+ const allNewGroups = [...(nonNullGroups !== null && nonNullGroups !== void 0 ? nonNullGroups : []), ...newEmptyGroups];
805
+ const updatedItem = Object.assign(Object.assign({}, oldItem), { groups: allNewGroups });
806
+ // update the old item price to the new item price
807
+ updatedItem.price = newItem.price;
808
+ const pricedItem = (0, exports.calcPrices)(updatedItem);
809
+ return pricedItem;
810
+ }
811
+ });
812
+ newCart = newCart.filter(i => i !== null);
813
+ if (missingItems.length || invalidItems.length) {
814
+ errors = { missingItems, invalidItems };
815
+ }
816
+ return { newCart: newCart, errors };
817
+ };
818
+ exports.validateCart = validateCart;
819
+ /* order submission */
820
+ const getDefaultTip = (config) => {
821
+ if (!config || !config.gratuity || !config.gratuity.has_tip)
822
+ return null;
823
+ return config.gratuity.default ? config.gratuity.default.amount : null;
824
+ };
825
+ exports.getDefaultTip = getDefaultTip;
826
+ const prepareOrder = (data) => {
827
+ const order = {
828
+ revenue_center_id: data.revenueCenterId,
829
+ service_type: data.serviceType,
830
+ requested_at: data.requestedAt,
831
+ cart: (0, exports.makeSimpleCart)(data.cart)
832
+ };
833
+ if (data.customer)
834
+ order.customer = data.customer;
835
+ if (data.details) {
836
+ const details = Object.assign({}, data.details);
837
+ // person_count must be submitted as integer
838
+ if (details.person_count)
839
+ details.person_count = parseInt(`${details.person_count}`) || null;
840
+ if (data.deviceType)
841
+ details.device_type = data.deviceType;
842
+ if (data.table || data.prepType) {
843
+ const prepType = data.prepType
844
+ ? data.prepType
845
+ : data.table
846
+ ? 'EAT_HERE'
847
+ : 'TAKE_OUT';
848
+ details.notes_internal =
849
+ prepType === 'EAT_HERE'
850
+ ? data.table
851
+ ? `DINE IN -- TABLE ${data.table}`
852
+ : 'DINE IN'
853
+ : 'TAKE OUT';
854
+ }
855
+ order.details = Object.assign({}, details);
856
+ }
857
+ if (data.surcharges)
858
+ order.surcharges = data.surcharges;
859
+ if (data.discounts)
860
+ order.discounts = data.discounts;
861
+ if (data.discountsInternal)
862
+ order.discounts_internal = data.discountsInternal;
863
+ if (data.promoCodes)
864
+ order.promo_codes = data.promoCodes;
865
+ if (data.points)
866
+ order.points = data.points;
867
+ if (data.tip)
868
+ order.tip = data.tip;
869
+ if (data.deposit)
870
+ order.deposit = data.deposit;
871
+ if (data.tenders)
872
+ order.tenders = data.tenders;
873
+ if (data.address && data.serviceType === 'DELIVERY') {
874
+ order.address = data.address;
875
+ }
876
+ else {
877
+ order.address = null;
878
+ }
879
+ if (data.orderId)
880
+ order.order_id = data.orderId;
881
+ if (data.cartId)
882
+ order.cart_id = data.cartId;
883
+ if (data.save)
884
+ order.save = data.save;
885
+ return order;
886
+ };
887
+ exports.prepareOrder = prepareOrder;
888
+ const checkAmountRemaining = (total, tenders) => {
889
+ if (!tenders)
890
+ return total ? parseFloat(total) : 0.0;
891
+ let remaining = parseFloat(total) -
892
+ tenders.reduce((t, i) => { var _a; return (t += parseFloat((_a = i.amount) !== null && _a !== void 0 ? _a : '0')); }, 0.0);
893
+ remaining = Math.round((remaining + Number.EPSILON) * 100) / 100;
894
+ return Object.is(remaining, -0) ? 0.0 : remaining;
895
+ };
896
+ exports.checkAmountRemaining = checkAmountRemaining;
897
+ const makeDeviceType = (deviceType) => {
898
+ switch (deviceType) {
899
+ case 'tablet':
900
+ return 'TABLET';
901
+ case 'mobile':
902
+ return 'MOBILE';
903
+ case 'browser':
904
+ return 'DESKTOP';
905
+ default:
906
+ return 'DESKTOP';
907
+ }
908
+ };
909
+ exports.makeDeviceType = makeDeviceType;