@open-tender/utils 0.4.52 → 0.4.54

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,4 +1,4 @@
1
- import { Address, FirstTimes, Hours, OrderType, RevenueCenter, ServiceType, Timezone, TimezonePython } from '@open-tender/types';
1
+ import { Address, OrderType, RevenueCenter, ServiceType } 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,5 +9,5 @@ export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, servic
9
9
  message: any;
10
10
  color: string;
11
11
  };
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;
12
+ export declare const isRevenueCenterOpen: (revenueCenter: RevenueCenter, serviceType: ServiceType) => boolean;
13
+ export declare const getRevenueCenterStatus: (revenueCenter: RevenueCenter, serviceType: ServiceType) => string;
@@ -5,7 +5,6 @@ const constants_1 = require("./constants");
5
5
  const datetimes_1 = require("./datetimes");
6
6
  const helpers_1 = require("./helpers");
7
7
  const date_fns_1 = require("date-fns");
8
- const date_fns_tz_1 = require("date-fns-tz");
9
8
  const makeOrderAddress = (address) => {
10
9
  const { street, unit, city, state, postal_code } = address || {};
11
10
  return street
@@ -125,34 +124,24 @@ const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMes
125
124
  return { message, color };
126
125
  };
127
126
  exports.makeRevenueCenterMsg = makeRevenueCenterMsg;
128
- const isRevenueCenterOpen = (hours, timezone, serviceType) => {
129
- const serviceHours = hours ? hours[serviceType] : null;
130
- if (!serviceHours)
127
+ const isRevenueCenterOpen = (revenueCenter, serviceType) => {
128
+ const { status, first_times, order_times } = revenueCenter;
129
+ const tempClosed = status === 'CLOSED_TEMPORARILY';
130
+ if (tempClosed)
131
131
  return false;
132
- const now = new Date();
133
- const zonedDate = (0, date_fns_tz_1.utcToZonedTime)(now, timezone);
134
- const currentWeekday = (0, date_fns_1.format)(zonedDate, 'EEEE').toUpperCase();
135
- const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
136
- if (!todayHours || !todayHours.open || !todayHours.close)
137
- return false;
138
- const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
139
- const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
140
- const openingTime = (0, date_fns_1.set)(zonedDate, { hours: openHours, minutes: openMinutes });
141
- const closingTime = (0, date_fns_1.set)(zonedDate, {
142
- hours: closeHours,
143
- minutes: closeMinutes
144
- });
145
- return (0, date_fns_1.isBefore)(openingTime, zonedDate) && (0, date_fns_1.isAfter)(closingTime, zonedDate);
132
+ const serviceOrderTimes = order_times ? order_times[serviceType] : null;
133
+ const serviceFirstTimes = first_times ? first_times[serviceType] : null;
134
+ const isHasFirstTimes = !!serviceFirstTimes;
135
+ const isHasOrderTime = !!serviceOrderTimes && serviceOrderTimes.length > 0;
136
+ return !isHasFirstTimes && !isHasOrderTime;
146
137
  };
147
138
  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);
139
+ const getRevenueCenterStatus = (revenueCenter, serviceType) => {
140
+ const { first_times } = revenueCenter;
141
+ const isOpen = (0, exports.isRevenueCenterOpen)(revenueCenter, serviceType);
153
142
  if (isOpen)
154
143
  return 'Open Now';
155
- const serviceFirstTimes = firstTimes[serviceType];
144
+ const serviceFirstTimes = first_times ? first_times[serviceType] : null;
156
145
  if (!serviceFirstTimes)
157
146
  return 'Closed';
158
147
  const firstTimeDate = (0, date_fns_1.parseISO)(serviceFirstTimes.date);
@@ -1,4 +1,4 @@
1
- import { Address, FirstTimes, Hours, OrderType, RevenueCenter, ServiceType, Timezone, TimezonePython } from '@open-tender/types';
1
+ import { Address, OrderType, RevenueCenter, ServiceType } 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,5 +9,5 @@ export declare const makeRevenueCenterMsg: (revenueCenter: RevenueCenter, servic
9
9
  message: any;
10
10
  color: string;
11
11
  };
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;
12
+ export declare const isRevenueCenterOpen: (revenueCenter: RevenueCenter, serviceType: ServiceType) => boolean;
13
+ export declare const getRevenueCenterStatus: (revenueCenter: RevenueCenter, serviceType: ServiceType) => string;
@@ -1,8 +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, parseISO, isToday, isTomorrow } from 'date-fns';
5
- import { utcToZonedTime } from 'date-fns-tz';
4
+ import { format, parseISO, isToday, isTomorrow } from 'date-fns';
6
5
  export const makeOrderAddress = (address) => {
7
6
  const { street, unit, city, state, postal_code } = address || {};
8
7
  return street
@@ -115,33 +114,23 @@ export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, st
115
114
  }
116
115
  return { message, color };
117
116
  };
118
- export const isRevenueCenterOpen = (hours, timezone, serviceType) => {
119
- const serviceHours = hours ? hours[serviceType] : null;
120
- if (!serviceHours)
117
+ export const isRevenueCenterOpen = (revenueCenter, serviceType) => {
118
+ const { status, first_times, order_times } = revenueCenter;
119
+ const tempClosed = status === 'CLOSED_TEMPORARILY';
120
+ if (tempClosed)
121
121
  return false;
122
- const now = new Date();
123
- const zonedDate = utcToZonedTime(now, timezone);
124
- const currentWeekday = format(zonedDate, 'EEEE').toUpperCase();
125
- const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
126
- if (!todayHours || !todayHours.open || !todayHours.close)
127
- return false;
128
- const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
129
- const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
130
- const openingTime = set(zonedDate, { hours: openHours, minutes: openMinutes });
131
- const closingTime = set(zonedDate, {
132
- hours: closeHours,
133
- minutes: closeMinutes
134
- });
135
- return isBefore(openingTime, zonedDate) && isAfter(closingTime, zonedDate);
122
+ const serviceOrderTimes = order_times ? order_times[serviceType] : null;
123
+ const serviceFirstTimes = first_times ? first_times[serviceType] : null;
124
+ const isHasFirstTimes = !!serviceFirstTimes;
125
+ const isHasOrderTime = !!serviceOrderTimes && serviceOrderTimes.length > 0;
126
+ return !isHasFirstTimes && !isHasOrderTime;
136
127
  };
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);
128
+ export const getRevenueCenterStatus = (revenueCenter, serviceType) => {
129
+ const { first_times } = revenueCenter;
130
+ const isOpen = isRevenueCenterOpen(revenueCenter, serviceType);
142
131
  if (isOpen)
143
132
  return 'Open Now';
144
- const serviceFirstTimes = firstTimes[serviceType];
133
+ const serviceFirstTimes = first_times ? first_times[serviceType] : null;
145
134
  if (!serviceFirstTimes)
146
135
  return 'Closed';
147
136
  const firstTimeDate = parseISO(serviceFirstTimes.date);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.4.52",
3
+ "version": "0.4.54",
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,6 +36,7 @@
36
36
  "private": false,
37
37
  "devDependencies": {
38
38
  "@emotion/react": "^11.10.4",
39
+ "@open-tender/types": "^0.4.77",
39
40
  "@reduxjs/toolkit": "^1.8.5",
40
41
  "@types/google.maps": "^3.53.5",
41
42
  "@types/jest": "^29.5.11",
@@ -1 +0,0 @@
1
- export {};
@@ -1,75 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("../../utils");
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
44
- }
45
- };
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);
59
- });
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);
65
- });
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');
68
- });
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');
74
- });
75
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,73 +0,0 @@
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
42
- }
43
- };
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);
57
- });
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);
63
- });
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');
66
- });
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');
72
- });
73
- });