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