@open-tender/utils 0.5.75 → 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.
- package/dist/cjs/__tests__/utils/cart.test.js +40 -0
- package/dist/cjs/__tests__/utils/itemMaxQuantity.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/itemMaxQuantity.test.js +177 -0
- package/dist/cjs/__tests__/utils/makeSimpleCart.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/makeSimpleCart.test.js +135 -0
- package/dist/cjs/hooks/useBuilder.d.ts +1 -1
- package/dist/cjs/hooks/useBuilder.js +9 -5
- package/dist/cjs/utils/cart.d.ts +5 -0
- package/dist/cjs/utils/cart.js +31 -13
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +1 -0
- package/dist/cjs/utils/itemMaxQuantity.d.ts +47 -0
- package/dist/cjs/utils/itemMaxQuantity.js +99 -0
- package/dist/esm/__tests__/utils/cart.test.js +41 -1
- package/dist/esm/__tests__/utils/itemMaxQuantity.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/itemMaxQuantity.test.js +175 -0
- package/dist/esm/__tests__/utils/makeSimpleCart.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/makeSimpleCart.test.js +133 -0
- package/dist/esm/hooks/useBuilder.d.ts +1 -1
- package/dist/esm/hooks/useBuilder.js +10 -6
- package/dist/esm/utils/cart.d.ts +5 -0
- package/dist/esm/utils/cart.js +29 -12
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/itemMaxQuantity.d.ts +47 -0
- package/dist/esm/utils/itemMaxQuantity.js +87 -0
- package/package.json +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getIncrementedQuantity = exports.findItemsOverMax = exports.checkItemIncrement = exports.checkItemAddition = exports.getRemainingAllowance = exports.getItemQuantityInCart = exports.makeMaxQuantityLookup = exports.getCartParticipant = exports.makeMaxQuantityMessage = exports.OWNER_PARTICIPANT = void 0;
|
|
4
|
+
const favorites_1 = require("./favorites");
|
|
5
|
+
exports.OWNER_PARTICIPANT = 'owner';
|
|
6
|
+
const makeMaxQuantityMessage = (max, name) => `Maximum ${max} ${name} allowed.`;
|
|
7
|
+
exports.makeMaxQuantityMessage = makeMaxQuantityMessage;
|
|
8
|
+
const getCartParticipant = (line, byParticipant) => byParticipant && line.cartGuestId
|
|
9
|
+
? `guest-${line.cartGuestId}`
|
|
10
|
+
: exports.OWNER_PARTICIPANT;
|
|
11
|
+
exports.getCartParticipant = getCartParticipant;
|
|
12
|
+
// The maximum on the current menu wins over the one saved on a cart line,
|
|
13
|
+
// which can be out of date for a cart restored from the browser.
|
|
14
|
+
const makeMaxQuantityLookup = (categories) => {
|
|
15
|
+
const menuItems = categories && categories.length ? (0, favorites_1.makeMenuItemLookup)(categories) : null;
|
|
16
|
+
return item => {
|
|
17
|
+
const menuItem = menuItems ? menuItems[item.id] : null;
|
|
18
|
+
return menuItem ? menuItem.max_quantity || 0 : item.max || 0;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.makeMaxQuantityLookup = makeMaxQuantityLookup;
|
|
22
|
+
const getItemQuantityInCart = (cart, line, options = {}) => {
|
|
23
|
+
const { byParticipant, excludeIndex } = options;
|
|
24
|
+
const participant = (0, exports.getCartParticipant)(line, byParticipant);
|
|
25
|
+
return (cart || []).reduce((total, i) => {
|
|
26
|
+
const skip = i.id !== line.id ||
|
|
27
|
+
(0, exports.getCartParticipant)(i, byParticipant) !== participant ||
|
|
28
|
+
(excludeIndex !== undefined && i.index === excludeIndex);
|
|
29
|
+
return skip ? total : total + i.quantity;
|
|
30
|
+
}, 0);
|
|
31
|
+
};
|
|
32
|
+
exports.getItemQuantityInCart = getItemQuantityInCart;
|
|
33
|
+
// How many more of an item can go in the cart, null when there is no limit.
|
|
34
|
+
const getRemainingAllowance = (cart, item, getMax, options = {}) => {
|
|
35
|
+
const max = getMax(item);
|
|
36
|
+
if (!max)
|
|
37
|
+
return null;
|
|
38
|
+
const inCart = (0, exports.getItemQuantityInCart)(cart, item, Object.assign(Object.assign({}, options), { excludeIndex: item.index }));
|
|
39
|
+
return Math.max(max - inCart, 0);
|
|
40
|
+
};
|
|
41
|
+
exports.getRemainingAllowance = getRemainingAllowance;
|
|
42
|
+
const check = (max, total, name) => max && total > max
|
|
43
|
+
? { allowed: false, message: (0, exports.makeMaxQuantityMessage)(max, name) }
|
|
44
|
+
: { allowed: true, message: null };
|
|
45
|
+
// An item with an index replaces that cart line, so the line is not counted
|
|
46
|
+
// twice, and a line can always be saved with no more than it already has.
|
|
47
|
+
const checkItemAddition = (cart, item, getMax, options = {}) => {
|
|
48
|
+
const previous = item.index !== undefined
|
|
49
|
+
? (cart || []).find(i => i.index === item.index)
|
|
50
|
+
: undefined;
|
|
51
|
+
if (previous && item.quantity <= previous.quantity) {
|
|
52
|
+
return { allowed: true, message: null };
|
|
53
|
+
}
|
|
54
|
+
const inCart = (0, exports.getItemQuantityInCart)(cart, item, Object.assign(Object.assign({}, options), { excludeIndex: item.index }));
|
|
55
|
+
return check(getMax(item), inCart + item.quantity, item.name);
|
|
56
|
+
};
|
|
57
|
+
exports.checkItemAddition = checkItemAddition;
|
|
58
|
+
const checkItemIncrement = (cart, item, getMax, options = {}) => {
|
|
59
|
+
const inCart = (0, exports.getItemQuantityInCart)(cart, item, options);
|
|
60
|
+
return check(getMax(item), inCart + (item.increment || 1), item.name);
|
|
61
|
+
};
|
|
62
|
+
exports.checkItemIncrement = checkItemIncrement;
|
|
63
|
+
// Every item that is over its maximum, once per participant.
|
|
64
|
+
const findItemsOverMax = (cart, getMax, options = {}) => {
|
|
65
|
+
const totals = (cart || []).reduce((obj, line) => {
|
|
66
|
+
const key = `${(0, exports.getCartParticipant)(line, options.byParticipant)}:${line.id}`;
|
|
67
|
+
const entry = obj[key] || { line, quantity: 0 };
|
|
68
|
+
return Object.assign(Object.assign({}, obj), { [key]: Object.assign(Object.assign({}, entry), { quantity: entry.quantity + line.quantity }) });
|
|
69
|
+
}, {});
|
|
70
|
+
return Object.values(totals).reduce((arr, entry) => {
|
|
71
|
+
const max = getMax(entry.line);
|
|
72
|
+
if (!max || entry.quantity <= max)
|
|
73
|
+
return arr;
|
|
74
|
+
const { id, name } = entry.line;
|
|
75
|
+
const message = (0, exports.makeMaxQuantityMessage)(max, name);
|
|
76
|
+
return [...arr, { id, name, max, quantity: entry.quantity, message }];
|
|
77
|
+
}, []);
|
|
78
|
+
};
|
|
79
|
+
exports.findItemsOverMax = findItemsOverMax;
|
|
80
|
+
// The quantity after the builder's + button. quantityLimit is how high this
|
|
81
|
+
// item can go in the cart:
|
|
82
|
+
// - undefined: keep the item's own maximum, as before
|
|
83
|
+
// - null: no limit
|
|
84
|
+
// - a number: the allowance left, 0 meaning none. An increment that would go
|
|
85
|
+
// over it is refused as a whole and never lowers the quantity.
|
|
86
|
+
const getIncrementedQuantity = (item, quantityLimit) => {
|
|
87
|
+
const next = item.quantity + item.increment;
|
|
88
|
+
if (quantityLimit === undefined) {
|
|
89
|
+
return {
|
|
90
|
+
quantity: item.max ? Math.min(next, item.max) : next,
|
|
91
|
+
refused: false
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (quantityLimit === null || next <= quantityLimit) {
|
|
95
|
+
return { quantity: next, refused: false };
|
|
96
|
+
}
|
|
97
|
+
return { quantity: item.quantity, refused: true };
|
|
98
|
+
};
|
|
99
|
+
exports.getIncrementedQuantity = getIncrementedQuantity;
|
|
@@ -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 = [
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { checkItemAddition, checkItemIncrement, findItemsOverMax, getIncrementedQuantity, getItemQuantityInCart, getRemainingAllowance, makeMaxQuantityLookup, makeMaxQuantityMessage } from '../../utils/itemMaxQuantity';
|
|
2
|
+
const SPOON = 5;
|
|
3
|
+
const FORK = 6;
|
|
4
|
+
const line = (id, quantity, extra = {}) => (Object.assign({ id, name: id === SPOON ? 'Spoon' : 'Fork', quantity, cartGuestId: null, max: 6 }, extra));
|
|
5
|
+
const lookup = makeMaxQuantityLookup(null);
|
|
6
|
+
const menu = (spoonMax) => [
|
|
7
|
+
{
|
|
8
|
+
items: [
|
|
9
|
+
{ id: SPOON, max_quantity: spoonMax },
|
|
10
|
+
{ id: FORK, max_quantity: 0 }
|
|
11
|
+
],
|
|
12
|
+
children: []
|
|
13
|
+
}
|
|
14
|
+
];
|
|
15
|
+
describe('maximum item quantity', () => {
|
|
16
|
+
it('uses the exact approved message', () => {
|
|
17
|
+
expect(makeMaxQuantityMessage(6, 'Spoon')).toBe('Maximum 6 Spoon allowed.');
|
|
18
|
+
});
|
|
19
|
+
it('adds up lines of the same item with different modifiers', () => {
|
|
20
|
+
const cart = [line(SPOON, 2), line(SPOON, 3, { notes: 'x' }), line(FORK, 9)];
|
|
21
|
+
expect(getItemQuantityInCart(cart, line(SPOON, 1))).toBe(5);
|
|
22
|
+
});
|
|
23
|
+
it('allows an addition that reaches the maximum', () => {
|
|
24
|
+
const cart = [line(SPOON, 4)];
|
|
25
|
+
expect(checkItemAddition(cart, line(SPOON, 2), lookup).allowed).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
it('rejects an addition over the maximum as a whole', () => {
|
|
28
|
+
const cart = [line(SPOON, 4)];
|
|
29
|
+
expect(checkItemAddition(cart, line(SPOON, 3), lookup)).toEqual({
|
|
30
|
+
allowed: false,
|
|
31
|
+
message: 'Maximum 6 Spoon allowed.'
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
it('does not count the line being edited against itself', () => {
|
|
35
|
+
const cart = [line(SPOON, 6, { index: 0 })];
|
|
36
|
+
const edited = line(SPOON, 6, { index: 0 });
|
|
37
|
+
expect(checkItemAddition(cart, edited, lookup).allowed).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
it('counts the other lines when a line is edited up', () => {
|
|
40
|
+
const cart = [line(SPOON, 4, { index: 0 }), line(SPOON, 2, { index: 1 })];
|
|
41
|
+
expect(checkItemAddition(cart, line(SPOON, 5, { index: 0 }), lookup).allowed).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
it('lets an over-limit line be saved smaller or unchanged but not larger', () => {
|
|
44
|
+
const cart = [line(SPOON, 5, { index: 0 }), line(SPOON, 5, { index: 1 })];
|
|
45
|
+
const edit = (quantity) => checkItemAddition(cart, line(SPOON, quantity, { index: 0 }), lookup);
|
|
46
|
+
expect(edit(5).allowed).toBe(true);
|
|
47
|
+
expect(edit(4).allowed).toBe(true);
|
|
48
|
+
expect(edit(6).allowed).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
it('treats a maximum of 0 as unlimited', () => {
|
|
51
|
+
const cart = [line(FORK, 40, { max: 0 })];
|
|
52
|
+
const fork = line(FORK, 40, { max: 0 });
|
|
53
|
+
expect(checkItemAddition(cart, fork, lookup).allowed).toBe(true);
|
|
54
|
+
expect(getRemainingAllowance(cart, fork, lookup)).toBe(null);
|
|
55
|
+
});
|
|
56
|
+
it('rejects an increment over the maximum across lines', () => {
|
|
57
|
+
const cart = [line(SPOON, 3), line(SPOON, 3, { notes: 'x' })];
|
|
58
|
+
expect(checkItemIncrement(cart, line(SPOON, 3), lookup).allowed).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
it('allows an increment up to the maximum', () => {
|
|
61
|
+
const cart = [line(SPOON, 3), line(SPOON, 2, { notes: 'x' })];
|
|
62
|
+
expect(checkItemIncrement(cart, line(SPOON, 2), lookup).allowed).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
it('reports what is left for the builder', () => {
|
|
65
|
+
expect(getRemainingAllowance([line(SPOON, 4)], line(SPOON, 1), lookup)).toBe(2);
|
|
66
|
+
expect(getRemainingAllowance([line(SPOON, 6)], line(SPOON, 1), lookup)).toBe(0);
|
|
67
|
+
});
|
|
68
|
+
it('counts a guest cart together even when only some lines are tagged', () => {
|
|
69
|
+
const cart = [
|
|
70
|
+
line(SPOON, 4, { cartGuestId: 11 }),
|
|
71
|
+
line(SPOON, 2, { cartGuestId: null })
|
|
72
|
+
];
|
|
73
|
+
expect(checkItemAddition(cart, line(SPOON, 1), lookup).allowed).toBe(false);
|
|
74
|
+
expect(findItemsOverMax(cart, lookup)).toEqual([]);
|
|
75
|
+
});
|
|
76
|
+
it('counts an owner cart together', () => {
|
|
77
|
+
const cart = [line(SPOON, 4), line(SPOON, 3)];
|
|
78
|
+
expect(findItemsOverMax(cart, lookup)).toHaveLength(1);
|
|
79
|
+
});
|
|
80
|
+
it('counts each guest of a group checkout cart on their own', () => {
|
|
81
|
+
const cart = [
|
|
82
|
+
line(SPOON, 6, { cartGuestId: 11 }),
|
|
83
|
+
line(SPOON, 6, { cartGuestId: 12 })
|
|
84
|
+
];
|
|
85
|
+
expect(findItemsOverMax(cart, lookup, { byParticipant: true })).toEqual([]);
|
|
86
|
+
});
|
|
87
|
+
it('counts the same cart together when it is not a group checkout', () => {
|
|
88
|
+
const cart = [
|
|
89
|
+
line(SPOON, 6, { cartGuestId: 11 }),
|
|
90
|
+
line(SPOON, 6, { cartGuestId: 12 })
|
|
91
|
+
];
|
|
92
|
+
expect(findItemsOverMax(cart, lookup)).toHaveLength(1);
|
|
93
|
+
});
|
|
94
|
+
it('counts the owner and each guest of a group checkout on their own', () => {
|
|
95
|
+
const cart = [
|
|
96
|
+
line(SPOON, 6),
|
|
97
|
+
line(SPOON, 6, { cartGuestId: 11 }),
|
|
98
|
+
line(SPOON, 6, { cartGuestId: 12 })
|
|
99
|
+
];
|
|
100
|
+
const options = { byParticipant: true };
|
|
101
|
+
expect(findItemsOverMax(cart, lookup, options)).toEqual([]);
|
|
102
|
+
expect(checkItemIncrement(cart, line(SPOON, 6, { cartGuestId: 11 }), lookup, options).allowed).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
it('finds each participant of a group checkout that is over the maximum', () => {
|
|
105
|
+
const cart = [
|
|
106
|
+
line(SPOON, 4),
|
|
107
|
+
line(SPOON, 3),
|
|
108
|
+
line(SPOON, 7, { cartGuestId: 11 }),
|
|
109
|
+
line(SPOON, 6, { cartGuestId: 12 })
|
|
110
|
+
];
|
|
111
|
+
const result = findItemsOverMax(cart, lookup, { byParticipant: true });
|
|
112
|
+
expect(result.map(i => i.quantity)).toEqual([7, 7]);
|
|
113
|
+
expect(result[0].message).toBe('Maximum 6 Spoon allowed.');
|
|
114
|
+
});
|
|
115
|
+
it('uses the maximum on the current menu over the one saved on the line', () => {
|
|
116
|
+
const cart = [line(SPOON, 5, { max: 6 })];
|
|
117
|
+
const current = makeMaxQuantityLookup(menu(4));
|
|
118
|
+
expect(findItemsOverMax(cart, current)[0].message).toBe('Maximum 4 Spoon allowed.');
|
|
119
|
+
});
|
|
120
|
+
it('stops limiting an item the current menu no longer limits', () => {
|
|
121
|
+
const cart = [line(SPOON, 9, { max: 6 })];
|
|
122
|
+
expect(findItemsOverMax(cart, makeMaxQuantityLookup(menu(0)))).toEqual([]);
|
|
123
|
+
});
|
|
124
|
+
it('falls back to the saved maximum while the menu is not loaded', () => {
|
|
125
|
+
const cart = [line(SPOON, 9, { max: 6 })];
|
|
126
|
+
expect(findItemsOverMax(cart, makeMaxQuantityLookup([]))).toHaveLength(1);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
describe('builder increment', () => {
|
|
130
|
+
const item = (quantity, max = 6) => ({ quantity, increment: 1, max });
|
|
131
|
+
it('keeps the item maximum when there is no limit argument', () => {
|
|
132
|
+
expect(getIncrementedQuantity(item(6), undefined)).toEqual({
|
|
133
|
+
quantity: 6,
|
|
134
|
+
refused: false
|
|
135
|
+
});
|
|
136
|
+
expect(getIncrementedQuantity(item(2), undefined)).toEqual({
|
|
137
|
+
quantity: 3,
|
|
138
|
+
refused: false
|
|
139
|
+
});
|
|
140
|
+
expect(getIncrementedQuantity(item(40, 0), undefined).quantity).toBe(41);
|
|
141
|
+
});
|
|
142
|
+
it('is unlimited for null even when the item has an old maximum', () => {
|
|
143
|
+
expect(getIncrementedQuantity(item(6, 6), null)).toEqual({
|
|
144
|
+
quantity: 7,
|
|
145
|
+
refused: false
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
it('refuses any increment when there is no allowance left', () => {
|
|
149
|
+
expect(getIncrementedQuantity(item(1), 0)).toEqual({
|
|
150
|
+
quantity: 1,
|
|
151
|
+
refused: true
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
it('allows an increment up to the allowance', () => {
|
|
155
|
+
expect(getIncrementedQuantity(item(1), 2)).toEqual({
|
|
156
|
+
quantity: 2,
|
|
157
|
+
refused: false
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
it('refuses an increment over the allowance and leaves the quantity', () => {
|
|
161
|
+
expect(getIncrementedQuantity(item(2), 2)).toEqual({
|
|
162
|
+
quantity: 2,
|
|
163
|
+
refused: true
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
it('refuses the whole of a larger increment instead of adding part of it', () => {
|
|
167
|
+
expect(getIncrementedQuantity({ quantity: 1, increment: 3, max: 0 }, 3)).toEqual({ quantity: 1, refused: true });
|
|
168
|
+
});
|
|
169
|
+
it('never lowers a quantity that is already over the allowance', () => {
|
|
170
|
+
expect(getIncrementedQuantity(item(5), 2)).toEqual({
|
|
171
|
+
quantity: 5,
|
|
172
|
+
refused: true
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { combineCarts, makeSimpleCart, prepareOrder, rehydrateCart } from '../../utils';
|
|
2
|
+
const line = (extra = {}) => (Object.assign({ id: 101, quantity: 2, groups: [], madeFor: null, notes: null }, extra));
|
|
3
|
+
describe('makeSimpleCart', () => {
|
|
4
|
+
it('sends the guest of a cart line as cart_guest_id', () => {
|
|
5
|
+
const [simple] = makeSimpleCart([line({ cartGuestId: 11 })]);
|
|
6
|
+
expect(simple.cart_guest_id).toBe(11);
|
|
7
|
+
});
|
|
8
|
+
it('does not claim a guest for an owner line', () => {
|
|
9
|
+
const owner = makeSimpleCart([
|
|
10
|
+
line({ cartGuestId: null, customerId: 7 }),
|
|
11
|
+
line({ customerId: 7 })
|
|
12
|
+
]);
|
|
13
|
+
owner.forEach(i => expect(i).not.toHaveProperty('cart_guest_id'));
|
|
14
|
+
owner.forEach(i => expect(i).not.toHaveProperty('customer_id'));
|
|
15
|
+
});
|
|
16
|
+
it('keeps a cart_guest_id that is already on a simple cart line', () => {
|
|
17
|
+
const [simple] = makeSimpleCart([line({ cart_guest_id: 12 })]);
|
|
18
|
+
expect(simple.cart_guest_id).toBe(12);
|
|
19
|
+
});
|
|
20
|
+
it('keeps the separate guests of a group cart', () => {
|
|
21
|
+
const simple = makeSimpleCart([
|
|
22
|
+
line({ cartGuestId: 11 }),
|
|
23
|
+
line({ cartGuestId: 12 }),
|
|
24
|
+
line({ cartGuestId: 11, quantity: 1 })
|
|
25
|
+
]);
|
|
26
|
+
expect(simple.map(i => i.cart_guest_id)).toEqual([11, 12, 11]);
|
|
27
|
+
});
|
|
28
|
+
it('leaves the payload of an individual order as it was', () => {
|
|
29
|
+
expect(makeSimpleCart([line({ madeFor: 'Pam', notes: 'No ice' })])).toEqual([{ id: 101, quantity: 2, groups: [], made_for: 'Pam', notes: 'No ice' }]);
|
|
30
|
+
});
|
|
31
|
+
it('leaves nested modifiers as they were', () => {
|
|
32
|
+
const nested = line({
|
|
33
|
+
cartGuestId: 11,
|
|
34
|
+
groups: [
|
|
35
|
+
{
|
|
36
|
+
id: 1,
|
|
37
|
+
options: [
|
|
38
|
+
{
|
|
39
|
+
id: 10,
|
|
40
|
+
quantity: 2,
|
|
41
|
+
groups: [
|
|
42
|
+
{ id: 2, options: [{ id: 20, quantity: 1, groups: [] }] }
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
{ id: 11, quantity: 0, groups: [] }
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
});
|
|
50
|
+
expect(makeSimpleCart([nested])[0].groups).toEqual([
|
|
51
|
+
{
|
|
52
|
+
id: 1,
|
|
53
|
+
options: [
|
|
54
|
+
{
|
|
55
|
+
id: 10,
|
|
56
|
+
quantity: 2,
|
|
57
|
+
groups: [{ id: 2, options: [{ id: 20, quantity: 1, groups: [] }] }]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
const menuItem = (id) => ({
|
|
65
|
+
id,
|
|
66
|
+
name: `item ${id}`,
|
|
67
|
+
pos_ext_id: null,
|
|
68
|
+
price: '10.00',
|
|
69
|
+
allergens: '',
|
|
70
|
+
tags: '',
|
|
71
|
+
increment: 1,
|
|
72
|
+
min_quantity: 1,
|
|
73
|
+
max_quantity: 6,
|
|
74
|
+
suspend_until: null,
|
|
75
|
+
nutritional_info: null,
|
|
76
|
+
option_groups: []
|
|
77
|
+
});
|
|
78
|
+
// The checkout cart of a group order owner, from the group cart saved on the
|
|
79
|
+
// server to the order request. The backend tests use this exact payload.
|
|
80
|
+
const GROUP_CHECKOUT_CART = [
|
|
81
|
+
{ id: 101, quantity: 6, groups: [], made_for: 'Dwight Schrute', notes: '' },
|
|
82
|
+
{
|
|
83
|
+
id: 101,
|
|
84
|
+
quantity: 2,
|
|
85
|
+
groups: [],
|
|
86
|
+
made_for: 'Jim Halpert',
|
|
87
|
+
notes: '',
|
|
88
|
+
cart_guest_id: 11
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: 101,
|
|
92
|
+
quantity: 3,
|
|
93
|
+
groups: [],
|
|
94
|
+
made_for: 'Pam Beesly',
|
|
95
|
+
notes: '',
|
|
96
|
+
cart_guest_id: 12
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
describe('group order checkout payload', () => {
|
|
100
|
+
const owner = { customer_id: 7, first_name: 'Dwight', last_name: 'Schrute' };
|
|
101
|
+
const guests = [
|
|
102
|
+
{ cart_guest_id: 11, first_name: 'Jim', last_name: 'Halpert' },
|
|
103
|
+
{ cart_guest_id: 12, first_name: 'Pam', last_name: 'Beesly' }
|
|
104
|
+
];
|
|
105
|
+
const saved = [
|
|
106
|
+
{ id: 101, quantity: 6, customer_id: 7, groups: [] },
|
|
107
|
+
{ id: 101, quantity: 2, cart_guest_id: 11, groups: [] },
|
|
108
|
+
{ id: 101, quantity: 3, cart_guest_id: 12, groups: [] }
|
|
109
|
+
];
|
|
110
|
+
const menuItems = [menuItem(101)];
|
|
111
|
+
const buildPayload = (ownerLines) => {
|
|
112
|
+
const { cart: ownerCart } = rehydrateCart(menuItems, ownerLines);
|
|
113
|
+
const guestLines = saved.filter(i => !i.customer_id);
|
|
114
|
+
const { cart: guestCart } = rehydrateCart(menuItems, guestLines);
|
|
115
|
+
const combined = combineCarts(ownerCart, guestCart, owner, guests);
|
|
116
|
+
return prepareOrder({
|
|
117
|
+
revenueCenterId: 3,
|
|
118
|
+
serviceType: 'PICKUP',
|
|
119
|
+
requestedAt: 'asap',
|
|
120
|
+
cart: combined,
|
|
121
|
+
cartId: 55
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
it('sends the guest ids of the saved group cart in the order request', () => {
|
|
125
|
+
const payload = buildPayload([saved[0]]);
|
|
126
|
+
expect(payload.cart_id).toBe(55);
|
|
127
|
+
expect(payload.cart).toEqual(GROUP_CHECKOUT_CART);
|
|
128
|
+
});
|
|
129
|
+
it('sends only the guests when the owner has no items', () => {
|
|
130
|
+
const payload = buildPayload([]);
|
|
131
|
+
expect(payload.cart).toEqual(GROUP_CHECKOUT_CART.slice(1));
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CartItem, CartLevels } from '@open-tender/types';
|
|
2
|
-
export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: () => void) => {
|
|
2
|
+
export declare const useBuilder: (orderItem: CartItem, orderTimeoutCallback?: () => void, quantityLimit?: number | null, onQuantityLimit?: () => void) => {
|
|
3
3
|
item: CartItem;
|
|
4
4
|
increment: () => void;
|
|
5
5
|
decrement: () => void;
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import { calcPrices } from '../utils';
|
|
3
|
-
|
|
2
|
+
import { calcPrices, getIncrementedQuantity } from '../utils';
|
|
3
|
+
// quantityLimit is how high the item can go in the cart (see
|
|
4
|
+
// getIncrementedQuantity), onQuantityLimit is called when + is refused.
|
|
5
|
+
export const useBuilder = (orderItem, orderTimeoutCallback, quantityLimit, onQuantityLimit) => {
|
|
4
6
|
const [item, setItem] = useState(orderItem);
|
|
5
7
|
useEffect(() => {
|
|
6
8
|
orderTimeoutCallback && orderTimeoutCallback();
|
|
7
9
|
}, [item, orderTimeoutCallback]);
|
|
8
10
|
const increment = () => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const { quantity, refused } = getIncrementedQuantity(item, quantityLimit);
|
|
12
|
+
if (refused) {
|
|
13
|
+
onQuantityLimit === null || onQuantityLimit === void 0 ? void 0 : onQuantityLimit();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const pricedItem = calcPrices(Object.assign(Object.assign({}, item), { quantity }));
|
|
13
17
|
setItem(pricedItem);
|
|
14
18
|
};
|
|
15
19
|
const decrement = () => {
|
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;
|
|
@@ -199,6 +201,8 @@ export declare const makeSimpleCart: <C extends {
|
|
|
199
201
|
madeFor?: string | null | undefined;
|
|
200
202
|
made_for?: string | null | undefined;
|
|
201
203
|
notes?: string | null | undefined;
|
|
204
|
+
cartGuestId?: number | null | undefined;
|
|
205
|
+
cart_guest_id?: number | null | undefined;
|
|
202
206
|
}[], T extends {
|
|
203
207
|
id: number;
|
|
204
208
|
options: K[];
|
|
@@ -207,6 +211,7 @@ export declare const makeSimpleCart: <C extends {
|
|
|
207
211
|
quantity: number;
|
|
208
212
|
groups: T[];
|
|
209
213
|
}>(cart: C) => {
|
|
214
|
+
cart_guest_id?: number | undefined;
|
|
210
215
|
id: number;
|
|
211
216
|
quantity: number;
|
|
212
217
|
groups: SimpleCartGroups;
|
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 ||
|
|
@@ -744,13 +764,10 @@ const makeSimpleGroups = (groups) => {
|
|
|
744
764
|
};
|
|
745
765
|
export const makeSimpleCart = (cart) => {
|
|
746
766
|
const simpleCart = cart.map(i => {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
made_for: i.madeFor || i.made_for || '',
|
|
752
|
-
notes: i.notes || ''
|
|
753
|
-
};
|
|
767
|
+
// Only a group order guest's line names a guest. The owner is the
|
|
768
|
+
// authenticated customer, so an owner line names no one.
|
|
769
|
+
const cartGuestId = i.cartGuestId || i.cart_guest_id;
|
|
770
|
+
return Object.assign({ id: i.id, quantity: i.quantity, groups: makeSimpleGroups(i.groups), made_for: i.madeFor || i.made_for || '', notes: i.notes || '' }, (cartGuestId ? { cart_guest_id: cartGuestId } : {}));
|
|
754
771
|
});
|
|
755
772
|
return simpleCart;
|
|
756
773
|
};
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CartItem, MenuCategories } from '@open-tender/types';
|
|
2
|
+
type CartLine = Pick<CartItem, 'id' | 'name' | 'quantity'> & {
|
|
3
|
+
cartGuestId?: number | null;
|
|
4
|
+
index?: number;
|
|
5
|
+
increment?: number;
|
|
6
|
+
max?: number;
|
|
7
|
+
};
|
|
8
|
+
export type MaxQuantityLookup = (item: {
|
|
9
|
+
id: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
}) => number;
|
|
12
|
+
export type MaxQuantityOptions = {
|
|
13
|
+
byParticipant?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type MaxQuantityViolation = {
|
|
16
|
+
id: number;
|
|
17
|
+
name: string;
|
|
18
|
+
max: number;
|
|
19
|
+
quantity: number;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
export declare const OWNER_PARTICIPANT = "owner";
|
|
23
|
+
export declare const makeMaxQuantityMessage: (max: number, name: string) => string;
|
|
24
|
+
export declare const getCartParticipant: (line: {
|
|
25
|
+
cartGuestId?: number | null;
|
|
26
|
+
}, byParticipant?: boolean) => string;
|
|
27
|
+
export declare const makeMaxQuantityLookup: (categories?: MenuCategories | null) => MaxQuantityLookup;
|
|
28
|
+
export declare const getItemQuantityInCart: (cart: CartLine[] | null | undefined, line: CartLine, options?: MaxQuantityOptions & {
|
|
29
|
+
excludeIndex?: number;
|
|
30
|
+
}) => number;
|
|
31
|
+
export declare const getRemainingAllowance: (cart: CartLine[] | null | undefined, item: CartLine, getMax: MaxQuantityLookup, options?: MaxQuantityOptions) => number | null;
|
|
32
|
+
type Check = {
|
|
33
|
+
allowed: boolean;
|
|
34
|
+
message: string | null;
|
|
35
|
+
};
|
|
36
|
+
export declare const checkItemAddition: (cart: CartLine[] | null | undefined, item: CartLine, getMax: MaxQuantityLookup, options?: MaxQuantityOptions) => Check;
|
|
37
|
+
export declare const checkItemIncrement: (cart: CartLine[] | null | undefined, item: CartLine, getMax: MaxQuantityLookup, options?: MaxQuantityOptions) => Check;
|
|
38
|
+
export declare const findItemsOverMax: (cart: CartLine[] | null | undefined, getMax: MaxQuantityLookup, options?: MaxQuantityOptions) => MaxQuantityViolation[];
|
|
39
|
+
export declare const getIncrementedQuantity: (item: {
|
|
40
|
+
quantity: number;
|
|
41
|
+
increment: number;
|
|
42
|
+
max: number;
|
|
43
|
+
}, quantityLimit?: number | null) => {
|
|
44
|
+
quantity: number;
|
|
45
|
+
refused: boolean;
|
|
46
|
+
};
|
|
47
|
+
export {};
|