@open-tender/utils 0.0.1

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.
Files changed (67) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/index.d.ts +1 -0
  4. package/dist/cjs/index.js +4 -0
  5. package/dist/cjs/utils/cards.d.ts +20 -0
  6. package/dist/cjs/utils/cards.js +153 -0
  7. package/dist/cjs/utils/cart.d.ts +422 -0
  8. package/dist/cjs/utils/cart.js +848 -0
  9. package/dist/cjs/utils/checkout.d.ts +23 -0
  10. package/dist/cjs/utils/checkout.js +108 -0
  11. package/dist/cjs/utils/constants.d.ts +52 -0
  12. package/dist/cjs/utils/constants.js +58 -0
  13. package/dist/cjs/utils/customer.d.ts +3 -0
  14. package/dist/cjs/utils/customer.js +23 -0
  15. package/dist/cjs/utils/datetimes.d.ts +174 -0
  16. package/dist/cjs/utils/datetimes.js +704 -0
  17. package/dist/cjs/utils/errors.d.ts +48 -0
  18. package/dist/cjs/utils/errors.js +143 -0
  19. package/dist/cjs/utils/helpers.d.ts +30 -0
  20. package/dist/cjs/utils/helpers.js +141 -0
  21. package/dist/cjs/utils/index.d.ts +14 -0
  22. package/dist/cjs/utils/index.js +17 -0
  23. package/dist/cjs/utils/keypad.d.ts +9 -0
  24. package/dist/cjs/utils/keypad.js +105 -0
  25. package/dist/cjs/utils/maps.d.ts +264 -0
  26. package/dist/cjs/utils/maps.js +287 -0
  27. package/dist/cjs/utils/orders.d.ts +40 -0
  28. package/dist/cjs/utils/orders.js +191 -0
  29. package/dist/cjs/utils/refunds.d.ts +35 -0
  30. package/dist/cjs/utils/refunds.js +98 -0
  31. package/dist/cjs/utils/rewards.d.ts +18 -0
  32. package/dist/cjs/utils/rewards.js +69 -0
  33. package/dist/cjs/utils/version.d.ts +4 -0
  34. package/dist/cjs/utils/version.js +38 -0
  35. package/dist/esm/index.d.ts +1 -0
  36. package/dist/esm/index.js +1 -0
  37. package/dist/esm/utils/cards.d.ts +20 -0
  38. package/dist/esm/utils/cards.js +144 -0
  39. package/dist/esm/utils/cart.d.ts +422 -0
  40. package/dist/esm/utils/cart.js +805 -0
  41. package/dist/esm/utils/checkout.d.ts +23 -0
  42. package/dist/esm/utils/checkout.js +100 -0
  43. package/dist/esm/utils/constants.d.ts +52 -0
  44. package/dist/esm/utils/constants.js +55 -0
  45. package/dist/esm/utils/customer.d.ts +3 -0
  46. package/dist/esm/utils/customer.js +18 -0
  47. package/dist/esm/utils/datetimes.d.ts +174 -0
  48. package/dist/esm/utils/datetimes.js +631 -0
  49. package/dist/esm/utils/errors.d.ts +48 -0
  50. package/dist/esm/utils/errors.js +135 -0
  51. package/dist/esm/utils/helpers.d.ts +30 -0
  52. package/dist/esm/utils/helpers.js +117 -0
  53. package/dist/esm/utils/index.d.ts +14 -0
  54. package/dist/esm/utils/index.js +14 -0
  55. package/dist/esm/utils/keypad.d.ts +9 -0
  56. package/dist/esm/utils/keypad.js +97 -0
  57. package/dist/esm/utils/maps.d.ts +264 -0
  58. package/dist/esm/utils/maps.js +269 -0
  59. package/dist/esm/utils/orders.d.ts +40 -0
  60. package/dist/esm/utils/orders.js +171 -0
  61. package/dist/esm/utils/refunds.d.ts +35 -0
  62. package/dist/esm/utils/refunds.js +90 -0
  63. package/dist/esm/utils/rewards.d.ts +18 -0
  64. package/dist/esm/utils/rewards.js +63 -0
  65. package/dist/esm/utils/version.d.ts +4 -0
  66. package/dist/esm/utils/version.js +34 -0
  67. package/package.json +65 -0
@@ -0,0 +1,704 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.time24ToDateStr = exports.time24ToDate = exports.minutesToDate = exports.setTimeForDate = exports.time24ToMinutes = exports.makeClosedWeekdays = exports.makeWeekdaysExcluded = exports.makeOppositeTimes = exports.makeEstimatedTime = exports.makeRequestedAtStr = exports.makeRequestedIso = exports.makeReadableDateStrFromIso = exports.isoToDateStrMinutes = exports.dateStrMinutesToIso = exports.formatTimeStr = exports.formatDateStr = exports.dateStrToZonedWeekday = exports.dateStrToZonedDate = exports.dateStrToDate = exports.currentLocalDateStr = exports.currentLocalDate = exports.dateToZonedIso = exports.dateToZonedDateStr = exports.adjustZonedIso = exports.adjustIso = exports.dateToIso = exports.cleanISOString = exports.isoToDateStr = exports.isoToDate = exports.tomorrowDate = exports.todayDate = exports.makeLocalDateStr = exports.zonedTimeToDateStr = exports.zonedTimeToDate = exports.makeLocalDate = exports.getUserTimezone = exports.fmtDate = exports.parseIsoToDate = exports.weekdayAndTimeToDate = exports.dateForWeekday = exports.timeLeft = exports.secondsToTime = exports.secondsLeft = exports.minutesLeft = exports.makeWeekday = exports.weekdayOptions = exports.weekdaysLower = exports.weekdaysUpper = exports.weekdays = exports.timezoneMap = void 0;
4
+ exports.formatTimeList = exports.makeIntervals = exports.getLastInterval = exports.makeGroupOrderTimeStr = exports.formatTime = exports.makeGroupOrderTime = exports.getFirstTime = exports.adjustRequestedAt = exports.getNextInterval = exports.makeFirstTimes = exports.makeFirstRequestedAt = exports.makeFirstTime = exports.findOrderTime = exports.makeOrderTimes = exports.makeWeekdayIndices = exports.makeDatePickerDates = exports.makeDatePickerTimes = exports.makeDatepickerArgs = exports.makeTimes = exports.makeDates = exports.makeTimeIntervals = exports.getNextIntervalMinutes = exports.getMinutesfromDate = exports.minutesToDates = void 0;
5
+ const date_fns_1 = require("date-fns");
6
+ const date_fns_tz_1 = require("date-fns-tz");
7
+ /* CONSTANTS */
8
+ const DATE = 'yyyy-MM-dd';
9
+ const HUMAN_DATE = 'MMM d, yyyy';
10
+ const DATETIME = 'yyyy-MM-dd h:mma';
11
+ const TIME = 'h:mma';
12
+ exports.timezoneMap = {
13
+ 'US/Eastern': 'America/New_York',
14
+ 'US/Central': 'America/Chicago',
15
+ 'US/Mountain': 'America/Denver',
16
+ 'US/Pacific': 'America/Los_Angeles'
17
+ };
18
+ exports.weekdays = [
19
+ 'Sunday',
20
+ 'Monday',
21
+ 'Tuesday',
22
+ 'Wednesday',
23
+ 'Thursday',
24
+ 'Friday',
25
+ 'Saturday'
26
+ ];
27
+ exports.weekdaysUpper = exports.weekdays.map(weekday => weekday.toUpperCase());
28
+ exports.weekdaysLower = exports.weekdays.map(weekday => weekday.toLowerCase());
29
+ exports.weekdayOptions = exports.weekdays.map(weekday => ({
30
+ value: weekday.toUpperCase(),
31
+ name: weekday
32
+ }));
33
+ const makeWeekday = (date = new Date()) => {
34
+ return (0, date_fns_tz_1.format)(date, 'EEEE').toUpperCase();
35
+ };
36
+ exports.makeWeekday = makeWeekday;
37
+ const minutesLeft = (start, end) => {
38
+ return Math.max((0, date_fns_1.differenceInMinutes)(start, end), 0);
39
+ };
40
+ exports.minutesLeft = minutesLeft;
41
+ const secondsLeft = (start, end) => {
42
+ return Math.max((0, date_fns_1.differenceInSeconds)(start, end), 0);
43
+ };
44
+ exports.secondsLeft = secondsLeft;
45
+ const secondsToTime = (seconds) => {
46
+ const mins = Math.floor(seconds / 60);
47
+ const secs = `${seconds % 60}`.padStart(2, '0');
48
+ return `${mins}:${secs}`;
49
+ };
50
+ exports.secondsToTime = secondsToTime;
51
+ const timeLeft = (start, end) => {
52
+ const seconds = (0, exports.secondsLeft)(start, end);
53
+ return (0, exports.secondsToTime)(seconds);
54
+ };
55
+ exports.timeLeft = timeLeft;
56
+ const dateForWeekday = (weekday) => {
57
+ const currentWeekday = (0, exports.makeWeekday)();
58
+ const currentIndex = exports.weekdaysUpper.findIndex(i => i === currentWeekday);
59
+ const index = exports.weekdaysUpper.findIndex(i => i === weekday.toUpperCase());
60
+ const offset = index - currentIndex + (index >= currentIndex ? 0 : 7);
61
+ return (0, date_fns_1.add)(new Date(), { days: offset });
62
+ };
63
+ exports.dateForWeekday = dateForWeekday;
64
+ const weekdayAndTimeToDate = (weekday, timeStr) => {
65
+ const date = (0, exports.dateForWeekday)(weekday);
66
+ const [hours, minutes] = timeStr.split(':').map(i => parseInt(i));
67
+ return (0, exports.setTimeForDate)(date, hours, minutes);
68
+ };
69
+ exports.weekdayAndTimeToDate = weekdayAndTimeToDate;
70
+ /* HELPERS */
71
+ const parseIsoToDate = (iso) => (0, date_fns_1.parseISO)(iso);
72
+ exports.parseIsoToDate = parseIsoToDate;
73
+ const fmtDate = (date, fmt) => {
74
+ return (0, date_fns_tz_1.format)(date, fmt);
75
+ };
76
+ exports.fmtDate = fmtDate;
77
+ // https://stackoverflow.com/questions/54555491/how-to-guess-users-timezone-using-date-fns-in-a-vuejs-app
78
+ const getUserTimezone = () => {
79
+ try {
80
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
81
+ }
82
+ catch (err) {
83
+ return 'America/New_York';
84
+ }
85
+ };
86
+ exports.getUserTimezone = getUserTimezone;
87
+ const makeLocalDate = (dateStr) => {
88
+ const tz = (0, exports.getUserTimezone)();
89
+ return (0, date_fns_tz_1.toDate)(dateStr, { timeZone: tz });
90
+ };
91
+ exports.makeLocalDate = makeLocalDate;
92
+ const zonedTimeToDate = (str, timezone) => {
93
+ const tz = exports.timezoneMap[timezone];
94
+ return (0, date_fns_tz_1.toDate)(str.split('.')[0], { timeZone: tz });
95
+ };
96
+ exports.zonedTimeToDate = zonedTimeToDate;
97
+ const zonedTimeToDateStr = (str, timezone, fmt = DATE) => {
98
+ const date = (0, exports.zonedTimeToDate)(str, timezone);
99
+ return (0, date_fns_tz_1.format)(date, fmt);
100
+ };
101
+ exports.zonedTimeToDateStr = zonedTimeToDateStr;
102
+ // returns a date string in a user's local time
103
+ const makeLocalDateStr = (date, days = 0, fmt = DATE) => {
104
+ return (0, date_fns_tz_1.format)((0, date_fns_1.add)(date || new Date(), { days: days }), fmt);
105
+ };
106
+ exports.makeLocalDateStr = makeLocalDateStr;
107
+ const todayDate = () => (0, exports.makeLocalDateStr)();
108
+ exports.todayDate = todayDate;
109
+ const tomorrowDate = () => (0, exports.makeLocalDateStr)(null, 1);
110
+ exports.tomorrowDate = tomorrowDate;
111
+ const isoToDate = (iso, tz) => {
112
+ return tz ? (0, date_fns_tz_1.utcToZonedTime)((0, date_fns_1.parseISO)(iso), tz) : (0, date_fns_1.parseISO)(iso);
113
+ };
114
+ exports.isoToDate = isoToDate;
115
+ const isoToDateStr = (iso, tz, fmt = DATETIME) => {
116
+ return (0, date_fns_tz_1.format)((0, exports.isoToDate)(iso, tz), fmt);
117
+ };
118
+ exports.isoToDateStr = isoToDateStr;
119
+ const cleanISOString = (date) => {
120
+ return (date.toISOString().split('.')[0] + 'Z');
121
+ };
122
+ exports.cleanISOString = cleanISOString;
123
+ const dateToIso = (date, tz) => {
124
+ return (0, exports.cleanISOString)((0, date_fns_tz_1.zonedTimeToUtc)(date, tz));
125
+ };
126
+ exports.dateToIso = dateToIso;
127
+ const adjustIso = (iso, tz, adjustment) => {
128
+ const date = (0, exports.isoToDate)(iso, tz);
129
+ return (0, exports.dateToIso)((0, date_fns_1.add)(date, adjustment), tz);
130
+ };
131
+ exports.adjustIso = adjustIso;
132
+ const adjustZonedIso = (zonedIso, tz, adjustment) => {
133
+ const adjusted = (0, date_fns_1.add)((0, date_fns_tz_1.toDate)(zonedIso), adjustment);
134
+ const zoned = (0, date_fns_tz_1.utcToZonedTime)(adjusted, tz);
135
+ const dateStr = (0, date_fns_tz_1.format)(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
136
+ return dateStr;
137
+ };
138
+ exports.adjustZonedIso = adjustZonedIso;
139
+ const dateToZonedDateStr = (date, tz, fmt = "yyyy-MM-dd'T'HH:mm:ssXXX") => {
140
+ const zoned = (0, date_fns_tz_1.utcToZonedTime)(date, tz);
141
+ return (0, date_fns_tz_1.format)(zoned, fmt, { timeZone: tz });
142
+ };
143
+ exports.dateToZonedDateStr = dateToZonedDateStr;
144
+ const dateToZonedIso = (date, tz) => {
145
+ const zoned = (0, date_fns_tz_1.utcToZonedTime)(date, tz);
146
+ return (0, date_fns_tz_1.format)(zoned, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone: tz });
147
+ };
148
+ exports.dateToZonedIso = dateToZonedIso;
149
+ const currentLocalDate = (tz) => {
150
+ const date = new Date();
151
+ return (0, exports.isoToDate)(date.toISOString(), tz);
152
+ };
153
+ exports.currentLocalDate = currentLocalDate;
154
+ const currentLocalDateStr = (tz, fmt = DATE) => {
155
+ const date = new Date();
156
+ return (0, date_fns_tz_1.format)((0, exports.isoToDate)(date.toISOString(), tz), fmt);
157
+ };
158
+ exports.currentLocalDateStr = currentLocalDateStr;
159
+ const dateStrToDate = (str) => (0, date_fns_tz_1.toDate)(str);
160
+ exports.dateStrToDate = dateStrToDate;
161
+ const dateStrToZonedDate = (str, tz) => {
162
+ return (0, date_fns_tz_1.toDate)(str, { timeZone: tz });
163
+ };
164
+ exports.dateStrToZonedDate = dateStrToZonedDate;
165
+ const dateStrToZonedWeekday = (str) => {
166
+ return (0, exports.makeWeekday)((0, date_fns_tz_1.toDate)(str));
167
+ };
168
+ exports.dateStrToZonedWeekday = dateStrToZonedWeekday;
169
+ const formatDateStr = (str, fmt = HUMAN_DATE) => {
170
+ return str ? (0, date_fns_tz_1.format)((0, date_fns_tz_1.toDate)(str), fmt) : null;
171
+ };
172
+ exports.formatDateStr = formatDateStr;
173
+ const formatTimeStr = (str) => {
174
+ const clean = str.replace(/\s/g, '').toLowerCase();
175
+ const parts = clean.split('-');
176
+ if (parts.length === 1)
177
+ return clean;
178
+ const [part1, part2] = parts;
179
+ const end1 = part1.substr(part1.length - 2);
180
+ const end2 = part2.substr(part1.length - 2);
181
+ const newPart1 = ['am', 'pm'].includes(end1) && end1 === end2
182
+ ? part1.slice(0, part1.length - 2)
183
+ : part1;
184
+ return [newPart1, part2].join('-');
185
+ };
186
+ exports.formatTimeStr = formatTimeStr;
187
+ const dateStrMinutesToIso = (dateStr, minutes, tz) => {
188
+ const orderDate = tz ? (0, date_fns_tz_1.toDate)(dateStr, { timeZone: tz }) : (0, date_fns_tz_1.toDate)(dateStr);
189
+ const hours = Math.floor(minutes / 60);
190
+ const mins = minutes % 60;
191
+ const orderDateTime = (0, exports.setTimeForDate)(orderDate, hours, mins);
192
+ return tz ? (0, exports.dateToIso)(orderDateTime, tz) : (0, exports.cleanISOString)(orderDateTime);
193
+ };
194
+ exports.dateStrMinutesToIso = dateStrMinutesToIso;
195
+ const isoToDateStrMinutes = (iso, tz) => {
196
+ const dateObj = (0, exports.isoToDate)(iso, tz);
197
+ const minutes = (0, exports.getMinutesfromDate)(dateObj);
198
+ const date = (0, date_fns_tz_1.format)(dateObj, DATE);
199
+ return { date, minutes };
200
+ };
201
+ exports.isoToDateStrMinutes = isoToDateStrMinutes;
202
+ const makeReadableDateStrFromIso = (iso, tz, verbose = false) => {
203
+ if (!iso || iso.toLowerCase() === 'asap')
204
+ return 'ASAP';
205
+ const date = (0, date_fns_tz_1.utcToZonedTime)((0, date_fns_1.parseISO)(iso), tz);
206
+ const timeString = (0, date_fns_tz_1.format)(date, TIME).toLowerCase();
207
+ const dateString = (0, exports.makeLocalDateStr)(date);
208
+ if (dateString === (0, exports.todayDate)()) {
209
+ return verbose ? `Today @ ${timeString}` : timeString;
210
+ }
211
+ else if (dateString === (0, exports.tomorrowDate)()) {
212
+ return `${verbose ? 'Tomorrow' : 'Tmrw'} @ ${timeString}`;
213
+ }
214
+ else {
215
+ return `${verbose ? (0, date_fns_tz_1.format)(date, 'EEE, MMM d') : (0, date_fns_tz_1.format)(date, 'M/d')} @ ${timeString}`;
216
+ }
217
+ };
218
+ exports.makeReadableDateStrFromIso = makeReadableDateStrFromIso;
219
+ const makeRequestedIso = (requestedAt) => {
220
+ return !requestedAt || requestedAt === 'asap'
221
+ ? (0, exports.cleanISOString)(new Date())
222
+ : requestedAt;
223
+ };
224
+ exports.makeRequestedIso = makeRequestedIso;
225
+ const makeRequestedAtStr = (requestedAt, tz, verbose = false) => {
226
+ if (!requestedAt || requestedAt.toLowerCase() === 'asap')
227
+ return 'ASAP';
228
+ return (0, exports.makeReadableDateStrFromIso)(requestedAt, tz, verbose);
229
+ };
230
+ exports.makeRequestedAtStr = makeRequestedAtStr;
231
+ const makeEstimatedTime = (requestedAt, revenueCenter, serviceType, verbose = false) => {
232
+ var _a, _b;
233
+ if (requestedAt !== 'asap' || !serviceType || !revenueCenter)
234
+ return null;
235
+ const settings = revenueCenter.settings || revenueCenter;
236
+ const { first_times } = settings;
237
+ if (!first_times)
238
+ return null;
239
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
240
+ const firstTime = first_times[st];
241
+ if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === (0, exports.todayDate)()) {
242
+ return `around ${firstTime.time}`;
243
+ }
244
+ else if ((firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) === (0, exports.tomorrowDate)()) {
245
+ return `tomorrow @ ${(0, exports.formatTimeStr)(firstTime === null || firstTime === void 0 ? void 0 : firstTime.time)}`;
246
+ }
247
+ else {
248
+ const tz = exports.timezoneMap[(_a = revenueCenter.timezone) !== null && _a !== void 0 ? _a : 'US/Eastern'];
249
+ const date = (0, date_fns_tz_1.toDate)((_b = firstTime === null || firstTime === void 0 ? void 0 : firstTime.date) !== null && _b !== void 0 ? _b : new Date(), { timeZone: tz });
250
+ return `${verbose ? (0, date_fns_tz_1.format)(date, 'EEEE, MMMM d') : (0, date_fns_tz_1.format)(date, 'M/d')} @ ${firstTime === null || firstTime === void 0 ? void 0 : firstTime.time}`;
251
+ }
252
+ };
253
+ exports.makeEstimatedTime = makeEstimatedTime;
254
+ function* range(start, end, step) {
255
+ for (let i = start; i <= end; i += step) {
256
+ yield i;
257
+ }
258
+ }
259
+ const makeOppositeTimes = (times, interval, min = 0, max = 1440) => {
260
+ return [...range(min, max - interval, interval)].filter(i => !times.includes(i));
261
+ };
262
+ exports.makeOppositeTimes = makeOppositeTimes;
263
+ const makeWeekdaysExcluded = (validTimes) => {
264
+ return Object.entries(validTimes).reduce((obj, entry) => {
265
+ const [weekday, dayparts] = entry;
266
+ const orderableDayparts = dayparts.filter(d => d.is_orderable);
267
+ if (!orderableDayparts.length) {
268
+ obj[weekday.toUpperCase()] = null;
269
+ return obj;
270
+ }
271
+ const interval = orderableDayparts[0].interval;
272
+ const orderableTimes = orderableDayparts.reduce((all, daypart) => {
273
+ var _a, _b;
274
+ return all.concat((_b = (_a = daypart.times) === null || _a === void 0 ? void 0 : _a.filter(i => i.is_orderable).map(i => i.minutes)) !== null && _b !== void 0 ? _b : []);
275
+ }, []);
276
+ obj[weekday.toUpperCase()] = (0, exports.makeOppositeTimes)(orderableTimes, interval);
277
+ return obj;
278
+ }, {});
279
+ };
280
+ exports.makeWeekdaysExcluded = makeWeekdaysExcluded;
281
+ const makeClosedWeekdays = (weekdayTimes) => {
282
+ return Object.entries(weekdayTimes)
283
+ .filter(([, times]) => !times)
284
+ .map(([weekday]) => exports.weekdaysUpper.indexOf(weekday));
285
+ };
286
+ exports.makeClosedWeekdays = makeClosedWeekdays;
287
+ const time24ToMinutes = (str) => {
288
+ const [hours, minutes] = str.split(':');
289
+ return parseInt(hours) * 60 + parseInt(minutes);
290
+ };
291
+ exports.time24ToMinutes = time24ToMinutes;
292
+ const setTimeForDate = (date, hours, minutes, seconds = 0) => {
293
+ date.setHours(hours);
294
+ date.setMinutes(minutes);
295
+ date.setSeconds(seconds);
296
+ date.setMilliseconds(0);
297
+ return date;
298
+ };
299
+ exports.setTimeForDate = setTimeForDate;
300
+ const minutesToDate = (minutes, date) => {
301
+ const hours = Math.floor(minutes / 60);
302
+ const mins = minutes % 60;
303
+ const dateCloned = date ? new Date(date.getTime()) : new Date();
304
+ return (0, exports.setTimeForDate)(dateCloned, hours, mins);
305
+ };
306
+ exports.minutesToDate = minutesToDate;
307
+ const time24ToDate = (str) => {
308
+ const minutes = (0, exports.time24ToMinutes)(str);
309
+ return (0, exports.minutesToDate)(minutes);
310
+ };
311
+ exports.time24ToDate = time24ToDate;
312
+ const time24ToDateStr = (str, fmt = TIME) => {
313
+ const minutes = (0, exports.time24ToMinutes)(str);
314
+ return (0, date_fns_tz_1.format)((0, exports.minutesToDate)(minutes), fmt).replace(':00', '').toLowerCase();
315
+ };
316
+ exports.time24ToDateStr = time24ToDateStr;
317
+ const minutesToDates = (minutes, date) => {
318
+ return minutes.map(minute => {
319
+ return (0, exports.minutesToDate)(minute, date);
320
+ });
321
+ };
322
+ exports.minutesToDates = minutesToDates;
323
+ const getMinutesfromDate = (date) => {
324
+ return date.getHours() * 60 + date.getMinutes();
325
+ };
326
+ exports.getMinutesfromDate = getMinutesfromDate;
327
+ const getNextIntervalMinutes = (minutes, interval) => {
328
+ const hours = Math.floor(minutes / 60);
329
+ const mins = minutes % 60;
330
+ const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
331
+ const nextMinute = intervals.filter(i => i >= mins)[0];
332
+ return hours * 60 + nextMinute;
333
+ };
334
+ exports.getNextIntervalMinutes = getNextIntervalMinutes;
335
+ // start and end should be submitted as minutes (integers)
336
+ const makeTimeIntervals = (date, start, end, interval, excludeTimes = []) => {
337
+ const nextStart = (0, exports.getNextIntervalMinutes)(start, interval);
338
+ const excluded = excludeTimes.map(i => (0, exports.getMinutesfromDate)(i));
339
+ const minutes = [...range(nextStart, end, interval)].filter(i => !excluded.includes(i));
340
+ const dates = (0, exports.minutesToDates)(minutes, date);
341
+ return dates.map(d => ({ label: (0, date_fns_tz_1.format)(d, 'h:mm aa'), value: d }));
342
+ };
343
+ exports.makeTimeIntervals = makeTimeIntervals;
344
+ const makeDates = (startDateStr, days, fmt = HUMAN_DATE) => {
345
+ const startDate = (0, exports.dateStrToDate)(startDateStr);
346
+ const dateArray = [];
347
+ for (let step = 0; step < days + 1; step++) {
348
+ const nextDate = (0, date_fns_1.add)(startDate, { days: step });
349
+ const value = (0, date_fns_tz_1.format)(nextDate, DATE);
350
+ const label = value === (0, exports.todayDate)()
351
+ ? 'Today'
352
+ : value === (0, exports.tomorrowDate)()
353
+ ? 'Tomorrow'
354
+ : (0, date_fns_tz_1.format)(nextDate, fmt);
355
+ dateArray.push({ label, value });
356
+ }
357
+ return dateArray;
358
+ };
359
+ exports.makeDates = makeDates;
360
+ const makeTimes = (date, firstTime, validTimes, holidays, serviceType) => {
361
+ var _a;
362
+ const weekday = (0, exports.dateStrToZonedWeekday)(date);
363
+ let times = null;
364
+ const holiday = holidays
365
+ ? holidays[date]
366
+ : null;
367
+ if (holiday) {
368
+ times = holiday[serviceType] ? (_a = holiday[serviceType]) === null || _a === void 0 ? void 0 : _a.valid_times : null;
369
+ }
370
+ else {
371
+ const validTime = validTimes[serviceType];
372
+ times = validTime ? validTime[weekday] : null;
373
+ }
374
+ if (!times)
375
+ return null;
376
+ // if first available date, remove times before first available time
377
+ if (date === firstTime.date) {
378
+ times = times.filter(t => t.minutes >= firstTime.minutes);
379
+ }
380
+ return times.map(t => ({
381
+ name: t.time,
382
+ value: t.minutes,
383
+ disabled: !t.is_orderable
384
+ }));
385
+ };
386
+ exports.makeTimes = makeTimes;
387
+ const makeDatepickerArgs = (requestedAtDate, weekdayTimes, excludedTimes, firstTimes, interval = 15, daysAhead) => {
388
+ const minDate = (0, exports.dateStrToDate)(firstTimes.date);
389
+ const maxDate = daysAhead != null ? (0, date_fns_1.add)(new Date(), { days: daysAhead }) : null;
390
+ const currentDate = requestedAtDate || new Date();
391
+ const weekday = (0, date_fns_tz_1.format)(currentDate, 'EEEE').toUpperCase();
392
+ const dateStr = (0, date_fns_tz_1.format)(currentDate, 'yyyy-MM-dd');
393
+ const minutes = requestedAtDate ? (0, exports.getMinutesfromDate)(requestedAtDate) : null;
394
+ const isToday = dateStr === (0, exports.todayDate)();
395
+ /* if today, excluded all times before the first minute */
396
+ const lastTime = Math.ceil((firstTimes.minutes - interval) / interval) * interval;
397
+ const todayExcludeded = isToday ? [...range(0, lastTime, interval)] : [];
398
+ /* weekdayExcluded = times excluded based on regular hours + blocked hours */
399
+ const weekdayExcluded = weekdayTimes[weekday] || [];
400
+ /* otherExcluded = times excluded due to holiday hours + throttled times */
401
+ const otherExcluded = excludedTimes[dateStr] || [];
402
+ const allExcluded = [
403
+ ...new Set([...todayExcludeded, ...weekdayExcluded, ...otherExcluded])
404
+ ].sort();
405
+ const orderableTimes = (0, exports.makeOppositeTimes)(allExcluded, interval);
406
+ const minOrderableTime = Math.min(...orderableTimes);
407
+ // minTime and maxTime aren't necessary because they're already
408
+ // blocked out by the exluded times
409
+ // const minTime = minutesToDate(minOrderableTime)
410
+ // const maxOrderableTime = Math.max(...orderableTimes)
411
+ // const maxTime = minutesToDate(maxOrderableTime)
412
+ let updatedDate = null;
413
+ if (minutes && allExcluded.includes(minutes)) {
414
+ updatedDate = new Date(requestedAtDate.getTime());
415
+ updatedDate.setHours(Math.floor(minOrderableTime / 60));
416
+ updatedDate.setMinutes(minOrderableTime % 60);
417
+ }
418
+ /* TODO: figure out how to use this to hide times before open & after close */
419
+ // const minOrderableTime = Math.min(...orderableTimes)
420
+ // const maxOrderableTime = Math.max(...orderableTimes)
421
+ // const adjustedExcluded = makeOppositeTimes(
422
+ // orderableTimes,
423
+ // interval,
424
+ // minOrderableTime,
425
+ // maxOrderableTime
426
+ // )
427
+ // const injectTimes = minutesToDates(orderableTimes)
428
+ // const excludeTimes = minutesToDates(adjustedExcluded)
429
+ /* convert excluded minutes to excluded dates */
430
+ const excludeTimes = (0, exports.minutesToDates)(allExcluded);
431
+ /* filter out days of the week that are always closed */
432
+ const closedWeekdays = (0, exports.makeClosedWeekdays)(weekdayTimes);
433
+ const isClosed = (date) => {
434
+ return !closedWeekdays.includes(date.getDay());
435
+ };
436
+ return { excludeTimes, isClosed, updatedDate, minDate, maxDate };
437
+ };
438
+ exports.makeDatepickerArgs = makeDatepickerArgs;
439
+ const makeDatePickerTimes = (settings, serviceType, date) => {
440
+ var _a, _b;
441
+ if (!date)
442
+ return { excludeTimes: [], interval: 15 };
443
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
444
+ const interval = (_a = settings.first_times[st]) === null || _a === void 0 ? void 0 : _a.interval;
445
+ const firstTimes = settings.first_times[st];
446
+ const excludedTimes = settings.excluded_times
447
+ ? settings.excluded_times[st]
448
+ : {};
449
+ const validTimes = settings.valid_times[st];
450
+ const weekdayTimes = (0, exports.makeWeekdaysExcluded)(validTimes !== null && validTimes !== void 0 ? validTimes : {});
451
+ const weekday = (0, date_fns_tz_1.format)(date, 'EEEE').toUpperCase();
452
+ const dateStr = (0, date_fns_tz_1.format)(date, 'yyyy-MM-dd');
453
+ const isToday = dateStr === (0, exports.todayDate)();
454
+ /* if today, excluded all times before the first minute */
455
+ const lastTime = Math.ceil((((_b = firstTimes === null || firstTimes === void 0 ? void 0 : firstTimes.minutes) !== null && _b !== void 0 ? _b : 0) - (interval !== null && interval !== void 0 ? interval : 0)) / (interval !== null && interval !== void 0 ? interval : 1)) * (interval !== null && interval !== void 0 ? interval : 0);
456
+ const todayExcludeded = isToday ? [...range(0, lastTime, interval !== null && interval !== void 0 ? interval : 0)] : [];
457
+ /* weekdayExcluded = times excluded based on regular hours + blocked hours */
458
+ const weekdayExcluded = weekdayTimes[weekday] || [];
459
+ /* otherExcluded = times excluded due to holiday hours + throttled times */
460
+ const otherExcluded = excludedTimes[dateStr] || [];
461
+ /* combine exclued times, remove duplicates and sort */
462
+ const allExcluded = [
463
+ ...new Set([...todayExcludeded, ...weekdayExcluded, ...otherExcluded])
464
+ ].sort();
465
+ const excludeTimes = (0, exports.minutesToDates)(allExcluded);
466
+ return { excludeTimes, interval: interval !== null && interval !== void 0 ? interval : 0 };
467
+ };
468
+ exports.makeDatePickerTimes = makeDatePickerTimes;
469
+ const makeDatePickerDates = (settings, serviceType) => {
470
+ var _a;
471
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
472
+ const validTimes = settings.valid_times[st];
473
+ const daysAhead = settings.days_ahead[st];
474
+ const firstTimes = settings.first_times[st];
475
+ const holidays = settings.holidays; //holidays[st] // TODO:- type error
476
+ const excludeDates = holidays === null || holidays === void 0 ? void 0 : holidays.map((i) => (0, exports.makeLocalDate)(i));
477
+ const minDate = (0, exports.dateStrToDate)((_a = firstTimes === null || firstTimes === void 0 ? void 0 : firstTimes.date) !== null && _a !== void 0 ? _a : new Date().toISOString());
478
+ const maxDate = daysAhead != null ? (0, date_fns_1.add)(new Date(), { days: daysAhead }) : null;
479
+ const weekdayTimes = (0, exports.makeWeekdaysExcluded)(validTimes !== null && validTimes !== void 0 ? validTimes : {});
480
+ const closedWeekdays = (0, exports.makeClosedWeekdays)(weekdayTimes);
481
+ const filterDate = (date) => {
482
+ return !closedWeekdays.includes(date.getDay());
483
+ };
484
+ return { minDate, maxDate, excludeDates, filterDate };
485
+ };
486
+ exports.makeDatePickerDates = makeDatePickerDates;
487
+ const makeWeekdayIndices = (weekdays) => {
488
+ if (!weekdays)
489
+ return [];
490
+ return weekdays.map(weekday => exports.weekdaysUpper.indexOf(weekday));
491
+ };
492
+ exports.makeWeekdayIndices = makeWeekdayIndices;
493
+ const makeOrderTimes = (orderTimes, tz) => {
494
+ var _a;
495
+ const currentDate = new Date();
496
+ const withDates = orderTimes === null || orderTimes === void 0 ? void 0 : orderTimes.map(i => {
497
+ const { weekday, time } = i.order_by;
498
+ let orderByDate = (0, exports.weekdayAndTimeToDate)(weekday, time);
499
+ let startDate = (0, exports.weekdayAndTimeToDate)(i.weekday, i.start_time);
500
+ // if (orderByDate < currentDate || startDate < currentDate) {
501
+ // startDate = add(startDate, { days: 7 })
502
+ // }
503
+ if (orderByDate < currentDate) {
504
+ orderByDate = (0, date_fns_1.add)(orderByDate, { days: 7 });
505
+ }
506
+ if (startDate < orderByDate) {
507
+ startDate = (0, date_fns_1.add)(startDate, { days: 7 });
508
+ }
509
+ const orderBy = Object.assign(Object.assign({}, i.order_by), { date: orderByDate });
510
+ return Object.assign(Object.assign({}, i), { date: startDate, iso: (0, exports.dateToIso)(startDate, tz), order_by: orderBy });
511
+ });
512
+ return (_a = withDates === null || withDates === void 0 ? void 0 : withDates.sort((a, b) => a.date.getTime() - b.date.getTime())) !== null && _a !== void 0 ? _a : [];
513
+ };
514
+ exports.makeOrderTimes = makeOrderTimes;
515
+ const findOrderTime = (orderTimes, tz, requestedAt) => {
516
+ const sortedTimes = (0, exports.makeOrderTimes)(orderTimes, tz);
517
+ const selected = requestedAt
518
+ ? sortedTimes.find(i => i.iso === requestedAt)
519
+ : null;
520
+ return selected || sortedTimes[0];
521
+ };
522
+ exports.findOrderTime = findOrderTime;
523
+ const makeFirstTime = (settings, tz, serviceType, requestedAt) => {
524
+ var _a;
525
+ const { first_times, order_times } = settings;
526
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
527
+ if (!first_times || !first_times[st]) {
528
+ if (!order_times || !order_times[st] || !((_a = order_times[st]) === null || _a === void 0 ? void 0 : _a.length))
529
+ return null;
530
+ const selected = (0, exports.findOrderTime)(order_times[st], tz, requestedAt);
531
+ return selected.iso;
532
+ }
533
+ const firstTime = first_times[st];
534
+ const firstDate = firstTime ? (0, exports.isoToDate)(firstTime === null || firstTime === void 0 ? void 0 : firstTime.utc, tz) : new Date();
535
+ const hasAsap = firstTime === null || firstTime === void 0 ? void 0 : firstTime.has_asap;
536
+ if (requestedAt === 'asap' && hasAsap) {
537
+ return 'asap';
538
+ }
539
+ const requestedDate = requestedAt && requestedAt !== 'asap'
540
+ ? (0, exports.isoToDate)(requestedAt, tz)
541
+ : null;
542
+ if (requestedDate && requestedDate > firstDate) {
543
+ return requestedAt;
544
+ }
545
+ const { minutes, interval } = firstTime || {};
546
+ if (hasAsap && (minutes !== null && minutes !== void 0 ? minutes : 0) % (interval !== null && interval !== void 0 ? interval : 0) !== 0) {
547
+ return 'asap';
548
+ }
549
+ return firstTime === null || firstTime === void 0 ? void 0 : firstTime.utc;
550
+ };
551
+ exports.makeFirstTime = makeFirstTime;
552
+ const makeFirstRequestedAt = (revenueCenter, serviceType, requestedAt) => {
553
+ const { timezone, revenue_center_type } = revenueCenter;
554
+ const settings = revenueCenter.settings || revenueCenter;
555
+ if (revenue_center_type === 'POS')
556
+ return 'asap';
557
+ const tz = exports.timezoneMap[timezone];
558
+ requestedAt = requestedAt || (revenue_center_type === 'OLO' ? 'asap' : null);
559
+ return (0, exports.makeFirstTime)(settings, tz, serviceType, requestedAt);
560
+ };
561
+ exports.makeFirstRequestedAt = makeFirstRequestedAt;
562
+ const makeFirstTimes = (revenueCenter, serviceType, requestedAt) => {
563
+ const { timezone } = revenueCenter;
564
+ const settings = revenueCenter.settings || revenueCenter;
565
+ const tz = exports.timezoneMap[timezone];
566
+ const otherServiceType = serviceType === 'DELIVERY' ? 'PICKUP' : 'DELIVERY';
567
+ const current = (0, exports.makeFirstTime)(settings, tz, serviceType, requestedAt);
568
+ const other = (0, exports.makeFirstTime)(settings, tz, otherServiceType, requestedAt);
569
+ if (!current && !other)
570
+ return null;
571
+ return [
572
+ current ? { serviceType: serviceType, requestedAt: current } : null,
573
+ other ? { serviceType: otherServiceType, requestedAt: other } : null
574
+ ];
575
+ // return { [serviceType]: current, [otherServiceType]: other }
576
+ };
577
+ exports.makeFirstTimes = makeFirstTimes;
578
+ const getNextInterval = (requestedAt, tz, interval) => {
579
+ const date = (0, exports.isoToDate)(requestedAt, tz);
580
+ const intervals = interval === 15 ? [0, 15, 30, 45, 60] : [0, 30, 60];
581
+ const nextInterval = intervals.filter(i => i >= date.getMinutes())[0];
582
+ const hours = nextInterval === 60 ? date.getHours() + 1 : date.getHours();
583
+ const minutes = nextInterval === 60 ? 0 : nextInterval;
584
+ date.setHours(hours);
585
+ date.setMinutes(minutes);
586
+ date.setSeconds(0);
587
+ date.setMilliseconds(0);
588
+ return date;
589
+ };
590
+ exports.getNextInterval = getNextInterval;
591
+ const adjustRequestedAt = (requestedAt, tz, interval, leadTime) => {
592
+ const nextDate = (0, exports.getNextInterval)(requestedAt, tz, interval);
593
+ const adjusted = (0, date_fns_1.add)(nextDate, { minutes: leadTime });
594
+ return (0, exports.dateToIso)(adjusted, tz);
595
+ };
596
+ exports.adjustRequestedAt = adjustRequestedAt;
597
+ const getFirstTime = (settings, serviceType, tz) => {
598
+ var _a;
599
+ const { first_times, order_times } = settings;
600
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
601
+ if (!first_times || !first_times[st]) {
602
+ if (!order_times || !order_times[st])
603
+ return null;
604
+ const orderTimes = (0, exports.makeOrderTimes)((_a = order_times[st]) !== null && _a !== void 0 ? _a : [], tz);
605
+ return orderTimes[0];
606
+ }
607
+ return first_times[st];
608
+ };
609
+ exports.getFirstTime = getFirstTime;
610
+ const makeGroupOrderTime = (revenueCenter, serviceType, requestedAt) => {
611
+ const { timezone } = revenueCenter;
612
+ const settings = revenueCenter.settings || revenueCenter;
613
+ const tz = exports.timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Eastern'];
614
+ const { first_times, order_times, wait_times, group_ordering } = settings;
615
+ const { prep_time, lead_time } = group_ordering || {};
616
+ const st = serviceType === 'WALKIN' ? 'PICKUP' : serviceType;
617
+ const waitTime = wait_times && wait_times[st] ? wait_times[st] : 0;
618
+ const prepTime = `${(waitTime !== null && waitTime !== void 0 ? waitTime : 0) + (prep_time !== null && prep_time !== void 0 ? prep_time : 0)}`;
619
+ if (requestedAt === 'asap')
620
+ return { prepTime };
621
+ const firstTime = first_times && first_times[st] ? first_times[st] : null;
622
+ const orderTimes = order_times && order_times[st] ? order_times[st] : null;
623
+ if (!firstTime && !orderTimes)
624
+ return {};
625
+ let adjustedIso, adjustedDate, cutoffDate, firstIso;
626
+ if (orderTimes) {
627
+ const orderTime = (0, exports.findOrderTime)(orderTimes, tz, requestedAt);
628
+ adjustedIso = orderTime.iso;
629
+ adjustedDate = (0, exports.isoToDate)(adjustedIso, tz);
630
+ cutoffDate = orderTime.order_by.date;
631
+ const allOrderTimes = (0, exports.makeOrderTimes)(orderTimes, tz);
632
+ firstIso = allOrderTimes[0].iso;
633
+ }
634
+ else {
635
+ // first available time depends on both suggested lead time and extra prep time
636
+ // lead_time = how much time cart guests have to place their orders
637
+ // prep_time = how much extra prep time is required for group orders
638
+ // over and above the pickup or delivery wait time
639
+ const leadTime = (prep_time !== null && prep_time !== void 0 ? prep_time : 0) + (lead_time !== null && lead_time !== void 0 ? lead_time : 0);
640
+ const interval = (firstTime === null || firstTime === void 0 ? void 0 : firstTime.interval) || 15;
641
+ firstIso = (0, exports.adjustRequestedAt)(firstTime === null || firstTime === void 0 ? void 0 : firstTime.utc, tz, interval, leadTime);
642
+ const firstDate = (0, exports.isoToDate)(firstIso, tz);
643
+ const requestedDate = requestedAt !== 'asap' ? (0, exports.isoToDate)(requestedAt, tz) : null;
644
+ adjustedIso =
645
+ requestedDate && requestedDate > firstDate ? requestedAt : firstIso;
646
+ adjustedDate = (0, exports.isoToDate)(adjustedIso, tz);
647
+ cutoffDate = (0, date_fns_1.sub)(adjustedDate, { minutes: parseFloat(prepTime) });
648
+ }
649
+ const cutoffIso = (0, exports.dateToIso)(cutoffDate, tz);
650
+ return {
651
+ isAdjusted: requestedAt !== adjustedIso,
652
+ firstIso,
653
+ iso: adjustedIso,
654
+ date: adjustedDate,
655
+ dateStr: (0, exports.makeReadableDateStrFromIso)(adjustedIso !== null && adjustedIso !== void 0 ? adjustedIso : '', tz, true),
656
+ cutoffIso,
657
+ cutoffDate,
658
+ cutoffDateStr: (0, exports.makeReadableDateStrFromIso)(cutoffIso, tz, true)
659
+ };
660
+ };
661
+ exports.makeGroupOrderTime = makeGroupOrderTime;
662
+ const formatTime = (time) => {
663
+ return time
664
+ ? time.replace('Today', 'today').replace('Tomorrow', 'tomorrow')
665
+ : '';
666
+ };
667
+ exports.formatTime = formatTime;
668
+ const makeGroupOrderTimeStr = (iso, tz) => {
669
+ if (!iso || iso.toLowerCase() === 'asap')
670
+ return 'ASAP';
671
+ const orderTime = iso && tz ? (0, exports.makeReadableDateStrFromIso)(iso, tz, true) : null;
672
+ return orderTime ? (0, exports.formatTime)(orderTime) : null;
673
+ };
674
+ exports.makeGroupOrderTimeStr = makeGroupOrderTimeStr;
675
+ const getLastInterval = (tz) => {
676
+ const date = (0, exports.isoToDate)(new Date().toISOString(), tz);
677
+ date.setHours(23);
678
+ date.setMinutes(45);
679
+ date.setSeconds(0);
680
+ date.setMilliseconds(0);
681
+ return date;
682
+ };
683
+ exports.getLastInterval = getLastInterval;
684
+ const makeIntervals = (tz) => {
685
+ const nextInterval = (0, exports.getNextInterval)(new Date().toISOString(), tz, 15);
686
+ const lastInterval = (0, exports.getLastInterval)(tz);
687
+ const diff = Math.max((0, date_fns_1.differenceInMinutes)(lastInterval, nextInterval), 0);
688
+ const steps = 24 * 4 + (diff ? Math.ceil(diff / 15) : 0);
689
+ let start = (0, date_fns_1.sub)(nextInterval, { minutes: 24 * 60 });
690
+ const intervals = [];
691
+ for (let step = 0; step < steps; step++) {
692
+ const end = (0, date_fns_1.add)(start, { minutes: 15 });
693
+ intervals.push({ start, end, orders: [] });
694
+ start = end;
695
+ }
696
+ return intervals;
697
+ };
698
+ exports.makeIntervals = makeIntervals;
699
+ const formatTimeList = (dateStr, tz, includeDate) => {
700
+ const date = (0, exports.isoToDate)(dateStr, tz);
701
+ const fmt = includeDate ? 'MMM d, h:mma' : 'h:mma';
702
+ return (0, date_fns_tz_1.format)(date, fmt).replace('AM', 'am').replace('PM', 'pm');
703
+ };
704
+ exports.formatTimeList = formatTimeList;