@open-tender/utils 0.0.68 → 0.1.0

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 (99) hide show
  1. package/dist/cjs/hooks/menu/useBuilder.d.ts +8 -7
  2. package/dist/cjs/hooks/menu/useBuilder.js +92 -86
  3. package/dist/cjs/hooks/menu/useOrderItem.d.ts +33 -43
  4. package/dist/cjs/hooks/menu/useOrderItem.js +21 -22
  5. package/dist/cjs/hooks/useAddressForm.js +1 -1
  6. package/dist/cjs/hooks/useAllergenForm.js +1 -1
  7. package/dist/cjs/hooks/useCartGuestForm.js +2 -2
  8. package/dist/cjs/hooks/useCheckoutLoginForm.js +1 -1
  9. package/dist/cjs/hooks/useCreditCard.d.ts +1 -2
  10. package/dist/cjs/hooks/useCreditCard.js +4 -3
  11. package/dist/cjs/hooks/useCreditCardForm.js +3 -3
  12. package/dist/cjs/hooks/useCurbside.js +1 -1
  13. package/dist/cjs/hooks/useDineInForm.js +1 -1
  14. package/dist/cjs/hooks/useDonationForm.js +5 -5
  15. package/dist/cjs/hooks/useGiftCardAssignForm.js +1 -1
  16. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +1 -1
  17. package/dist/cjs/hooks/useGiftCardForm.js +6 -4
  18. package/dist/cjs/hooks/useGiftCardsForm.js +22 -9
  19. package/dist/cjs/hooks/useGuestForm.js +1 -1
  20. package/dist/cjs/hooks/useImage.d.ts +1 -2
  21. package/dist/cjs/hooks/useImage.js +2 -1
  22. package/dist/cjs/hooks/useLevelUpForm.d.ts +1 -2
  23. package/dist/cjs/hooks/useLevelUpForm.js +3 -2
  24. package/dist/cjs/hooks/useOrderFulfillmentForm.js +3 -3
  25. package/dist/cjs/hooks/useOrderRating.js +1 -1
  26. package/dist/cjs/hooks/useOrderRatingForm.js +1 -1
  27. package/dist/cjs/hooks/useProfileForm.js +3 -3
  28. package/dist/cjs/hooks/useResetPasswordForm.js +2 -2
  29. package/dist/cjs/hooks/useSendResetForm.js +4 -2
  30. package/dist/cjs/hooks/useSignUpForm.js +14 -10
  31. package/dist/cjs/hooks/useSignUpGuestForm.js +3 -3
  32. package/dist/cjs/hooks/useSigninForm.js +1 -1
  33. package/dist/cjs/utils/cart.d.ts +97 -453
  34. package/dist/cjs/utils/cart.js +288 -361
  35. package/dist/cjs/utils/customer.js +1 -1
  36. package/dist/cjs/utils/errors.js +4 -2
  37. package/dist/cjs/utils/favorites.d.ts +3 -3
  38. package/dist/cjs/utils/favorites.js +11 -4
  39. package/dist/cjs/utils/helpers.d.ts +8 -3
  40. package/dist/cjs/utils/helpers.js +31 -3
  41. package/dist/cjs/utils/index.d.ts +1 -0
  42. package/dist/cjs/utils/index.js +1 -0
  43. package/dist/cjs/utils/maps.js +1 -1
  44. package/dist/cjs/utils/orders.d.ts +3 -3
  45. package/dist/cjs/utils/refunds.d.ts +4 -4
  46. package/dist/cjs/utils/revenueCenters.d.ts +11 -0
  47. package/dist/cjs/utils/revenueCenters.js +125 -0
  48. package/dist/cjs/utils/rewards.js +5 -5
  49. package/dist/cjs/utils/theme.js +7 -7
  50. package/dist/esm/hooks/menu/useBuilder.d.ts +8 -7
  51. package/dist/esm/hooks/menu/useBuilder.js +92 -86
  52. package/dist/esm/hooks/menu/useOrderItem.d.ts +33 -43
  53. package/dist/esm/hooks/menu/useOrderItem.js +22 -23
  54. package/dist/esm/hooks/useAddressForm.js +1 -1
  55. package/dist/esm/hooks/useAllergenForm.js +1 -1
  56. package/dist/esm/hooks/useCartGuestForm.js +2 -2
  57. package/dist/esm/hooks/useCheckoutLoginForm.js +1 -1
  58. package/dist/esm/hooks/useCreditCard.d.ts +1 -2
  59. package/dist/esm/hooks/useCreditCard.js +4 -5
  60. package/dist/esm/hooks/useCreditCardForm.js +3 -3
  61. package/dist/esm/hooks/useCurbside.js +1 -1
  62. package/dist/esm/hooks/useDineInForm.js +1 -1
  63. package/dist/esm/hooks/useDonationForm.js +5 -5
  64. package/dist/esm/hooks/useGiftCardAssignForm.js +1 -1
  65. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +1 -1
  66. package/dist/esm/hooks/useGiftCardForm.js +6 -4
  67. package/dist/esm/hooks/useGiftCardsForm.js +22 -9
  68. package/dist/esm/hooks/useGuestForm.js +1 -1
  69. package/dist/esm/hooks/useImage.d.ts +1 -2
  70. package/dist/esm/hooks/useImage.js +1 -2
  71. package/dist/esm/hooks/useLevelUpForm.d.ts +1 -2
  72. package/dist/esm/hooks/useLevelUpForm.js +2 -3
  73. package/dist/esm/hooks/useOrderFulfillmentForm.js +3 -3
  74. package/dist/esm/hooks/useOrderRating.js +1 -1
  75. package/dist/esm/hooks/useOrderRatingForm.js +1 -1
  76. package/dist/esm/hooks/useProfileForm.js +3 -3
  77. package/dist/esm/hooks/useResetPasswordForm.js +2 -2
  78. package/dist/esm/hooks/useSendResetForm.js +4 -2
  79. package/dist/esm/hooks/useSignUpForm.js +14 -10
  80. package/dist/esm/hooks/useSignUpGuestForm.js +3 -3
  81. package/dist/esm/hooks/useSigninForm.js +1 -1
  82. package/dist/esm/utils/cart.d.ts +97 -453
  83. package/dist/esm/utils/cart.js +276 -339
  84. package/dist/esm/utils/customer.js +1 -1
  85. package/dist/esm/utils/errors.js +4 -2
  86. package/dist/esm/utils/favorites.d.ts +3 -3
  87. package/dist/esm/utils/favorites.js +11 -4
  88. package/dist/esm/utils/helpers.d.ts +8 -3
  89. package/dist/esm/utils/helpers.js +25 -2
  90. package/dist/esm/utils/index.d.ts +1 -0
  91. package/dist/esm/utils/index.js +1 -0
  92. package/dist/esm/utils/maps.js +1 -1
  93. package/dist/esm/utils/orders.d.ts +3 -3
  94. package/dist/esm/utils/refunds.d.ts +4 -4
  95. package/dist/esm/utils/revenueCenters.d.ts +11 -0
  96. package/dist/esm/utils/revenueCenters.js +115 -0
  97. package/dist/esm/utils/rewards.js +1 -1
  98. package/dist/esm/utils/theme.js +7 -7
  99. package/package.json +2 -4
@@ -1,30 +1,5 @@
1
- import { serviceTypeNamesMap } from './constants';
2
- import { timezoneMap, makeOrderTimes, makeReadableDateStrFromIso, isoToDate, time24ToDate, formatDateStr, makeWeekday, fmtDate } from './datetimes';
3
- import { capitalize } from './helpers';
4
- export const displayPrice = (price) => {
5
- return parseFloat(price).toFixed(2);
6
- };
7
- export const makeDisplayPrice = (item) => {
8
- const sizeGroup = item.option_groups && item.option_groups.find(i => i.is_size);
9
- if (sizeGroup) {
10
- const prices = sizeGroup.option_items.map(i => `$${displayPrice(i.price)}`);
11
- return prices.join(' / ');
12
- }
13
- const { totalPrice } = makeOrderItem(item);
14
- return totalPrice > 0 ? `$${displayPrice(`${totalPrice}`)}` : null;
15
- };
16
- export const addCommas = (x, d) => {
17
- return x.toFixed(d).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
18
- };
19
- export const formatQuantity = (n) => {
20
- return addCommas(parseFloat(n), 0);
21
- };
22
- export const formatDollars = (str, space = '', decimals = 2) => {
23
- const floatPrice = parseFloat(str);
24
- return floatPrice < 0
25
- ? `($${addCommas(Math.abs(floatPrice), decimals)})`
26
- : `$${addCommas(floatPrice, decimals)}${space}`;
27
- };
1
+ import { convertStringToArray } from './helpers';
2
+ // TODO: refactor for nested mods
28
3
  export const getItemOptions = (item) => {
29
4
  if (!item.groups || !item.groups.length)
30
5
  return [];
@@ -33,271 +8,279 @@ export const getItemOptions = (item) => {
33
8
  .reduce((arr, i) => [...(arr !== null && arr !== void 0 ? arr : []), ...(i !== null && i !== void 0 ? i : [])], []);
34
9
  return options;
35
10
  };
11
+ // TODO: refactor for nested mods
36
12
  export const makeModifierNames = (item) => {
37
13
  var _a;
38
14
  return (_a = getItemOptions(item)) === null || _a === void 0 ? void 0 : _a.map(option => option.name).join(', ');
39
15
  };
40
- export const convertStringToArray = (str) => {
41
- return str
42
- ? str
43
- .split(',')
44
- .map(i => i.trim())
45
- .filter(i => i.length)
46
- : [];
47
- };
48
- export const makeOrderTypeFromParam = (param) => {
49
- switch (param) {
50
- case 'outpost':
51
- return ['OLO', 'PICKUP', true];
52
- case 'pickup':
53
- return ['OLO', 'PICKUP'];
54
- case 'delivery':
55
- return ['OLO', 'DELIVERY'];
56
- case 'catering':
57
- return ['CATERING', 'DELIVERY'];
58
- case 'merch':
59
- return ['MERCH', 'DELIVERY'];
60
- default:
61
- return null;
62
- }
63
- };
64
- export const makeServiceTypeName = (serviceType, isCatering, isOutpost, outpostName) => {
65
- let serviceTypeName = serviceTypeNamesMap[serviceType];
66
- if (isCatering) {
67
- serviceTypeName = `Catering ${serviceTypeName}`;
68
- }
69
- else if (isOutpost) {
70
- serviceTypeName = `${outpostName || 'Outpost'} ${serviceTypeName}`;
71
- }
72
- return serviceTypeName;
16
+ export const hasGroupsBelowMin = (option, groups) => {
17
+ if (!groups || !groups.length)
18
+ return false;
19
+ if (option && option.quantity === 0)
20
+ return false;
21
+ const childGroupsBelow = groups.filter(g => {
22
+ const optionsBelowMin = g.options.filter(o => hasGroupsBelowMin(o, o.groups));
23
+ return optionsBelowMin.length > 0;
24
+ });
25
+ if (childGroupsBelow.length > 0)
26
+ return true;
27
+ return groups.filter(g => ((g === null || g === void 0 ? void 0 : g.quantity) || 0) < g.min).length > 0;
28
+ };
29
+ export const makeModifierGroups = (groups) => {
30
+ if (!groups || !groups.length)
31
+ return [[], []];
32
+ const otherGroups = groups.filter(g => !g.isSize);
33
+ if (otherGroups.length)
34
+ return [groups, []];
35
+ const sizeGroup = groups.find(g => g.isSize);
36
+ if (!sizeGroup)
37
+ return [[], []];
38
+ const sizeGroupNested = sizeGroup.options.filter(o => o.groups && o.groups.length);
39
+ if (!sizeGroupNested || !sizeGroupNested.length)
40
+ return [[], []];
41
+ const selectedOption = sizeGroup.options.find(o => o.quantity > 0);
42
+ return selectedOption
43
+ ? [selectedOption.groups, [[sizeGroup.id, selectedOption.id]]]
44
+ : [[], []];
73
45
  };
74
- export const makeOrderTypeName = (orderType, serviceType) => {
75
- return orderType === 'OLO'
76
- ? serviceTypeNamesMap[serviceType]
77
- : capitalize(orderType);
46
+ export const makeUpsellItems = (itemIds, itemLookup) => {
47
+ return itemIds
48
+ .map(id => itemLookup[id])
49
+ .filter(i => i)
50
+ .map(i => makeOrderItem(i))
51
+ .filter(i => !i.isSoldOut)
52
+ .filter(i => {
53
+ const belowMin = i.groups.filter(g => !g.isSize && (g.quantity || 0) < g.min);
54
+ return belowMin.length === 0;
55
+ });
78
56
  };
79
- const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTime) => {
80
- var _a, _b;
81
- if (!firstTime && !orderTime)
82
- return null;
83
- let readableDate;
84
- if (firstTime) {
85
- const seconds = firstTime.utc ? isoToDate(firstTime.utc).getSeconds() : 0;
86
- const estimate = requestedAt !== 'asap' && seconds ? 'at about' : 'at';
87
- readableDate =
88
- requestedAt === 'asap' && firstTime.has_asap
89
- ? `in about ${waitTime} minutes`
90
- : makeReadableDateStrFromIso(firstTime.utc, tz, true)
91
- .replace('Today', 'today')
92
- .replace('Tomorrow', 'TOMORROW')
93
- .replace('@', estimate);
94
- }
95
- else {
96
- const orderTimes = makeOrderTimes([orderTime], tz);
97
- const { start_time, end_time } = orderTimes[0];
98
- const startTime = time24ToDate(start_time);
99
- const startTimeStr = (_a = formatDateStr(startTime.toISOString(), 'h:mma')) === null || _a === void 0 ? void 0 : _a.toLowerCase();
100
- const endTime = time24ToDate(end_time);
101
- const endTimeStr = (_b = formatDateStr(endTime.toISOString(), 'h:mma')) === null || _b === void 0 ? void 0 : _b.toLowerCase();
102
- const todayWeekday = makeWeekday();
103
- const weekday = orderTimes[0].weekday === todayWeekday
104
- ? ''
105
- : `${capitalize(orderTimes[0].weekday)}, ${fmtDate(orderTimes[0].date, 'MMMM d')} @ `;
106
- readableDate =
107
- startTimeStr === endTimeStr
108
- ? `${weekday}${startTimeStr}`
109
- : `${weekday}${startTimeStr} to ${endTimeStr}`;
110
- }
111
- const serviceTypeName = serviceTypeNamesMap[serviceType];
112
- const orderMsg = `The first available ${serviceTypeName === null || serviceTypeName === void 0 ? void 0 : serviceTypeName.toLowerCase()} time is ${readableDate}`;
113
- return orderMsg;
114
- };
115
- export const makeNotAcceptingOrdersMsg = (serviceType) => {
116
- const serviceTypeName = serviceTypeNamesMap[serviceType];
117
- if (!serviceTypeName) {
118
- return 'This location is not currently accepting orders.';
119
- }
120
- return `This location is not currently accepting ${serviceTypeName.toLowerCase()} orders. Please try switching to a different order type.`;
57
+ const nutritionKeys = [
58
+ 'calories',
59
+ 'cholesterol',
60
+ 'dietary_fiber',
61
+ 'protein',
62
+ 'saturated_fat',
63
+ 'sodium',
64
+ 'sugars',
65
+ 'total_carbs',
66
+ 'total_fat',
67
+ 'trans_fat'
68
+ ];
69
+ export const calcNutrition = (item) => {
70
+ const { nutritionalInfo, groups } = item;
71
+ let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
72
+ const options = groups.reduce((arr, g) => {
73
+ return [...arr, ...g.options.filter(o => o.quantity > 0)];
74
+ }, []);
75
+ options.forEach(option => {
76
+ const optionNutrition = calcNutrition(option);
77
+ nutritionKeys.forEach(key => {
78
+ const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
79
+ const optionVal = optionPer * option.quantity;
80
+ const itemVal = nutrition[key] || 0;
81
+ nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
82
+ });
83
+ });
84
+ return nutrition;
121
85
  };
122
- export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMessages) => {
123
- var _a;
124
- const settings = revenueCenter.settings || revenueCenter;
125
- const { first_times, order_times, wait_times } = settings;
126
- const tz = timezoneMap[(_a = revenueCenter === null || revenueCenter === void 0 ? void 0 : revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Central'];
127
- const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
128
- const firstTime = first_times ? first_times[st] : null;
129
- const orderTime = order_times ? order_times[st] : null;
130
- const waitTime = wait_times ? wait_times[st] : null;
131
- const tempClosed = revenueCenter.status === 'CLOSED_TEMPORARILY';
132
- const statusMsg = !tempClosed ? statusMessages[revenueCenter.status] : null;
133
- const orderMsg = !statusMsg && (firstTime || (orderTime && orderTime.length))
134
- ? makeOrderMsg(tz, serviceType, requestedAt, firstTime, orderTime ? orderTime[0] : null, waitTime !== null && waitTime !== void 0 ? waitTime : 0)
86
+ export const calcPrices = (item) => {
87
+ const groups = item.groups.map(g => {
88
+ let groupQuantity = 0;
89
+ const optionsWithPrices = g.options.map((o, index) => {
90
+ return Object.assign(Object.assign({}, calcPrices(o)), { index });
91
+ });
92
+ const sortedOptions = optionsWithPrices.sort((a, b) => {
93
+ const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
94
+ const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
95
+ return pricePerA - pricePerB;
96
+ });
97
+ const pricedOptions = sortedOptions.map(o => {
98
+ const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
99
+ const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
100
+ const includedRemaining = Math.max(g.included - groupQuantity, 0);
101
+ const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
102
+ const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals: o.totalCals || 0 });
103
+ groupQuantity += o.quantity || 0;
104
+ return option;
105
+ });
106
+ const options = pricedOptions.sort((a, b) => a.index - b.index);
107
+ return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
108
+ });
109
+ const optionsPrice = groups.reduce((t, g) => {
110
+ return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
111
+ }, 0.0);
112
+ const totalPrice = item.quantity * (item.price + optionsPrice);
113
+ const optionsPoints = groups.reduce((t, g) => {
114
+ return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
115
+ }, 0.0);
116
+ const totalPoints = item.points
117
+ ? item.quantity * (item.points + optionsPoints)
135
118
  : null;
136
- let message = orderMsg ||
137
- (statusMsg ? statusMsg.subtitle : makeNotAcceptingOrdersMsg(serviceType));
138
- let color = orderMsg ? 'success' : 'alert';
139
- if (tempClosed) {
140
- color = 'alert';
141
- message = message.charAt(0).toLowerCase() + message.substring(1);
142
- message = `This location is temporarily closed for technical or operational reasons. Please try back later today, otherwise ${message} if you want to place an order now.`;
143
- }
144
- return { message, color };
119
+ const optionsCals = groups.reduce((t, g) => {
120
+ return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
121
+ }, 0.0);
122
+ const totalCals = item.cals || item.cals === 0
123
+ ? item.quantity * (item.cals + optionsCals)
124
+ : null;
125
+ return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals });
126
+ };
127
+ export const checkSoldOut = (id, suspendUntil, soldOut = []) => {
128
+ if (soldOut.includes(id))
129
+ return true;
130
+ return suspendUntil ? suspendUntil > Date.now() / 1000 : false;
145
131
  };
146
132
  const makeOrderItemGroups = (optionGroups, isEdit, soldOut = []) => {
147
133
  if (!optionGroups)
148
134
  return [];
149
135
  const groups = optionGroups.map(g => {
150
136
  const options = g.option_items.map(o => {
151
- const isSoldOut = soldOut.includes(o.id);
137
+ const isSoldOut = checkSoldOut(o.id, o.suspend_until, soldOut);
152
138
  const quantity = isSoldOut
153
139
  ? 0
154
140
  : o.opt_is_default && !isEdit
155
141
  ? o.min_quantity || 1
156
142
  : 0;
157
143
  const cals = o.nutritional_info
158
- ? o.nutritional_info.calories
144
+ ? parseInt(o.nutritional_info.calories.toString())
159
145
  : null;
146
+ const groups = makeOrderItemGroups(o.option_groups, isEdit, soldOut);
160
147
  const option = {
161
- id: o.id,
162
- name: o.name,
163
- shortName: o.short_name,
148
+ allergens: convertStringToArray(o.allergens),
149
+ cals: cals,
164
150
  description: o.description,
151
+ groups: groups,
152
+ id: o.id,
165
153
  imageUrl: o.small_image_url,
166
- allergens: convertStringToArray(o.allergens),
167
- tags: convertStringToArray(o.tags),
154
+ increment: o.increment,
168
155
  ingredients: o.ingredients,
169
- nutritionalInfo: o.nutritional_info,
170
- cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
171
- price: parseFloat(o.price),
172
- quantity: quantity,
173
156
  isDefault: o.opt_is_default,
174
- increment: o.increment,
157
+ isSoldOut: isSoldOut,
175
158
  max: isSoldOut ? 0 : o.max_quantity,
176
159
  min: o.min_quantity,
177
- isSoldOut: isSoldOut,
178
- points: o.points || 0
160
+ name: o.name,
161
+ nutritionalInfo: o.nutritional_info,
162
+ points: o.points || 0,
163
+ price: parseFloat(o.price),
164
+ quantity: quantity,
165
+ shortDescription: o.short_description,
166
+ shortName: o.short_name,
167
+ slug: o.slug,
168
+ tags: convertStringToArray(o.tags),
169
+ totalCals: null,
170
+ totalPoints: null,
171
+ totalPrice: null
179
172
  };
180
173
  return option;
181
174
  });
182
175
  const group = {
183
- id: g.id,
184
- name: g.name,
185
176
  description: g.description,
177
+ id: g.id,
186
178
  imageUrl: g.small_image_url,
187
179
  included: g.included_options,
180
+ isPizza: !!g.is_pizza,
181
+ isSize: !!g.is_size,
188
182
  max: g.max_options,
189
183
  min: g.min_options,
190
- isSize: !!g.is_size,
184
+ name: g.name,
191
185
  options: options
192
186
  };
193
187
  return group;
194
188
  });
195
189
  return groups;
196
190
  };
197
- export const calcPrices = (item) => {
198
- var _a, _b, _c, _d, _e;
199
- const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map(g => {
200
- var _a;
201
- let groupQuantity = 0;
202
- const options = (_a = g === null || g === void 0 ? void 0 : g.options) === null || _a === void 0 ? void 0 : _a.map(o => {
203
- var _a, _b, _c, _d, _e;
204
- const includedRemaining = Math.max(((_a = g.included) !== null && _a !== void 0 ? _a : 0) - groupQuantity, 0);
205
- const priceQuantity = Math.max(((_b = o.quantity) !== null && _b !== void 0 ? _b : 0) - includedRemaining, 0);
206
- const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * ((_c = o.price) !== null && _c !== void 0 ? _c : 0), totalPoints: priceQuantity * ((_d = o.points) !== null && _d !== void 0 ? _d : 0), totalCals: o.cals ? ((_e = o.quantity) !== null && _e !== void 0 ? _e : 0) * o.cals : 0 });
207
- groupQuantity += o.quantity || 0;
208
- return option;
209
- });
210
- return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
211
- });
212
- const optionsPrice = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
213
- var _a, _b;
214
- return t + ((_b = (_a = g.options) === null || _a === void 0 ? void 0 : _a.reduce((ot, o) => ot + o.totalPrice, 0.0)) !== null && _b !== void 0 ? _b : 0);
215
- }, 0.0);
216
- const totalPrice = ((_b = item.quantity) !== null && _b !== void 0 ? _b : 0) * (((_c = item.price) !== null && _c !== void 0 ? _c : 0) + (optionsPrice !== null && optionsPrice !== void 0 ? optionsPrice : 0));
217
- const optionsPoints = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
218
- var _a, _b;
219
- return t + ((_b = (_a = g.options) === null || _a === void 0 ? void 0 : _a.reduce((ot, o) => ot + o.totalPoints, 0)) !== null && _b !== void 0 ? _b : 0);
220
- }, 0.0);
221
- const totalPoints = item.points
222
- ? ((_d = item.quantity) !== null && _d !== void 0 ? _d : 0) * (item.points + (optionsPoints !== null && optionsPoints !== void 0 ? optionsPoints : 0))
223
- : null;
224
- const optionsCals = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
225
- var _a, _b;
226
- return t + ((_b = (_a = g.options) === null || _a === void 0 ? void 0 : _a.reduce((ot, o) => ot + o.totalCals, 0)) !== null && _b !== void 0 ? _b : 0);
227
- }, 0.0);
228
- const totalCals = item.cals || item.cals === 0
229
- ? ((_e = item.quantity) !== null && _e !== void 0 ? _e : 0) * (item.cals + (optionsCals !== null && optionsCals !== void 0 ? optionsCals : 0))
230
- : null;
231
- return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals });
232
- };
233
191
  export const makeOrderItem = (item, isEdit, soldOut = [], simpleItem, hasPoints = false) => {
234
192
  const groups = makeOrderItemGroups(item.option_groups, isEdit, soldOut);
235
193
  const cals = item.nutritional_info ? item.nutritional_info.calories : null;
194
+ const isSoldOut = checkSoldOut(item.id, item.suspend_until, soldOut);
236
195
  const orderItem = {
237
- id: item.id,
238
- name: item.name,
239
- shortName: item.short_name,
240
- category: item.category_name,
196
+ allergens: convertStringToArray(item.allergens),
197
+ cals: cals,
198
+ cartGuestId: simpleItem ? simpleItem.cart_guest_id || null : null,
199
+ customerId: simpleItem ? simpleItem.customer_id || null : null,
241
200
  description: item.description,
242
- imageUrl: item.large_image_url,
243
- slug: item.slug,
244
- allergens: convertStringToArray(Array.from(item.allergens).join()),
245
- tags: convertStringToArray(Array.from(item.tags).join()),
246
- ingredients: item.ingredients,
247
- nutritionalInfo: item.nutritional_info,
248
- cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
249
201
  groups: groups,
250
- quantity: item.min_quantity || 1 * item.increment,
251
- price: parseFloat(item.price),
202
+ id: item.id,
203
+ imageUrl: item.large_image_url,
252
204
  increment: item.increment,
205
+ // index: null,
206
+ ingredients: item.ingredients,
207
+ isSoldOut: isSoldOut,
208
+ madeFor: simpleItem ? simpleItem.made_for || null : null,
253
209
  max: item.max_quantity,
254
210
  min: item.min_quantity,
211
+ name: item.name,
212
+ notes: simpleItem ? simpleItem.notes || null : null,
213
+ nutritionalInfo: item.nutritional_info,
255
214
  points: hasPoints ? item.points || null : null,
256
- upsellItems: item.upsell_items || [],
257
- similarItems: item.similar_items || []
215
+ price: parseFloat(item.price),
216
+ quantity: item.min_quantity || 1 * item.increment,
217
+ shortDescription: item.short_description,
218
+ shortName: item.short_name,
219
+ similarItems: item.similar_items,
220
+ slug: item.slug,
221
+ tags: convertStringToArray(item.tags),
222
+ totalCals: null,
223
+ totalPoints: null,
224
+ totalPrice: null,
225
+ upsellItems: item.upsell_items
258
226
  };
259
- if (simpleItem) {
260
- const { cart_guest_id, customer_id, made_for, notes } = simpleItem;
261
- if (cart_guest_id)
262
- orderItem.cart_guest_id = cart_guest_id;
263
- if (customer_id)
264
- orderItem.customer_id = customer_id;
265
- if (made_for)
266
- orderItem.madeFor = made_for;
267
- if (notes)
268
- orderItem.notes = notes;
269
- }
270
227
  const pricedItem = calcPrices(orderItem);
271
228
  return pricedItem;
272
229
  };
273
- export const rehydrateOrderItem = (menuItem, simpleCartItem) => {
274
- var _a;
275
- const orderItem = makeOrderItem(menuItem, true, [], simpleCartItem);
230
+ export const calcCartCounts = (cart) => {
231
+ return cart.reduce((obj, item) => {
232
+ const newCount = (obj[item.id.toString()] || 0) + item.quantity;
233
+ return Object.assign(Object.assign({}, obj), { [item.id]: newCount });
234
+ }, {});
235
+ };
236
+ const makeSimpleGroupsLookup = (groups) => {
237
+ if (!groups || !groups.length)
238
+ return null;
239
+ return groups.reduce((grpObj, g) => {
240
+ const options = g.options.reduce((optObj, o) => {
241
+ optObj[o.id] = {
242
+ quantity: o.quantity,
243
+ groupsLookup: makeSimpleGroupsLookup(o.groups)
244
+ };
245
+ return optObj;
246
+ }, {});
247
+ grpObj[g.id] = { options };
248
+ return grpObj;
249
+ }, {});
250
+ };
251
+ const rehydrateGroups = (groups, groupsLookup) => {
252
+ if (!groupsLookup)
253
+ return groups;
254
+ groups.forEach(group => {
255
+ const simpleGroup = groupsLookup[group.id];
256
+ if (simpleGroup) {
257
+ group.options.forEach(option => {
258
+ const simpleOption = simpleGroup.options[option.id];
259
+ if (simpleOption) {
260
+ option.quantity = simpleOption.quantity;
261
+ option.groups = rehydrateGroups(option.groups, simpleOption.groupsLookup);
262
+ }
263
+ });
264
+ }
265
+ });
266
+ return groups;
267
+ };
268
+ export const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = []) => {
269
+ const orderItem = makeOrderItem(menuItem, true, soldOut, simpleCartItem);
276
270
  orderItem.quantity = simpleCartItem.quantity || 1;
277
- if (simpleCartItem.groups && simpleCartItem.groups.length) {
278
- const groupsLookup = makeGroupsLookup(simpleCartItem);
279
- (_a = orderItem.groups) === null || _a === void 0 ? void 0 : _a.forEach(group => {
280
- var _a;
281
- const simpleGroup = group.id ? groupsLookup[group.id] : null;
282
- if (simpleGroup) {
283
- (_a = group.options) === null || _a === void 0 ? void 0 : _a.forEach(option => {
284
- if (option.id) {
285
- const simpleOption = simpleGroup.options[option.id];
286
- option.quantity = simpleOption ? simpleOption.quantity : 0;
287
- }
288
- });
289
- }
290
- });
271
+ const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
272
+ if (groupsLookup) {
273
+ orderItem.groups = rehydrateGroups(orderItem.groups, groupsLookup);
291
274
  }
292
275
  const pricedItem = calcPrices(orderItem);
293
276
  return pricedItem;
294
277
  };
295
- export const rehydrateCart = (menuItems, simpleCartItems) => {
278
+ export const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
296
279
  const orderItems = [];
297
280
  simpleCartItems.forEach(item => {
298
281
  const menuItem = menuItems.find(i => i.id === item.id);
299
282
  if (menuItem) {
300
- const orderItem = rehydrateOrderItem(menuItem, item);
283
+ const orderItem = rehydrateOrderItem(menuItem, item, soldOut);
301
284
  orderItems.push(orderItem);
302
285
  }
303
286
  });
@@ -305,6 +288,7 @@ export const rehydrateCart = (menuItems, simpleCartItems) => {
305
288
  const cartCounts = calcCartCounts(cart);
306
289
  return { cart, cartCounts };
307
290
  };
291
+ // TODO: maybe refactor
308
292
  const rehydrateDetails = (details) => {
309
293
  return {
310
294
  cart_id: details === null || details === void 0 ? void 0 : details.cart_id,
@@ -315,6 +299,7 @@ const rehydrateDetails = (details) => {
315
299
  tax_exempt_id: details === null || details === void 0 ? void 0 : details.tax_exempt_id
316
300
  };
317
301
  };
302
+ // TODO: maybe refactor
318
303
  const rehydrateCustomer = (customer) => {
319
304
  const newCustomer = {
320
305
  first_name: customer === null || customer === void 0 ? void 0 : customer.first_name,
@@ -328,6 +313,7 @@ const rehydrateCustomer = (customer) => {
328
313
  }
329
314
  return newCustomer;
330
315
  };
316
+ // TODO: maybe refactor
331
317
  const rehydrateAddress = (address) => {
332
318
  if (!address)
333
319
  return {};
@@ -338,15 +324,19 @@ const rehydrateAddress = (address) => {
338
324
  phone: address.phone
339
325
  };
340
326
  };
327
+ // TODO: maybe refactor
341
328
  const rehydrateSurcharges = (surcharges) => {
342
329
  return surcharges === null || surcharges === void 0 ? void 0 : surcharges.filter(i => i.is_optional).map(i => ({ id: i.surcharge_id }));
343
330
  };
331
+ // TODO: maybe refactor
344
332
  const rehydrateDiscounts = (discounts) => {
345
333
  return discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => !i.is_promo_code).map(i => ({ id: i.discount_id || i.id, ext_id: i.ext_id }));
346
334
  };
335
+ // TODO: maybe refactor
347
336
  const rehydratePromoCodes = (discounts) => {
348
337
  return discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => i.is_promo_code).map(i => i.name);
349
338
  };
339
+ // TODO: maybe refactor
350
340
  const rehydrateTenders = (tenders) => {
351
341
  // const giftCards = tenders.filter((i) => i.tender_type === 'GIFT_CARD')
352
342
  const other = tenders === null || tenders === void 0 ? void 0 : tenders.map(i => {
@@ -362,6 +352,7 @@ const rehydrateTenders = (tenders) => {
362
352
  });
363
353
  return other;
364
354
  };
355
+ // TODO: maybe refactor
365
356
  export const rehydrateCheckoutForm = (order) => {
366
357
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
367
358
  const form = {
@@ -378,7 +369,7 @@ export const rehydrateCheckoutForm = (order) => {
378
369
  };
379
370
  // group orders
380
371
  export const makeGuestLookup = (cartGuests) => {
381
- return cartGuests.reduce((obj, i) => (i.cart_guest_id ? Object.assign(Object.assign({}, obj), { [i.cart_guest_id]: i }) : Object.assign({}, obj)), {});
372
+ return cartGuests.reduce((obj, i) => i.cart_guest_id ? Object.assign(Object.assign({}, obj), { [i.cart_guest_id]: i }) : Object.assign({}, obj), {});
382
373
  };
383
374
  export const combineCarts = (cart, guestCart, cartOwner, cartGuests) => {
384
375
  const guestLookup = makeGuestLookup(cartGuests);
@@ -400,68 +391,38 @@ export const makeItemImageUrl = (images) => {
400
391
  .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.type]: i.url })), {});
401
392
  return (imageMap.SMALL_IMAGE || imageMap.LARGE_IMAGE || imageMap.APP_IMAGE || null);
402
393
  };
403
- export const makeItemSignature = (item) => {
404
- const optionIds = !item.groups
405
- ? []
406
- : item.groups
407
- .reduce((arr, group) => {
408
- var _a;
409
- const ids = (_a = group.options) === null || _a === void 0 ? void 0 : _a.map(o => o.id);
410
- return [...arr, ...(ids !== null && ids !== void 0 ? ids : [])];
411
- }, [])
412
- .sort((a, b) => a - b);
413
- return [item.id, ...optionIds].join('.');
414
- };
415
- export const makeCartItemSignature = (item) => {
416
- var _a;
417
- const optionIds = ((_a = item.groups) !== null && _a !== void 0 ? _a : [])
418
- .reduce((arr, group) => {
419
- var _a;
420
- const ids = (_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.filter(o => { var _a; return ((_a = o.quantity) !== null && _a !== void 0 ? _a : 0) > 0; }).reduce((optionsArr, o) => {
421
- const optionArr = new Array(o.quantity);
422
- optionArr.fill(o.id);
423
- return [...optionsArr, ...optionArr];
424
- }, []);
425
- return [...arr, ...(ids !== null && ids !== void 0 ? ids : [])];
426
- }, [])
427
- .sort((a, b) => a - b);
428
- const itemIds = [item.id, ...optionIds].join('.');
429
- const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
430
- return signature;
431
- };
432
394
  export const makeDisplayItemGroups = (optionGroups) => {
433
395
  if (!optionGroups || !optionGroups.length)
434
396
  return [];
435
- return optionGroups.map((g) => {
397
+ return optionGroups.map(g => {
436
398
  var _a;
437
399
  const options = (_a = g.options) === null || _a === void 0 ? void 0 : _a.map(o => makeDisplayItem(o));
438
400
  return Object.assign(Object.assign({}, g), { options });
439
401
  });
440
402
  };
441
403
  export const makeDisplayItem = (item, isOption = false) => {
442
- var _a, _b, _c;
404
+ var _a;
443
405
  const cals = item.nutritional_info ? (_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories : null;
444
406
  let displayItem = {
407
+ allergens: item.allergens,
408
+ cals: cals,
409
+ description: item.description,
410
+ groups: makeDisplayItemGroups(item.groups),
411
+ imageUrl: makeItemImageUrl(item.images),
445
412
  id: item.id,
446
413
  name: item.name,
414
+ nutritionalInfo: item.nutritional_info,
415
+ quantity: item.quantity,
416
+ price: item.price,
447
417
  shortDescription: item.short_description,
448
- description: item.description,
449
- imageUrl: makeItemImageUrl((_b = item.images) !== null && _b !== void 0 ? _b : []),
450
- allergens: item.allergens || [],
451
- tags: item.tags || [],
452
- nutritionalInfo: item.nutritional_info || null,
453
- cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
454
- groups: makeDisplayItemGroups((_c = item.groups) !== null && _c !== void 0 ? _c : []),
455
- quantity: item.quantity || 1,
456
- price: item.price !== undefined ? item.price : undefined,
457
- totalPrice: item.price_total !== undefined ? item.price_total : undefined
418
+ tags: item.tags,
419
+ totalPrice: item.price_total
458
420
  };
459
421
  if (!isOption) {
460
422
  const itemDetails = {
461
423
  madeFor: item.made_for,
462
424
  notes: item.notes,
463
- favoriteId: item.favorite_id,
464
- signature: makeItemSignature(item)
425
+ signature: makeItemSignature(displayItem)
465
426
  };
466
427
  displayItem = Object.assign(Object.assign({}, displayItem), itemDetails);
467
428
  }
@@ -491,36 +452,43 @@ export const makeFavoritesLookup = (favorites) => {
491
452
  .filter(i => i.cart)
492
453
  .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [makeItemSignature(i.cart)]: i.favorite_id })), {});
493
454
  };
494
- export const makeOrderAddress = (address) => {
495
- const { street, unit, city, state, postal_code } = address || {};
496
- return street
497
- ? `${street}${unit ? `, ${unit}` : ''}`
498
- : postal_code
499
- ? `${postal_code} ${city}, ${state}`
500
- : 'No address provided';
455
+ const makeCartItemOptionIds = (groups) => {
456
+ if (!groups || !groups.length)
457
+ return [];
458
+ return groups
459
+ .reduce((arr, group) => {
460
+ const ids = group.options
461
+ .filter(o => o.quantity > 0)
462
+ .reduce((optionsArr, o) => {
463
+ const optionArr = new Array(o.quantity);
464
+ optionArr.fill(o.id);
465
+ const nestedIds = makeCartItemOptionIds(o.groups);
466
+ return [...optionsArr, ...optionArr, ...nestedIds];
467
+ }, []);
468
+ return [...arr, ...ids];
469
+ }, [])
470
+ .sort((a, b) => a - b);
501
471
  };
502
- export const makeFullAddress = (address) => {
503
- const { street, city, state, postal_code } = address || {};
504
- return `${street ? `${street}, ` : ''}${city}, ${state} ${postal_code}`;
472
+ export const makeItemSignature = (item) => {
473
+ const optionIds = makeCartItemOptionIds(item.groups);
474
+ return [item.id, ...optionIds].join('.');
505
475
  };
506
- export const calcCartCounts = (cart) => {
507
- return cart.reduce((obj, item) => {
508
- var _a;
509
- const newCount = (obj[(_a = item.id) !== null && _a !== void 0 ? _a : 0] || 0) + item.quantity;
510
- return item.id ? Object.assign(Object.assign({}, obj), { [item.id]: newCount }) : Object.assign({}, obj);
511
- }, {});
476
+ export const makeCartItemSignature = (item) => {
477
+ const optionIds = makeCartItemOptionIds(item.groups);
478
+ const itemIds = [item.id, ...optionIds].join('.');
479
+ const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
480
+ return signature;
512
481
  };
513
482
  export const addItem = (cart, item) => {
514
- var _a;
515
483
  if (typeof item.index === 'undefined') {
516
484
  const itemSignature = makeCartItemSignature(item);
517
485
  const itemSignatures = cart.map(i => (Object.assign({ signature: makeCartItemSignature(i) }, i)));
518
486
  const match = itemSignatures.find(i => i.signature === itemSignature);
519
- if (match) {
487
+ if (match && match.index) {
520
488
  const quantity = item.quantity + match.quantity;
521
489
  const newItem = Object.assign(Object.assign({}, item), { quantity });
522
490
  const updated = calcPrices(newItem);
523
- cart[(_a = match.index) !== null && _a !== void 0 ? _a : 0] = Object.assign(Object.assign({}, updated), { index: match.index });
491
+ cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
524
492
  }
525
493
  else {
526
494
  cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
@@ -551,7 +519,7 @@ export const incrementItem = (cart, index) => {
551
519
  newQuantity =
552
520
  item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
553
521
  const newItem = calcPrices(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
554
- cart[realIndex !== null && realIndex !== void 0 ? realIndex : 0] = newItem;
522
+ cart[realIndex] = newItem;
555
523
  }
556
524
  }
557
525
  const cartCounts = calcCartCounts(cart);
@@ -574,18 +542,24 @@ export const decrementItem = (cart, index) => {
574
542
  const cartCounts = calcCartCounts(cart);
575
543
  return { cart, cartCounts };
576
544
  };
545
+ const makeSimpleGroups = (groups) => {
546
+ return groups.map(g => {
547
+ const options = g.options
548
+ .filter(o => o.quantity !== 0)
549
+ .map(o => ({
550
+ id: o.id,
551
+ quantity: o.quantity,
552
+ groups: makeSimpleGroups(o.groups || [])
553
+ }));
554
+ return { id: g.id, options: options };
555
+ });
556
+ };
577
557
  export const makeSimpleCart = (cart) => {
578
558
  const simpleCart = cart.map(i => {
579
- var _a;
580
- const groups = (_a = i.groups) === null || _a === void 0 ? void 0 : _a.map(g => {
581
- var _a;
582
- const options = (_a = g.options) === null || _a === void 0 ? void 0 : _a.filter(o => o.quantity !== 0).map(o => ({ id: o.id, quantity: o.quantity }));
583
- return { id: g.id, options: options };
584
- });
585
559
  return {
586
560
  id: i.id,
587
561
  quantity: i.quantity,
588
- groups: groups,
562
+ groups: makeSimpleGroups(i.groups),
589
563
  made_for: i.madeFor || '',
590
564
  notes: i.notes || ''
591
565
  };
@@ -593,6 +567,7 @@ export const makeSimpleCart = (cart) => {
593
567
  return simpleCart;
594
568
  };
595
569
  /* cart validation */
570
+ // TODO: refactor for nested modifiers
596
571
  // convert menu items to order items for validation but only include
597
572
  // items that are already in the cart to save wasted effort
598
573
  const makeItemLookup = (categories, itemIds) => {
@@ -615,6 +590,7 @@ const makeItemLookup = (categories, itemIds) => {
615
590
  });
616
591
  return itemLookup;
617
592
  };
593
+ // TODO: refactor for nested modifiers
618
594
  const makeGroupsLookup = (item) => {
619
595
  var _a;
620
596
  return (_a = item.groups) === null || _a === void 0 ? void 0 : _a.reduce((grpObj, i) => {
@@ -631,6 +607,7 @@ const makeGroupsLookup = (item) => {
631
607
  return grpObj;
632
608
  }, {});
633
609
  };
610
+ // TODO: refactor for nested modifiers
634
611
  export const validateCart = (cart, categories, soldOut) => {
635
612
  const itemIds = cart.map(item => item.id);
636
613
  const itemLookup = makeItemLookup(categories, itemIds);
@@ -653,7 +630,8 @@ export const validateCart = (cart, categories, soldOut) => {
653
630
  const newRequiredGroups = Object.values(newGroups).filter((i) => i.min > 0);
654
631
  const oldGroupIds = (_a = oldItem.groups) === null || _a === void 0 ? void 0 : _a.map(i => i.id);
655
632
  missingGroups = newRequiredGroups.filter((i) => !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id)));
656
- const newEmptyGroups = Object.values(newGroups).filter((i) => {
633
+ const newEmptyGroups = Object.values(newGroups)
634
+ .filter((i) => {
657
635
  return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
658
636
  })
659
637
  .map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
@@ -806,44 +784,3 @@ export const checkAmountRemaining = (total, tenders) => {
806
784
  remaining = Math.round((remaining + Number.EPSILON) * 100) / 100;
807
785
  return Object.is(remaining, -0) ? 0.0 : remaining;
808
786
  };
809
- export const makeUpsellItems = (itemIds, itemLookup) => {
810
- return itemIds
811
- .map((id) => itemLookup[id])
812
- .filter((i) => i)
813
- .map((i) => makeOrderItem(i))
814
- .filter((i) => {
815
- var _a;
816
- const belowMin = (_a = i.groups) === null || _a === void 0 ? void 0 : _a.filter((g) => { var _a; return !g.isSize && g.quantity < ((_a = g.min) !== null && _a !== void 0 ? _a : 0); });
817
- return (belowMin === null || belowMin === void 0 ? void 0 : belowMin.length) === 0;
818
- });
819
- };
820
- const nutritionKeys = [
821
- 'calories',
822
- 'cholesterol',
823
- 'dietary_fiber',
824
- 'protein',
825
- 'saturated_fat',
826
- 'sodium',
827
- 'sugars',
828
- 'total_carbs',
829
- 'total_fat',
830
- 'trans_fat',
831
- ];
832
- export const calcNutrition = (item) => {
833
- const { nutritionalInfo, groups } = item;
834
- let nutrition = nutritionalInfo ? Object.assign({}, nutritionalInfo) : {};
835
- const options = groups.reduce((arr, g) => {
836
- var _a, _b;
837
- return [...arr, ...((_b = (_a = g.options) === null || _a === void 0 ? void 0 : _a.filter((o) => { var _a; return ((_a = o.quantity) !== null && _a !== void 0 ? _a : 0) > 0; })) !== null && _b !== void 0 ? _b : [])];
838
- }, []);
839
- options.forEach((o) => {
840
- nutritionKeys === null || nutritionKeys === void 0 ? void 0 : nutritionKeys.forEach((key) => {
841
- const optionVal = o.nutritionalInfo
842
- ? parseInt(o.nutritionalInfo[key]) || 0
843
- : 0;
844
- const itemVal = parseInt(nutrition[key]) || 0;
845
- nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
846
- });
847
- });
848
- return nutrition;
849
- };