@open-tender/utils 0.4.38 → 0.4.40
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.d.ts +1 -0
- package/dist/cjs/__tests__/utils/revenueCenters.test.js +61 -0
- package/dist/cjs/hooks/useSignUpGuestForm.d.ts +1 -9
- package/dist/cjs/hooks/useSignUpGuestForm.js +9 -2
- package/dist/cjs/utils/revenueCenters.d.ts +1 -0
- package/dist/cjs/utils/revenueCenters.js +22 -1
- package/dist/esm/__tests__/utils/revenueCenters.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/revenueCenters.test.js +59 -0
- package/dist/esm/hooks/useSignUpGuestForm.d.ts +1 -9
- package/dist/esm/hooks/useSignUpGuestForm.js +10 -3
- package/dist/esm/utils/revenueCenters.d.ts +1 -0
- package/dist/esm/utils/revenueCenters.js +20 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
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
|
+
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,
|
|
35
|
+
}
|
|
36
|
+
};
|
|
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);
|
|
44
|
+
});
|
|
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);
|
|
52
|
+
});
|
|
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);
|
|
56
|
+
});
|
|
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);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -4,15 +4,7 @@ export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCust
|
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: import("@open-tender/types").FormFieldType[];
|
|
7
|
-
data:
|
|
8
|
-
email: string;
|
|
9
|
-
company?: string | undefined;
|
|
10
|
-
customer_id?: number | undefined;
|
|
11
|
-
first_name?: string | undefined;
|
|
12
|
-
password?: string | undefined;
|
|
13
|
-
phone?: string | undefined;
|
|
14
|
-
last_name?: string | undefined;
|
|
15
|
-
};
|
|
7
|
+
data: CustomerCreate;
|
|
16
8
|
errors: Record<string, string>;
|
|
17
9
|
disabled: boolean;
|
|
18
10
|
guestDisabled: boolean;
|
|
@@ -7,6 +7,7 @@ const initialData = {
|
|
|
7
7
|
first_name: '',
|
|
8
8
|
last_name: '',
|
|
9
9
|
phone: '',
|
|
10
|
+
email: '',
|
|
10
11
|
password: ''
|
|
11
12
|
};
|
|
12
13
|
const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, submitGuest, tpls) => {
|
|
@@ -19,7 +20,9 @@ const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, s
|
|
|
19
20
|
const { guestData, guestIncomplete } = (0, utils_1.checkGuestData)(data, email);
|
|
20
21
|
const hasThanx = tpls === 'THANX';
|
|
21
22
|
const hasPunchh = tpls === 'PUNCHH';
|
|
22
|
-
const isIncomplete = hasThanx
|
|
23
|
+
const isIncomplete = hasThanx
|
|
24
|
+
? guestIncomplete
|
|
25
|
+
: Object.values(data).filter(i => !i).length > 0;
|
|
23
26
|
const isLoading = loading === 'pending';
|
|
24
27
|
const disabled = isIncomplete || isLoading;
|
|
25
28
|
const guestDisabled = guestIncomplete || isLoading;
|
|
@@ -77,7 +80,11 @@ const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, s
|
|
|
77
80
|
if (!disabled) {
|
|
78
81
|
setErrors({});
|
|
79
82
|
setSubmitting(true);
|
|
80
|
-
|
|
83
|
+
const values = Object.assign(Object.assign({}, data), { email });
|
|
84
|
+
if (values.birth_date) {
|
|
85
|
+
values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
|
|
86
|
+
}
|
|
87
|
+
signUp(values);
|
|
81
88
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
82
89
|
}
|
|
83
90
|
};
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeRevenueCenterMsg = exports.makeNotAcceptingOrdersMsg = exports.makeOrderTypeName = exports.makeServiceTypeName = exports.makeOrderTypeFromParam = exports.makeFullAddress = exports.makeOrderAddress = void 0;
|
|
3
|
+
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");
|
|
7
|
+
const date_fns_1 = require("date-fns");
|
|
8
|
+
const date_fns_tz_1 = require("date-fns-tz");
|
|
7
9
|
const makeOrderAddress = (address) => {
|
|
8
10
|
const { street, unit, city, state, postal_code } = address || {};
|
|
9
11
|
return street
|
|
@@ -123,3 +125,22 @@ const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, statusMes
|
|
|
123
125
|
return { message, color };
|
|
124
126
|
};
|
|
125
127
|
exports.makeRevenueCenterMsg = makeRevenueCenterMsg;
|
|
128
|
+
const isRevenueCenterOpen = (revenueCenter, serviceType) => {
|
|
129
|
+
const { hours, timezone } = revenueCenter;
|
|
130
|
+
const serviceHours = hours ? hours[serviceType] : null;
|
|
131
|
+
if (!serviceHours)
|
|
132
|
+
return false;
|
|
133
|
+
const tz = datetimes_1.timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
|
|
134
|
+
const now = new Date();
|
|
135
|
+
const zonedDate = (0, date_fns_tz_1.utcToZonedTime)(now, tz);
|
|
136
|
+
const currentWeekday = (0, date_fns_1.format)(zonedDate, 'EEEE').toUpperCase();
|
|
137
|
+
const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
|
|
138
|
+
if (!todayHours || !todayHours.open || !todayHours.close)
|
|
139
|
+
return false;
|
|
140
|
+
const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
|
|
141
|
+
const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
|
|
142
|
+
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 });
|
|
144
|
+
return (0, date_fns_1.isBefore)(openingTime, zonedDate) && (0, date_fns_1.isAfter)(closingTime, zonedDate);
|
|
145
|
+
};
|
|
146
|
+
exports.isRevenueCenterOpen = isRevenueCenterOpen;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
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,
|
|
33
|
+
}
|
|
34
|
+
};
|
|
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);
|
|
42
|
+
});
|
|
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);
|
|
50
|
+
});
|
|
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);
|
|
54
|
+
});
|
|
55
|
+
it('returns false if there are no hours for the current weekday', () => {
|
|
56
|
+
const result = isRevenueCenterOpen(revenueCenter, 'WALKIN');
|
|
57
|
+
expect(result).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -4,15 +4,7 @@ export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCust
|
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: import("@open-tender/types").FormFieldType[];
|
|
7
|
-
data:
|
|
8
|
-
email: string;
|
|
9
|
-
company?: string | undefined;
|
|
10
|
-
customer_id?: number | undefined;
|
|
11
|
-
first_name?: string | undefined;
|
|
12
|
-
password?: string | undefined;
|
|
13
|
-
phone?: string | undefined;
|
|
14
|
-
last_name?: string | undefined;
|
|
15
|
-
};
|
|
7
|
+
data: CustomerCreate;
|
|
16
8
|
errors: Record<string, string>;
|
|
17
9
|
disabled: boolean;
|
|
18
10
|
guestDisabled: boolean;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useRef, useState, useEffect } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { checkGuestData, makeBirthDate, makeFormErrors, makePhone, slashesToDashes } from '../utils';
|
|
3
3
|
const initialData = {
|
|
4
4
|
first_name: '',
|
|
5
5
|
last_name: '',
|
|
6
6
|
phone: '',
|
|
7
|
+
email: '',
|
|
7
8
|
password: ''
|
|
8
9
|
};
|
|
9
10
|
export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, submitGuest, tpls) => {
|
|
@@ -16,7 +17,9 @@ export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, si
|
|
|
16
17
|
const { guestData, guestIncomplete } = checkGuestData(data, email);
|
|
17
18
|
const hasThanx = tpls === 'THANX';
|
|
18
19
|
const hasPunchh = tpls === 'PUNCHH';
|
|
19
|
-
const isIncomplete = hasThanx
|
|
20
|
+
const isIncomplete = hasThanx
|
|
21
|
+
? guestIncomplete
|
|
22
|
+
: Object.values(data).filter(i => !i).length > 0;
|
|
20
23
|
const isLoading = loading === 'pending';
|
|
21
24
|
const disabled = isIncomplete || isLoading;
|
|
22
25
|
const guestDisabled = guestIncomplete || isLoading;
|
|
@@ -74,7 +77,11 @@ export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, si
|
|
|
74
77
|
if (!disabled) {
|
|
75
78
|
setErrors({});
|
|
76
79
|
setSubmitting(true);
|
|
77
|
-
|
|
80
|
+
const values = Object.assign(Object.assign({}, data), { email });
|
|
81
|
+
if (values.birth_date) {
|
|
82
|
+
values.birth_date = slashesToDashes(values.birth_date);
|
|
83
|
+
}
|
|
84
|
+
signUp(values);
|
|
78
85
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
79
86
|
}
|
|
80
87
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
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';
|
|
5
|
+
import { utcToZonedTime } from 'date-fns-tz';
|
|
4
6
|
export const makeOrderAddress = (address) => {
|
|
5
7
|
const { street, unit, city, state, postal_code } = address || {};
|
|
6
8
|
return street
|
|
@@ -113,3 +115,21 @@ export const makeRevenueCenterMsg = (revenueCenter, serviceType, requestedAt, st
|
|
|
113
115
|
}
|
|
114
116
|
return { message, color };
|
|
115
117
|
};
|
|
118
|
+
export const isRevenueCenterOpen = (revenueCenter, serviceType) => {
|
|
119
|
+
const { hours, timezone } = revenueCenter;
|
|
120
|
+
const serviceHours = hours ? hours[serviceType] : null;
|
|
121
|
+
if (!serviceHours)
|
|
122
|
+
return false;
|
|
123
|
+
const tz = timezoneMap[timezone !== null && timezone !== void 0 ? timezone : 'US/Central'];
|
|
124
|
+
const now = new Date();
|
|
125
|
+
const zonedDate = utcToZonedTime(now, tz);
|
|
126
|
+
const currentWeekday = format(zonedDate, 'EEEE').toUpperCase();
|
|
127
|
+
const todayHours = serviceHours.find(hour => hour.weekday === currentWeekday);
|
|
128
|
+
if (!todayHours || !todayHours.open || !todayHours.close)
|
|
129
|
+
return false;
|
|
130
|
+
const [openHours, openMinutes] = todayHours.open.split(':').map(Number);
|
|
131
|
+
const [closeHours, closeMinutes] = todayHours.close.split(':').map(Number);
|
|
132
|
+
const openingTime = set(zonedDate, { hours: openHours, minutes: openMinutes });
|
|
133
|
+
const closingTime = set(zonedDate, { hours: closeHours, minutes: closeMinutes });
|
|
134
|
+
return isBefore(openingTime, zonedDate) && isAfter(closingTime, zonedDate);
|
|
135
|
+
};
|
package/package.json
CHANGED