@open-tender/utils 0.0.69 → 0.1.1
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.
- package/dist/cjs/hooks/menu/useBuilder.d.ts +8 -7
- package/dist/cjs/hooks/menu/useBuilder.js +92 -86
- package/dist/cjs/hooks/menu/useOrderItem.d.ts +33 -43
- package/dist/cjs/hooks/menu/useOrderItem.js +21 -22
- package/dist/cjs/hooks/useAddressForm.js +1 -1
- package/dist/cjs/hooks/useAllergenForm.js +1 -1
- package/dist/cjs/hooks/useCartGuestForm.js +2 -2
- package/dist/cjs/hooks/useCheckoutLoginForm.js +1 -1
- package/dist/cjs/hooks/useCreditCard.js +2 -2
- package/dist/cjs/hooks/useCreditCardForm.js +3 -3
- package/dist/cjs/hooks/useCurbside.js +1 -1
- package/dist/cjs/hooks/useDineInForm.js +1 -1
- package/dist/cjs/hooks/useDonationForm.js +5 -5
- package/dist/cjs/hooks/useGiftCardAssignForm.js +1 -1
- package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +1 -1
- package/dist/cjs/hooks/useGiftCardForm.js +6 -4
- package/dist/cjs/hooks/useGiftCardsForm.js +22 -9
- package/dist/cjs/hooks/useGuestForm.js +1 -1
- package/dist/cjs/hooks/useImage.d.ts +1 -1
- package/dist/cjs/hooks/useImage.js +2 -1
- package/dist/cjs/hooks/useLevelUpForm.js +1 -1
- package/dist/cjs/hooks/useOrderFulfillmentForm.js +3 -3
- package/dist/cjs/hooks/useOrderRating.js +1 -1
- package/dist/cjs/hooks/useOrderRatingForm.js +1 -1
- package/dist/cjs/hooks/useProfileForm.js +3 -3
- package/dist/cjs/hooks/useResetPasswordForm.js +2 -2
- package/dist/cjs/hooks/useSendResetForm.js +4 -2
- package/dist/cjs/hooks/useSignUpForm.js +14 -10
- package/dist/cjs/hooks/useSignUpGuestForm.js +3 -3
- package/dist/cjs/hooks/useSigninForm.js +1 -1
- package/dist/cjs/utils/cart.d.ts +97 -453
- package/dist/cjs/utils/cart.js +297 -376
- package/dist/cjs/utils/customer.js +1 -1
- package/dist/cjs/utils/errors.js +4 -2
- package/dist/cjs/utils/favorites.d.ts +3 -3
- package/dist/cjs/utils/favorites.js +11 -4
- package/dist/cjs/utils/helpers.d.ts +8 -3
- package/dist/cjs/utils/helpers.js +31 -3
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +1 -0
- package/dist/cjs/utils/maps.js +1 -1
- package/dist/cjs/utils/orders.d.ts +3 -3
- package/dist/cjs/utils/refunds.d.ts +4 -4
- package/dist/cjs/utils/revenueCenters.d.ts +11 -0
- package/dist/cjs/utils/revenueCenters.js +125 -0
- package/dist/cjs/utils/rewards.js +5 -5
- package/dist/cjs/utils/theme.js +7 -7
- package/dist/esm/hooks/menu/useBuilder.d.ts +8 -7
- package/dist/esm/hooks/menu/useBuilder.js +92 -86
- package/dist/esm/hooks/menu/useOrderItem.d.ts +33 -43
- package/dist/esm/hooks/menu/useOrderItem.js +22 -23
- package/dist/esm/hooks/useAddressForm.js +1 -1
- package/dist/esm/hooks/useAllergenForm.js +1 -1
- package/dist/esm/hooks/useCartGuestForm.js +2 -2
- package/dist/esm/hooks/useCheckoutLoginForm.js +1 -1
- package/dist/esm/hooks/useCreditCard.js +3 -3
- package/dist/esm/hooks/useCreditCardForm.js +3 -3
- package/dist/esm/hooks/useCurbside.js +1 -1
- package/dist/esm/hooks/useDineInForm.js +1 -1
- package/dist/esm/hooks/useDonationForm.js +5 -5
- package/dist/esm/hooks/useGiftCardAssignForm.js +1 -1
- package/dist/esm/hooks/useGiftCardAssignOtherForm.js +1 -1
- package/dist/esm/hooks/useGiftCardForm.js +6 -4
- package/dist/esm/hooks/useGiftCardsForm.js +22 -9
- package/dist/esm/hooks/useGuestForm.js +1 -1
- package/dist/esm/hooks/useImage.d.ts +1 -1
- package/dist/esm/hooks/useImage.js +2 -1
- package/dist/esm/hooks/useLevelUpForm.js +1 -1
- package/dist/esm/hooks/useOrderFulfillmentForm.js +3 -3
- package/dist/esm/hooks/useOrderRating.js +1 -1
- package/dist/esm/hooks/useOrderRatingForm.js +1 -1
- package/dist/esm/hooks/useProfileForm.js +3 -3
- package/dist/esm/hooks/useResetPasswordForm.js +2 -2
- package/dist/esm/hooks/useSendResetForm.js +4 -2
- package/dist/esm/hooks/useSignUpForm.js +14 -10
- package/dist/esm/hooks/useSignUpGuestForm.js +3 -3
- package/dist/esm/hooks/useSigninForm.js +1 -1
- package/dist/esm/utils/cart.d.ts +97 -453
- package/dist/esm/utils/cart.js +285 -354
- package/dist/esm/utils/customer.js +1 -1
- package/dist/esm/utils/errors.js +4 -2
- package/dist/esm/utils/favorites.d.ts +3 -3
- package/dist/esm/utils/favorites.js +11 -4
- package/dist/esm/utils/helpers.d.ts +8 -3
- package/dist/esm/utils/helpers.js +25 -2
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/maps.js +1 -1
- package/dist/esm/utils/orders.d.ts +3 -3
- package/dist/esm/utils/refunds.d.ts +4 -4
- package/dist/esm/utils/revenueCenters.d.ts +11 -0
- package/dist/esm/utils/revenueCenters.js +115 -0
- package/dist/esm/utils/rewards.js +1 -1
- package/dist/esm/utils/theme.js +7 -7
- package/package.json +2 -4
package/dist/cjs/utils/cart.js
CHANGED
|
@@ -1,38 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
const datetimes_1 = require("./datetimes");
|
|
3
|
+
exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutrition = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNames = exports.getItemOptions = void 0;
|
|
6
4
|
const helpers_1 = require("./helpers");
|
|
7
|
-
|
|
8
|
-
return parseFloat(price).toFixed(2);
|
|
9
|
-
};
|
|
10
|
-
exports.displayPrice = displayPrice;
|
|
11
|
-
const makeDisplayPrice = (item) => {
|
|
12
|
-
const sizeGroup = item.option_groups && item.option_groups.find(i => i.is_size);
|
|
13
|
-
if (sizeGroup) {
|
|
14
|
-
const prices = sizeGroup.option_items.map(i => `$${(0, exports.displayPrice)(i.price)}`);
|
|
15
|
-
return prices.join(' / ');
|
|
16
|
-
}
|
|
17
|
-
const { totalPrice } = (0, exports.makeOrderItem)(item);
|
|
18
|
-
return totalPrice > 0 ? `$${(0, exports.displayPrice)(`${totalPrice}`)}` : null;
|
|
19
|
-
};
|
|
20
|
-
exports.makeDisplayPrice = makeDisplayPrice;
|
|
21
|
-
const addCommas = (x, d) => {
|
|
22
|
-
return x.toFixed(d).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
23
|
-
};
|
|
24
|
-
exports.addCommas = addCommas;
|
|
25
|
-
const formatQuantity = (n) => {
|
|
26
|
-
return (0, exports.addCommas)(parseFloat(n), 0);
|
|
27
|
-
};
|
|
28
|
-
exports.formatQuantity = formatQuantity;
|
|
29
|
-
const formatDollars = (str, space = '', decimals = 2) => {
|
|
30
|
-
const floatPrice = parseFloat(str);
|
|
31
|
-
return floatPrice < 0
|
|
32
|
-
? `($${(0, exports.addCommas)(Math.abs(floatPrice), decimals)})`
|
|
33
|
-
: `$${(0, exports.addCommas)(floatPrice, decimals)}${space}`;
|
|
34
|
-
};
|
|
35
|
-
exports.formatDollars = formatDollars;
|
|
5
|
+
// TODO: refactor for nested mods
|
|
36
6
|
const getItemOptions = (item) => {
|
|
37
7
|
if (!item.groups || !item.groups.length)
|
|
38
8
|
return [];
|
|
@@ -42,281 +12,289 @@ const getItemOptions = (item) => {
|
|
|
42
12
|
return options;
|
|
43
13
|
};
|
|
44
14
|
exports.getItemOptions = getItemOptions;
|
|
15
|
+
// TODO: refactor for nested mods
|
|
45
16
|
const makeModifierNames = (item) => {
|
|
46
17
|
var _a;
|
|
47
18
|
return (_a = (0, exports.getItemOptions)(item)) === null || _a === void 0 ? void 0 : _a.map(option => option.name).join(', ');
|
|
48
19
|
};
|
|
49
20
|
exports.makeModifierNames = makeModifierNames;
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
21
|
+
const hasGroupsBelowMin = (option, groups) => {
|
|
22
|
+
if (!groups || !groups.length)
|
|
23
|
+
return false;
|
|
24
|
+
if (option && option.quantity === 0)
|
|
25
|
+
return false;
|
|
26
|
+
const childGroupsBelow = groups.filter(g => {
|
|
27
|
+
const optionsBelowMin = g.options.filter(o => (0, exports.hasGroupsBelowMin)(o, o.groups));
|
|
28
|
+
return optionsBelowMin.length > 0;
|
|
29
|
+
});
|
|
30
|
+
if (childGroupsBelow.length > 0)
|
|
31
|
+
return true;
|
|
32
|
+
return groups.filter(g => ((g === null || g === void 0 ? void 0 : g.quantity) || 0) < g.min).length > 0;
|
|
33
|
+
};
|
|
34
|
+
exports.hasGroupsBelowMin = hasGroupsBelowMin;
|
|
35
|
+
const makeModifierGroups = (groups) => {
|
|
36
|
+
if (!groups || !groups.length)
|
|
37
|
+
return [[], []];
|
|
38
|
+
const otherGroups = groups.filter(g => !g.isSize);
|
|
39
|
+
if (otherGroups.length)
|
|
40
|
+
return [groups, []];
|
|
41
|
+
const sizeGroup = groups.find(g => g.isSize);
|
|
42
|
+
if (!sizeGroup)
|
|
43
|
+
return [[], []];
|
|
44
|
+
const sizeGroupNested = sizeGroup.options.filter(o => o.groups && o.groups.length);
|
|
45
|
+
if (!sizeGroupNested || !sizeGroupNested.length)
|
|
46
|
+
return [[], []];
|
|
47
|
+
const selectedOption = sizeGroup.options.find(o => o.quantity > 0);
|
|
48
|
+
return selectedOption
|
|
49
|
+
? [selectedOption.groups, [[sizeGroup.id, selectedOption.id]]]
|
|
50
|
+
: [[], []];
|
|
51
|
+
};
|
|
52
|
+
exports.makeModifierGroups = makeModifierGroups;
|
|
53
|
+
const makeUpsellItems = (itemIds, itemLookup) => {
|
|
54
|
+
return itemIds
|
|
55
|
+
.map(id => itemLookup[id])
|
|
56
|
+
.filter(i => i)
|
|
57
|
+
.map(i => (0, exports.makeOrderItem)(i))
|
|
58
|
+
.filter(i => !i.isSoldOut)
|
|
59
|
+
.filter(i => {
|
|
60
|
+
const belowMin = i.groups.filter(g => !g.isSize && (g.quantity || 0) < g.min);
|
|
61
|
+
return belowMin.length === 0;
|
|
62
|
+
});
|
|
74
63
|
};
|
|
75
|
-
exports.
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
: (0, datetimes_1.makeReadableDateStrFromIso)(firstTime.utc, tz, true)
|
|
105
|
-
.replace('Today', 'today')
|
|
106
|
-
.replace('Tomorrow', 'TOMORROW')
|
|
107
|
-
.replace('@', estimate);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
const orderTimes = (0, datetimes_1.makeOrderTimes)([orderTime], tz);
|
|
111
|
-
const { start_time, end_time } = orderTimes[0];
|
|
112
|
-
const startTime = (0, datetimes_1.time24ToDate)(start_time);
|
|
113
|
-
const startTimeStr = (_a = (0, datetimes_1.formatDateStr)(startTime.toISOString(), 'h:mma')) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
114
|
-
const endTime = (0, datetimes_1.time24ToDate)(end_time);
|
|
115
|
-
const endTimeStr = (_b = (0, datetimes_1.formatDateStr)(endTime.toISOString(), 'h:mma')) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
|
116
|
-
const todayWeekday = (0, datetimes_1.makeWeekday)();
|
|
117
|
-
const weekday = orderTimes[0].weekday === todayWeekday
|
|
118
|
-
? ''
|
|
119
|
-
: `${(0, helpers_1.capitalize)(orderTimes[0].weekday)}, ${(0, datetimes_1.fmtDate)(orderTimes[0].date, 'MMMM d')} @ `;
|
|
120
|
-
readableDate =
|
|
121
|
-
startTimeStr === endTimeStr
|
|
122
|
-
? `${weekday}${startTimeStr}`
|
|
123
|
-
: `${weekday}${startTimeStr} to ${endTimeStr}`;
|
|
124
|
-
}
|
|
125
|
-
const serviceTypeName = constants_1.serviceTypeNamesMap[serviceType];
|
|
126
|
-
const orderMsg = `The first available ${serviceTypeName === null || serviceTypeName === void 0 ? void 0 : serviceTypeName.toLowerCase()} time is ${readableDate}`;
|
|
127
|
-
return orderMsg;
|
|
128
|
-
};
|
|
129
|
-
const makeNotAcceptingOrdersMsg = (serviceType) => {
|
|
130
|
-
const serviceTypeName = constants_1.serviceTypeNamesMap[serviceType];
|
|
131
|
-
if (!serviceTypeName) {
|
|
132
|
-
return 'This location is not currently accepting orders.';
|
|
133
|
-
}
|
|
134
|
-
return `This location is not currently accepting ${serviceTypeName.toLowerCase()} orders. Please try switching to a different order type.`;
|
|
64
|
+
exports.makeUpsellItems = makeUpsellItems;
|
|
65
|
+
const nutritionKeys = [
|
|
66
|
+
'calories',
|
|
67
|
+
'cholesterol',
|
|
68
|
+
'dietary_fiber',
|
|
69
|
+
'protein',
|
|
70
|
+
'saturated_fat',
|
|
71
|
+
'sodium',
|
|
72
|
+
'sugars',
|
|
73
|
+
'total_carbs',
|
|
74
|
+
'total_fat',
|
|
75
|
+
'trans_fat'
|
|
76
|
+
];
|
|
77
|
+
const calcNutrition = (item) => {
|
|
78
|
+
const { nutritionalInfo, groups } = item;
|
|
79
|
+
let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
|
|
80
|
+
const options = groups.reduce((arr, g) => {
|
|
81
|
+
return [...arr, ...g.options.filter(o => o.quantity > 0)];
|
|
82
|
+
}, []);
|
|
83
|
+
options.forEach(option => {
|
|
84
|
+
const optionNutrition = (0, exports.calcNutrition)(option);
|
|
85
|
+
nutritionKeys.forEach(key => {
|
|
86
|
+
const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
|
|
87
|
+
const optionVal = optionPer * option.quantity;
|
|
88
|
+
const itemVal = nutrition[key] || 0;
|
|
89
|
+
nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return nutrition;
|
|
135
93
|
};
|
|
136
|
-
exports.
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
94
|
+
exports.calcNutrition = calcNutrition;
|
|
95
|
+
const calcPrices = (item) => {
|
|
96
|
+
const groups = item.groups.map(g => {
|
|
97
|
+
let groupQuantity = 0;
|
|
98
|
+
const optionsWithPrices = g.options.map((o, index) => {
|
|
99
|
+
return Object.assign(Object.assign({}, (0, exports.calcPrices)(o)), { index });
|
|
100
|
+
});
|
|
101
|
+
const sortedOptions = optionsWithPrices.sort((a, b) => {
|
|
102
|
+
const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
|
|
103
|
+
const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
|
|
104
|
+
return pricePerA - pricePerB;
|
|
105
|
+
});
|
|
106
|
+
const pricedOptions = sortedOptions.map(o => {
|
|
107
|
+
const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
|
|
108
|
+
const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
|
|
109
|
+
const includedRemaining = Math.max(g.included - groupQuantity, 0);
|
|
110
|
+
const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
|
|
111
|
+
const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals: o.totalCals || 0 });
|
|
112
|
+
groupQuantity += o.quantity || 0;
|
|
113
|
+
return option;
|
|
114
|
+
});
|
|
115
|
+
const options = pricedOptions.sort((a, b) => a.index - b.index);
|
|
116
|
+
return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
|
|
117
|
+
});
|
|
118
|
+
const optionsPrice = groups.reduce((t, g) => {
|
|
119
|
+
return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
|
|
120
|
+
}, 0.0);
|
|
121
|
+
const totalPrice = item.quantity * (item.price + optionsPrice);
|
|
122
|
+
const optionsPoints = groups.reduce((t, g) => {
|
|
123
|
+
return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
|
|
124
|
+
}, 0.0);
|
|
125
|
+
const totalPoints = item.points
|
|
126
|
+
? item.quantity * (item.points + optionsPoints)
|
|
150
127
|
: null;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
128
|
+
const optionsCals = groups.reduce((t, g) => {
|
|
129
|
+
return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
|
|
130
|
+
}, 0.0);
|
|
131
|
+
const totalCals = item.cals || item.cals === 0
|
|
132
|
+
? item.quantity * (item.cals + optionsCals)
|
|
133
|
+
: null;
|
|
134
|
+
return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals });
|
|
135
|
+
};
|
|
136
|
+
exports.calcPrices = calcPrices;
|
|
137
|
+
const checkSoldOut = (id, suspendUntil, soldOut = []) => {
|
|
138
|
+
if (soldOut.includes(id))
|
|
139
|
+
return true;
|
|
140
|
+
return suspendUntil ? suspendUntil > Date.now() / 1000 : false;
|
|
160
141
|
};
|
|
161
|
-
exports.
|
|
142
|
+
exports.checkSoldOut = checkSoldOut;
|
|
162
143
|
const makeOrderItemGroups = (optionGroups, isEdit, soldOut = []) => {
|
|
163
144
|
if (!optionGroups)
|
|
164
145
|
return [];
|
|
165
146
|
const groups = optionGroups.map(g => {
|
|
166
147
|
const options = g.option_items.map(o => {
|
|
167
|
-
const isSoldOut =
|
|
148
|
+
const isSoldOut = (0, exports.checkSoldOut)(o.id, o.suspend_until, soldOut);
|
|
168
149
|
const quantity = isSoldOut
|
|
169
150
|
? 0
|
|
170
151
|
: o.opt_is_default && !isEdit
|
|
171
152
|
? o.min_quantity || 1
|
|
172
153
|
: 0;
|
|
173
154
|
const cals = o.nutritional_info
|
|
174
|
-
? o.nutritional_info.calories
|
|
155
|
+
? parseInt(o.nutritional_info.calories.toString())
|
|
175
156
|
: null;
|
|
157
|
+
const groups = makeOrderItemGroups(o.option_groups, isEdit, soldOut);
|
|
176
158
|
const option = {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
shortName: o.short_name,
|
|
159
|
+
allergens: (0, helpers_1.convertStringToArray)(o.allergens),
|
|
160
|
+
cals: cals,
|
|
180
161
|
description: o.description,
|
|
162
|
+
groups: groups,
|
|
163
|
+
id: o.id,
|
|
181
164
|
imageUrl: o.small_image_url,
|
|
182
|
-
|
|
183
|
-
tags: (0, exports.convertStringToArray)(o.tags),
|
|
165
|
+
increment: o.increment,
|
|
184
166
|
ingredients: o.ingredients,
|
|
185
|
-
nutritionalInfo: o.nutritional_info,
|
|
186
|
-
cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
|
|
187
|
-
price: parseFloat(o.price),
|
|
188
|
-
quantity: quantity,
|
|
189
167
|
isDefault: o.opt_is_default,
|
|
190
|
-
|
|
168
|
+
isSoldOut: isSoldOut,
|
|
191
169
|
max: isSoldOut ? 0 : o.max_quantity,
|
|
192
170
|
min: o.min_quantity,
|
|
193
|
-
|
|
194
|
-
|
|
171
|
+
name: o.name,
|
|
172
|
+
nutritionalInfo: o.nutritional_info,
|
|
173
|
+
points: o.points || 0,
|
|
174
|
+
price: parseFloat(o.price),
|
|
175
|
+
quantity: quantity,
|
|
176
|
+
shortDescription: o.short_description,
|
|
177
|
+
shortName: o.short_name,
|
|
178
|
+
slug: o.slug,
|
|
179
|
+
tags: (0, helpers_1.convertStringToArray)(o.tags),
|
|
180
|
+
totalCals: null,
|
|
181
|
+
totalPoints: null,
|
|
182
|
+
totalPrice: null
|
|
195
183
|
};
|
|
196
184
|
return option;
|
|
197
185
|
});
|
|
198
186
|
const group = {
|
|
199
|
-
id: g.id,
|
|
200
|
-
name: g.name,
|
|
201
187
|
description: g.description,
|
|
188
|
+
id: g.id,
|
|
202
189
|
imageUrl: g.small_image_url,
|
|
203
190
|
included: g.included_options,
|
|
191
|
+
isPizza: !!g.is_pizza,
|
|
192
|
+
isSize: !!g.is_size,
|
|
204
193
|
max: g.max_options,
|
|
205
194
|
min: g.min_options,
|
|
206
|
-
|
|
195
|
+
name: g.name,
|
|
207
196
|
options: options
|
|
208
197
|
};
|
|
209
198
|
return group;
|
|
210
199
|
});
|
|
211
200
|
return groups;
|
|
212
201
|
};
|
|
213
|
-
const calcPrices = (item) => {
|
|
214
|
-
var _a, _b, _c, _d, _e;
|
|
215
|
-
const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map(g => {
|
|
216
|
-
var _a;
|
|
217
|
-
let groupQuantity = 0;
|
|
218
|
-
const options = (_a = g === null || g === void 0 ? void 0 : g.options) === null || _a === void 0 ? void 0 : _a.map(o => {
|
|
219
|
-
var _a, _b, _c, _d, _e;
|
|
220
|
-
const includedRemaining = Math.max(((_a = g.included) !== null && _a !== void 0 ? _a : 0) - groupQuantity, 0);
|
|
221
|
-
const priceQuantity = Math.max(((_b = o.quantity) !== null && _b !== void 0 ? _b : 0) - includedRemaining, 0);
|
|
222
|
-
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 });
|
|
223
|
-
groupQuantity += o.quantity || 0;
|
|
224
|
-
return option;
|
|
225
|
-
});
|
|
226
|
-
return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
|
|
227
|
-
});
|
|
228
|
-
const optionsPrice = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
|
|
229
|
-
var _a, _b;
|
|
230
|
-
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);
|
|
231
|
-
}, 0.0);
|
|
232
|
-
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));
|
|
233
|
-
const optionsPoints = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
|
|
234
|
-
var _a, _b;
|
|
235
|
-
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);
|
|
236
|
-
}, 0.0);
|
|
237
|
-
const totalPoints = item.points
|
|
238
|
-
? ((_d = item.quantity) !== null && _d !== void 0 ? _d : 0) * (item.points + (optionsPoints !== null && optionsPoints !== void 0 ? optionsPoints : 0))
|
|
239
|
-
: null;
|
|
240
|
-
const optionsCals = groups === null || groups === void 0 ? void 0 : groups.reduce((t, g) => {
|
|
241
|
-
var _a, _b;
|
|
242
|
-
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);
|
|
243
|
-
}, 0.0);
|
|
244
|
-
const totalCals = item.cals || item.cals === 0
|
|
245
|
-
? ((_e = item.quantity) !== null && _e !== void 0 ? _e : 0) * (item.cals + (optionsCals !== null && optionsCals !== void 0 ? optionsCals : 0))
|
|
246
|
-
: null;
|
|
247
|
-
return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals });
|
|
248
|
-
};
|
|
249
|
-
exports.calcPrices = calcPrices;
|
|
250
202
|
const makeOrderItem = (item, isEdit, soldOut = [], simpleItem, hasPoints = false) => {
|
|
251
203
|
const groups = makeOrderItemGroups(item.option_groups, isEdit, soldOut);
|
|
252
204
|
const cals = item.nutritional_info ? item.nutritional_info.calories : null;
|
|
205
|
+
const isSoldOut = (0, exports.checkSoldOut)(item.id, item.suspend_until, soldOut);
|
|
253
206
|
const orderItem = {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
207
|
+
allergens: (0, helpers_1.convertStringToArray)(item.allergens),
|
|
208
|
+
cals: cals,
|
|
209
|
+
cartGuestId: simpleItem ? simpleItem.cart_guest_id || null : null,
|
|
210
|
+
customerId: simpleItem ? simpleItem.customer_id || null : null,
|
|
258
211
|
description: item.description,
|
|
259
|
-
imageUrl: item.large_image_url,
|
|
260
|
-
slug: item.slug,
|
|
261
|
-
allergens: (0, exports.convertStringToArray)(Array.from(item.allergens).join()),
|
|
262
|
-
tags: (0, exports.convertStringToArray)(Array.from(item.tags).join()),
|
|
263
|
-
ingredients: item.ingredients,
|
|
264
|
-
nutritionalInfo: item.nutritional_info,
|
|
265
|
-
cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
|
|
266
212
|
groups: groups,
|
|
267
|
-
|
|
268
|
-
|
|
213
|
+
id: item.id,
|
|
214
|
+
imageUrl: item.large_image_url,
|
|
269
215
|
increment: item.increment,
|
|
216
|
+
// index: null,
|
|
217
|
+
ingredients: item.ingredients,
|
|
218
|
+
isSoldOut: isSoldOut,
|
|
219
|
+
madeFor: simpleItem ? simpleItem.made_for || null : null,
|
|
270
220
|
max: item.max_quantity,
|
|
271
221
|
min: item.min_quantity,
|
|
222
|
+
name: item.name,
|
|
223
|
+
notes: simpleItem ? simpleItem.notes || null : null,
|
|
224
|
+
nutritionalInfo: item.nutritional_info,
|
|
272
225
|
points: hasPoints ? item.points || null : null,
|
|
273
|
-
|
|
274
|
-
|
|
226
|
+
price: parseFloat(item.price),
|
|
227
|
+
quantity: item.min_quantity || 1 * item.increment,
|
|
228
|
+
shortDescription: item.short_description,
|
|
229
|
+
shortName: item.short_name,
|
|
230
|
+
similarItems: item.similar_items,
|
|
231
|
+
slug: item.slug,
|
|
232
|
+
tags: (0, helpers_1.convertStringToArray)(item.tags),
|
|
233
|
+
totalCals: null,
|
|
234
|
+
totalPoints: null,
|
|
235
|
+
totalPrice: null,
|
|
236
|
+
upsellItems: item.upsell_items
|
|
275
237
|
};
|
|
276
|
-
if (simpleItem) {
|
|
277
|
-
const { cart_guest_id, customer_id, made_for, notes } = simpleItem;
|
|
278
|
-
if (cart_guest_id)
|
|
279
|
-
orderItem.cart_guest_id = cart_guest_id;
|
|
280
|
-
if (customer_id)
|
|
281
|
-
orderItem.customer_id = customer_id;
|
|
282
|
-
if (made_for)
|
|
283
|
-
orderItem.madeFor = made_for;
|
|
284
|
-
if (notes)
|
|
285
|
-
orderItem.notes = notes;
|
|
286
|
-
}
|
|
287
238
|
const pricedItem = (0, exports.calcPrices)(orderItem);
|
|
288
239
|
return pricedItem;
|
|
289
240
|
};
|
|
290
241
|
exports.makeOrderItem = makeOrderItem;
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
242
|
+
const calcCartCounts = (cart) => {
|
|
243
|
+
return cart.reduce((obj, item) => {
|
|
244
|
+
const newCount = (obj[item.id.toString()] || 0) + item.quantity;
|
|
245
|
+
return Object.assign(Object.assign({}, obj), { [item.id]: newCount });
|
|
246
|
+
}, {});
|
|
247
|
+
};
|
|
248
|
+
exports.calcCartCounts = calcCartCounts;
|
|
249
|
+
const makeSimpleGroupsLookup = (groups) => {
|
|
250
|
+
if (!groups || !groups.length)
|
|
251
|
+
return null;
|
|
252
|
+
return groups.reduce((grpObj, g) => {
|
|
253
|
+
const options = g.options.reduce((optObj, o) => {
|
|
254
|
+
optObj[o.id] = {
|
|
255
|
+
quantity: o.quantity,
|
|
256
|
+
groupsLookup: makeSimpleGroupsLookup(o.groups)
|
|
257
|
+
};
|
|
258
|
+
return optObj;
|
|
259
|
+
}, {});
|
|
260
|
+
grpObj[g.id] = { options };
|
|
261
|
+
return grpObj;
|
|
262
|
+
}, {});
|
|
263
|
+
};
|
|
264
|
+
const rehydrateGroups = (groups, groupsLookup) => {
|
|
265
|
+
if (!groupsLookup)
|
|
266
|
+
return groups;
|
|
267
|
+
groups.forEach(group => {
|
|
268
|
+
const simpleGroup = groupsLookup[group.id];
|
|
269
|
+
if (simpleGroup) {
|
|
270
|
+
group.options.forEach(option => {
|
|
271
|
+
const simpleOption = simpleGroup.options[option.id];
|
|
272
|
+
if (simpleOption) {
|
|
273
|
+
option.quantity = simpleOption.quantity;
|
|
274
|
+
option.groups = rehydrateGroups(option.groups, simpleOption.groupsLookup);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
return groups;
|
|
280
|
+
};
|
|
281
|
+
const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = []) => {
|
|
282
|
+
const orderItem = (0, exports.makeOrderItem)(menuItem, true, soldOut, simpleCartItem);
|
|
294
283
|
orderItem.quantity = simpleCartItem.quantity || 1;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
var _a;
|
|
299
|
-
const simpleGroup = group.id ? groupsLookup[group.id] : null;
|
|
300
|
-
if (simpleGroup) {
|
|
301
|
-
(_a = group.options) === null || _a === void 0 ? void 0 : _a.forEach(option => {
|
|
302
|
-
if (option.id) {
|
|
303
|
-
const simpleOption = simpleGroup.options[option.id];
|
|
304
|
-
option.quantity = simpleOption ? simpleOption.quantity : 0;
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
});
|
|
284
|
+
const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
|
|
285
|
+
if (groupsLookup) {
|
|
286
|
+
orderItem.groups = rehydrateGroups(orderItem.groups, groupsLookup);
|
|
309
287
|
}
|
|
310
288
|
const pricedItem = (0, exports.calcPrices)(orderItem);
|
|
311
289
|
return pricedItem;
|
|
312
290
|
};
|
|
313
291
|
exports.rehydrateOrderItem = rehydrateOrderItem;
|
|
314
|
-
const rehydrateCart = (menuItems, simpleCartItems) => {
|
|
292
|
+
const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
|
|
315
293
|
const orderItems = [];
|
|
316
294
|
simpleCartItems.forEach(item => {
|
|
317
295
|
const menuItem = menuItems.find(i => i.id === item.id);
|
|
318
296
|
if (menuItem) {
|
|
319
|
-
const orderItem = (0, exports.rehydrateOrderItem)(menuItem, item);
|
|
297
|
+
const orderItem = (0, exports.rehydrateOrderItem)(menuItem, item, soldOut);
|
|
320
298
|
orderItems.push(orderItem);
|
|
321
299
|
}
|
|
322
300
|
});
|
|
@@ -325,6 +303,7 @@ const rehydrateCart = (menuItems, simpleCartItems) => {
|
|
|
325
303
|
return { cart, cartCounts };
|
|
326
304
|
};
|
|
327
305
|
exports.rehydrateCart = rehydrateCart;
|
|
306
|
+
// TODO: maybe refactor
|
|
328
307
|
const rehydrateDetails = (details) => {
|
|
329
308
|
return {
|
|
330
309
|
cart_id: details === null || details === void 0 ? void 0 : details.cart_id,
|
|
@@ -335,6 +314,7 @@ const rehydrateDetails = (details) => {
|
|
|
335
314
|
tax_exempt_id: details === null || details === void 0 ? void 0 : details.tax_exempt_id
|
|
336
315
|
};
|
|
337
316
|
};
|
|
317
|
+
// TODO: maybe refactor
|
|
338
318
|
const rehydrateCustomer = (customer) => {
|
|
339
319
|
const newCustomer = {
|
|
340
320
|
first_name: customer === null || customer === void 0 ? void 0 : customer.first_name,
|
|
@@ -348,6 +328,7 @@ const rehydrateCustomer = (customer) => {
|
|
|
348
328
|
}
|
|
349
329
|
return newCustomer;
|
|
350
330
|
};
|
|
331
|
+
// TODO: maybe refactor
|
|
351
332
|
const rehydrateAddress = (address) => {
|
|
352
333
|
if (!address)
|
|
353
334
|
return {};
|
|
@@ -358,15 +339,19 @@ const rehydrateAddress = (address) => {
|
|
|
358
339
|
phone: address.phone
|
|
359
340
|
};
|
|
360
341
|
};
|
|
342
|
+
// TODO: maybe refactor
|
|
361
343
|
const rehydrateSurcharges = (surcharges) => {
|
|
362
344
|
return surcharges === null || surcharges === void 0 ? void 0 : surcharges.filter(i => i.is_optional).map(i => ({ id: i.surcharge_id }));
|
|
363
345
|
};
|
|
346
|
+
// TODO: maybe refactor
|
|
364
347
|
const rehydrateDiscounts = (discounts) => {
|
|
365
348
|
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 }));
|
|
366
349
|
};
|
|
350
|
+
// TODO: maybe refactor
|
|
367
351
|
const rehydratePromoCodes = (discounts) => {
|
|
368
352
|
return discounts === null || discounts === void 0 ? void 0 : discounts.filter(i => i.is_promo_code).map(i => i.name);
|
|
369
353
|
};
|
|
354
|
+
// TODO: maybe refactor
|
|
370
355
|
const rehydrateTenders = (tenders) => {
|
|
371
356
|
// const giftCards = tenders.filter((i) => i.tender_type === 'GIFT_CARD')
|
|
372
357
|
const other = tenders === null || tenders === void 0 ? void 0 : tenders.map(i => {
|
|
@@ -382,6 +367,7 @@ const rehydrateTenders = (tenders) => {
|
|
|
382
367
|
});
|
|
383
368
|
return other;
|
|
384
369
|
};
|
|
370
|
+
// TODO: maybe refactor
|
|
385
371
|
const rehydrateCheckoutForm = (order) => {
|
|
386
372
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
387
373
|
const form = {
|
|
@@ -399,7 +385,7 @@ const rehydrateCheckoutForm = (order) => {
|
|
|
399
385
|
exports.rehydrateCheckoutForm = rehydrateCheckoutForm;
|
|
400
386
|
// group orders
|
|
401
387
|
const makeGuestLookup = (cartGuests) => {
|
|
402
|
-
return cartGuests.reduce((obj, i) =>
|
|
388
|
+
return cartGuests.reduce((obj, i) => i.cart_guest_id ? Object.assign(Object.assign({}, obj), { [i.cart_guest_id]: i }) : Object.assign({}, obj), {});
|
|
403
389
|
};
|
|
404
390
|
exports.makeGuestLookup = makeGuestLookup;
|
|
405
391
|
const combineCarts = (cart, guestCart, cartOwner, cartGuests) => {
|
|
@@ -424,41 +410,10 @@ const makeItemImageUrl = (images) => {
|
|
|
424
410
|
return (imageMap.SMALL_IMAGE || imageMap.LARGE_IMAGE || imageMap.APP_IMAGE || null);
|
|
425
411
|
};
|
|
426
412
|
exports.makeItemImageUrl = makeItemImageUrl;
|
|
427
|
-
const makeItemSignature = (item) => {
|
|
428
|
-
const optionIds = !item.groups
|
|
429
|
-
? []
|
|
430
|
-
: item.groups
|
|
431
|
-
.reduce((arr, group) => {
|
|
432
|
-
var _a;
|
|
433
|
-
const ids = (_a = group.options) === null || _a === void 0 ? void 0 : _a.map(o => o.id);
|
|
434
|
-
return [...arr, ...(ids !== null && ids !== void 0 ? ids : [])];
|
|
435
|
-
}, [])
|
|
436
|
-
.sort((a, b) => a - b);
|
|
437
|
-
return [item.id, ...optionIds].join('.');
|
|
438
|
-
};
|
|
439
|
-
exports.makeItemSignature = makeItemSignature;
|
|
440
|
-
const makeCartItemSignature = (item) => {
|
|
441
|
-
var _a;
|
|
442
|
-
const optionIds = ((_a = item.groups) !== null && _a !== void 0 ? _a : [])
|
|
443
|
-
.reduce((arr, group) => {
|
|
444
|
-
var _a;
|
|
445
|
-
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) => {
|
|
446
|
-
const optionArr = new Array(o.quantity);
|
|
447
|
-
optionArr.fill(o.id);
|
|
448
|
-
return [...optionsArr, ...optionArr];
|
|
449
|
-
}, []);
|
|
450
|
-
return [...arr, ...(ids !== null && ids !== void 0 ? ids : [])];
|
|
451
|
-
}, [])
|
|
452
|
-
.sort((a, b) => a - b);
|
|
453
|
-
const itemIds = [item.id, ...optionIds].join('.');
|
|
454
|
-
const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
|
|
455
|
-
return signature;
|
|
456
|
-
};
|
|
457
|
-
exports.makeCartItemSignature = makeCartItemSignature;
|
|
458
413
|
const makeDisplayItemGroups = (optionGroups) => {
|
|
459
414
|
if (!optionGroups || !optionGroups.length)
|
|
460
415
|
return [];
|
|
461
|
-
return optionGroups.map(
|
|
416
|
+
return optionGroups.map(g => {
|
|
462
417
|
var _a;
|
|
463
418
|
const options = (_a = g.options) === null || _a === void 0 ? void 0 : _a.map(o => (0, exports.makeDisplayItem)(o));
|
|
464
419
|
return Object.assign(Object.assign({}, g), { options });
|
|
@@ -466,29 +421,28 @@ const makeDisplayItemGroups = (optionGroups) => {
|
|
|
466
421
|
};
|
|
467
422
|
exports.makeDisplayItemGroups = makeDisplayItemGroups;
|
|
468
423
|
const makeDisplayItem = (item, isOption = false) => {
|
|
469
|
-
var _a
|
|
424
|
+
var _a;
|
|
470
425
|
const cals = item.nutritional_info ? (_a = item.nutritional_info) === null || _a === void 0 ? void 0 : _a.calories : null;
|
|
471
426
|
let displayItem = {
|
|
427
|
+
allergens: item.allergens,
|
|
428
|
+
cals: cals,
|
|
429
|
+
description: item.description,
|
|
430
|
+
groups: (0, exports.makeDisplayItemGroups)(item.groups),
|
|
431
|
+
imageUrl: (0, exports.makeItemImageUrl)(item.images),
|
|
472
432
|
id: item.id,
|
|
473
433
|
name: item.name,
|
|
434
|
+
nutritionalInfo: item.nutritional_info,
|
|
435
|
+
quantity: item.quantity,
|
|
436
|
+
price: item.price,
|
|
474
437
|
shortDescription: item.short_description,
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
allergens: item.allergens || [],
|
|
478
|
-
tags: item.tags || [],
|
|
479
|
-
nutritionalInfo: item.nutritional_info || null,
|
|
480
|
-
cals: isNaN(cals !== null && cals !== void 0 ? cals : 0) ? null : cals,
|
|
481
|
-
groups: (0, exports.makeDisplayItemGroups)((_c = item.groups) !== null && _c !== void 0 ? _c : []),
|
|
482
|
-
quantity: item.quantity || 1,
|
|
483
|
-
price: item.price !== undefined ? item.price : undefined,
|
|
484
|
-
totalPrice: item.price_total !== undefined ? item.price_total : undefined
|
|
438
|
+
tags: item.tags,
|
|
439
|
+
totalPrice: item.price_total
|
|
485
440
|
};
|
|
486
441
|
if (!isOption) {
|
|
487
442
|
const itemDetails = {
|
|
488
443
|
madeFor: item.made_for,
|
|
489
444
|
notes: item.notes,
|
|
490
|
-
|
|
491
|
-
signature: (0, exports.makeItemSignature)(item)
|
|
445
|
+
signature: (0, exports.makeItemSignature)(displayItem)
|
|
492
446
|
};
|
|
493
447
|
displayItem = Object.assign(Object.assign({}, displayItem), itemDetails);
|
|
494
448
|
}
|
|
@@ -522,39 +476,45 @@ const makeFavoritesLookup = (favorites) => {
|
|
|
522
476
|
.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [(0, exports.makeItemSignature)(i.cart)]: i.favorite_id })), {});
|
|
523
477
|
};
|
|
524
478
|
exports.makeFavoritesLookup = makeFavoritesLookup;
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
var _a;
|
|
542
|
-
const newCount = (obj[(_a = item.id) !== null && _a !== void 0 ? _a : 0] || 0) + item.quantity;
|
|
543
|
-
return item.id ? Object.assign(Object.assign({}, obj), { [item.id]: newCount }) : Object.assign({}, obj);
|
|
544
|
-
}, {});
|
|
479
|
+
const makeCartItemOptionIds = (groups) => {
|
|
480
|
+
if (!groups || !groups.length)
|
|
481
|
+
return [];
|
|
482
|
+
return groups
|
|
483
|
+
.reduce((arr, group) => {
|
|
484
|
+
const ids = group.options
|
|
485
|
+
.filter(o => o.quantity > 0)
|
|
486
|
+
.reduce((optionsArr, o) => {
|
|
487
|
+
const optionArr = new Array(o.quantity);
|
|
488
|
+
optionArr.fill(o.id);
|
|
489
|
+
const nestedIds = makeCartItemOptionIds(o.groups);
|
|
490
|
+
return [...optionsArr, ...optionArr, ...nestedIds];
|
|
491
|
+
}, []);
|
|
492
|
+
return [...arr, ...ids];
|
|
493
|
+
}, [])
|
|
494
|
+
.sort((a, b) => a - b);
|
|
545
495
|
};
|
|
546
|
-
|
|
496
|
+
const makeItemSignature = (item) => {
|
|
497
|
+
const optionIds = makeCartItemOptionIds(item.groups);
|
|
498
|
+
return [item.id, ...optionIds].join('.');
|
|
499
|
+
};
|
|
500
|
+
exports.makeItemSignature = makeItemSignature;
|
|
501
|
+
const makeCartItemSignature = (item) => {
|
|
502
|
+
const optionIds = makeCartItemOptionIds(item.groups);
|
|
503
|
+
const itemIds = [item.id, ...optionIds].join('.');
|
|
504
|
+
const signature = `${itemIds}.${item.madeFor}.${item.notes}`;
|
|
505
|
+
return signature;
|
|
506
|
+
};
|
|
507
|
+
exports.makeCartItemSignature = makeCartItemSignature;
|
|
547
508
|
const addItem = (cart, item) => {
|
|
548
|
-
var _a;
|
|
549
509
|
if (typeof item.index === 'undefined') {
|
|
550
510
|
const itemSignature = (0, exports.makeCartItemSignature)(item);
|
|
551
511
|
const itemSignatures = cart.map(i => (Object.assign({ signature: (0, exports.makeCartItemSignature)(i) }, i)));
|
|
552
512
|
const match = itemSignatures.find(i => i.signature === itemSignature);
|
|
553
|
-
if (match) {
|
|
513
|
+
if (match && match.index) {
|
|
554
514
|
const quantity = item.quantity + match.quantity;
|
|
555
515
|
const newItem = Object.assign(Object.assign({}, item), { quantity });
|
|
556
516
|
const updated = (0, exports.calcPrices)(newItem);
|
|
557
|
-
cart[
|
|
517
|
+
cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
|
|
558
518
|
}
|
|
559
519
|
else {
|
|
560
520
|
cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
|
|
@@ -587,7 +547,7 @@ const incrementItem = (cart, index) => {
|
|
|
587
547
|
newQuantity =
|
|
588
548
|
item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
|
|
589
549
|
const newItem = (0, exports.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
|
|
590
|
-
cart[realIndex
|
|
550
|
+
cart[realIndex] = newItem;
|
|
591
551
|
}
|
|
592
552
|
}
|
|
593
553
|
const cartCounts = (0, exports.calcCartCounts)(cart);
|
|
@@ -612,18 +572,24 @@ const decrementItem = (cart, index) => {
|
|
|
612
572
|
return { cart, cartCounts };
|
|
613
573
|
};
|
|
614
574
|
exports.decrementItem = decrementItem;
|
|
575
|
+
const makeSimpleGroups = (groups) => {
|
|
576
|
+
return groups.map(g => {
|
|
577
|
+
const options = g.options
|
|
578
|
+
.filter(o => o.quantity !== 0)
|
|
579
|
+
.map(o => ({
|
|
580
|
+
id: o.id,
|
|
581
|
+
quantity: o.quantity,
|
|
582
|
+
groups: makeSimpleGroups(o.groups || [])
|
|
583
|
+
}));
|
|
584
|
+
return { id: g.id, options: options };
|
|
585
|
+
});
|
|
586
|
+
};
|
|
615
587
|
const makeSimpleCart = (cart) => {
|
|
616
588
|
const simpleCart = cart.map(i => {
|
|
617
|
-
var _a;
|
|
618
|
-
const groups = (_a = i.groups) === null || _a === void 0 ? void 0 : _a.map(g => {
|
|
619
|
-
var _a;
|
|
620
|
-
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 }));
|
|
621
|
-
return { id: g.id, options: options };
|
|
622
|
-
});
|
|
623
589
|
return {
|
|
624
590
|
id: i.id,
|
|
625
591
|
quantity: i.quantity,
|
|
626
|
-
groups: groups,
|
|
592
|
+
groups: makeSimpleGroups(i.groups),
|
|
627
593
|
made_for: i.madeFor || '',
|
|
628
594
|
notes: i.notes || ''
|
|
629
595
|
};
|
|
@@ -632,44 +598,41 @@ const makeSimpleCart = (cart) => {
|
|
|
632
598
|
};
|
|
633
599
|
exports.makeSimpleCart = makeSimpleCart;
|
|
634
600
|
/* cart validation */
|
|
601
|
+
// TODO: refactor for nested modifiers
|
|
635
602
|
// convert menu items to order items for validation but only include
|
|
636
603
|
// items that are already in the cart to save wasted effort
|
|
637
604
|
const makeItemLookup = (categories, itemIds) => {
|
|
638
605
|
const itemLookup = {};
|
|
639
606
|
categories.forEach(category => {
|
|
640
607
|
category.items.forEach(item => {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
itemLookup[(_b = item.id) !== null && _b !== void 0 ? _b : 0] = (0, exports.makeOrderItem)(item, true);
|
|
608
|
+
if (itemIds.includes(item.id)) {
|
|
609
|
+
itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
|
|
644
610
|
}
|
|
645
611
|
});
|
|
646
612
|
category.children.forEach(child => {
|
|
647
613
|
child.items.forEach(item => {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
itemLookup[(_b = item.id) !== null && _b !== void 0 ? _b : 0] = (0, exports.makeOrderItem)(item, true);
|
|
614
|
+
if (itemIds.includes(item.id)) {
|
|
615
|
+
itemLookup[item.id] = (0, exports.makeOrderItem)(item, true);
|
|
651
616
|
}
|
|
652
617
|
});
|
|
653
618
|
});
|
|
654
619
|
});
|
|
655
620
|
return itemLookup;
|
|
656
621
|
};
|
|
622
|
+
// TODO: refactor for nested modifiers
|
|
657
623
|
const makeGroupsLookup = (item) => {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
var _a;
|
|
661
|
-
const options = (_a = i.options) === null || _a === void 0 ? void 0 : _a.reduce((optObj, o) => {
|
|
624
|
+
return item.groups.reduce((grpObj, i) => {
|
|
625
|
+
const options = i.options.reduce((optObj, o) => {
|
|
662
626
|
if (o.id) {
|
|
663
627
|
optObj[o.id] = o;
|
|
664
628
|
}
|
|
665
629
|
return optObj;
|
|
666
630
|
}, {});
|
|
667
|
-
|
|
668
|
-
grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
|
|
669
|
-
}
|
|
631
|
+
grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
|
|
670
632
|
return grpObj;
|
|
671
633
|
}, {});
|
|
672
634
|
};
|
|
635
|
+
// TODO: refactor for nested modifiers
|
|
673
636
|
const validateCart = (cart, categories, soldOut) => {
|
|
674
637
|
const itemIds = cart.map(item => item.id);
|
|
675
638
|
const itemLookup = makeItemLookup(categories, itemIds);
|
|
@@ -689,10 +652,11 @@ const validateCart = (cart, categories, soldOut) => {
|
|
|
689
652
|
const invalidOptions = [];
|
|
690
653
|
const newGroups = makeGroupsLookup(newItem);
|
|
691
654
|
// check if any required groups for the new item are missing from the old item
|
|
692
|
-
const newRequiredGroups = Object.values(newGroups).filter(
|
|
655
|
+
const newRequiredGroups = Object.values(newGroups).filter(i => i.min > 0);
|
|
693
656
|
const oldGroupIds = (_a = oldItem.groups) === null || _a === void 0 ? void 0 : _a.map(i => i.id);
|
|
694
|
-
missingGroups = newRequiredGroups.filter(
|
|
695
|
-
const newEmptyGroups = Object.values(newGroups)
|
|
657
|
+
missingGroups = newRequiredGroups.filter(i => !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id)));
|
|
658
|
+
const newEmptyGroups = Object.values(newGroups)
|
|
659
|
+
.filter((i) => {
|
|
696
660
|
return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
|
|
697
661
|
})
|
|
698
662
|
.map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
|
|
@@ -849,46 +813,3 @@ const checkAmountRemaining = (total, tenders) => {
|
|
|
849
813
|
return Object.is(remaining, -0) ? 0.0 : remaining;
|
|
850
814
|
};
|
|
851
815
|
exports.checkAmountRemaining = checkAmountRemaining;
|
|
852
|
-
const makeUpsellItems = (itemIds, itemLookup) => {
|
|
853
|
-
return itemIds
|
|
854
|
-
.map((id) => itemLookup[id])
|
|
855
|
-
.filter((i) => i)
|
|
856
|
-
.map((i) => (0, exports.makeOrderItem)(i))
|
|
857
|
-
.filter((i) => {
|
|
858
|
-
var _a;
|
|
859
|
-
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); });
|
|
860
|
-
return (belowMin === null || belowMin === void 0 ? void 0 : belowMin.length) === 0;
|
|
861
|
-
});
|
|
862
|
-
};
|
|
863
|
-
exports.makeUpsellItems = makeUpsellItems;
|
|
864
|
-
const nutritionKeys = [
|
|
865
|
-
'calories',
|
|
866
|
-
'cholesterol',
|
|
867
|
-
'dietary_fiber',
|
|
868
|
-
'protein',
|
|
869
|
-
'saturated_fat',
|
|
870
|
-
'sodium',
|
|
871
|
-
'sugars',
|
|
872
|
-
'total_carbs',
|
|
873
|
-
'total_fat',
|
|
874
|
-
'trans_fat',
|
|
875
|
-
];
|
|
876
|
-
const calcNutrition = (item) => {
|
|
877
|
-
const { nutritionalInfo, groups } = item;
|
|
878
|
-
let nutrition = nutritionalInfo ? Object.assign({}, nutritionalInfo) : {};
|
|
879
|
-
const options = groups.reduce((arr, g) => {
|
|
880
|
-
var _a, _b;
|
|
881
|
-
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 : [])];
|
|
882
|
-
}, []);
|
|
883
|
-
options.forEach((o) => {
|
|
884
|
-
nutritionKeys === null || nutritionKeys === void 0 ? void 0 : nutritionKeys.forEach((key) => {
|
|
885
|
-
const optionVal = o.nutritionalInfo
|
|
886
|
-
? parseInt(o.nutritionalInfo[key]) || 0
|
|
887
|
-
: 0;
|
|
888
|
-
const itemVal = parseInt(nutrition[key]) || 0;
|
|
889
|
-
nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + optionVal });
|
|
890
|
-
});
|
|
891
|
-
});
|
|
892
|
-
return nutrition;
|
|
893
|
-
};
|
|
894
|
-
exports.calcNutrition = calcNutrition;
|