@open-tender/utils 0.5.72 → 0.5.74

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.
@@ -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.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.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");
@@ -346,6 +346,7 @@ const makeSimpleGroupsLookup = (groups) => {
346
346
  return groups.reduce((grpObj, g) => {
347
347
  const options = g.options.reduce((optObj, o) => {
348
348
  optObj[o.id] = {
349
+ name: o.name,
349
350
  quantity: o.quantity,
350
351
  pos_ext_id: o.pos_ext_id || null,
351
352
  groupsLookup: makeSimpleGroupsLookup(o.groups)
@@ -384,18 +385,114 @@ const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = [], requestedAt
384
385
  return pricedItem;
385
386
  };
386
387
  exports.rehydrateOrderItem = rehydrateOrderItem;
387
- const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
388
+ // A saved option is the same as a current option when they have the same id
389
+ // and group, unless both carry a Checkmate id and the ids differ.
390
+ const isSameReorderOption = (simpleOption, option) => {
391
+ if (!simpleOption.pos_ext_id || !option.pos_ext_id)
392
+ return true;
393
+ return simpleOption.pos_ext_id === option.pos_ext_id;
394
+ };
395
+ // Reorder only. Saved options are matched to the options of the current menu
396
+ // item by group and id first. The remaining ones are matched by their
397
+ // Checkmate id alone, which is stable when an option, its group or its
398
+ // category is renamed or moved in Checkmate and gets a new id.
399
+ // Saved options that are not found are added to `unavailable`, and the
400
+ // caller drops the whole item.
401
+ const rehydrateReorderGroups = (groups, groupsLookup, unavailable) => {
402
+ if (!groupsLookup)
403
+ return groups;
404
+ const matches = new Map();
405
+ const used = new Set();
406
+ groups = groups || [];
407
+ groups.forEach(group => {
408
+ const simpleGroup = groupsLookup[group.id];
409
+ if (!simpleGroup)
410
+ return;
411
+ (group.options || []).forEach(option => {
412
+ const simpleOption = simpleGroup.options[option.id];
413
+ if (simpleOption && isSameReorderOption(simpleOption, option)) {
414
+ matches.set(option, simpleOption);
415
+ used.add(simpleOption);
416
+ }
417
+ });
418
+ });
419
+ const simpleOptions = Object.values(groupsLookup).flatMap(simpleGroup => Object.values(simpleGroup.options));
420
+ groups.forEach(group => {
421
+ ;
422
+ (group.options || []).forEach(option => {
423
+ if (matches.has(option) || !option.pos_ext_id)
424
+ return;
425
+ const simpleOption = simpleOptions.find(i => !used.has(i) && i.pos_ext_id === option.pos_ext_id);
426
+ if (simpleOption) {
427
+ matches.set(option, simpleOption);
428
+ used.add(simpleOption);
429
+ }
430
+ });
431
+ });
432
+ matches.forEach((simpleOption, option) => {
433
+ option.quantity = simpleOption.quantity;
434
+ option.groups = rehydrateReorderGroups(option.groups, simpleOption.groupsLookup, unavailable);
435
+ });
436
+ simpleOptions
437
+ .filter(i => !used.has(i))
438
+ .forEach(i => unavailable.push({ name: i.name || '' }));
439
+ return groups;
440
+ };
441
+ const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
442
+ const orderItem = (0, exports.makeOrderItem)(menuItem, true, soldOut, simpleCartItem, undefined, null);
443
+ orderItem.quantity = simpleCartItem.quantity || 1;
444
+ const unavailable = [];
445
+ const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
446
+ if (groupsLookup) {
447
+ orderItem.groups = rehydrateReorderGroups(orderItem.groups, groupsLookup, unavailable);
448
+ }
449
+ const pricedItem = (0, exports.calcPrices)(orderItem);
450
+ return { item: pricedItem, unavailable };
451
+ };
452
+ // Reorder only. The Checkmate id of an item survives renames and category
453
+ // moves, which change the id of the item on the menu, so it is tried before
454
+ // the id.
455
+ const findReorderMenuItem = (menuItems, simpleCartItem) => {
456
+ const { id, pos_ext_id } = simpleCartItem;
457
+ const items = menuItems || [];
458
+ if (pos_ext_id) {
459
+ 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];
462
+ }
463
+ return items.find(i => i.id === id);
464
+ };
465
+ // With `isReorder`, an item is dropped and reported in `unavailable` when it
466
+ // or any of its saved options is gone, or a required group is left unfilled,
467
+ // so the guest never gets something different from what they ordered.
468
+ // Without it, items that are not on the menu are skipped and everything else
469
+ // is kept, as it is for edited orders and group orders.
470
+ const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
388
471
  const orderItems = [];
389
- simpleCartItems.forEach(item => {
390
- const menuItem = menuItems.find(i => i.id === item.id);
391
- if (menuItem) {
392
- const orderItem = (0, exports.rehydrateOrderItem)(menuItem, item, soldOut);
393
- orderItems.push(orderItem);
472
+ const unavailable = [];
473
+ const lines = options.isReorder ? simpleCartItems || [] : simpleCartItems;
474
+ lines.forEach(item => {
475
+ if (!options.isReorder) {
476
+ const menuItem = menuItems.find(i => i.id === item.id);
477
+ if (menuItem)
478
+ orderItems.push((0, exports.rehydrateOrderItem)(menuItem, item, soldOut));
479
+ return;
480
+ }
481
+ if (!item)
482
+ return;
483
+ const menuItem = findReorderMenuItem(menuItems, item);
484
+ const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
485
+ if (!rehydrated ||
486
+ rehydrated.unavailable.length ||
487
+ (0, exports.hasGroupsBelowMin)(null, rehydrated.item.groups)) {
488
+ unavailable.push({ name: item.name || '' });
489
+ return;
394
490
  }
491
+ orderItems.push(rehydrated.item);
395
492
  });
396
493
  const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
397
494
  const cartCounts = (0, exports.calcCartCounts)(cart);
398
- return { cart, cartCounts };
495
+ return { cart, cartCounts, unavailable };
399
496
  };
400
497
  exports.rehydrateCart = rehydrateCart;
401
498
  // TODO: maybe refactor
@@ -693,6 +790,34 @@ const makeSimpleCart = (cart) => {
693
790
  return simpleCart;
694
791
  };
695
792
  exports.makeSimpleCart = makeSimpleCart;
793
+ const makeReorderGroups = (groups) => {
794
+ return (groups || []).map(g => {
795
+ const options = (g.options || [])
796
+ .filter(o => o.quantity !== 0)
797
+ .map(o => ({
798
+ id: o.id,
799
+ name: o.name,
800
+ pos_ext_id: o.pos_ext_id || null,
801
+ quantity: o.quantity,
802
+ groups: makeReorderGroups(o.groups || [])
803
+ }));
804
+ return { id: g.id, options: options };
805
+ });
806
+ };
807
+ // Same as makeSimpleCart, but keeps the names and Checkmate ids needed to find
808
+ // the items of a past order on the current menu and to name those that are gone.
809
+ const makeReorderCart = (cart) => {
810
+ return (cart || []).map(i => ({
811
+ id: i.id,
812
+ name: i.name,
813
+ pos_ext_id: i.pos_ext_id || null,
814
+ quantity: i.quantity,
815
+ groups: makeReorderGroups(i.groups),
816
+ made_for: i.madeFor || i.made_for || '',
817
+ notes: i.notes || ''
818
+ }));
819
+ };
820
+ exports.makeReorderCart = makeReorderCart;
696
821
  /* cart validation */
697
822
  // TODO: refactor for nested modifiers
698
823
  // convert menu items to order items for validation but only include
@@ -53,7 +53,7 @@ export declare const isoToDateStrMinutes: (iso: ISOString, tz: Timezone) => {
53
53
  };
54
54
  export declare const makeReadableDateStrFromIso: (iso: string, tz: Timezone, verbose?: boolean, withTime?: boolean) => string;
55
55
  export declare const makeRequestedIso: (requestedAt: string) => string;
56
- export declare const makeRequestedAtStr: (requestedAt: string | null, tz: Timezone, verbose?: boolean) => string;
56
+ export declare const makeRequestedAtStr: (requestedAt: string | null, tz: Timezone, verbose?: boolean, rangeMinutes?: number) => string;
57
57
  export declare const makeEstimatedTime: (requestedAt: ISOString | string, revenueCenter: RevenueCenter, serviceType: ServiceType, verbose?: boolean) => string | null;
58
58
  export declare const makeOppositeTimes: (times: Array<number>, interval: number, min?: number, max?: number) => Array<number>;
59
59
  export declare const time24ToMinutes: (str: string) => number;
@@ -237,10 +237,16 @@ const makeRequestedIso = (requestedAt) => {
237
237
  : requestedAt;
238
238
  };
239
239
  exports.makeRequestedIso = makeRequestedIso;
240
- const makeRequestedAtStr = (requestedAt, tz, verbose = false) => {
240
+ const makeRequestedAtStr = (requestedAt, tz, verbose = false, rangeMinutes) => {
241
241
  if (!requestedAt || requestedAt.toLowerCase() === 'asap')
242
242
  return 'ASAP';
243
- return (0, exports.makeReadableDateStrFromIso)(requestedAt, tz, verbose);
243
+ const dateStr = (0, exports.makeReadableDateStrFromIso)(requestedAt, tz, verbose);
244
+ if (!rangeMinutes)
245
+ return dateStr;
246
+ const startDate = (0, date_fns_tz_1.utcToZonedTime)((0, date_fns_1.parseISO)(requestedAt), tz);
247
+ const endDate = (0, date_fns_1.add)(startDate, { minutes: rangeMinutes });
248
+ const endTimeStr = (0, date_fns_tz_1.format)(endDate, exports.TIME).toLowerCase();
249
+ return `${dateStr}-${endTimeStr}`;
244
250
  };
245
251
  exports.makeRequestedAtStr = makeRequestedAtStr;
246
252
  const makeEstimatedTime = (requestedAt, revenueCenter, serviceType, verbose = false) => {
@@ -5,7 +5,7 @@ export declare const makeOrderTypeFromParam: (param: string) => (string | boolea
5
5
  export declare const makeServiceTypeName: (serviceType: ServiceType, isCatering: boolean, isOutpost: boolean, outpostName: string) => string | undefined;
6
6
  export declare const makeOrderTypeName: (orderType: OrderType, serviceType: ServiceType) => string | undefined;
7
7
  export declare const makeNotAcceptingOrdersMsg: (serviceType: ServiceType) => string;
8
- export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, serviceType: ServiceType, requestedAt: string, statusMessages: Record<string, string>) => {
8
+ export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, serviceType: ServiceType, requestedAt: string, statusMessages: Record<string, string>, showDeliveryRange?: boolean) => {
9
9
  message: any;
10
10
  color: string;
11
11
  };
@@ -54,7 +54,7 @@ const makeOrderTypeName = (orderType, serviceType) => {
54
54
  : (0, helpers_1.capitalize)(orderType);
55
55
  };
56
56
  exports.makeOrderTypeName = makeOrderTypeName;
57
- const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTime) => {
57
+ const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTime, timeRange) => {
58
58
  var _a, _b;
59
59
  if (!firstTime && !orderTime)
60
60
  return null;
@@ -64,7 +64,9 @@ const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTi
64
64
  const estimate = requestedAt !== 'asap' && seconds ? 'at about' : 'at';
65
65
  readableDate =
66
66
  requestedAt === 'asap' && firstTime.has_asap
67
- ? `in about ${waitTime} minutes`
67
+ ? timeRange
68
+ ? `in about ${waitTime}-${(waitTime !== null && waitTime !== void 0 ? waitTime : 0) + timeRange} minutes`
69
+ : `in about ${waitTime} minutes`
68
70
  : (0, datetimes_1.makeReadableDateStrFromIso)(firstTime.utc, tz, true)
69
71
  .replace('Today', 'today')
70
72
  .replace('Tomorrow', 'TOMORROW')
@@ -98,21 +100,22 @@ const makeNotAcceptingOrdersMsg = (serviceType) => {
98
100
  return `This location is not currently accepting ${serviceTypeName.toLowerCase()} orders.`;
99
101
  };
100
102
  exports.makeNotAcceptingOrdersMsg = makeNotAcceptingOrdersMsg;
101
- const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMessages) => {
102
- var _a;
103
+ const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMessages, showDeliveryRange = false) => {
104
+ var _a, _b;
103
105
  const settings = revenueCenter;
104
- const { first_times, order_times, wait_times } = settings;
106
+ const { first_times, order_times, wait_times, time_ranges } = settings;
105
107
  const tz = datetimes_1.timezoneMap[(_a = revenueCenter === null || revenueCenter === void 0 ? void 0 : revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Central'];
106
108
  const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
107
109
  const firstTime = first_times ? first_times[st] : null;
108
110
  const orderTime = order_times ? order_times[st] : null;
109
111
  const waitTime = wait_times ? wait_times[st] : null;
112
+ const timeRange = showDeliveryRange && st === 'DELIVERY' ? (_b = time_ranges === null || time_ranges === void 0 ? void 0 : time_ranges[st]) !== null && _b !== void 0 ? _b : null : null;
110
113
  const tempClosed = revenueCenter.status === 'CLOSED_TEMPORARILY';
111
114
  const statusMsg = !tempClosed
112
115
  ? statusMessages[revenueCenter.status]
113
116
  : null;
114
117
  const orderMsg = !statusMsg && (firstTime || (orderTime && orderTime.length))
115
- ? makeOrderMsg(tz, serviceType, requestedAt, firstTime, orderTime ? orderTime[0] : null, waitTime !== null && waitTime !== void 0 ? waitTime : 0)
118
+ ? makeOrderMsg(tz, serviceType, requestedAt, firstTime, orderTime ? orderTime[0] : null, waitTime !== null && waitTime !== void 0 ? waitTime : 0, timeRange !== null && timeRange !== void 0 ? timeRange : undefined)
116
119
  : null;
117
120
  let message = orderMsg ||
118
121
  (statusMsg ? statusMsg.subtitle : (0, exports.makeNotAcceptingOrdersMsg)(serviceType));
@@ -0,0 +1 @@
1
+ export {};