@open-tender/utils 0.5.77 → 0.5.79

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeDeviceType = exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeReorderCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.compareCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.makeMenuItemIds = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItemsForCart = exports.makeUpsellItemIds = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNamesForGroups = exports.makeModifierNames = exports.getItemOptions = void 0;
3
+ exports.incrementItemKiosk = exports.addItemKiosk = exports.rehydrateCartKiosk = exports.rehydrateOrderItemKiosk = exports.makeUpsellItemsForCartKiosk = exports.makeUpsellItemsKiosk = exports.makeOrderItemKiosk = exports.calcPricesKiosk = exports.calcNutritionalInfoKiosk = exports.calcNutritionKiosk = exports.makeDeviceType = exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeReorderCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.compareCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.makeMenuItemIds = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItemsForCart = exports.makeUpsellItemIds = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNamesForGroups = exports.makeModifierNames = exports.getItemOptions = void 0;
4
+ exports.decrementItemKiosk = void 0;
4
5
  const types_1 = require("@open-tender/types");
5
6
  const helpers_1 = require("./helpers");
6
7
  const favorites_1 = require("./favorites");
@@ -1056,6 +1057,8 @@ const prepareOrder = (data) => {
1056
1057
  }
1057
1058
  if (data.orderId)
1058
1059
  order.order_id = data.orderId;
1060
+ if (data.orderId && data.orderUuid)
1061
+ order.order_uuid = data.orderUuid;
1059
1062
  if (data.cartId)
1060
1063
  order.cart_id = data.cartId;
1061
1064
  if (data.save)
@@ -1098,3 +1101,260 @@ const makeDeviceType = (deviceType, deviceId) => {
1098
1101
  }
1099
1102
  };
1100
1103
  exports.makeDeviceType = makeDeviceType;
1104
+ /* kiosk variants */
1105
+ // Everything below is used by the kiosk app (open-tender-kiosk-app) and by the
1106
+ // Kiosk containers of @open-tender/store. Each function is the kiosk
1107
+ // counterpart of the function of the same name without the suffix, which is
1108
+ // left as it is for the other apps. A function that calls a kiosk function has
1109
+ // a kiosk variant too, so an item is priced the same way wherever the kiosk
1110
+ // builds, edits or re-prices it.
1111
+ const nutritionKeysKiosk = [
1112
+ 'calories',
1113
+ 'calories_from_fat',
1114
+ 'cholesterol',
1115
+ 'dietary_fiber',
1116
+ 'protein',
1117
+ 'saturated_fat',
1118
+ 'sodium',
1119
+ 'sugars',
1120
+ 'total_carbs',
1121
+ 'total_fat',
1122
+ 'trans_fat',
1123
+ 'serving_weight'
1124
+ ];
1125
+ // A "Remove ..." option carries the nutrition of the ingredient it removes.
1126
+ // It counts while it is not selected (the ingredient is still on the item) and
1127
+ // drops out once it is.
1128
+ const isRemoveOptionKiosk = (option) => (option.name || '').toLowerCase().startsWith('remove');
1129
+ // Unlike calcNutrition, this walks every option, selected or not, so that
1130
+ // unselected "Remove ..." options are counted.
1131
+ const calcNutritionKiosk = (item) => {
1132
+ const { nutritionalInfo, groups } = item;
1133
+ let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
1134
+ groups.forEach(g => {
1135
+ g.options.forEach(option => {
1136
+ const isRemove = isRemoveOptionKiosk(option);
1137
+ const selected = (option.quantity || 0) > 0;
1138
+ const optionNutrition = (0, exports.calcNutritionKiosk)(option);
1139
+ nutritionKeysKiosk.forEach(key => {
1140
+ const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
1141
+ const itemVal = nutrition[key] || 0;
1142
+ const contribution = isRemove
1143
+ ? selected
1144
+ ? 0
1145
+ : Math.abs(optionPer)
1146
+ : optionPer * (option.quantity || 0);
1147
+ nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + contribution });
1148
+ });
1149
+ });
1150
+ });
1151
+ return nutrition;
1152
+ };
1153
+ exports.calcNutritionKiosk = calcNutritionKiosk;
1154
+ const calcNutritionalInfoKiosk = (item) => {
1155
+ const nutrition = (0, exports.calcNutritionKiosk)(item);
1156
+ const { serving_size = '0.00' } = item.nutritionalInfo || {};
1157
+ return Object.assign(Object.assign({}, nutrition), { serving_size });
1158
+ };
1159
+ exports.calcNutritionalInfoKiosk = calcNutritionalInfoKiosk;
1160
+ // calcPrices, plus:
1161
+ // - "Remove ..." options count their calories while they are not selected
1162
+ // - the options of a meal get the fields of CartItemOptionKioskDeltas
1163
+ // - an item without calories of its own still gets a totalCals when its
1164
+ // options have some
1165
+ const calcPricesKiosk = (item) => {
1166
+ var _a;
1167
+ const itemName = (item.name || '').toLowerCase();
1168
+ const isMeal = itemName.includes('meal') || itemName.includes('meat & 3');
1169
+ const groups = item.groups.map(g => {
1170
+ let groupQuantity = 0;
1171
+ const optionsWithPrices = g.options.map((o, index) => {
1172
+ return Object.assign(Object.assign({}, (0, exports.calcPricesKiosk)(o)), { index });
1173
+ });
1174
+ const sortedOptions = optionsWithPrices.sort((a, b) => {
1175
+ const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
1176
+ const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
1177
+ return pricePerA - pricePerB;
1178
+ });
1179
+ const pricedOptions = sortedOptions.map(o => {
1180
+ const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
1181
+ const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
1182
+ const includedRemaining = Math.max(g.included - groupQuantity, 0);
1183
+ const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
1184
+ const selected = (o.quantity || 0) > 0;
1185
+ const totalCals = isRemoveOptionKiosk(o)
1186
+ ? selected
1187
+ ? 0
1188
+ : Math.abs(o.cals || 0)
1189
+ : o.totalCals || 0;
1190
+ const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals });
1191
+ groupQuantity += o.quantity || 0;
1192
+ return option;
1193
+ });
1194
+ const defaultSibling = g.options.find(o => o.isDefault);
1195
+ const hasDefault = !!defaultSibling;
1196
+ const defaultUnitPrice = defaultSibling ? defaultSibling.price || 0 : 0;
1197
+ const withDeltas = isMeal
1198
+ ? pricedOptions.map(o => {
1199
+ const priceDelta = hasDefault
1200
+ ? (o.price || 0) - defaultUnitPrice
1201
+ : null;
1202
+ const userExtra = (o.groups || []).reduce((sum, gg) => {
1203
+ const children = (gg.options || []);
1204
+ return (sum +
1205
+ children.reduce((s, child) => {
1206
+ const childSelected = (child.quantity || 0) > 0;
1207
+ if (!childSelected)
1208
+ return s;
1209
+ const ownAdded = !child.isDefault
1210
+ ? (child.price || 0) * child.quantity
1211
+ : 0;
1212
+ const rolledUp = child.userExtra || 0;
1213
+ return s + ownAdded + rolledUp;
1214
+ }, 0));
1215
+ }, 0);
1216
+ const displayDelta = hasDefault
1217
+ ? (priceDelta || 0) + userExtra
1218
+ : (o.price || 0) + userExtra;
1219
+ return Object.assign(Object.assign({}, o), { priceDelta,
1220
+ userExtra,
1221
+ displayDelta, hasDefaultSibling: hasDefault });
1222
+ })
1223
+ : pricedOptions;
1224
+ const options = withDeltas.sort((a, b) => a.index - b.index);
1225
+ return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
1226
+ });
1227
+ const optionsPrice = groups.reduce((t, g) => {
1228
+ return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
1229
+ }, 0.0);
1230
+ const totalPrice = item.quantity * (item.price + optionsPrice);
1231
+ const optionsPoints = groups.reduce((t, g) => {
1232
+ return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
1233
+ }, 0.0);
1234
+ const totalPoints = item.points
1235
+ ? item.quantity * (item.points + optionsPoints)
1236
+ : null;
1237
+ const optionsCals = groups.reduce((t, g) => {
1238
+ return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
1239
+ }, 0.0);
1240
+ const totalCals = item.cals != null || optionsCals > 0
1241
+ ? item.quantity * (((_a = item.cals) !== null && _a !== void 0 ? _a : 0) + optionsCals)
1242
+ : null;
1243
+ const nonSizeGroups = groups ? groups.filter(i => !i.isSize) : null;
1244
+ const currentOptions = nonSizeGroups
1245
+ ? (0, exports.getItemOptions)(Object.assign(Object.assign({}, item), { groups: nonSizeGroups }))
1246
+ : [];
1247
+ const modifiersAllergens = currentOptions.reduce((t, o) => {
1248
+ return [...t, ...o.allergens];
1249
+ }, []);
1250
+ const totalAllergens = [...new Set(item.allergens.concat(modifiersAllergens))];
1251
+ return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals, totalAllergens });
1252
+ };
1253
+ exports.calcPricesKiosk = calcPricesKiosk;
1254
+ // calcPrices and calcPricesKiosk work out every total from the price,
1255
+ // quantity, calories and points of the item and its options and never from a
1256
+ // total that is already there, so an item priced by calcPrices can be priced
1257
+ // again by calcPricesKiosk. The kiosk variants below rely on this to reuse the
1258
+ // functions they are a variant of.
1259
+ // makeOrderItem priced with calcPricesKiosk, plus smallImageUrl, the small
1260
+ // image the kiosk shows on its menu tiles.
1261
+ const makeOrderItemKiosk = (item, isEdit, soldOut = [], simpleItem, hasPoints = false, requestedAt = null) => {
1262
+ const orderItem = (0, exports.makeOrderItem)(item, isEdit, soldOut, simpleItem, hasPoints, requestedAt);
1263
+ const kioskItem = Object.assign(Object.assign({}, orderItem), { smallImageUrl: item.small_image_url });
1264
+ return (0, exports.calcPricesKiosk)(kioskItem);
1265
+ };
1266
+ exports.makeOrderItemKiosk = makeOrderItemKiosk;
1267
+ const makeUpsellItemsKiosk = (itemIds, itemLookup) => {
1268
+ return itemIds
1269
+ .map(id => itemLookup[id])
1270
+ .filter(i => i)
1271
+ .map(i => (0, exports.makeOrderItemKiosk)(i))
1272
+ .filter(i => !i.isSoldOut);
1273
+ };
1274
+ exports.makeUpsellItemsKiosk = makeUpsellItemsKiosk;
1275
+ const makeUpsellItemsForCartKiosk = (categories, cartIds, soldOut) => {
1276
+ const itemLookup = (0, favorites_1.makeMenuItemLookup)(categories);
1277
+ const menuItems = cartIds.map(id => itemLookup[id]).filter(Boolean);
1278
+ const upsellItemIds = menuItems.reduce((arr, i) => {
1279
+ const upsellsItems = i.upsell_items.slice(0, cartIds.length > 2 ? 2 : undefined);
1280
+ return [...arr, ...upsellsItems];
1281
+ }, []);
1282
+ const uniqueIds = Array.from(new Set(upsellItemIds));
1283
+ const itemIds = uniqueIds.filter(id => !cartIds.includes(id) && !soldOut.includes(id));
1284
+ return (0, exports.makeUpsellItemsKiosk)(itemIds, itemLookup);
1285
+ };
1286
+ exports.makeUpsellItemsForCartKiosk = makeUpsellItemsForCartKiosk;
1287
+ const rehydrateOrderItemKiosk = (menuItem, simpleCartItem, soldOut = [], requestedAt = null) => {
1288
+ const orderItem = (0, exports.rehydrateOrderItem)(menuItem, simpleCartItem, soldOut, requestedAt);
1289
+ const kioskItem = Object.assign(Object.assign({}, orderItem), { smallImageUrl: menuItem.small_image_url });
1290
+ return (0, exports.calcPricesKiosk)(kioskItem);
1291
+ };
1292
+ exports.rehydrateOrderItemKiosk = rehydrateOrderItemKiosk;
1293
+ const rehydrateCartKiosk = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
1294
+ const rehydrated = (0, exports.rehydrateCart)(menuItems, simpleCartItems, soldOut, options);
1295
+ const cart = rehydrated.cart.map(item => {
1296
+ // a rehydrated item has the id of the menu item it was made from
1297
+ const menuItem = (menuItems || []).find(i => i.id === item.id);
1298
+ const smallImageUrl = menuItem ? menuItem.small_image_url : undefined;
1299
+ const kioskItem = Object.assign(Object.assign({}, item), { smallImageUrl });
1300
+ return (0, exports.calcPricesKiosk)(kioskItem);
1301
+ });
1302
+ return Object.assign(Object.assign({}, rehydrated), { cart });
1303
+ };
1304
+ exports.rehydrateCartKiosk = rehydrateCartKiosk;
1305
+ const addItemKiosk = (cart, item) => {
1306
+ if (typeof item.index === 'undefined') {
1307
+ const itemSignature = (0, exports.makeCartItemSignature)(item);
1308
+ const itemSignatures = cart.map(i => (Object.assign({ signature: (0, exports.makeCartItemSignature)(i) }, i)));
1309
+ const match = itemSignatures.find(i => i.signature === itemSignature);
1310
+ if (match && match.index !== undefined) {
1311
+ const quantity = item.quantity + match.quantity;
1312
+ const newItem = Object.assign(Object.assign({}, item), { quantity });
1313
+ const updated = (0, exports.calcPricesKiosk)(newItem);
1314
+ cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
1315
+ }
1316
+ else {
1317
+ cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
1318
+ }
1319
+ }
1320
+ else {
1321
+ cart[item.index] = item;
1322
+ }
1323
+ const cartCounts = (0, exports.calcCartCounts)(cart);
1324
+ return { cart, cartCounts };
1325
+ };
1326
+ exports.addItemKiosk = addItemKiosk;
1327
+ const incrementItemKiosk = (cart, index) => {
1328
+ const realIndex = getItemRealIndex(cart, index);
1329
+ if (realIndex > -1) {
1330
+ const item = cart[realIndex];
1331
+ if (item.max === 0 || item.quantity < item.max) {
1332
+ let newQuantity = item.quantity + item.increment;
1333
+ newQuantity =
1334
+ item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
1335
+ const newItem = (0, exports.calcPricesKiosk)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
1336
+ cart[realIndex] = newItem;
1337
+ }
1338
+ }
1339
+ const cartCounts = (0, exports.calcCartCounts)(cart);
1340
+ return { cart, cartCounts };
1341
+ };
1342
+ exports.incrementItemKiosk = incrementItemKiosk;
1343
+ const decrementItemKiosk = (cart, index) => {
1344
+ const realIndex = getItemRealIndex(cart, index);
1345
+ if (realIndex > -1) {
1346
+ const item = cart[realIndex];
1347
+ const newQuantity = Math.max(item.quantity - item.increment, 0);
1348
+ if (newQuantity === 0 || newQuantity < item.min) {
1349
+ cart.splice(realIndex, 1);
1350
+ cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
1351
+ }
1352
+ else {
1353
+ const newItem = (0, exports.calcPricesKiosk)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
1354
+ cart[realIndex] = newItem;
1355
+ }
1356
+ }
1357
+ const cartCounts = (0, exports.calcCartCounts)(cart);
1358
+ return { cart, cartCounts };
1359
+ };
1360
+ exports.decrementItemKiosk = decrementItemKiosk;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,174 @@
1
+ import { addItemKiosk, calcNutritionalInfo, calcNutritionalInfoKiosk, calcPrices, calcPricesKiosk, decrementItemKiosk, incrementItemKiosk, makeOrderItem, makeOrderItemKiosk, makeSimpleCart, makeUpsellItemsKiosk, rehydrateCartKiosk, rehydrateOrderItemKiosk } from '../../utils';
2
+ const makeOption = ({ id, name, price = '0.00', calories, isDefault = false, groups = [] }) => ({
3
+ id,
4
+ name,
5
+ price,
6
+ allergens: '',
7
+ tags: '',
8
+ opt_is_default: isDefault,
9
+ min_quantity: 0,
10
+ max_quantity: 10,
11
+ increment: 1,
12
+ suspend_until: null,
13
+ small_image_url: null,
14
+ nutritional_info: calories === undefined ? null : { calories, sodium: calories * 2 },
15
+ option_groups: groups
16
+ });
17
+ const makeGroup = (id, options, included = 0) => ({
18
+ id,
19
+ name: `group ${id}`,
20
+ included_options: included,
21
+ min_options: 0,
22
+ max_options: 0,
23
+ option_items: options
24
+ });
25
+ const makeMenuItem = (id, name, groups = [], calories = 500) => ({
26
+ id,
27
+ name,
28
+ price: '10.00',
29
+ allergens: '',
30
+ tags: '',
31
+ increment: 1,
32
+ min_quantity: 1,
33
+ max_quantity: 10,
34
+ suspend_until: null,
35
+ large_image_url: `large-${id}.png`,
36
+ small_image_url: `small-${id}.png`,
37
+ upsell_items: [],
38
+ nutritional_info: calories === null ? null : { calories, sodium: calories * 2 },
39
+ option_groups: groups
40
+ });
41
+ const findOption = (item, id) => {
42
+ const options = item.groups.flatMap(g => g.options);
43
+ return options.find(o => o.id === id);
44
+ };
45
+ const select = (item, quantities) => {
46
+ const update = (node) => (Object.assign(Object.assign({}, node), { groups: node.groups.map(g => (Object.assign(Object.assign({}, g), { options: g.options.map(o => { var _a; return update(Object.assign(Object.assign({}, o), { quantity: (_a = quantities[o.id]) !== null && _a !== void 0 ? _a : o.quantity })); }) }))) }));
47
+ return update(item);
48
+ };
49
+ const sandwich = makeMenuItem(1, 'Roast Beef', [
50
+ makeGroup(10, [
51
+ makeOption({ id: 11, name: 'Remove Cheese', calories: -80 }),
52
+ makeOption({ id: 12, name: 'Add Bacon', price: '1.50', calories: 90 })
53
+ ])
54
+ ]);
55
+ describe('calcPricesKiosk', () => {
56
+ it('prices an item the way calcPrices does', () => {
57
+ const item = select(makeOrderItem(sandwich), { 12: 2 });
58
+ const priced = calcPricesKiosk(item);
59
+ expect(priced.totalPrice).toBe(calcPrices(item).totalPrice);
60
+ expect(priced.totalPrice).toBe(13);
61
+ });
62
+ it('counts the calories of a "Remove" option until it is selected', () => {
63
+ const item = makeOrderItem(sandwich);
64
+ const notRemoved = calcPricesKiosk(item);
65
+ expect(findOption(notRemoved, 11).totalCals).toBe(80);
66
+ expect(notRemoved.totalCals).toBe(580);
67
+ const removed = calcPricesKiosk(select(item, { 11: 1 }));
68
+ expect(findOption(removed, 11).totalCals).toBe(0);
69
+ expect(removed.totalCals).toBe(500);
70
+ // calcPrices is not changed
71
+ expect(calcPrices(item).totalCals).toBe(500);
72
+ expect(calcPrices(select(item, { 11: 1 })).totalCals).toBe(420);
73
+ });
74
+ it('totals the calories of the options of an item that has none', () => {
75
+ const menuItem = makeMenuItem(2, 'Build Your Own', [makeGroup(20, [makeOption({ id: 21, name: 'Beef', calories: 200 })])], null);
76
+ const item = select(makeOrderItem(menuItem), { 21: 1 });
77
+ expect(calcPrices(item).totalCals).toBeNull();
78
+ expect(calcPricesKiosk(item).totalCals).toBe(200);
79
+ expect(calcPricesKiosk(select(item, { 21: 0 })).totalCals).toBeNull();
80
+ });
81
+ it('adds the price deltas to the options of a meal', () => {
82
+ const meal = makeMenuItem(3, 'Roast Beef Meal', [
83
+ makeGroup(30, [
84
+ makeOption({ id: 31, name: 'Small Fries', isDefault: true }),
85
+ makeOption({
86
+ id: 32,
87
+ name: 'Large Meal Fries',
88
+ price: '1.00',
89
+ groups: [
90
+ makeGroup(40, [
91
+ makeOption({ id: 41, name: 'Cheese Sauce', price: '0.50' }),
92
+ makeOption({ id: 42, name: 'Salt', isDefault: true })
93
+ ])
94
+ ]
95
+ })
96
+ ]),
97
+ makeGroup(50, [makeOption({ id: 51, name: 'Cookie', price: '2.00' })])
98
+ ]);
99
+ const priced = calcPricesKiosk(select(makeOrderItem(meal), { 41: 2 }));
100
+ expect(findOption(priced, 31)).toMatchObject({
101
+ priceDelta: 0,
102
+ userExtra: 0,
103
+ displayDelta: 0,
104
+ hasDefaultSibling: true
105
+ });
106
+ // 1.00 over the default option, plus 2 x 0.50 added inside it
107
+ expect(findOption(priced, 32)).toMatchObject({
108
+ priceDelta: 1,
109
+ userExtra: 1,
110
+ displayDelta: 2,
111
+ hasDefaultSibling: true
112
+ });
113
+ // a group without a default option shows the price of the option
114
+ expect(findOption(priced, 51)).toMatchObject({
115
+ priceDelta: null,
116
+ displayDelta: 2,
117
+ hasDefaultSibling: false
118
+ });
119
+ });
120
+ it('does not add the price deltas to an item that is not a meal', () => {
121
+ const priced = calcPricesKiosk(makeOrderItem(sandwich));
122
+ expect(findOption(priced, 12).displayDelta).toBeUndefined();
123
+ });
124
+ it('prices an item that calcPrices priced before', () => {
125
+ const item = select(makeOrderItem(sandwich), { 12: 3 });
126
+ expect(calcPricesKiosk(calcPrices(item))).toEqual(calcPricesKiosk(item));
127
+ });
128
+ });
129
+ describe('calcNutritionalInfoKiosk', () => {
130
+ it('counts a "Remove" option until it is selected', () => {
131
+ const item = makeOrderItem(sandwich);
132
+ expect(calcNutritionalInfoKiosk(item)).toMatchObject({
133
+ calories: 580,
134
+ sodium: 1160
135
+ });
136
+ expect(calcNutritionalInfoKiosk(select(item, { 11: 1, 12: 1 }))).toMatchObject({ calories: 590, sodium: 1180 });
137
+ // calcNutritionalInfo is not changed
138
+ expect(calcNutritionalInfo(item)).toMatchObject({ calories: 500 });
139
+ });
140
+ });
141
+ describe('kiosk cart functions', () => {
142
+ it('makes an item with its small image and kiosk prices', () => {
143
+ const item = makeOrderItemKiosk(sandwich);
144
+ expect(item.smallImageUrl).toBe('small-1.png');
145
+ expect(item.imageUrl).toBe('large-1.png');
146
+ expect(item.totalCals).toBe(580);
147
+ const [upsell] = makeUpsellItemsKiosk([1], { 1: sandwich });
148
+ expect(upsell).toEqual(item);
149
+ });
150
+ it('rehydrates items with kiosk prices', () => {
151
+ const built = select(makeOrderItemKiosk(sandwich), { 12: 1 });
152
+ const [simpleItem] = makeSimpleCart([Object.assign(Object.assign({}, built), { quantity: 2 })]);
153
+ const item = rehydrateOrderItemKiosk(sandwich, simpleItem);
154
+ expect(item.quantity).toBe(2);
155
+ expect(item.totalPrice).toBe(23);
156
+ expect(item.totalCals).toBe(1340);
157
+ const { cart, cartCounts } = rehydrateCartKiosk([sandwich], [simpleItem]);
158
+ expect(cart).toEqual([Object.assign(Object.assign({}, item), { index: 0 })]);
159
+ expect(cartCounts).toEqual({ 1: 2 });
160
+ });
161
+ it('keeps kiosk prices when the quantity of a cart item changes', () => {
162
+ const item = makeOrderItemKiosk(sandwich);
163
+ let cart = addItemKiosk([], item).cart;
164
+ cart = addItemKiosk([...cart], item).cart;
165
+ expect(cart).toHaveLength(1);
166
+ expect(cart[0].quantity).toBe(2);
167
+ expect(cart[0].totalCals).toBe(1160);
168
+ cart = incrementItemKiosk([...cart], 0).cart;
169
+ expect(cart[0].totalCals).toBe(1740);
170
+ cart = decrementItemKiosk([...cart], 0).cart;
171
+ expect(cart[0].totalCals).toBe(1160);
172
+ expect(cart[0].totalPrice).toBe(20);
173
+ });
174
+ });
@@ -131,3 +131,41 @@ describe('group order checkout payload', () => {
131
131
  expect(payload.cart).toEqual(GROUP_CHECKOUT_CART.slice(1));
132
132
  });
133
133
  });
134
+ describe('existing order payload', () => {
135
+ const orderData = {
136
+ address: null,
137
+ cart: [],
138
+ cartId: null,
139
+ customer: null,
140
+ deposit: null,
141
+ details: null,
142
+ deviceType: null,
143
+ discounts: null,
144
+ orderId: null,
145
+ points: null,
146
+ prepType: null,
147
+ promoCodes: null,
148
+ requestedAt: 'asap',
149
+ revenueCenterId: 3,
150
+ serviceType: 'PICKUP',
151
+ surcharges: null,
152
+ table: null,
153
+ tenders: null,
154
+ tip: null
155
+ };
156
+ it('sends the order uuid together with the order id', () => {
157
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: 'abc-123' }));
158
+ expect(payload.order_id).toBe(42);
159
+ expect(payload.order_uuid).toBe('abc-123');
160
+ });
161
+ it('omits the order uuid without an order id', () => {
162
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderUuid: 'abc-123' }));
163
+ expect(payload).not.toHaveProperty('order_id');
164
+ expect(payload).not.toHaveProperty('order_uuid');
165
+ });
166
+ it('omits the order uuid when it is null', () => {
167
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: null }));
168
+ expect(payload.order_id).toBe(42);
169
+ expect(payload).not.toHaveProperty('order_uuid');
170
+ });
171
+ });
@@ -1,8 +1,10 @@
1
1
  export * from './types';
2
2
  export * from './useAccordion';
3
+ export * from './useAccordionKiosk';
3
4
  export * from './useAddressForm';
4
5
  export * from './useAllergenForm';
5
6
  export * from './useBuilder';
7
+ export * from './useBuilderKiosk';
6
8
  export * from './useCartGuestForm';
7
9
  export * from './useCartPoints';
8
10
  export * from './useClaimRewardCodeForm';
@@ -1,8 +1,10 @@
1
1
  export * from './types';
2
2
  export * from './useAccordion';
3
+ export * from './useAccordionKiosk';
3
4
  export * from './useAddressForm';
4
5
  export * from './useAllergenForm';
5
6
  export * from './useBuilder';
7
+ export * from './useBuilderKiosk';
6
8
  export * from './useCartGuestForm';
7
9
  export * from './useCartPoints';
8
10
  export * from './useClaimRewardCodeForm';
@@ -0,0 +1,22 @@
1
+ /// <reference types="react" />
2
+ import { CartItem, CartLevels, ConfigAccordion } from '@open-tender/types';
3
+ export declare const useAccordionKiosk: (builtItem: CartItem, toggleOption: (levels: CartLevels) => void, cartId?: number | null, config?: ConfigAccordion, setImageUrl?: ((imageUrl: string | null) => void) | undefined) => {
4
+ open: string | null;
5
+ setOpen: import("react").Dispatch<import("react").SetStateAction<string | null>>;
6
+ groups: import("@open-tender/types").CartItemGroups;
7
+ ingredients: string | null;
8
+ totalCals: number | null;
9
+ madeFor: string | null;
10
+ notes: string | null;
11
+ hasMadeFor: boolean;
12
+ hasNotes: boolean;
13
+ hasInstructions: boolean;
14
+ hasCals: boolean;
15
+ nutritionalInfo: import("@open-tender/types").NutritionalInfo | null;
16
+ hasIngredients: boolean;
17
+ instructionsTitle: string;
18
+ sizeGroup: import("@open-tender/types").CartItemGroup | null | undefined;
19
+ selectedSize: import("@open-tender/types").CartItemOption | null | undefined;
20
+ showAccordion: boolean | import("@open-tender/types").CartItemGroup;
21
+ toggleSize: (optionId: number) => void;
22
+ };
@@ -0,0 +1,61 @@
1
+ import { useState } from 'react';
2
+ import { calcNutritionalInfoKiosk } from '../utils';
3
+ // The kiosk variant of useAccordion, left as it is for the other apps:
4
+ // - the nutritional info comes from calcNutritionalInfoKiosk
5
+ // - setImageUrl, when given, gets the image of the size the guest picks
6
+ export const useAccordionKiosk = (builtItem, toggleOption, cartId, config, setImageUrl) => {
7
+ var _a;
8
+ const { hasCals: showCals = false, hasCustomize = false, hasMadeFor: showMadeFor = true, hasNotes: showNotes = true } = config || {};
9
+ const [open, setOpen] = useState(null);
10
+ const { groups, ingredients, totalCals, madeFor, notes } = builtItem;
11
+ const hasMadeFor = showMadeFor && !cartId ? true : false;
12
+ const hasNotes = showNotes ? true : false;
13
+ const hasInstructions = hasMadeFor || hasNotes;
14
+ const hasCals = showCals && totalCals ? true : false;
15
+ const nutritionalInfo = hasCals ? calcNutritionalInfoKiosk(builtItem) : null;
16
+ const hasIngredients = ingredients && ingredients.length > 0 ? true : false;
17
+ const instructionsTitle = hasMadeFor && hasNotes
18
+ ? 'Name / Special Instructions'
19
+ : hasMadeFor
20
+ ? 'Name'
21
+ : hasNotes
22
+ ? 'Special Instructions'
23
+ : '';
24
+ const sizeGroup = groups ? groups.find(i => i.isSize) : null;
25
+ const selectedSize = sizeGroup
26
+ ? (_a = sizeGroup === null || sizeGroup === void 0 ? void 0 : sizeGroup.options) === null || _a === void 0 ? void 0 : _a.find(i => { var _a; return ((_a = i.quantity) !== null && _a !== void 0 ? _a : 0) >= 1; })
27
+ : null;
28
+ const showAccordion = sizeGroup || hasCustomize || hasInstructions || hasIngredients || hasCals;
29
+ const toggleSize = (optionId) => {
30
+ if (sizeGroup) {
31
+ toggleOption([[sizeGroup.id, optionId]]);
32
+ if (setImageUrl) {
33
+ const selectedOption = sizeGroup.options.find(i => i.id === optionId);
34
+ if (selectedOption && selectedOption.imageUrl) {
35
+ setImageUrl(selectedOption.imageUrl);
36
+ }
37
+ }
38
+ setOpen(null);
39
+ }
40
+ };
41
+ return {
42
+ open,
43
+ setOpen,
44
+ groups,
45
+ ingredients,
46
+ totalCals,
47
+ madeFor,
48
+ notes,
49
+ hasMadeFor,
50
+ hasNotes,
51
+ hasInstructions,
52
+ hasCals,
53
+ nutritionalInfo,
54
+ hasIngredients,
55
+ instructionsTitle,
56
+ sizeGroup,
57
+ selectedSize,
58
+ showAccordion,
59
+ toggleSize
60
+ };
61
+ };
@@ -0,0 +1,16 @@
1
+ import { CartItem, CartItemGroups, CartLevels } from '@open-tender/types';
2
+ export declare const useBuilderKiosk: (orderItem: CartItem, orderTimeoutCallback?: () => void, quantityLimit?: number | null, onQuantityLimit?: () => void) => {
3
+ item: CartItem;
4
+ increment: () => void;
5
+ decrement: () => void;
6
+ setQuantity: (quantity: number) => void;
7
+ setMadeFor: (madeFor: string) => void;
8
+ setNotes: (notes: string) => void;
9
+ setMadeForNotes: (madeFor: string, notes: string) => void;
10
+ toggleOption: (levels: CartLevels) => CartItem;
11
+ incrementOption: (levels: CartLevels) => CartItem;
12
+ decrementOption: (levels: CartLevels) => CartItem;
13
+ setOptionQuantity: (levels: CartLevels, quantity: number) => CartItem;
14
+ setGroups: (groups: CartItemGroups) => void;
15
+ setImageUrl: (imageUrl: string | null) => void;
16
+ };