@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.
@@ -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 = [
@@ -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
- const useBuilder = (orderItem, orderTimeoutCallback) => {
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 newQuantity = item.max
13
- ? Math.min(item.quantity + item.increment, item.max)
14
- : item.quantity + item.increment;
15
- const pricedItem = (0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
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 = () => {
@@ -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;
@@ -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
- const findReorderMenuItem = (menuItems, simpleCartItem) => {
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) || matches[0];
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 ||
@@ -779,13 +800,10 @@ const makeSimpleGroups = (groups) => {
779
800
  };
780
801
  const makeSimpleCart = (cart) => {
781
802
  const simpleCart = cart.map(i => {
782
- return {
783
- id: i.id,
784
- quantity: i.quantity,
785
- groups: makeSimpleGroups(i.groups),
786
- made_for: i.madeFor || i.made_for || '',
787
- notes: i.notes || ''
788
- };
803
+ // Only a group order guest's line names a guest. The owner is the
804
+ // authenticated customer, so an owner line names no one.
805
+ const cartGuestId = i.cartGuestId || i.cart_guest_id;
806
+ 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
807
  });
790
808
  return simpleCart;
791
809
  };
@@ -17,5 +17,6 @@ export * from './rewards';
17
17
  export * from './version';
18
18
  export * from './theme';
19
19
  export * from './favorites';
20
+ export * from './itemMaxQuantity';
20
21
  export * from './pizzaBuilder';
21
22
  export * from './passwordValidation';
@@ -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 {};