onroute-policy-engine 2.24.0 → 2.24.2
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/helper/date.helper.d.ts +28 -0
- package/dist/helper/date.helper.js +51 -0
- package/dist/helper/date.helper.spec.d.ts +1 -0
- package/dist/helper/date.helper.spec.js +26 -0
- package/dist/helper/facts.helper.js +11 -13
- package/dist/rule-operator/custom-operators.js +3 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dayjs } from 'dayjs';
|
|
2
|
+
export declare const TIMEZONE_IDS: {
|
|
3
|
+
PACIFIC: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const DATE_FORMATS: {
|
|
6
|
+
DATE_ONLY: string;
|
|
7
|
+
DATETIME: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Convert a datetime to a specified timezone.
|
|
11
|
+
*
|
|
12
|
+
* @param datetime Datetime to be converted
|
|
13
|
+
* @param timezoneId Timezone identifier (eg. 'Canada/Pacific')
|
|
14
|
+
* @returns Dayjs object in the specified timezone
|
|
15
|
+
*/
|
|
16
|
+
export declare const convertToTimezone: (datetime: string | Dayjs | Date, timezoneId: string) => Dayjs;
|
|
17
|
+
/**
|
|
18
|
+
* Get UTC datetime.
|
|
19
|
+
*
|
|
20
|
+
* @returns Dayjs object representing datetime in UTC
|
|
21
|
+
*/
|
|
22
|
+
export declare const getUtcDatetime: (datetime?: Dayjs | Date | string) => Dayjs;
|
|
23
|
+
/**
|
|
24
|
+
* Get datetime representing the end of the quarter for a given datetime.
|
|
25
|
+
* @param datetime Datetime being used for reference
|
|
26
|
+
* @returns Dayjs object representing the end of the quarter for the inputted datetime
|
|
27
|
+
*/
|
|
28
|
+
export declare const getEndOfQuarter: (datetime: Dayjs) => Dayjs;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getEndOfQuarter = exports.getUtcDatetime = exports.convertToTimezone = exports.DATE_FORMATS = exports.TIMEZONE_IDS = void 0;
|
|
7
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
|
+
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
|
|
9
|
+
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
|
|
10
|
+
const duration_1 = __importDefault(require("dayjs/plugin/duration"));
|
|
11
|
+
const quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
|
|
12
|
+
dayjs_1.default.extend(utc_1.default);
|
|
13
|
+
dayjs_1.default.extend(timezone_1.default);
|
|
14
|
+
dayjs_1.default.extend(duration_1.default);
|
|
15
|
+
dayjs_1.default.extend(quarterOfYear_1.default);
|
|
16
|
+
exports.TIMEZONE_IDS = {
|
|
17
|
+
PACIFIC: 'Canada/Pacific',
|
|
18
|
+
};
|
|
19
|
+
exports.DATE_FORMATS = {
|
|
20
|
+
DATE_ONLY: 'YYYY-MM-DD',
|
|
21
|
+
DATETIME: 'YYYY-MM-DD HH:mm:ss',
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Convert a datetime to a specified timezone.
|
|
25
|
+
*
|
|
26
|
+
* @param datetime Datetime to be converted
|
|
27
|
+
* @param timezoneId Timezone identifier (eg. 'Canada/Pacific')
|
|
28
|
+
* @returns Dayjs object in the specified timezone
|
|
29
|
+
*/
|
|
30
|
+
const convertToTimezone = (datetime, timezoneId) => {
|
|
31
|
+
return dayjs_1.default.tz(datetime, timezoneId);
|
|
32
|
+
};
|
|
33
|
+
exports.convertToTimezone = convertToTimezone;
|
|
34
|
+
/**
|
|
35
|
+
* Get UTC datetime.
|
|
36
|
+
*
|
|
37
|
+
* @returns Dayjs object representing datetime in UTC
|
|
38
|
+
*/
|
|
39
|
+
const getUtcDatetime = (datetime) => {
|
|
40
|
+
return dayjs_1.default.utc(datetime);
|
|
41
|
+
};
|
|
42
|
+
exports.getUtcDatetime = getUtcDatetime;
|
|
43
|
+
/**
|
|
44
|
+
* Get datetime representing the end of the quarter for a given datetime.
|
|
45
|
+
* @param datetime Datetime being used for reference
|
|
46
|
+
* @returns Dayjs object representing the end of the quarter for the inputted datetime
|
|
47
|
+
*/
|
|
48
|
+
const getEndOfQuarter = (datetime) => {
|
|
49
|
+
return datetime.endOf('quarter');
|
|
50
|
+
};
|
|
51
|
+
exports.getEndOfQuarter = getEndOfQuarter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const date_helper_1 = require("./date.helper");
|
|
4
|
+
describe('Date Helper Tests', () => {
|
|
5
|
+
it('should have correct local datetime components when converting datetime string', () => {
|
|
6
|
+
const localDtStr = '2026-08-10';
|
|
7
|
+
const localTzId = date_helper_1.TIMEZONE_IDS.PACIFIC;
|
|
8
|
+
const localDt = (0, date_helper_1.convertToTimezone)(localDtStr, localTzId);
|
|
9
|
+
expect(localDt.year()).toBe(2026);
|
|
10
|
+
expect(localDt.month()).toBe(7);
|
|
11
|
+
expect(localDt.date()).toBe(10);
|
|
12
|
+
expect(localDt.hour()).toBe(0);
|
|
13
|
+
expect(localDt.minute()).toBe(0);
|
|
14
|
+
expect(localDt.second()).toBe(0);
|
|
15
|
+
});
|
|
16
|
+
it('should have correct local datetime components when converting UTC datetime', () => {
|
|
17
|
+
const utcDtStr = '2026-08-10 22:00:00';
|
|
18
|
+
const utcDt = (0, date_helper_1.getUtcDatetime)(utcDtStr);
|
|
19
|
+
const localTzId = date_helper_1.TIMEZONE_IDS.PACIFIC;
|
|
20
|
+
const localConvertedDt = (0, date_helper_1.convertToTimezone)(utcDt, localTzId);
|
|
21
|
+
expect(utcDt.hour()).toBe(22);
|
|
22
|
+
expect(localConvertedDt.hour()).toBe(15);
|
|
23
|
+
expect(localConvertedDt.date()).toBe(utcDt.date());
|
|
24
|
+
expect(utcDt.diff(localConvertedDt)).toBe(0);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.addRuntimeFacts = addRuntimeFacts;
|
|
7
|
-
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
|
-
const quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
|
|
9
4
|
const enum_1 = require("onroute-policy-engine/enum");
|
|
10
5
|
const policy_check_helper_1 = require("./policy-check.helper");
|
|
11
6
|
const stgvwi_1 = require("../constants/stgvwi");
|
|
12
7
|
const overload_1 = require("../constants/overload");
|
|
13
|
-
|
|
8
|
+
const date_helper_1 = require("./date.helper");
|
|
14
9
|
const getAxleCalculationInputs = async (policy, almanac) => {
|
|
15
10
|
var _a, _b;
|
|
16
11
|
const vehicleDetails = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.VehicleDetails);
|
|
@@ -57,7 +52,10 @@ const getOverloadCost = (overloadKg, totalDistance) => {
|
|
|
57
52
|
* @param engine json-rules-engine Engine instance to add facts to.
|
|
58
53
|
*/
|
|
59
54
|
function addRuntimeFacts(engine, policy) {
|
|
60
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Add runtime fact for today's date (based on Pacific timezone)
|
|
57
|
+
*/
|
|
58
|
+
const today = (0, date_helper_1.convertToTimezone)((0, date_helper_1.getUtcDatetime)(), date_helper_1.TIMEZONE_IDS.PACIFIC).format(enum_1.PermitAppInfo.PermitDateFormat);
|
|
61
59
|
engine.addFact(enum_1.PolicyFacts.ValidationDate, today);
|
|
62
60
|
/**
|
|
63
61
|
* Add runtime fact for number of days in the permit year.
|
|
@@ -66,7 +64,7 @@ function addRuntimeFacts(engine, policy) {
|
|
|
66
64
|
*/
|
|
67
65
|
engine.addFact(enum_1.PolicyFacts.DaysInPermitYear, async function (params, almanac) {
|
|
68
66
|
const startDate = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitStartDate);
|
|
69
|
-
const dateFrom = (0,
|
|
67
|
+
const dateFrom = (0, date_helper_1.convertToTimezone)(startDate, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
70
68
|
const daysInPermitYear = dateFrom.add(1, 'year').diff(dateFrom, 'day');
|
|
71
69
|
return daysInPermitYear;
|
|
72
70
|
});
|
|
@@ -77,8 +75,8 @@ function addRuntimeFacts(engine, policy) {
|
|
|
77
75
|
*/
|
|
78
76
|
engine.addFact(enum_1.PolicyFacts.EndOfPermitQuarter, async function (params, almanac) {
|
|
79
77
|
const startDate = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitStartDate);
|
|
80
|
-
const dateFrom = (0,
|
|
81
|
-
const endOfQuarter =
|
|
78
|
+
const dateFrom = (0, date_helper_1.convertToTimezone)(startDate, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
79
|
+
const endOfQuarter = (0, date_helper_1.getEndOfQuarter)(dateFrom);
|
|
82
80
|
return endOfQuarter.format(enum_1.PermitAppInfo.PermitDateFormat);
|
|
83
81
|
});
|
|
84
82
|
/**
|
|
@@ -88,7 +86,7 @@ function addRuntimeFacts(engine, policy) {
|
|
|
88
86
|
*/
|
|
89
87
|
engine.addFact(enum_1.PolicyFacts.EndOfPermitYear, async function (params, almanac) {
|
|
90
88
|
const startDate = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitStartDate);
|
|
91
|
-
const dateFrom = (0,
|
|
89
|
+
const dateFrom = (0, date_helper_1.convertToTimezone)(startDate, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
92
90
|
const endOfYear = dateFrom
|
|
93
91
|
.endOf('year')
|
|
94
92
|
.format(enum_1.PermitAppInfo.PermitDateFormat);
|
|
@@ -209,8 +207,8 @@ function addRuntimeFacts(engine, policy) {
|
|
|
209
207
|
engine.addFact(enum_1.PolicyFacts.DaysBetween.toString(), async function (params, almanac) {
|
|
210
208
|
const dateFromStr = await almanac.factValue(params.dateFrom.fact, {}, params.dateFrom.path);
|
|
211
209
|
const dateToStr = await almanac.factValue(params.dateTo.fact, {}, params.dateTo.path);
|
|
212
|
-
const dateFrom = (0,
|
|
213
|
-
const dateTo = (0,
|
|
210
|
+
const dateFrom = (0, date_helper_1.convertToTimezone)(dateFromStr, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
211
|
+
const dateTo = (0, date_helper_1.convertToTimezone)(dateToStr, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
214
212
|
const daysBetween = dateTo.diff(dateFrom, 'day');
|
|
215
213
|
return daysBetween;
|
|
216
214
|
});
|
|
@@ -14,6 +14,7 @@ exports.CustomOperators = void 0;
|
|
|
14
14
|
const json_rules_engine_1 = require("json-rules-engine");
|
|
15
15
|
const enum_1 = require("onroute-policy-engine/enum");
|
|
16
16
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
17
|
+
const date_helper_1 = require("../helper/date.helper");
|
|
17
18
|
/**
|
|
18
19
|
* Validates that the input is a string type
|
|
19
20
|
* @param a - The value to validate
|
|
@@ -63,8 +64,8 @@ CustomOperators.push(new json_rules_engine_1.Operator(enum_1.CustomOperator.Stri
|
|
|
63
64
|
*/
|
|
64
65
|
CustomOperators.push(new json_rules_engine_1.Operator(enum_1.CustomOperator.DateLessThan, (a, b) => {
|
|
65
66
|
// Parse both dates using the permit application date format
|
|
66
|
-
const firstDate = (0,
|
|
67
|
-
const secondDate = (0,
|
|
67
|
+
const firstDate = (0, date_helper_1.convertToTimezone)(a, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
68
|
+
const secondDate = (0, date_helper_1.convertToTimezone)(b, date_helper_1.TIMEZONE_IDS.PACIFIC);
|
|
68
69
|
// Return true if first date is before second date (difference is negative)
|
|
69
70
|
return firstDate.diff(secondDate) < 0;
|
|
70
71
|
}, dateStringValidator));
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "v2.24.
|
|
1
|
+
export declare const version = "v2.24.2";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED