@open-tender/utils 0.5.73 → 0.5.74
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/__tests__/utils/cart.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/cart.test.js +469 -0
- package/dist/cjs/utils/cart.d.ts +24 -2
- package/dist/cjs/utils/cart.js +133 -8
- package/dist/esm/__tests__/utils/cart.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/cart.test.js +467 -0
- package/dist/esm/utils/cart.d.ts +24 -2
- package/dist/esm/utils/cart.js +131 -7
- package/package.json +3 -3
package/dist/cjs/utils/cart.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeDeviceType = exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.compareCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItemsForCart = exports.makeUpsellItemIds = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNamesForGroups = exports.makeModifierNames = exports.getItemOptions = void 0;
|
|
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.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
4
|
const types_1 = require("@open-tender/types");
|
|
5
5
|
const helpers_1 = require("./helpers");
|
|
6
6
|
const favorites_1 = require("./favorites");
|
|
@@ -346,6 +346,7 @@ const makeSimpleGroupsLookup = (groups) => {
|
|
|
346
346
|
return groups.reduce((grpObj, g) => {
|
|
347
347
|
const options = g.options.reduce((optObj, o) => {
|
|
348
348
|
optObj[o.id] = {
|
|
349
|
+
name: o.name,
|
|
349
350
|
quantity: o.quantity,
|
|
350
351
|
pos_ext_id: o.pos_ext_id || null,
|
|
351
352
|
groupsLookup: makeSimpleGroupsLookup(o.groups)
|
|
@@ -384,18 +385,114 @@ const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = [], requestedAt
|
|
|
384
385
|
return pricedItem;
|
|
385
386
|
};
|
|
386
387
|
exports.rehydrateOrderItem = rehydrateOrderItem;
|
|
387
|
-
|
|
388
|
+
// A saved option is the same as a current option when they have the same id
|
|
389
|
+
// and group, unless both carry a Checkmate id and the ids differ.
|
|
390
|
+
const isSameReorderOption = (simpleOption, option) => {
|
|
391
|
+
if (!simpleOption.pos_ext_id || !option.pos_ext_id)
|
|
392
|
+
return true;
|
|
393
|
+
return simpleOption.pos_ext_id === option.pos_ext_id;
|
|
394
|
+
};
|
|
395
|
+
// Reorder only. Saved options are matched to the options of the current menu
|
|
396
|
+
// item by group and id first. The remaining ones are matched by their
|
|
397
|
+
// Checkmate id alone, which is stable when an option, its group or its
|
|
398
|
+
// category is renamed or moved in Checkmate and gets a new id.
|
|
399
|
+
// Saved options that are not found are added to `unavailable`, and the
|
|
400
|
+
// caller drops the whole item.
|
|
401
|
+
const rehydrateReorderGroups = (groups, groupsLookup, unavailable) => {
|
|
402
|
+
if (!groupsLookup)
|
|
403
|
+
return groups;
|
|
404
|
+
const matches = new Map();
|
|
405
|
+
const used = new Set();
|
|
406
|
+
groups = groups || [];
|
|
407
|
+
groups.forEach(group => {
|
|
408
|
+
const simpleGroup = groupsLookup[group.id];
|
|
409
|
+
if (!simpleGroup)
|
|
410
|
+
return;
|
|
411
|
+
(group.options || []).forEach(option => {
|
|
412
|
+
const simpleOption = simpleGroup.options[option.id];
|
|
413
|
+
if (simpleOption && isSameReorderOption(simpleOption, option)) {
|
|
414
|
+
matches.set(option, simpleOption);
|
|
415
|
+
used.add(simpleOption);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
const simpleOptions = Object.values(groupsLookup).flatMap(simpleGroup => Object.values(simpleGroup.options));
|
|
420
|
+
groups.forEach(group => {
|
|
421
|
+
;
|
|
422
|
+
(group.options || []).forEach(option => {
|
|
423
|
+
if (matches.has(option) || !option.pos_ext_id)
|
|
424
|
+
return;
|
|
425
|
+
const simpleOption = simpleOptions.find(i => !used.has(i) && i.pos_ext_id === option.pos_ext_id);
|
|
426
|
+
if (simpleOption) {
|
|
427
|
+
matches.set(option, simpleOption);
|
|
428
|
+
used.add(simpleOption);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
matches.forEach((simpleOption, option) => {
|
|
433
|
+
option.quantity = simpleOption.quantity;
|
|
434
|
+
option.groups = rehydrateReorderGroups(option.groups, simpleOption.groupsLookup, unavailable);
|
|
435
|
+
});
|
|
436
|
+
simpleOptions
|
|
437
|
+
.filter(i => !used.has(i))
|
|
438
|
+
.forEach(i => unavailable.push({ name: i.name || '' }));
|
|
439
|
+
return groups;
|
|
440
|
+
};
|
|
441
|
+
const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
|
|
442
|
+
const orderItem = (0, exports.makeOrderItem)(menuItem, true, soldOut, simpleCartItem, undefined, null);
|
|
443
|
+
orderItem.quantity = simpleCartItem.quantity || 1;
|
|
444
|
+
const unavailable = [];
|
|
445
|
+
const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
|
|
446
|
+
if (groupsLookup) {
|
|
447
|
+
orderItem.groups = rehydrateReorderGroups(orderItem.groups, groupsLookup, unavailable);
|
|
448
|
+
}
|
|
449
|
+
const pricedItem = (0, exports.calcPrices)(orderItem);
|
|
450
|
+
return { item: pricedItem, unavailable };
|
|
451
|
+
};
|
|
452
|
+
// Reorder only. The Checkmate id of an item survives renames and category
|
|
453
|
+
// moves, which change the id of the item on the menu, so it is tried before
|
|
454
|
+
// the id.
|
|
455
|
+
const findReorderMenuItem = (menuItems, simpleCartItem) => {
|
|
456
|
+
const { id, pos_ext_id } = simpleCartItem;
|
|
457
|
+
const items = menuItems || [];
|
|
458
|
+
if (pos_ext_id) {
|
|
459
|
+
const matches = items.filter(i => i.pos_ext_id === pos_ext_id);
|
|
460
|
+
if (matches.length)
|
|
461
|
+
return matches.find(i => i.id === id) || matches[0];
|
|
462
|
+
}
|
|
463
|
+
return items.find(i => i.id === id);
|
|
464
|
+
};
|
|
465
|
+
// With `isReorder`, an item is dropped and reported in `unavailable` when it
|
|
466
|
+
// or any of its saved options is gone, or a required group is left unfilled,
|
|
467
|
+
// so the guest never gets something different from what they ordered.
|
|
468
|
+
// Without it, items that are not on the menu are skipped and everything else
|
|
469
|
+
// is kept, as it is for edited orders and group orders.
|
|
470
|
+
const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
|
|
388
471
|
const orderItems = [];
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
472
|
+
const unavailable = [];
|
|
473
|
+
const lines = options.isReorder ? simpleCartItems || [] : simpleCartItems;
|
|
474
|
+
lines.forEach(item => {
|
|
475
|
+
if (!options.isReorder) {
|
|
476
|
+
const menuItem = menuItems.find(i => i.id === item.id);
|
|
477
|
+
if (menuItem)
|
|
478
|
+
orderItems.push((0, exports.rehydrateOrderItem)(menuItem, item, soldOut));
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
if (!item)
|
|
482
|
+
return;
|
|
483
|
+
const menuItem = findReorderMenuItem(menuItems, item);
|
|
484
|
+
const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
|
|
485
|
+
if (!rehydrated ||
|
|
486
|
+
rehydrated.unavailable.length ||
|
|
487
|
+
(0, exports.hasGroupsBelowMin)(null, rehydrated.item.groups)) {
|
|
488
|
+
unavailable.push({ name: item.name || '' });
|
|
489
|
+
return;
|
|
394
490
|
}
|
|
491
|
+
orderItems.push(rehydrated.item);
|
|
395
492
|
});
|
|
396
493
|
const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
|
|
397
494
|
const cartCounts = (0, exports.calcCartCounts)(cart);
|
|
398
|
-
return { cart, cartCounts };
|
|
495
|
+
return { cart, cartCounts, unavailable };
|
|
399
496
|
};
|
|
400
497
|
exports.rehydrateCart = rehydrateCart;
|
|
401
498
|
// TODO: maybe refactor
|
|
@@ -693,6 +790,34 @@ const makeSimpleCart = (cart) => {
|
|
|
693
790
|
return simpleCart;
|
|
694
791
|
};
|
|
695
792
|
exports.makeSimpleCart = makeSimpleCart;
|
|
793
|
+
const makeReorderGroups = (groups) => {
|
|
794
|
+
return (groups || []).map(g => {
|
|
795
|
+
const options = (g.options || [])
|
|
796
|
+
.filter(o => o.quantity !== 0)
|
|
797
|
+
.map(o => ({
|
|
798
|
+
id: o.id,
|
|
799
|
+
name: o.name,
|
|
800
|
+
pos_ext_id: o.pos_ext_id || null,
|
|
801
|
+
quantity: o.quantity,
|
|
802
|
+
groups: makeReorderGroups(o.groups || [])
|
|
803
|
+
}));
|
|
804
|
+
return { id: g.id, options: options };
|
|
805
|
+
});
|
|
806
|
+
};
|
|
807
|
+
// Same as makeSimpleCart, but keeps the names and Checkmate ids needed to find
|
|
808
|
+
// the items of a past order on the current menu and to name those that are gone.
|
|
809
|
+
const makeReorderCart = (cart) => {
|
|
810
|
+
return (cart || []).map(i => ({
|
|
811
|
+
id: i.id,
|
|
812
|
+
name: i.name,
|
|
813
|
+
pos_ext_id: i.pos_ext_id || null,
|
|
814
|
+
quantity: i.quantity,
|
|
815
|
+
groups: makeReorderGroups(i.groups),
|
|
816
|
+
made_for: i.madeFor || i.made_for || '',
|
|
817
|
+
notes: i.notes || ''
|
|
818
|
+
}));
|
|
819
|
+
};
|
|
820
|
+
exports.makeReorderCart = makeReorderCart;
|
|
696
821
|
/* cart validation */
|
|
697
822
|
// TODO: refactor for nested modifiers
|
|
698
823
|
// convert menu items to order items for validation but only include
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import { makeReorderCart, rehydrateCart, rehydrateOrderItem } from '../../utils';
|
|
2
|
+
const makeOption = (id, posExtId, optionGroups = []) => ({
|
|
3
|
+
id,
|
|
4
|
+
name: `option ${id}`,
|
|
5
|
+
pos_ext_id: posExtId,
|
|
6
|
+
price: '0.00',
|
|
7
|
+
allergens: '',
|
|
8
|
+
tags: '',
|
|
9
|
+
opt_is_default: false,
|
|
10
|
+
min_quantity: 0,
|
|
11
|
+
max_quantity: 10,
|
|
12
|
+
increment: 1,
|
|
13
|
+
suspend_until: null,
|
|
14
|
+
nutritional_info: null,
|
|
15
|
+
option_groups: optionGroups
|
|
16
|
+
});
|
|
17
|
+
const makeGroup = (id, options, minOptions = 0) => ({
|
|
18
|
+
id,
|
|
19
|
+
name: `group ${id}`,
|
|
20
|
+
included: 0,
|
|
21
|
+
min_options: minOptions,
|
|
22
|
+
option_items: options
|
|
23
|
+
});
|
|
24
|
+
const makeMenuItem = (id, posExtId, groups = []) => ({
|
|
25
|
+
id,
|
|
26
|
+
name: `item ${id}`,
|
|
27
|
+
pos_ext_id: posExtId,
|
|
28
|
+
price: '10.00',
|
|
29
|
+
allergens: '',
|
|
30
|
+
tags: '',
|
|
31
|
+
increment: 1,
|
|
32
|
+
min_quantity: 1,
|
|
33
|
+
max_quantity: 10,
|
|
34
|
+
suspend_until: null,
|
|
35
|
+
nutritional_info: null,
|
|
36
|
+
option_groups: groups
|
|
37
|
+
});
|
|
38
|
+
const makeSimpleItem = (id, posExtId, groups = []) => ({
|
|
39
|
+
id,
|
|
40
|
+
name: `old item ${id}`,
|
|
41
|
+
pos_ext_id: posExtId,
|
|
42
|
+
quantity: 2,
|
|
43
|
+
groups
|
|
44
|
+
});
|
|
45
|
+
const reorderCart = (menuItems, simpleCart) => rehydrateCart(menuItems, simpleCart, [], { isReorder: true });
|
|
46
|
+
describe('rehydrateCart with isReorder', () => {
|
|
47
|
+
it('matches an item by id when the order has no Checkmate id', () => {
|
|
48
|
+
const menuItems = [makeMenuItem(10, null)];
|
|
49
|
+
const { cart, unavailable } = reorderCart(menuItems, [
|
|
50
|
+
makeSimpleItem(10, null)
|
|
51
|
+
]);
|
|
52
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
53
|
+
expect(cart[0].quantity).toBe(2);
|
|
54
|
+
expect(unavailable).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
it('finds a renamed item by its Checkmate id', () => {
|
|
57
|
+
const menuItems = [makeMenuItem(99, 'cm-item')];
|
|
58
|
+
const { cart, unavailable } = reorderCart(menuItems, [
|
|
59
|
+
makeSimpleItem(10, 'cm-item')
|
|
60
|
+
]);
|
|
61
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
62
|
+
expect(unavailable).toEqual([]);
|
|
63
|
+
});
|
|
64
|
+
it('prefers the old id when the same Checkmate item is on the menu twice', () => {
|
|
65
|
+
const menuItems = [
|
|
66
|
+
makeMenuItem(99, 'cm-item'),
|
|
67
|
+
makeMenuItem(120, 'cm-item')
|
|
68
|
+
];
|
|
69
|
+
const { cart } = reorderCart(menuItems, [makeSimpleItem(120, 'cm-item')]);
|
|
70
|
+
expect(cart.map(i => i.id)).toEqual([120]);
|
|
71
|
+
});
|
|
72
|
+
it('uses the first match when the old id is not on the menu any more', () => {
|
|
73
|
+
const menuItems = [
|
|
74
|
+
makeMenuItem(99, 'cm-item'),
|
|
75
|
+
makeMenuItem(120, 'cm-item')
|
|
76
|
+
];
|
|
77
|
+
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
78
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
79
|
+
});
|
|
80
|
+
it('falls back to the id when the Checkmate id is not on the menu', () => {
|
|
81
|
+
const menuItems = [makeMenuItem(10, 'cm-other')];
|
|
82
|
+
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
83
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
84
|
+
});
|
|
85
|
+
it('reports items that are no longer on the menu and keeps the others', () => {
|
|
86
|
+
const menuItems = [makeMenuItem(10, 'cm-a')];
|
|
87
|
+
const { cart, unavailable } = reorderCart(menuItems, [
|
|
88
|
+
makeSimpleItem(10, 'cm-a'),
|
|
89
|
+
makeSimpleItem(11, 'cm-gone')
|
|
90
|
+
]);
|
|
91
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
92
|
+
expect(unavailable).toEqual([{ name: 'old item 11' }]);
|
|
93
|
+
});
|
|
94
|
+
it('restores an option after its group and id changed', () => {
|
|
95
|
+
const menuItems = [
|
|
96
|
+
makeMenuItem(99, 'cm-item', [makeGroup(7, [makeOption(55, 'cm-mod')])])
|
|
97
|
+
];
|
|
98
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
99
|
+
{
|
|
100
|
+
id: 5,
|
|
101
|
+
options: [
|
|
102
|
+
{
|
|
103
|
+
id: 20,
|
|
104
|
+
name: 'Cheese',
|
|
105
|
+
pos_ext_id: 'cm-mod',
|
|
106
|
+
quantity: 3,
|
|
107
|
+
groups: []
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
]);
|
|
112
|
+
const { cart, unavailable } = reorderCart(menuItems, [simple]);
|
|
113
|
+
const option = cart[0].groups[0].options[0];
|
|
114
|
+
expect(option.id).toBe(55);
|
|
115
|
+
expect(option.quantity).toBe(3);
|
|
116
|
+
expect(unavailable).toEqual([]);
|
|
117
|
+
});
|
|
118
|
+
it('restores nested options by their Checkmate id', () => {
|
|
119
|
+
const nestedGroup = makeGroup(8, [makeOption(77, 'cm-nested')]);
|
|
120
|
+
const menuItems = [
|
|
121
|
+
makeMenuItem(99, 'cm-item', [
|
|
122
|
+
makeGroup(7, [makeOption(55, 'cm-mod', [nestedGroup])])
|
|
123
|
+
])
|
|
124
|
+
];
|
|
125
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
126
|
+
{
|
|
127
|
+
id: 5,
|
|
128
|
+
options: [
|
|
129
|
+
{
|
|
130
|
+
id: 20,
|
|
131
|
+
pos_ext_id: 'cm-mod',
|
|
132
|
+
quantity: 1,
|
|
133
|
+
groups: [
|
|
134
|
+
{
|
|
135
|
+
id: 6,
|
|
136
|
+
options: [
|
|
137
|
+
{ id: 30, pos_ext_id: 'cm-nested', quantity: 2, groups: [] }
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
]);
|
|
145
|
+
const { cart } = reorderCart(menuItems, [simple]);
|
|
146
|
+
const nested = cart[0].groups[0].options[0].groups[0].options[0];
|
|
147
|
+
expect(nested.id).toBe(77);
|
|
148
|
+
expect(nested.quantity).toBe(2);
|
|
149
|
+
});
|
|
150
|
+
it('uses each saved option once when a modifier is in two current groups', () => {
|
|
151
|
+
const menuItems = [
|
|
152
|
+
makeMenuItem(99, 'cm-item', [
|
|
153
|
+
makeGroup(7, [makeOption(55, 'cm-mod')]),
|
|
154
|
+
makeGroup(8, [makeOption(66, 'cm-mod')])
|
|
155
|
+
])
|
|
156
|
+
];
|
|
157
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
158
|
+
{
|
|
159
|
+
id: 5,
|
|
160
|
+
options: [{ id: 20, pos_ext_id: 'cm-mod', quantity: 1, groups: [] }]
|
|
161
|
+
}
|
|
162
|
+
]);
|
|
163
|
+
const { cart } = reorderCart(menuItems, [simple]);
|
|
164
|
+
const quantities = cart[0].groups.map(g => g.options[0].quantity);
|
|
165
|
+
expect(quantities.sort()).toEqual([0, 1]);
|
|
166
|
+
});
|
|
167
|
+
it('keeps matching options by group and id when there is no Checkmate id', () => {
|
|
168
|
+
const menuItems = [
|
|
169
|
+
makeMenuItem(10, null, [makeGroup(5, [makeOption(20, null)])])
|
|
170
|
+
];
|
|
171
|
+
const simple = makeSimpleItem(10, null, [
|
|
172
|
+
{ id: 5, options: [{ id: 20, quantity: 2, groups: [] }] }
|
|
173
|
+
]);
|
|
174
|
+
const { cart, unavailable } = reorderCart(menuItems, [simple]);
|
|
175
|
+
expect(cart[0].groups[0].options[0].quantity).toBe(2);
|
|
176
|
+
expect(unavailable).toEqual([]);
|
|
177
|
+
});
|
|
178
|
+
it('drops an item when one of its options is no longer on the item', () => {
|
|
179
|
+
const menuItems = [
|
|
180
|
+
makeMenuItem(99, 'cm-item', [makeGroup(7, [makeOption(55, 'cm-other')])])
|
|
181
|
+
];
|
|
182
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
183
|
+
{
|
|
184
|
+
id: 5,
|
|
185
|
+
options: [
|
|
186
|
+
{
|
|
187
|
+
id: 20,
|
|
188
|
+
name: 'Old Topping',
|
|
189
|
+
pos_ext_id: 'cm-gone',
|
|
190
|
+
quantity: 1,
|
|
191
|
+
groups: []
|
|
192
|
+
}
|
|
193
|
+
]
|
|
194
|
+
}
|
|
195
|
+
]);
|
|
196
|
+
const { cart, unavailable } = reorderCart(menuItems, [simple]);
|
|
197
|
+
expect(cart).toEqual([]);
|
|
198
|
+
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
199
|
+
});
|
|
200
|
+
it('drops an item whose whole option group is gone and keeps the others', () => {
|
|
201
|
+
const menuItems = [
|
|
202
|
+
makeMenuItem(99, 'cm-item', [makeGroup(7, [makeOption(55, 'cm-kept')])]),
|
|
203
|
+
makeMenuItem(98, 'cm-plain')
|
|
204
|
+
];
|
|
205
|
+
const withGroup = makeSimpleItem(10, 'cm-item', [
|
|
206
|
+
{
|
|
207
|
+
id: 5,
|
|
208
|
+
options: [
|
|
209
|
+
{ id: 21, pos_ext_id: 'cm-kept', quantity: 1, groups: [] },
|
|
210
|
+
{ id: 22, pos_ext_id: 'cm-gone', quantity: 1, groups: [] }
|
|
211
|
+
]
|
|
212
|
+
}
|
|
213
|
+
]);
|
|
214
|
+
const { cart, unavailable } = reorderCart(menuItems, [
|
|
215
|
+
withGroup,
|
|
216
|
+
makeSimpleItem(11, 'cm-plain')
|
|
217
|
+
]);
|
|
218
|
+
expect(cart.map(i => i.id)).toEqual([98]);
|
|
219
|
+
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
220
|
+
});
|
|
221
|
+
it('drops an item with a required group the past order did not fill', () => {
|
|
222
|
+
const menuItems = [
|
|
223
|
+
makeMenuItem(99, 'cm-item', [
|
|
224
|
+
makeGroup(7, [makeOption(55, 'cm-new')], 1)
|
|
225
|
+
]),
|
|
226
|
+
makeMenuItem(98, 'cm-plain')
|
|
227
|
+
];
|
|
228
|
+
const { cart, unavailable } = reorderCart(menuItems, [
|
|
229
|
+
makeSimpleItem(10, 'cm-item'),
|
|
230
|
+
makeSimpleItem(11, 'cm-plain')
|
|
231
|
+
]);
|
|
232
|
+
expect(cart.map(i => i.id)).toEqual([98]);
|
|
233
|
+
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
234
|
+
});
|
|
235
|
+
it('keeps an item whose required group is filled by the past order', () => {
|
|
236
|
+
const menuItems = [
|
|
237
|
+
makeMenuItem(99, 'cm-item', [makeGroup(7, [makeOption(55, 'cm-opt')], 1)])
|
|
238
|
+
];
|
|
239
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
240
|
+
{
|
|
241
|
+
id: 7,
|
|
242
|
+
options: [{ id: 55, pos_ext_id: 'cm-opt', quantity: 1, groups: [] }]
|
|
243
|
+
}
|
|
244
|
+
]);
|
|
245
|
+
const { cart, unavailable } = reorderCart(menuItems, [simple]);
|
|
246
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
247
|
+
expect(unavailable).toEqual([]);
|
|
248
|
+
});
|
|
249
|
+
it('drops an item when a nested option is gone', () => {
|
|
250
|
+
const menuItems = [
|
|
251
|
+
makeMenuItem(99, 'cm-item', [
|
|
252
|
+
makeGroup(7, [
|
|
253
|
+
makeOption(55, 'cm-parent', [
|
|
254
|
+
makeGroup(8, [makeOption(66, 'cm-other')])
|
|
255
|
+
])
|
|
256
|
+
])
|
|
257
|
+
])
|
|
258
|
+
];
|
|
259
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
260
|
+
{
|
|
261
|
+
id: 7,
|
|
262
|
+
options: [
|
|
263
|
+
{
|
|
264
|
+
id: 55,
|
|
265
|
+
pos_ext_id: 'cm-parent',
|
|
266
|
+
quantity: 1,
|
|
267
|
+
groups: [
|
|
268
|
+
{
|
|
269
|
+
id: 8,
|
|
270
|
+
options: [
|
|
271
|
+
{ id: 67, pos_ext_id: 'cm-gone', quantity: 1, groups: [] }
|
|
272
|
+
]
|
|
273
|
+
}
|
|
274
|
+
]
|
|
275
|
+
}
|
|
276
|
+
]
|
|
277
|
+
}
|
|
278
|
+
]);
|
|
279
|
+
const { cart, unavailable } = reorderCart(menuItems, [simple]);
|
|
280
|
+
expect(cart).toEqual([]);
|
|
281
|
+
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
describe('rehydrateCart without isReorder', () => {
|
|
285
|
+
it('matches by id only and ignores the Checkmate id', () => {
|
|
286
|
+
const menuItems = [
|
|
287
|
+
makeMenuItem(99, 'cm-item'),
|
|
288
|
+
makeMenuItem(10, 'cm-other')
|
|
289
|
+
];
|
|
290
|
+
const { cart, unavailable } = rehydrateCart(menuItems, [
|
|
291
|
+
makeSimpleItem(10, 'cm-item'),
|
|
292
|
+
makeSimpleItem(11, 'cm-item')
|
|
293
|
+
]);
|
|
294
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
295
|
+
expect(unavailable).toEqual([]);
|
|
296
|
+
});
|
|
297
|
+
it('keeps an item when one of its options is gone and leaves the defaults', () => {
|
|
298
|
+
const menuItems = [
|
|
299
|
+
makeMenuItem(10, 'cm-item', [makeGroup(7, [makeOption(55, 'cm-other')])])
|
|
300
|
+
];
|
|
301
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
302
|
+
{
|
|
303
|
+
id: 7,
|
|
304
|
+
options: [{ id: 20, pos_ext_id: 'cm-gone', quantity: 1, groups: [] }]
|
|
305
|
+
}
|
|
306
|
+
]);
|
|
307
|
+
const { cart, unavailable } = rehydrateCart(menuItems, [simple]);
|
|
308
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
309
|
+
expect(cart[0].groups[0].options[0].quantity).toBe(0);
|
|
310
|
+
expect(unavailable).toEqual([]);
|
|
311
|
+
});
|
|
312
|
+
it('keeps an item whose required group the saved cart did not fill', () => {
|
|
313
|
+
const menuItems = [
|
|
314
|
+
makeMenuItem(10, null, [makeGroup(7, [makeOption(55, null)], 1)])
|
|
315
|
+
];
|
|
316
|
+
const { cart } = rehydrateCart(menuItems, [makeSimpleItem(10, null)]);
|
|
317
|
+
expect(cart.map(i => i.id)).toEqual([10]);
|
|
318
|
+
});
|
|
319
|
+
it('does not move a saved option into another group by its Checkmate id', () => {
|
|
320
|
+
const menuItems = [
|
|
321
|
+
makeMenuItem(10, null, [
|
|
322
|
+
makeGroup(7, [makeOption(55, 'cm-mod')]),
|
|
323
|
+
makeGroup(8, [makeOption(66, 'cm-mod')])
|
|
324
|
+
])
|
|
325
|
+
];
|
|
326
|
+
const simple = makeSimpleItem(10, null, [
|
|
327
|
+
{
|
|
328
|
+
id: 5,
|
|
329
|
+
options: [{ id: 20, pos_ext_id: 'cm-mod', quantity: 1, groups: [] }]
|
|
330
|
+
}
|
|
331
|
+
]);
|
|
332
|
+
const { cart } = rehydrateCart(menuItems, [simple]);
|
|
333
|
+
const quantities = cart[0].groups.map(g => g.options[0].quantity);
|
|
334
|
+
expect(quantities).toEqual([0, 0]);
|
|
335
|
+
});
|
|
336
|
+
it('matches options by group and id like before', () => {
|
|
337
|
+
const menuItems = [
|
|
338
|
+
makeMenuItem(10, 'cm-item', [makeGroup(5, [makeOption(20, 'cm-mod')])])
|
|
339
|
+
];
|
|
340
|
+
const simple = makeSimpleItem(10, 'cm-item', [
|
|
341
|
+
{
|
|
342
|
+
id: 5,
|
|
343
|
+
options: [{ id: 20, pos_ext_id: 'cm-mod', quantity: 2, groups: [] }]
|
|
344
|
+
}
|
|
345
|
+
]);
|
|
346
|
+
const { cart } = rehydrateCart(menuItems, [simple]);
|
|
347
|
+
expect(cart[0].groups[0].options[0].quantity).toBe(2);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
describe('rehydrateOrderItem', () => {
|
|
351
|
+
it('returns the priced item only', () => {
|
|
352
|
+
const item = rehydrateOrderItem(makeMenuItem(10, null), makeSimpleItem(10, null));
|
|
353
|
+
expect(item.id).toBe(10);
|
|
354
|
+
expect(item.quantity).toBe(2);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
describe('makeReorderCart', () => {
|
|
358
|
+
it('keeps names and Checkmate ids and drops options with zero quantity', () => {
|
|
359
|
+
const orderCart = [
|
|
360
|
+
{
|
|
361
|
+
id: 10,
|
|
362
|
+
name: 'Pizza',
|
|
363
|
+
pos_ext_id: 'cm-item',
|
|
364
|
+
quantity: 1,
|
|
365
|
+
madeFor: 'Sam',
|
|
366
|
+
notes: 'extra crispy',
|
|
367
|
+
groups: [
|
|
368
|
+
{
|
|
369
|
+
id: 5,
|
|
370
|
+
options: [
|
|
371
|
+
{
|
|
372
|
+
id: 20,
|
|
373
|
+
name: 'Cheese',
|
|
374
|
+
pos_ext_id: 'cm-mod',
|
|
375
|
+
quantity: 2,
|
|
376
|
+
groups: []
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
id: 21,
|
|
380
|
+
name: 'Olives',
|
|
381
|
+
pos_ext_id: 'cm-olive',
|
|
382
|
+
quantity: 0,
|
|
383
|
+
groups: []
|
|
384
|
+
}
|
|
385
|
+
]
|
|
386
|
+
}
|
|
387
|
+
]
|
|
388
|
+
}
|
|
389
|
+
];
|
|
390
|
+
const cart = makeReorderCart(orderCart);
|
|
391
|
+
expect(cart).toEqual([
|
|
392
|
+
{
|
|
393
|
+
id: 10,
|
|
394
|
+
name: 'Pizza',
|
|
395
|
+
pos_ext_id: 'cm-item',
|
|
396
|
+
quantity: 1,
|
|
397
|
+
made_for: 'Sam',
|
|
398
|
+
notes: 'extra crispy',
|
|
399
|
+
groups: [
|
|
400
|
+
{
|
|
401
|
+
id: 5,
|
|
402
|
+
options: [
|
|
403
|
+
{
|
|
404
|
+
id: 20,
|
|
405
|
+
name: 'Cheese',
|
|
406
|
+
pos_ext_id: 'cm-mod',
|
|
407
|
+
quantity: 2,
|
|
408
|
+
groups: []
|
|
409
|
+
}
|
|
410
|
+
]
|
|
411
|
+
}
|
|
412
|
+
]
|
|
413
|
+
}
|
|
414
|
+
]);
|
|
415
|
+
});
|
|
416
|
+
it('uses null for a missing Checkmate id', () => {
|
|
417
|
+
const cart = makeReorderCart([
|
|
418
|
+
{ id: 10, quantity: 1, groups: [] }
|
|
419
|
+
]);
|
|
420
|
+
expect(cart[0].pos_ext_id).toBeNull();
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
describe('reorder null safety', () => {
|
|
424
|
+
it('makeReorderCart tolerates a missing cart, groups and options', () => {
|
|
425
|
+
expect(makeReorderCart(null)).toEqual([]);
|
|
426
|
+
const cart = [
|
|
427
|
+
{ id: 1, quantity: 1, groups: undefined },
|
|
428
|
+
{ id: 2, quantity: 1, groups: [{ id: 5, options: undefined }] }
|
|
429
|
+
];
|
|
430
|
+
const result = makeReorderCart(cart);
|
|
431
|
+
expect(result.map(i => i.groups)).toEqual([[], [{ id: 5, options: [] }]]);
|
|
432
|
+
});
|
|
433
|
+
it('rehydrateCart with isReorder reports every item when the menu is null', () => {
|
|
434
|
+
const items = [makeSimpleItem(1, 'a'), makeSimpleItem(2, null)];
|
|
435
|
+
const { cart, unavailable } = rehydrateCart(null, items, [], {
|
|
436
|
+
isReorder: true
|
|
437
|
+
});
|
|
438
|
+
expect(cart).toEqual([]);
|
|
439
|
+
expect(unavailable.map(i => i.name)).toEqual(['old item 1', 'old item 2']);
|
|
440
|
+
});
|
|
441
|
+
it('rehydrateCart with isReorder returns an empty cart for null items', () => {
|
|
442
|
+
const result = rehydrateCart([makeMenuItem(1, 'a')], null, [], {
|
|
443
|
+
isReorder: true
|
|
444
|
+
});
|
|
445
|
+
expect(result.cart).toEqual([]);
|
|
446
|
+
expect(result.unavailable).toEqual([]);
|
|
447
|
+
});
|
|
448
|
+
it('rehydrateCart with isReorder skips a null line and keeps the others', () => {
|
|
449
|
+
const items = [null, makeSimpleItem(1, 'a')];
|
|
450
|
+
const result = rehydrateCart([makeMenuItem(1, 'a')], items, [], {
|
|
451
|
+
isReorder: true
|
|
452
|
+
});
|
|
453
|
+
expect(result.cart).toHaveLength(1);
|
|
454
|
+
expect(result.unavailable).toEqual([]);
|
|
455
|
+
});
|
|
456
|
+
it('rehydrateCart with isReorder tolerates a menu item with no option groups', () => {
|
|
457
|
+
const menuItem = Object.assign(Object.assign({}, makeMenuItem(1, 'a')), { option_groups: undefined });
|
|
458
|
+
const saved = makeSimpleItem(1, 'a', [
|
|
459
|
+
{ id: 9, options: [{ id: 3, quantity: 1, pos_ext_id: 'x', groups: [] }] }
|
|
460
|
+
]);
|
|
461
|
+
const result = rehydrateCart([menuItem], [saved], [], {
|
|
462
|
+
isReorder: true
|
|
463
|
+
});
|
|
464
|
+
expect(result.cart).toEqual([]);
|
|
465
|
+
expect(result.unavailable).toHaveLength(1);
|
|
466
|
+
});
|
|
467
|
+
});
|