onroute-policy-engine 0.4.6 → 0.6.0

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,9 +1,14 @@
1
1
  export declare enum PolicyFacts {
2
2
  AllowedVehicles = "allowedVehicles",
3
3
  DaysInPermitYear = "daysInPermitYear",
4
+ EndOfPermitQuarter = "endOfPermitQuarter",
4
5
  ValidationDate = "validationDate",
6
+ ConfigurationIsValid = "configurationIsValid"
7
+ }
8
+ export declare enum CostFacts {
9
+ ConditionalFixedCost = "conditionalFixedCost",
5
10
  FixedCost = "fixedCost",
6
11
  CostPerMonth = "costPerMonth",
7
- RangeMatrixCostLookup = "rangeMatrixCostLookup",
8
- ConfigurationIsValid = "configurationIsValid"
12
+ CostPerKilometre = "costPerKilometre",
13
+ RangeMatrixCostLookup = "rangeMatrixCostLookup"
9
14
  }
@@ -1,13 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PolicyFacts = void 0;
3
+ exports.CostFacts = exports.PolicyFacts = void 0;
4
4
  var PolicyFacts;
5
5
  (function (PolicyFacts) {
6
6
  PolicyFacts["AllowedVehicles"] = "allowedVehicles";
7
7
  PolicyFacts["DaysInPermitYear"] = "daysInPermitYear";
8
+ PolicyFacts["EndOfPermitQuarter"] = "endOfPermitQuarter";
8
9
  PolicyFacts["ValidationDate"] = "validationDate";
9
- PolicyFacts["FixedCost"] = "fixedCost";
10
- PolicyFacts["CostPerMonth"] = "costPerMonth";
11
- PolicyFacts["RangeMatrixCostLookup"] = "rangeMatrixCostLookup";
12
10
  PolicyFacts["ConfigurationIsValid"] = "configurationIsValid";
13
11
  })(PolicyFacts || (exports.PolicyFacts = PolicyFacts = {}));
12
+ var CostFacts;
13
+ (function (CostFacts) {
14
+ CostFacts["ConditionalFixedCost"] = "conditionalFixedCost";
15
+ CostFacts["FixedCost"] = "fixedCost";
16
+ CostFacts["CostPerMonth"] = "costPerMonth";
17
+ CostFacts["CostPerKilometre"] = "costPerKilometre";
18
+ CostFacts["RangeMatrixCostLookup"] = "rangeMatrixCostLookup";
19
+ })(CostFacts || (exports.CostFacts = CostFacts = {}));
@@ -1,5 +1,5 @@
1
1
  export { AccessoryVehicleType } from './accessory-vehicle-type';
2
- export { PolicyFacts } from './facts';
2
+ export { PolicyFacts, CostFacts } from './facts';
3
3
  export { PermitAppInfo } from './permit-app-info';
4
4
  export { RelativePosition } from './relative-position';
5
5
  export { ValidationResultCode } from './validation-result-code';
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VehicleTypes = exports.ValidationResultType = exports.ValidationResultCode = exports.RelativePosition = exports.PermitAppInfo = exports.PolicyFacts = exports.AccessoryVehicleType = void 0;
3
+ exports.VehicleTypes = exports.ValidationResultType = exports.ValidationResultCode = exports.RelativePosition = exports.PermitAppInfo = exports.CostFacts = exports.PolicyFacts = exports.AccessoryVehicleType = void 0;
4
4
  var accessory_vehicle_type_1 = require("./accessory-vehicle-type");
5
5
  Object.defineProperty(exports, "AccessoryVehicleType", { enumerable: true, get: function () { return accessory_vehicle_type_1.AccessoryVehicleType; } });
6
6
  var facts_1 = require("./facts");
7
7
  Object.defineProperty(exports, "PolicyFacts", { enumerable: true, get: function () { return facts_1.PolicyFacts; } });
8
+ Object.defineProperty(exports, "CostFacts", { enumerable: true, get: function () { return facts_1.CostFacts; } });
8
9
  var permit_app_info_1 = require("./permit-app-info");
9
10
  Object.defineProperty(exports, "PermitAppInfo", { enumerable: true, get: function () { return permit_app_info_1.PermitAppInfo; } });
10
11
  var relative_position_1 = require("./relative-position");
@@ -12,5 +12,6 @@ export declare enum PermitAppInfo {
12
12
  PermitDuration = "$.permitDuration",
13
13
  PowerUnitType = "$.vehicleDetails.vehicleSubType",
14
14
  TrailerList = "$.vehicleConfiguration.trailers",
15
- Commodity = "$.permittedCommodity.commodityType"
15
+ Commodity = "$.permittedCommodity.commodityType",
16
+ TotalDistance = "$.permittedRoute.manualRoute.totalDistance"
16
17
  }
@@ -17,4 +17,5 @@ var PermitAppInfo;
17
17
  PermitAppInfo["PowerUnitType"] = "$.vehicleDetails.vehicleSubType";
18
18
  PermitAppInfo["TrailerList"] = "$.vehicleConfiguration.trailers";
19
19
  PermitAppInfo["Commodity"] = "$.permittedCommodity.commodityType";
20
+ PermitAppInfo["TotalDistance"] = "$.permittedRoute.manualRoute.totalDistance";
20
21
  })(PermitAppInfo || (exports.PermitAppInfo = PermitAppInfo = {}));
@@ -5,7 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.addRuntimeFacts = addRuntimeFacts;
7
7
  const dayjs_1 = __importDefault(require("dayjs"));
8
+ const quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
8
9
  const enum_1 = require("onroute-policy-engine/enum");
10
+ dayjs_1.default.extend(quarterOfYear_1.default);
9
11
  /**
10
12
  * Adds runtime facts for the validation. For example, adds the
11
13
  * validation date for comparison against startDate of the permit.
@@ -25,6 +27,17 @@ function addRuntimeFacts(engine, policy) {
25
27
  const daysInPermitYear = dateFrom.add(1, 'year').diff(dateFrom, 'day');
26
28
  return daysInPermitYear;
27
29
  });
30
+ /**
31
+ * Add runtime fact to get the last day of the quarter
32
+ * that the permit start date falls in. Returns the date
33
+ * formatted as a string in the standard permit date format.
34
+ */
35
+ engine.addFact(enum_1.PolicyFacts.EndOfPermitQuarter, async function (params, almanac) {
36
+ const startDate = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitStartDate);
37
+ const dateFrom = (0, dayjs_1.default)(startDate, enum_1.PermitAppInfo.PermitDateFormat);
38
+ const endOfQuarter = dateFrom.endOf('quarter');
39
+ return endOfQuarter.format(enum_1.PermitAppInfo.PermitDateFormat);
40
+ });
28
41
  /**
29
42
  * Adds a runtime fact specifying whether the vehicle configuration
30
43
  * in the permit application is valid for the permit type and
@@ -52,15 +65,29 @@ function addRuntimeFacts(engine, policy) {
52
65
  * Add runtime fact for a fixed permit cost, the cost supplied
53
66
  * as a parameter.
54
67
  */
55
- engine.addFact(enum_1.PolicyFacts.FixedCost.toString(), async function (params) {
68
+ engine.addFact(enum_1.CostFacts.FixedCost.toString(), async function (params) {
56
69
  return params.cost;
57
70
  });
71
+ /**
72
+ * Add runtime fact for a conditional fixed permit cost, where the
73
+ * cost rule applied only when the fact and value supplied as parameters
74
+ * match.
75
+ */
76
+ engine.addFact(enum_1.CostFacts.ConditionalFixedCost.toString(), async function (params, almanac) {
77
+ const conditionValue = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, params.fact);
78
+ if (conditionValue && conditionValue === params.value) {
79
+ return params.cost;
80
+ }
81
+ else {
82
+ return 0;
83
+ }
84
+ });
58
85
  /**
59
86
  * Add runtime fact for cost per month, where month is defined by
60
87
  * policy as a 30 day period or portion thereof, except in the
61
88
  * case of a full year in which case it is 12 months.
62
89
  */
63
- engine.addFact(enum_1.PolicyFacts.CostPerMonth.toString(), async function (params, almanac) {
90
+ engine.addFact(enum_1.CostFacts.CostPerMonth.toString(), async function (params, almanac) {
64
91
  const duration = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitDuration);
65
92
  const daysInPermitYear = await almanac.factValue(enum_1.PolicyFacts.DaysInPermitYear.toString());
66
93
  let months = Math.floor(duration / 30);
@@ -74,4 +101,18 @@ function addRuntimeFacts(engine, policy) {
74
101
  }
75
102
  return months * params.cost;
76
103
  });
104
+ /**
105
+ * Add runtime fact for cost per kilometre driven in the applicant's
106
+ * permitted route. Accepts an optional parameter minValue which
107
+ * specifies the minimum value for the permit if the kilometre cost
108
+ * is less than that value.
109
+ */
110
+ engine.addFact(enum_1.CostFacts.CostPerKilometre.toString(), async function (params, almanac) {
111
+ const distance = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.TotalDistance);
112
+ let cost = distance * params.cost;
113
+ if (typeof params.minValue === 'number') {
114
+ cost = Math.max(cost, params.minValue);
115
+ }
116
+ return cost;
117
+ });
77
118
  }
package/package.json CHANGED
@@ -86,5 +86,5 @@
86
86
  ],
87
87
  "testEnvironment": "node"
88
88
  },
89
- "version": "v0.4.6"
89
+ "version": "v0.6.0"
90
90
  }