@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.
@@ -329,6 +329,7 @@ const makeSimpleGroupsLookup = (groups) => {
329
329
  return groups.reduce((grpObj, g) => {
330
330
  const options = g.options.reduce((optObj, o) => {
331
331
  optObj[o.id] = {
332
+ name: o.name,
332
333
  quantity: o.quantity,
333
334
  pos_ext_id: o.pos_ext_id || null,
334
335
  groupsLookup: makeSimpleGroupsLookup(o.groups)
@@ -366,18 +367,114 @@ export const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = [], reque
366
367
  const pricedItem = calcPrices(orderItem);
367
368
  return pricedItem;
368
369
  };
369
- export const rehydrateCart = (menuItems, simpleCartItems, soldOut = []) => {
370
+ // A saved option is the same as a current option when they have the same id
371
+ // and group, unless both carry a Checkmate id and the ids differ.
372
+ const isSameReorderOption = (simpleOption, option) => {
373
+ if (!simpleOption.pos_ext_id || !option.pos_ext_id)
374
+ return true;
375
+ return simpleOption.pos_ext_id === option.pos_ext_id;
376
+ };
377
+ // Reorder only. Saved options are matched to the options of the current menu
378
+ // item by group and id first. The remaining ones are matched by their
379
+ // Checkmate id alone, which is stable when an option, its group or its
380
+ // category is renamed or moved in Checkmate and gets a new id.
381
+ // Saved options that are not found are added to `unavailable`, and the
382
+ // caller drops the whole item.
383
+ const rehydrateReorderGroups = (groups, groupsLookup, unavailable) => {
384
+ if (!groupsLookup)
385
+ return groups;
386
+ const matches = new Map();
387
+ const used = new Set();
388
+ groups = groups || [];
389
+ groups.forEach(group => {
390
+ const simpleGroup = groupsLookup[group.id];
391
+ if (!simpleGroup)
392
+ return;
393
+ (group.options || []).forEach(option => {
394
+ const simpleOption = simpleGroup.options[option.id];
395
+ if (simpleOption && isSameReorderOption(simpleOption, option)) {
396
+ matches.set(option, simpleOption);
397
+ used.add(simpleOption);
398
+ }
399
+ });
400
+ });
401
+ const simpleOptions = Object.values(groupsLookup).flatMap(simpleGroup => Object.values(simpleGroup.options));
402
+ groups.forEach(group => {
403
+ ;
404
+ (group.options || []).forEach(option => {
405
+ if (matches.has(option) || !option.pos_ext_id)
406
+ return;
407
+ const simpleOption = simpleOptions.find(i => !used.has(i) && i.pos_ext_id === option.pos_ext_id);
408
+ if (simpleOption) {
409
+ matches.set(option, simpleOption);
410
+ used.add(simpleOption);
411
+ }
412
+ });
413
+ });
414
+ matches.forEach((simpleOption, option) => {
415
+ option.quantity = simpleOption.quantity;
416
+ option.groups = rehydrateReorderGroups(option.groups, simpleOption.groupsLookup, unavailable);
417
+ });
418
+ simpleOptions
419
+ .filter(i => !used.has(i))
420
+ .forEach(i => unavailable.push({ name: i.name || '' }));
421
+ return groups;
422
+ };
423
+ const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
424
+ const orderItem = makeOrderItem(menuItem, true, soldOut, simpleCartItem, undefined, null);
425
+ orderItem.quantity = simpleCartItem.quantity || 1;
426
+ const unavailable = [];
427
+ const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
428
+ if (groupsLookup) {
429
+ orderItem.groups = rehydrateReorderGroups(orderItem.groups, groupsLookup, unavailable);
430
+ }
431
+ const pricedItem = calcPrices(orderItem);
432
+ return { item: pricedItem, unavailable };
433
+ };
434
+ // Reorder only. The Checkmate id of an item survives renames and category
435
+ // moves, which change the id of the item on the menu, so it is tried before
436
+ // the id.
437
+ const findReorderMenuItem = (menuItems, simpleCartItem) => {
438
+ const { id, pos_ext_id } = simpleCartItem;
439
+ const items = menuItems || [];
440
+ if (pos_ext_id) {
441
+ 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];
444
+ }
445
+ return items.find(i => i.id === id);
446
+ };
447
+ // With `isReorder`, an item is dropped and reported in `unavailable` when it
448
+ // or any of its saved options is gone, or a required group is left unfilled,
449
+ // so the guest never gets something different from what they ordered.
450
+ // Without it, items that are not on the menu are skipped and everything else
451
+ // is kept, as it is for edited orders and group orders.
452
+ export const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
370
453
  const orderItems = [];
371
- simpleCartItems.forEach(item => {
372
- const menuItem = menuItems.find(i => i.id === item.id);
373
- if (menuItem) {
374
- const orderItem = rehydrateOrderItem(menuItem, item, soldOut);
375
- orderItems.push(orderItem);
454
+ const unavailable = [];
455
+ const lines = options.isReorder ? simpleCartItems || [] : simpleCartItems;
456
+ lines.forEach(item => {
457
+ if (!options.isReorder) {
458
+ const menuItem = menuItems.find(i => i.id === item.id);
459
+ if (menuItem)
460
+ orderItems.push(rehydrateOrderItem(menuItem, item, soldOut));
461
+ return;
462
+ }
463
+ if (!item)
464
+ return;
465
+ const menuItem = findReorderMenuItem(menuItems, item);
466
+ const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
467
+ if (!rehydrated ||
468
+ rehydrated.unavailable.length ||
469
+ hasGroupsBelowMin(null, rehydrated.item.groups)) {
470
+ unavailable.push({ name: item.name || '' });
471
+ return;
376
472
  }
473
+ orderItems.push(rehydrated.item);
377
474
  });
378
475
  const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
379
476
  const cartCounts = calcCartCounts(cart);
380
- return { cart, cartCounts };
477
+ return { cart, cartCounts, unavailable };
381
478
  };
382
479
  // TODO: maybe refactor
383
480
  const rehydrateDetails = (details) => {
@@ -657,6 +754,33 @@ export const makeSimpleCart = (cart) => {
657
754
  });
658
755
  return simpleCart;
659
756
  };
757
+ const makeReorderGroups = (groups) => {
758
+ return (groups || []).map(g => {
759
+ const options = (g.options || [])
760
+ .filter(o => o.quantity !== 0)
761
+ .map(o => ({
762
+ id: o.id,
763
+ name: o.name,
764
+ pos_ext_id: o.pos_ext_id || null,
765
+ quantity: o.quantity,
766
+ groups: makeReorderGroups(o.groups || [])
767
+ }));
768
+ return { id: g.id, options: options };
769
+ });
770
+ };
771
+ // Same as makeSimpleCart, but keeps the names and Checkmate ids needed to find
772
+ // the items of a past order on the current menu and to name those that are gone.
773
+ export const makeReorderCart = (cart) => {
774
+ return (cart || []).map(i => ({
775
+ id: i.id,
776
+ name: i.name,
777
+ pos_ext_id: i.pos_ext_id || null,
778
+ quantity: i.quantity,
779
+ groups: makeReorderGroups(i.groups),
780
+ made_for: i.madeFor || i.made_for || '',
781
+ notes: i.notes || ''
782
+ }));
783
+ };
660
784
  /* cart validation */
661
785
  // TODO: refactor for nested modifiers
662
786
  // 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;
@@ -196,10 +196,16 @@ export const makeRequestedIso = (requestedAt) => {
196
196
  ? cleanISOString(new Date())
197
197
  : requestedAt;
198
198
  };
199
- export const makeRequestedAtStr = (requestedAt, tz, verbose = false) => {
199
+ export const makeRequestedAtStr = (requestedAt, tz, verbose = false, rangeMinutes) => {
200
200
  if (!requestedAt || requestedAt.toLowerCase() === 'asap')
201
201
  return 'ASAP';
202
- return makeReadableDateStrFromIso(requestedAt, tz, verbose);
202
+ const dateStr = makeReadableDateStrFromIso(requestedAt, tz, verbose);
203
+ if (!rangeMinutes)
204
+ return dateStr;
205
+ const startDate = utcToZonedTime(parseISO(requestedAt), tz);
206
+ const endDate = add(startDate, { minutes: rangeMinutes });
207
+ const endTimeStr = format(endDate, TIME).toLowerCase();
208
+ return `${dateStr}-${endTimeStr}`;
203
209
  };
204
210
  export const makeEstimatedTime = (requestedAt, revenueCenter, serviceType, verbose = false) => {
205
211
  var _a, _b;
@@ -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
  };
@@ -46,7 +46,7 @@ export const makeOrderTypeName = (orderType, serviceType) => {
46
46
  ? serviceTypeNamesMap[serviceType]
47
47
  : capitalize(orderType);
48
48
  };
49
- const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTime) => {
49
+ const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTime, timeRange) => {
50
50
  var _a, _b;
51
51
  if (!firstTime && !orderTime)
52
52
  return null;
@@ -56,7 +56,9 @@ const makeOrderMsg = (tz, serviceType, requestedAt, firstTime, orderTime, waitTi
56
56
  const estimate = requestedAt !== 'asap' && seconds ? 'at about' : 'at';
57
57
  readableDate =
58
58
  requestedAt === 'asap' && firstTime.has_asap
59
- ? `in about ${waitTime} minutes`
59
+ ? timeRange
60
+ ? `in about ${waitTime}-${(waitTime !== null && waitTime !== void 0 ? waitTime : 0) + timeRange} minutes`
61
+ : `in about ${waitTime} minutes`
60
62
  : makeReadableDateStrFromIso(firstTime.utc, tz, true)
61
63
  .replace('Today', 'today')
62
64
  .replace('Tomorrow', 'TOMORROW')
@@ -89,21 +91,22 @@ export const makeNotAcceptingOrdersMsg = (serviceType) => {
89
91
  }
90
92
  return `This location is not currently accepting ${serviceTypeName.toLowerCase()} orders.`;
91
93
  };
92
- export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMessages) => {
93
- var _a;
94
+ export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMessages, showDeliveryRange = false) => {
95
+ var _a, _b;
94
96
  const settings = revenueCenter;
95
- const { first_times, order_times, wait_times } = settings;
97
+ const { first_times, order_times, wait_times, time_ranges } = settings;
96
98
  const tz = timezoneMap[(_a = revenueCenter === null || revenueCenter === void 0 ? void 0 : revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Central'];
97
99
  const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
98
100
  const firstTime = first_times ? first_times[st] : null;
99
101
  const orderTime = order_times ? order_times[st] : null;
100
102
  const waitTime = wait_times ? wait_times[st] : null;
103
+ 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;
101
104
  const tempClosed = revenueCenter.status === 'CLOSED_TEMPORARILY';
102
105
  const statusMsg = !tempClosed
103
106
  ? statusMessages[revenueCenter.status]
104
107
  : null;
105
108
  const orderMsg = !statusMsg && (firstTime || (orderTime && orderTime.length))
106
- ? makeOrderMsg(tz, serviceType, requestedAt, firstTime, orderTime ? orderTime[0] : null, waitTime !== null && waitTime !== void 0 ? waitTime : 0)
109
+ ? makeOrderMsg(tz, serviceType, requestedAt, firstTime, orderTime ? orderTime[0] : null, waitTime !== null && waitTime !== void 0 ? waitTime : 0, timeRange !== null && timeRange !== void 0 ? timeRange : undefined)
107
110
  : null;
108
111
  let message = orderMsg ||
109
112
  (statusMsg ? statusMsg.subtitle : makeNotAcceptingOrdersMsg(serviceType));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.72",
3
+ "version": "0.5.74",
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",
@@ -36,7 +36,7 @@
36
36
  "private": false,
37
37
  "devDependencies": {
38
38
  "@emotion/react": "^11.10.4",
39
- "@open-tender/types": "^0.4.182",
39
+ "@open-tender/types": "^0.4.189",
40
40
  "@reduxjs/toolkit": "^1.8.5",
41
41
  "@types/google.maps": "^3.53.5",
42
42
  "@types/jest": "^29.5.11",
@@ -58,7 +58,7 @@
58
58
  "typescript": "^4.8.2"
59
59
  },
60
60
  "peerDependencies": {
61
- "@open-tender/types": "^0.4.182",
61
+ "@open-tender/types": "^0.4.189",
62
62
  "@reduxjs/toolkit": "^1.8.5",
63
63
  "react": "^18.2.0"
64
64
  },