@open-tender/utils 0.4.41 → 0.4.43
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.
- package/dist/cjs/__tests__/utils/revenueCenters.test.js +65 -51
- package/dist/cjs/utils/revenueCenters.d.ts +3 -2
- package/dist/cjs/utils/revenueCenters.js +27 -6
- package/dist/esm/__tests__/utils/revenueCenters.test.js +66 -52
- package/dist/esm/utils/revenueCenters.d.ts +3 -2
- package/dist/esm/utils/revenueCenters.js +26 -6
- package/package.json +1 -1
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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: (
|
|
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 = (
|
|
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,
|
|
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,30 @@ 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, {
|
|
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, date_fns_1.isTomorrow)(firstTimeDate)
|
|
163
|
+
? 'tomorrow'
|
|
164
|
+
: (0, date_fns_1.format)(firstTimeDate, 'EEEE');
|
|
165
|
+
return `Closed for the Day, Order for ${nextAvailableDate}`;
|
|
166
|
+
};
|
|
167
|
+
exports.getRevenueCenterStatus = getRevenueCenterStatus;
|
|
@@ -1,59 +1,73 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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: (
|
|
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, isTomorrow } 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 = (
|
|
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,
|
|
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,28 @@ 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, {
|
|
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 = isTomorrow(firstTimeDate)
|
|
152
|
+
? 'tomorrow'
|
|
153
|
+
: format(firstTimeDate, 'EEEE');
|
|
154
|
+
return `Closed for the Day, Order for ${nextAvailableDate}`;
|
|
155
|
+
};
|
package/package.json
CHANGED