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