@open-tender/utils 0.5.76 → 0.5.78

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 = [
@@ -133,3 +133,41 @@ describe('group order checkout payload', () => {
133
133
  expect(payload.cart).toEqual(GROUP_CHECKOUT_CART.slice(1));
134
134
  });
135
135
  });
136
+ describe('existing order payload', () => {
137
+ const orderData = {
138
+ address: null,
139
+ cart: [],
140
+ cartId: null,
141
+ customer: null,
142
+ deposit: null,
143
+ details: null,
144
+ deviceType: null,
145
+ discounts: null,
146
+ orderId: null,
147
+ points: null,
148
+ prepType: null,
149
+ promoCodes: null,
150
+ requestedAt: 'asap',
151
+ revenueCenterId: 3,
152
+ serviceType: 'PICKUP',
153
+ surcharges: null,
154
+ table: null,
155
+ tenders: null,
156
+ tip: null
157
+ };
158
+ it('sends the order uuid together with the order id', () => {
159
+ const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: 'abc-123' }));
160
+ expect(payload.order_id).toBe(42);
161
+ expect(payload.order_uuid).toBe('abc-123');
162
+ });
163
+ it('omits the order uuid without an order id', () => {
164
+ const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderUuid: 'abc-123' }));
165
+ expect(payload).not.toHaveProperty('order_id');
166
+ expect(payload).not.toHaveProperty('order_uuid');
167
+ });
168
+ it('omits the order uuid when it is null', () => {
169
+ const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: null }));
170
+ expect(payload.order_id).toBe(42);
171
+ expect(payload).not.toHaveProperty('order_uuid');
172
+ });
173
+ });
@@ -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;
@@ -240,6 +242,8 @@ export declare const validateCart: (cart: Cart, categories: MenuCategories, sold
240
242
  errors: CartErrors | null;
241
243
  };
242
244
  export declare const getDefaultTip: (config: CheckoutConfig) => string | null;
243
- export declare const prepareOrder: (data: OrderData) => OrderCreate;
245
+ export declare const prepareOrder: (data: OrderData & {
246
+ orderUuid?: string | null;
247
+ }) => OrderCreate;
244
248
  export declare const checkAmountRemaining: (total: Money, tenders: CheckoutTenders) => number;
245
249
  export declare const makeDeviceType: (deviceType: string, deviceId?: string) => "DESKTOP" | "MOBILE" | "TABLET";
@@ -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 ||
@@ -1035,6 +1056,8 @@ const prepareOrder = (data) => {
1035
1056
  }
1036
1057
  if (data.orderId)
1037
1058
  order.order_id = data.orderId;
1059
+ if (data.orderId && data.orderUuid)
1060
+ order.order_uuid = data.orderUuid;
1038
1061
  if (data.cartId)
1039
1062
  order.cart_id = data.cartId;
1040
1063
  if (data.save)
@@ -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 = [
@@ -131,3 +131,41 @@ describe('group order checkout payload', () => {
131
131
  expect(payload.cart).toEqual(GROUP_CHECKOUT_CART.slice(1));
132
132
  });
133
133
  });
134
+ describe('existing order payload', () => {
135
+ const orderData = {
136
+ address: null,
137
+ cart: [],
138
+ cartId: null,
139
+ customer: null,
140
+ deposit: null,
141
+ details: null,
142
+ deviceType: null,
143
+ discounts: null,
144
+ orderId: null,
145
+ points: null,
146
+ prepType: null,
147
+ promoCodes: null,
148
+ requestedAt: 'asap',
149
+ revenueCenterId: 3,
150
+ serviceType: 'PICKUP',
151
+ surcharges: null,
152
+ table: null,
153
+ tenders: null,
154
+ tip: null
155
+ };
156
+ it('sends the order uuid together with the order id', () => {
157
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: 'abc-123' }));
158
+ expect(payload.order_id).toBe(42);
159
+ expect(payload.order_uuid).toBe('abc-123');
160
+ });
161
+ it('omits the order uuid without an order id', () => {
162
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderUuid: 'abc-123' }));
163
+ expect(payload).not.toHaveProperty('order_id');
164
+ expect(payload).not.toHaveProperty('order_uuid');
165
+ });
166
+ it('omits the order uuid when it is null', () => {
167
+ const payload = prepareOrder(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: null }));
168
+ expect(payload.order_id).toBe(42);
169
+ expect(payload).not.toHaveProperty('order_uuid');
170
+ });
171
+ });
@@ -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;
@@ -240,6 +242,8 @@ export declare const validateCart: (cart: Cart, categories: MenuCategories, sold
240
242
  errors: CartErrors | null;
241
243
  };
242
244
  export declare const getDefaultTip: (config: CheckoutConfig) => string | null;
243
- export declare const prepareOrder: (data: OrderData) => OrderCreate;
245
+ export declare const prepareOrder: (data: OrderData & {
246
+ orderUuid?: string | null;
247
+ }) => OrderCreate;
244
248
  export declare const checkAmountRemaining: (total: Money, tenders: CheckoutTenders) => number;
245
249
  export declare const makeDeviceType: (deviceType: string, deviceId?: string) => "DESKTOP" | "MOBILE" | "TABLET";
@@ -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
- const findReorderMenuItem = (menuItems, simpleCartItem) => {
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) || matches[0];
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 ||
@@ -996,6 +1016,8 @@ export const prepareOrder = (data) => {
996
1016
  }
997
1017
  if (data.orderId)
998
1018
  order.order_id = data.orderId;
1019
+ if (data.orderId && data.orderUuid)
1020
+ order.order_uuid = data.orderUuid;
999
1021
  if (data.cartId)
1000
1022
  order.cart_id = data.cartId;
1001
1023
  if (data.save)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.76",
3
+ "version": "0.5.78",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",