@open-tender/utils 0.5.76 → 0.5.77
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.
|
@@ -79,6 +79,31 @@ describe('rehydrateCart with isReorder', () => {
|
|
|
79
79
|
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
80
80
|
expect(cart.map(i => i.id)).toEqual([99]);
|
|
81
81
|
});
|
|
82
|
+
it('prefers the copy on the revenue center menu over an earlier copy', () => {
|
|
83
|
+
const menuItems = [
|
|
84
|
+
makeMenuItem(99, 'cm-item'),
|
|
85
|
+
makeMenuItem(120, 'cm-item')
|
|
86
|
+
];
|
|
87
|
+
const { cart, unavailable } = (0, utils_1.rehydrateCart)(menuItems, [makeSimpleItem(10, 'cm-item')], [], { isReorder: true, preferredIds: new Set([120]) });
|
|
88
|
+
expect(cart.map(i => i.id)).toEqual([120]);
|
|
89
|
+
expect(unavailable).toEqual([]);
|
|
90
|
+
});
|
|
91
|
+
it('still prefers the old id over the revenue center menu ids', () => {
|
|
92
|
+
const menuItems = [
|
|
93
|
+
makeMenuItem(99, 'cm-item'),
|
|
94
|
+
makeMenuItem(120, 'cm-item')
|
|
95
|
+
];
|
|
96
|
+
const { cart } = (0, utils_1.rehydrateCart)(menuItems, [makeSimpleItem(99, 'cm-item')], [], { isReorder: true, preferredIds: new Set([120]) });
|
|
97
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
98
|
+
});
|
|
99
|
+
it('uses the first match when no copy is on the revenue center menu', () => {
|
|
100
|
+
const menuItems = [
|
|
101
|
+
makeMenuItem(99, 'cm-item'),
|
|
102
|
+
makeMenuItem(120, 'cm-item')
|
|
103
|
+
];
|
|
104
|
+
const { cart } = (0, utils_1.rehydrateCart)(menuItems, [makeSimpleItem(10, 'cm-item')], [], { isReorder: true, preferredIds: new Set([500]) });
|
|
105
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
106
|
+
});
|
|
82
107
|
it('falls back to the id when the Checkmate id is not on the menu', () => {
|
|
83
108
|
const menuItems = [makeMenuItem(10, 'cm-other')];
|
|
84
109
|
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
@@ -283,6 +308,21 @@ describe('rehydrateCart with isReorder', () => {
|
|
|
283
308
|
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
284
309
|
});
|
|
285
310
|
});
|
|
311
|
+
describe('makeMenuItemIds', () => {
|
|
312
|
+
it('collects item ids from categories and their children', () => {
|
|
313
|
+
const categories = [
|
|
314
|
+
{
|
|
315
|
+
items: [{ id: 1 }, { id: 2 }],
|
|
316
|
+
children: [{ items: [{ id: 3 }], children: [] }]
|
|
317
|
+
},
|
|
318
|
+
{ items: [{ id: 2 }, { id: 4 }], children: [] }
|
|
319
|
+
];
|
|
320
|
+
expect([...(0, utils_1.makeMenuItemIds)(categories)].sort()).toEqual([1, 2, 3, 4]);
|
|
321
|
+
});
|
|
322
|
+
it('returns an empty set for a missing menu', () => {
|
|
323
|
+
expect((0, utils_1.makeMenuItemIds)(undefined).size).toBe(0);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
286
326
|
describe('rehydrateCart without isReorder', () => {
|
|
287
327
|
it('matches by id only and ignores the Checkmate id', () => {
|
|
288
328
|
const menuItems = [
|
package/dist/cjs/utils/cart.d.ts
CHANGED
|
@@ -14,8 +14,10 @@ export declare const checkSoldOut: (id: number, suspendUntil: number | null, sol
|
|
|
14
14
|
export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean, requestedAt?: RequestedAt | null) => CartItemOption | CartItem;
|
|
15
15
|
export declare const calcCartCounts: (cart: Cart) => Record<string, number>;
|
|
16
16
|
export declare const rehydrateOrderItem: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[], requestedAt?: RequestedAt | null) => CartItem;
|
|
17
|
+
export declare const makeMenuItemIds: (categories: MenuCategories) => Set<number>;
|
|
17
18
|
export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[], options?: {
|
|
18
19
|
isReorder?: boolean;
|
|
20
|
+
preferredIds?: Set<number>;
|
|
19
21
|
}) => {
|
|
20
22
|
cart: {
|
|
21
23
|
index: number;
|
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.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;
|
|
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;
|
|
4
4
|
const types_1 = require("@open-tender/types");
|
|
5
5
|
const helpers_1 = require("./helpers");
|
|
6
6
|
const favorites_1 = require("./favorites");
|
|
@@ -449,16 +449,37 @@ const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
|
|
|
449
449
|
const pricedItem = (0, exports.calcPrices)(orderItem);
|
|
450
450
|
return { item: pricedItem, unavailable };
|
|
451
451
|
};
|
|
452
|
+
// The ids of every item on a menu, including items in child categories.
|
|
453
|
+
const makeMenuItemIds = (categories) => {
|
|
454
|
+
const ids = new Set();
|
|
455
|
+
const addCategories = (cats) => {
|
|
456
|
+
;
|
|
457
|
+
(cats || []).forEach(category => {
|
|
458
|
+
;
|
|
459
|
+
(category.items || []).forEach(item => ids.add(item.id));
|
|
460
|
+
addCategories(category.children);
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
addCategories(categories);
|
|
464
|
+
return ids;
|
|
465
|
+
};
|
|
466
|
+
exports.makeMenuItemIds = makeMenuItemIds;
|
|
452
467
|
// Reorder only. The Checkmate id of an item survives renames and category
|
|
453
468
|
// moves, which change the id of the item on the menu, so it is tried before
|
|
454
|
-
// the id.
|
|
455
|
-
|
|
469
|
+
// the id. `menuItems` covers every revenue center of the store, so the same
|
|
470
|
+
// Checkmate item can be listed more than once. `preferredIds` are the ids on
|
|
471
|
+
// the menu of the revenue center being ordered from, and they win over a copy
|
|
472
|
+
// that belongs to another revenue center.
|
|
473
|
+
const findReorderMenuItem = (menuItems, simpleCartItem, preferredIds) => {
|
|
456
474
|
const { id, pos_ext_id } = simpleCartItem;
|
|
457
475
|
const items = menuItems || [];
|
|
458
476
|
if (pos_ext_id) {
|
|
459
477
|
const matches = items.filter(i => i.pos_ext_id === pos_ext_id);
|
|
460
|
-
if (matches.length)
|
|
461
|
-
return matches.find(i => i.id === id) ||
|
|
478
|
+
if (matches.length) {
|
|
479
|
+
return (matches.find(i => i.id === id) ||
|
|
480
|
+
matches.find(i => preferredIds === null || preferredIds === void 0 ? void 0 : preferredIds.has(i.id)) ||
|
|
481
|
+
matches[0]);
|
|
482
|
+
}
|
|
462
483
|
}
|
|
463
484
|
return items.find(i => i.id === id);
|
|
464
485
|
};
|
|
@@ -480,7 +501,7 @@ const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options = {}) =
|
|
|
480
501
|
}
|
|
481
502
|
if (!item)
|
|
482
503
|
return;
|
|
483
|
-
const menuItem = findReorderMenuItem(menuItems, item);
|
|
504
|
+
const menuItem = findReorderMenuItem(menuItems, item, options.preferredIds);
|
|
484
505
|
const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
|
|
485
506
|
if (!rehydrated ||
|
|
486
507
|
rehydrated.unavailable.length ||
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { makeReorderCart, rehydrateCart, rehydrateOrderItem } from '../../utils';
|
|
1
|
+
import { makeMenuItemIds, makeReorderCart, rehydrateCart, rehydrateOrderItem } from '../../utils';
|
|
2
2
|
const makeOption = (id, posExtId, optionGroups = []) => ({
|
|
3
3
|
id,
|
|
4
4
|
name: `option ${id}`,
|
|
@@ -77,6 +77,31 @@ describe('rehydrateCart with isReorder', () => {
|
|
|
77
77
|
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
78
78
|
expect(cart.map(i => i.id)).toEqual([99]);
|
|
79
79
|
});
|
|
80
|
+
it('prefers the copy on the revenue center menu over an earlier copy', () => {
|
|
81
|
+
const menuItems = [
|
|
82
|
+
makeMenuItem(99, 'cm-item'),
|
|
83
|
+
makeMenuItem(120, 'cm-item')
|
|
84
|
+
];
|
|
85
|
+
const { cart, unavailable } = rehydrateCart(menuItems, [makeSimpleItem(10, 'cm-item')], [], { isReorder: true, preferredIds: new Set([120]) });
|
|
86
|
+
expect(cart.map(i => i.id)).toEqual([120]);
|
|
87
|
+
expect(unavailable).toEqual([]);
|
|
88
|
+
});
|
|
89
|
+
it('still prefers the old id over the revenue center menu ids', () => {
|
|
90
|
+
const menuItems = [
|
|
91
|
+
makeMenuItem(99, 'cm-item'),
|
|
92
|
+
makeMenuItem(120, 'cm-item')
|
|
93
|
+
];
|
|
94
|
+
const { cart } = rehydrateCart(menuItems, [makeSimpleItem(99, 'cm-item')], [], { isReorder: true, preferredIds: new Set([120]) });
|
|
95
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
96
|
+
});
|
|
97
|
+
it('uses the first match when no copy is on the revenue center menu', () => {
|
|
98
|
+
const menuItems = [
|
|
99
|
+
makeMenuItem(99, 'cm-item'),
|
|
100
|
+
makeMenuItem(120, 'cm-item')
|
|
101
|
+
];
|
|
102
|
+
const { cart } = rehydrateCart(menuItems, [makeSimpleItem(10, 'cm-item')], [], { isReorder: true, preferredIds: new Set([500]) });
|
|
103
|
+
expect(cart.map(i => i.id)).toEqual([99]);
|
|
104
|
+
});
|
|
80
105
|
it('falls back to the id when the Checkmate id is not on the menu', () => {
|
|
81
106
|
const menuItems = [makeMenuItem(10, 'cm-other')];
|
|
82
107
|
const { cart } = reorderCart(menuItems, [makeSimpleItem(10, 'cm-item')]);
|
|
@@ -281,6 +306,21 @@ describe('rehydrateCart with isReorder', () => {
|
|
|
281
306
|
expect(unavailable).toEqual([{ name: 'old item 10' }]);
|
|
282
307
|
});
|
|
283
308
|
});
|
|
309
|
+
describe('makeMenuItemIds', () => {
|
|
310
|
+
it('collects item ids from categories and their children', () => {
|
|
311
|
+
const categories = [
|
|
312
|
+
{
|
|
313
|
+
items: [{ id: 1 }, { id: 2 }],
|
|
314
|
+
children: [{ items: [{ id: 3 }], children: [] }]
|
|
315
|
+
},
|
|
316
|
+
{ items: [{ id: 2 }, { id: 4 }], children: [] }
|
|
317
|
+
];
|
|
318
|
+
expect([...makeMenuItemIds(categories)].sort()).toEqual([1, 2, 3, 4]);
|
|
319
|
+
});
|
|
320
|
+
it('returns an empty set for a missing menu', () => {
|
|
321
|
+
expect(makeMenuItemIds(undefined).size).toBe(0);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
284
324
|
describe('rehydrateCart without isReorder', () => {
|
|
285
325
|
it('matches by id only and ignores the Checkmate id', () => {
|
|
286
326
|
const menuItems = [
|
package/dist/esm/utils/cart.d.ts
CHANGED
|
@@ -14,8 +14,10 @@ export declare const checkSoldOut: (id: number, suspendUntil: number | null, sol
|
|
|
14
14
|
export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean, requestedAt?: RequestedAt | null) => CartItemOption | CartItem;
|
|
15
15
|
export declare const calcCartCounts: (cart: Cart) => Record<string, number>;
|
|
16
16
|
export declare const rehydrateOrderItem: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[], requestedAt?: RequestedAt | null) => CartItem;
|
|
17
|
+
export declare const makeMenuItemIds: (categories: MenuCategories) => Set<number>;
|
|
17
18
|
export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[], options?: {
|
|
18
19
|
isReorder?: boolean;
|
|
20
|
+
preferredIds?: Set<number>;
|
|
19
21
|
}) => {
|
|
20
22
|
cart: {
|
|
21
23
|
index: number;
|
package/dist/esm/utils/cart.js
CHANGED
|
@@ -431,16 +431,36 @@ const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
|
|
|
431
431
|
const pricedItem = calcPrices(orderItem);
|
|
432
432
|
return { item: pricedItem, unavailable };
|
|
433
433
|
};
|
|
434
|
+
// The ids of every item on a menu, including items in child categories.
|
|
435
|
+
export const makeMenuItemIds = (categories) => {
|
|
436
|
+
const ids = new Set();
|
|
437
|
+
const addCategories = (cats) => {
|
|
438
|
+
;
|
|
439
|
+
(cats || []).forEach(category => {
|
|
440
|
+
;
|
|
441
|
+
(category.items || []).forEach(item => ids.add(item.id));
|
|
442
|
+
addCategories(category.children);
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
addCategories(categories);
|
|
446
|
+
return ids;
|
|
447
|
+
};
|
|
434
448
|
// Reorder only. The Checkmate id of an item survives renames and category
|
|
435
449
|
// moves, which change the id of the item on the menu, so it is tried before
|
|
436
|
-
// the id.
|
|
437
|
-
|
|
450
|
+
// the id. `menuItems` covers every revenue center of the store, so the same
|
|
451
|
+
// Checkmate item can be listed more than once. `preferredIds` are the ids on
|
|
452
|
+
// the menu of the revenue center being ordered from, and they win over a copy
|
|
453
|
+
// that belongs to another revenue center.
|
|
454
|
+
const findReorderMenuItem = (menuItems, simpleCartItem, preferredIds) => {
|
|
438
455
|
const { id, pos_ext_id } = simpleCartItem;
|
|
439
456
|
const items = menuItems || [];
|
|
440
457
|
if (pos_ext_id) {
|
|
441
458
|
const matches = items.filter(i => i.pos_ext_id === pos_ext_id);
|
|
442
|
-
if (matches.length)
|
|
443
|
-
return matches.find(i => i.id === id) ||
|
|
459
|
+
if (matches.length) {
|
|
460
|
+
return (matches.find(i => i.id === id) ||
|
|
461
|
+
matches.find(i => preferredIds === null || preferredIds === void 0 ? void 0 : preferredIds.has(i.id)) ||
|
|
462
|
+
matches[0]);
|
|
463
|
+
}
|
|
444
464
|
}
|
|
445
465
|
return items.find(i => i.id === id);
|
|
446
466
|
};
|
|
@@ -462,7 +482,7 @@ export const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options
|
|
|
462
482
|
}
|
|
463
483
|
if (!item)
|
|
464
484
|
return;
|
|
465
|
-
const menuItem = findReorderMenuItem(menuItems, item);
|
|
485
|
+
const menuItem = findReorderMenuItem(menuItems, item, options.preferredIds);
|
|
466
486
|
const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
|
|
467
487
|
if (!rehydrated ||
|
|
468
488
|
rehydrated.unavailable.length ||
|
package/package.json
CHANGED