@open-tender/utils 0.4.40 → 0.4.42

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,61 +1,75 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const date_fns_1 = require("date-fns");
4
- const date_fns_tz_1 = require("date-fns-tz");
5
3
  const utils_1 = require("../../utils");
6
- jest.mock('date-fns');
7
- jest.mock('date-fns-tz');
8
- describe('isRevenueCenterOpen', () => {
9
- const mockDate = new Date('2024-06-11T12:00:00Z');
10
- beforeEach(() => {
11
- jest.resetAllMocks();
12
- date_fns_tz_1.utcToZonedTime.mockReturnValue(mockDate);
13
- date_fns_1.format.mockImplementation((_, formatString) => {
14
- if (formatString === 'EEEE')
15
- return 'TUESDAY';
16
- if (formatString === 'HH:mm')
17
- return '07:00';
18
- return '';
19
- });
20
- });
21
- const revenueCenter = {
22
- timezone: 'US/Central',
23
- hours: {
24
- description: "",
25
- WALKIN: [
26
- {
27
- close: '22:00',
28
- holiday: null,
29
- open: '05:00',
30
- weekday: 'TUESDAY'
31
- },
32
- ],
33
- PICKUP: null,
34
- DELIVERY: null,
4
+ describe('Revenue Center Status', () => {
5
+ const hours = {
6
+ WALKIN: [
7
+ { weekday: 'MONDAY', open: '08:00', close: '17:00', holiday: null },
8
+ { weekday: 'TUESDAY', open: '08:00', close: '17:00', holiday: null },
9
+ { weekday: 'WEDNESDAY', open: '08:00', close: '17:00', holiday: null },
10
+ { weekday: 'THURSDAY', open: '08:00', close: '17:00', holiday: null },
11
+ { weekday: 'FRIDAY', open: '08:00', close: '17:00', holiday: null }
12
+ ],
13
+ DELIVERY: null,
14
+ PICKUP: null,
15
+ description: 'Example Revenue Center'
16
+ };
17
+ const firstTimes = {
18
+ WALKIN: {
19
+ date: '2024-06-13',
20
+ utc: '2024-06-13T18:30:00Z',
21
+ has_asap: false,
22
+ weekday: 'THURSDAY',
23
+ time: '18:30 PM',
24
+ minutes: 30,
25
+ wait_minutes: null
26
+ },
27
+ DELIVERY: {
28
+ date: '2024-06-13',
29
+ utc: '2024-06-13T18:30:00Z',
30
+ has_asap: false,
31
+ weekday: 'THURSDAY',
32
+ time: '18:30 PM',
33
+ minutes: 30,
34
+ wait_minutes: null
35
+ },
36
+ PICKUP: {
37
+ date: '2024-06-13',
38
+ utc: '2024-06-13T18:30:00Z',
39
+ has_asap: false,
40
+ weekday: 'THURSDAY',
41
+ time: '18:30 PM',
42
+ minutes: 30,
43
+ wait_minutes: null
35
44
  }
36
45
  };
37
- it('returns true if the current time is within the operating hours', () => {
38
- date_fns_1.parse.mockReturnValueOnce(new Date('2024-06-11T05:00:00-05:00'));
39
- date_fns_1.parse.mockReturnValueOnce(new Date('2024-06-11T22:00:00-05:00'));
40
- date_fns_1.isBefore.mockReturnValue(true);
41
- date_fns_1.isAfter.mockReturnValue(true);
42
- const result = (0, utils_1.isRevenueCenterOpen)(revenueCenter, 'WALKIN');
43
- expect(result).toBe(true);
46
+ const timezone = 'America/New_York';
47
+ const pythonTimezone = 'US/Central';
48
+ const serviceType = 'WALKIN';
49
+ beforeEach(() => {
50
+ jest
51
+ .spyOn(global.Date, 'now')
52
+ .mockImplementation(() => new Date('2024-06-13T12:00:00Z').valueOf());
53
+ });
54
+ afterEach(() => {
55
+ jest.restoreAllMocks();
56
+ });
57
+ test('isRevenueCenterOpen - should return true if center is open now', () => {
58
+ expect((0, utils_1.isRevenueCenterOpen)(hours, timezone, serviceType)).toBe(true);
44
59
  });
45
- it('returns false if the current time is outside the operating hours', () => {
46
- date_fns_1.parse.mockReturnValueOnce(new Date('2024-06-11T05:00:00-05:00'));
47
- date_fns_1.parse.mockReturnValueOnce(new Date('2024-06-11T22:00:00-05:00'));
48
- date_fns_1.isBefore.mockReturnValue(false);
49
- date_fns_1.isAfter.mockReturnValue(true);
50
- const result = (0, utils_1.isRevenueCenterOpen)(revenueCenter, 'WALKIN');
51
- expect(result).toBe(false);
60
+ test('isRevenueCenterOpen - should return false if center is closed now', () => {
61
+ jest
62
+ .spyOn(global.Date, 'now')
63
+ .mockImplementation(() => new Date('2024-06-13T18:00:00Z').valueOf());
64
+ expect((0, utils_1.isRevenueCenterOpen)(hours, timezone, serviceType)).toBe(false);
52
65
  });
53
- it('returns false if there are no service hours for the given service type', () => {
54
- const result = (0, utils_1.isRevenueCenterOpen)(revenueCenter, 'DELIVERY');
55
- expect(result).toBe(false);
66
+ test('getRevenueCenterStatus - should return "Currently Closed, Order for later today"', () => {
67
+ expect((0, utils_1.getRevenueCenterStatus)(hours, firstTimes, serviceType, pythonTimezone)).toBe('Currently Closed, Order for later today');
56
68
  });
57
- it('returns false if there are no hours for the current weekday', () => {
58
- const result = (0, utils_1.isRevenueCenterOpen)(revenueCenter, 'WALKIN');
59
- expect(result).toBe(false);
69
+ test('getRevenueCenterStatus - should return "Closed for the Day, Order for tomorrow at 5:00 PM"', () => {
70
+ jest
71
+ .spyOn(global.Date, 'now')
72
+ .mockImplementation(() => new Date('2024-06-13T22:00:00Z').valueOf());
73
+ expect((0, utils_1.getRevenueCenterStatus)(hours, firstTimes, serviceType, pythonTimezone)).toBe('Closed for the Day, Order for June 14th at 5:00 PM');
60
74
  });
61
75
  });
@@ -1,4 +1,4 @@
1
- import { Address, OrderType, RevenueCenter, ServiceType } from '@open-tender/types';
1
+ import { Address, FirstTimes, Hours, OrderType, RevenueCenter, ServiceType, Timezone, TimezonePython } from '@open-tender/types';
2
2
  export declare const makeOrderAddress: (address: Address | null) => string;
3
3
  export declare const makeFullAddress: (address: Address) => string;
4
4
  export declare const makeOrderTypeFromParam: (param: string) => (string | boolean)[] | null;
@@ -9,4 +9,5 @@ export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, servic
9
9
  message: any;
10
10
  color: string;
11
11
  };
12
- export declare const isRevenueCenterOpen: (revenueCenter: RevenueCenter, serviceType: ServiceType) => boolean;
12
+ export declare const isRevenueCenterOpen: (hours: Hours, timezone: Timezone, serviceType: ServiceType) => boolean;
13
+ export declare const getRevenueCenterStatus: (hours: Hours | null, firstTimes: FirstTimes | null, serviceType: ServiceType | null, timezone: TimezonePython) => string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isRevenueCenterOpen = exports.makeRevenueCenterMsg = exports.makeNotAcceptingOrdersMsg = exports.makeOrderTypeName = exports.makeServiceTypeName = exports.makeOrderTypeFromParam = exports.makeFullAddress = exports.makeOrderAddress = void 0;
3
+ exports.getRevenueCenterStatus = exports.isRevenueCenterOpen = exports.makeRevenueCenterMsg = exports.makeNotAcceptingOrdersMsg = exports.makeOrderTypeName = exports.makeServiceTypeName = exports.makeOrderTypeFromParam = exports.makeFullAddress = exports.makeOrderAddress = void 0;
4
4
  const constants_1 = require("./constants");
5
5
  const datetimes_1 = require("./datetimes");
6
6
  const helpers_1 = require("./helpers");
@@ -125,14 +125,12 @@ const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMes
125
125
  return { message, color };
126
126
  };
127
127
  exports.makeRevenueCenterMsg = makeRevenueCenterMsg;
128
- const isRevenueCenterOpen = (revenueCenter, serviceType) => {
129
- const { hours, timezone } = revenueCenter;
128
+ const isRevenueCenterOpen = (hours, timezone, serviceType) => {
130
129
  const serviceHours = hours ? hours[serviceType] : null;
131
130
  if (!serviceHours)
132
131
  return false;
133
- const tz = datetimes_1.timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
134
132
  const now = new Date();
135
- const zonedDate = (0, date_fns_tz_1.utcToZonedTime)(now, tz);
133
+ const zonedDate = (0, date_fns_tz_1.utcToZonedTime)(now, timezone);
136
134
  const currentWeekday = (0, date_fns_1.format)(zonedDate, 'EEEE').toUpperCase();
137
135
  const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
138
136
  if (!todayHours || !todayHours.open || !todayHours.close)
@@ -140,7 +138,28 @@ const isRevenueCenterOpen = (revenueCenter, serviceType) => {
140
138
  const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
141
139
  const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
142
140
  const openingTime = (0, date_fns_1.set)(zonedDate, { hours: openHours, minutes: openMinutes });
143
- const closingTime = (0, date_fns_1.set)(zonedDate, { hours: closeHours, minutes: closeMinutes });
141
+ const closingTime = (0, date_fns_1.set)(zonedDate, {
142
+ hours: closeHours,
143
+ minutes: closeMinutes
144
+ });
144
145
  return (0, date_fns_1.isBefore)(openingTime, zonedDate) && (0, date_fns_1.isAfter)(closingTime, zonedDate);
145
146
  };
146
147
  exports.isRevenueCenterOpen = isRevenueCenterOpen;
148
+ const getRevenueCenterStatus = (hours, firstTimes, serviceType, timezone) => {
149
+ if (!hours || !serviceType || !firstTimes)
150
+ return 'Closed';
151
+ const tz = datetimes_1.timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
152
+ const isOpen = (0, exports.isRevenueCenterOpen)(hours, tz, serviceType);
153
+ if (isOpen)
154
+ return 'Open Now';
155
+ const serviceFirstTimes = firstTimes[serviceType];
156
+ if (!serviceFirstTimes)
157
+ return 'Closed';
158
+ const firstTimeDate = (0, date_fns_1.parseISO)(serviceFirstTimes.date);
159
+ const canOrderToday = (0, date_fns_1.isToday)(firstTimeDate);
160
+ if (canOrderToday)
161
+ return 'Currently Closed, Order for later today';
162
+ const nextAvailableDate = (0, datetimes_1.makeReadableDateStrFromIso)(serviceFirstTimes.utc, tz, true);
163
+ return `Closed for the Day, Order for ${nextAvailableDate}`;
164
+ };
165
+ exports.getRevenueCenterStatus = getRevenueCenterStatus;
@@ -1,59 +1,73 @@
1
- import { format, parse, isBefore, isAfter } from 'date-fns';
2
- import { utcToZonedTime } from 'date-fns-tz';
3
- import { isRevenueCenterOpen } from '../../utils';
4
- jest.mock('date-fns');
5
- jest.mock('date-fns-tz');
6
- describe('isRevenueCenterOpen', () => {
7
- const mockDate = new Date('2024-06-11T12:00:00Z');
8
- beforeEach(() => {
9
- jest.resetAllMocks();
10
- utcToZonedTime.mockReturnValue(mockDate);
11
- format.mockImplementation((_, formatString) => {
12
- if (formatString === 'EEEE')
13
- return 'TUESDAY';
14
- if (formatString === 'HH:mm')
15
- return '07:00';
16
- return '';
17
- });
18
- });
19
- const revenueCenter = {
20
- timezone: 'US/Central',
21
- hours: {
22
- description: "",
23
- WALKIN: [
24
- {
25
- close: '22:00',
26
- holiday: null,
27
- open: '05:00',
28
- weekday: 'TUESDAY'
29
- },
30
- ],
31
- PICKUP: null,
32
- DELIVERY: null,
1
+ import { getRevenueCenterStatus, isRevenueCenterOpen } from '../../utils';
2
+ describe('Revenue Center Status', () => {
3
+ const hours = {
4
+ WALKIN: [
5
+ { weekday: 'MONDAY', open: '08:00', close: '17:00', holiday: null },
6
+ { weekday: 'TUESDAY', open: '08:00', close: '17:00', holiday: null },
7
+ { weekday: 'WEDNESDAY', open: '08:00', close: '17:00', holiday: null },
8
+ { weekday: 'THURSDAY', open: '08:00', close: '17:00', holiday: null },
9
+ { weekday: 'FRIDAY', open: '08:00', close: '17:00', holiday: null }
10
+ ],
11
+ DELIVERY: null,
12
+ PICKUP: null,
13
+ description: 'Example Revenue Center'
14
+ };
15
+ const firstTimes = {
16
+ WALKIN: {
17
+ date: '2024-06-13',
18
+ utc: '2024-06-13T18:30:00Z',
19
+ has_asap: false,
20
+ weekday: 'THURSDAY',
21
+ time: '18:30 PM',
22
+ minutes: 30,
23
+ wait_minutes: null
24
+ },
25
+ DELIVERY: {
26
+ date: '2024-06-13',
27
+ utc: '2024-06-13T18:30:00Z',
28
+ has_asap: false,
29
+ weekday: 'THURSDAY',
30
+ time: '18:30 PM',
31
+ minutes: 30,
32
+ wait_minutes: null
33
+ },
34
+ PICKUP: {
35
+ date: '2024-06-13',
36
+ utc: '2024-06-13T18:30:00Z',
37
+ has_asap: false,
38
+ weekday: 'THURSDAY',
39
+ time: '18:30 PM',
40
+ minutes: 30,
41
+ wait_minutes: null
33
42
  }
34
43
  };
35
- it('returns true if the current time is within the operating hours', () => {
36
- parse.mockReturnValueOnce(new Date('2024-06-11T05:00:00-05:00'));
37
- parse.mockReturnValueOnce(new Date('2024-06-11T22:00:00-05:00'));
38
- isBefore.mockReturnValue(true);
39
- isAfter.mockReturnValue(true);
40
- const result = isRevenueCenterOpen(revenueCenter, 'WALKIN');
41
- expect(result).toBe(true);
44
+ const timezone = 'America/New_York';
45
+ const pythonTimezone = 'US/Central';
46
+ const serviceType = 'WALKIN';
47
+ beforeEach(() => {
48
+ jest
49
+ .spyOn(global.Date, 'now')
50
+ .mockImplementation(() => new Date('2024-06-13T12:00:00Z').valueOf());
51
+ });
52
+ afterEach(() => {
53
+ jest.restoreAllMocks();
54
+ });
55
+ test('isRevenueCenterOpen - should return true if center is open now', () => {
56
+ expect(isRevenueCenterOpen(hours, timezone, serviceType)).toBe(true);
42
57
  });
43
- it('returns false if the current time is outside the operating hours', () => {
44
- parse.mockReturnValueOnce(new Date('2024-06-11T05:00:00-05:00'));
45
- parse.mockReturnValueOnce(new Date('2024-06-11T22:00:00-05:00'));
46
- isBefore.mockReturnValue(false);
47
- isAfter.mockReturnValue(true);
48
- const result = isRevenueCenterOpen(revenueCenter, 'WALKIN');
49
- expect(result).toBe(false);
58
+ test('isRevenueCenterOpen - should return false if center is closed now', () => {
59
+ jest
60
+ .spyOn(global.Date, 'now')
61
+ .mockImplementation(() => new Date('2024-06-13T18:00:00Z').valueOf());
62
+ expect(isRevenueCenterOpen(hours, timezone, serviceType)).toBe(false);
50
63
  });
51
- it('returns false if there are no service hours for the given service type', () => {
52
- const result = isRevenueCenterOpen(revenueCenter, 'DELIVERY');
53
- expect(result).toBe(false);
64
+ test('getRevenueCenterStatus - should return "Currently Closed, Order for later today"', () => {
65
+ expect(getRevenueCenterStatus(hours, firstTimes, serviceType, pythonTimezone)).toBe('Currently Closed, Order for later today');
54
66
  });
55
- it('returns false if there are no hours for the current weekday', () => {
56
- const result = isRevenueCenterOpen(revenueCenter, 'WALKIN');
57
- expect(result).toBe(false);
67
+ test('getRevenueCenterStatus - should return "Closed for the Day, Order for tomorrow at 5:00 PM"', () => {
68
+ jest
69
+ .spyOn(global.Date, 'now')
70
+ .mockImplementation(() => new Date('2024-06-13T22:00:00Z').valueOf());
71
+ expect(getRevenueCenterStatus(hours, firstTimes, serviceType, pythonTimezone)).toBe('Closed for the Day, Order for June 14th at 5:00 PM');
58
72
  });
59
73
  });
@@ -1,4 +1,4 @@
1
- import { Address, OrderType, RevenueCenter, ServiceType } from '@open-tender/types';
1
+ import { Address, FirstTimes, Hours, OrderType, RevenueCenter, ServiceType, Timezone, TimezonePython } from '@open-tender/types';
2
2
  export declare const makeOrderAddress: (address: Address | null) => string;
3
3
  export declare const makeFullAddress: (address: Address) => string;
4
4
  export declare const makeOrderTypeFromParam: (param: string) => (string | boolean)[] | null;
@@ -9,4 +9,5 @@ export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, servic
9
9
  message: any;
10
10
  color: string;
11
11
  };
12
- export declare const isRevenueCenterOpen: (revenueCenter: RevenueCenter, serviceType: ServiceType) => boolean;
12
+ export declare const isRevenueCenterOpen: (hours: Hours, timezone: Timezone, serviceType: ServiceType) => boolean;
13
+ export declare const getRevenueCenterStatus: (hours: Hours | null, firstTimes: FirstTimes | null, serviceType: ServiceType | null, timezone: TimezonePython) => string;
@@ -1,7 +1,7 @@
1
1
  import { serviceTypeNamesMap } from './constants';
2
2
  import { fmtDate, formatDateStr, isoToDate, makeOrderTimes, makeReadableDateStrFromIso, makeWeekday, time24ToDate, timezoneMap } from './datetimes';
3
3
  import { capitalize } from './helpers';
4
- import { isAfter, isBefore, format, set } from 'date-fns';
4
+ import { isAfter, isBefore, format, set, parseISO, isToday } from 'date-fns';
5
5
  import { utcToZonedTime } from 'date-fns-tz';
6
6
  export const makeOrderAddress = (address) => {
7
7
  const { street, unit, city, state, postal_code } = address || {};
@@ -115,14 +115,12 @@ export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, st
115
115
  }
116
116
  return { message, color };
117
117
  };
118
- export const isRevenueCenterOpen = (revenueCenter, serviceType) => {
119
- const { hours, timezone } = revenueCenter;
118
+ export const isRevenueCenterOpen = (hours, timezone, serviceType) => {
120
119
  const serviceHours = hours ? hours[serviceType] : null;
121
120
  if (!serviceHours)
122
121
  return false;
123
- const tz = timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
124
122
  const now = new Date();
125
- const zonedDate = utcToZonedTime(now, tz);
123
+ const zonedDate = utcToZonedTime(now, timezone);
126
124
  const currentWeekday = format(zonedDate, 'EEEE').toUpperCase();
127
125
  const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
128
126
  if (!todayHours || !todayHours.open || !todayHours.close)
@@ -130,6 +128,26 @@ export const isRevenueCenterOpen = (revenueCenter, serviceType) => {
130
128
  const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
131
129
  const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
132
130
  const openingTime = set(zonedDate, { hours: openHours, minutes: openMinutes });
133
- const closingTime = set(zonedDate, { hours: closeHours, minutes: closeMinutes });
131
+ const closingTime = set(zonedDate, {
132
+ hours: closeHours,
133
+ minutes: closeMinutes
134
+ });
134
135
  return isBefore(openingTime, zonedDate) && isAfter(closingTime, zonedDate);
135
136
  };
137
+ export const getRevenueCenterStatus = (hours, firstTimes, serviceType, timezone) => {
138
+ if (!hours || !serviceType || !firstTimes)
139
+ return 'Closed';
140
+ const tz = timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
141
+ const isOpen = isRevenueCenterOpen(hours, tz, serviceType);
142
+ if (isOpen)
143
+ return 'Open Now';
144
+ const serviceFirstTimes = firstTimes[serviceType];
145
+ if (!serviceFirstTimes)
146
+ return 'Closed';
147
+ const firstTimeDate = parseISO(serviceFirstTimes.date);
148
+ const canOrderToday = isToday(firstTimeDate);
149
+ if (canOrderToday)
150
+ return 'Currently Closed, Order for later today';
151
+ const nextAvailableDate = makeReadableDateStrFromIso(serviceFirstTimes.utc, tz, true);
152
+ return `Closed for the Day, Order for ${nextAvailableDate}`;
153
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.4.40",
3
+ "version": "0.4.42",
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",