@legalplace/conditions-runner 1.1.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.
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ *.test.ts
2
+ *.test.tsx
package/.eslintrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["@legalplace/eslint-config"]
3
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 1.1.0 (2021-12-15)
7
+
8
+
9
+ ### Features
10
+
11
+ * add ConditionsRunner package api[#4501](https://git.legalplace.eu/legalplace/monorepo/issues/4501) ([5b55892](https://git.legalplace.eu/legalplace/monorepo/commits/5b558924c72a11c059b7f0296848af1a0e5c80e8))
@@ -0,0 +1,19 @@
1
+ import { Types } from "@legalplace/referencesparser";
2
+ import { ConditionV3 } from "@legalplace/models-v3-types";
3
+ import type InputsType from "@legalplace/types/dist/inputs";
4
+ import { ConditionDataMap } from "./types/conditions.d";
5
+ declare class ConditionsRunner {
6
+ private references;
7
+ private ovc;
8
+ constructor(references: Types.ReferencesType, ovc: InputsType);
9
+ isOptionRelatedToOption(optionA: number, optionB: number): boolean;
10
+ isVariableRelatedToOption(variableId: number, optionId: number): boolean;
11
+ isVariableRelatedToVariable(variableA: number, variableB: number): boolean;
12
+ private executeConditionMemo;
13
+ private buildMemoizeKey;
14
+ executeCondition(condition: {
15
+ dataMap: ConditionDataMap;
16
+ conditions: ConditionV3;
17
+ }, id: number, index: number, conditionType: "options" | "optionValidator" | "variables" | "prefillers" | "variableValidator" | "sections" | "documents"): any;
18
+ }
19
+ export default ConditionsRunner;
@@ -0,0 +1,84 @@
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
+ const lplogic_1 = __importDefault(require("@legalplace/lplogic"));
7
+ const DataPopulator_1 = __importDefault(require("./DataPopulator"));
8
+ class ConditionsRunner {
9
+ constructor(references, ovc) {
10
+ this.executeConditionMemo = {};
11
+ this.buildMemoizeKey = (condition, id, index, conditionType) => `${JSON.stringify(condition)}-${id}-${index}-${conditionType}`;
12
+ this.references = references;
13
+ this.ovc = ovc;
14
+ }
15
+ isOptionRelatedToOption(optionA, optionB) {
16
+ const OptionBParents = this.references.relations.options[optionB].parents;
17
+ const OptionBRoot = OptionBParents[OptionBParents.length - 1];
18
+ const OptionBRootRelations = OptionBRoot
19
+ ? this.references.relations.options[OptionBRoot]
20
+ : undefined;
21
+ if (OptionBRootRelations) {
22
+ if (optionA === optionB ||
23
+ OptionBRootRelations.children.options.includes(optionA) ||
24
+ OptionBRootRelations.dependants.includes(optionA)) {
25
+ return true;
26
+ }
27
+ }
28
+ return false;
29
+ }
30
+ isVariableRelatedToOption(variableId, optionId) {
31
+ const variableParentId = this.references.relations.variables[variableId].parents[0];
32
+ const optionParents = this.references.relations.options[variableParentId].parents;
33
+ const optionRoot = optionParents[optionParents.length - 1];
34
+ const optionRootRelations = optionRoot
35
+ ? this.references.relations.options[optionRoot]
36
+ : undefined;
37
+ if (optionRootRelations) {
38
+ if (optionRootRelations.children.options.includes(optionId) ||
39
+ optionRootRelations.dependants.includes(optionId)) {
40
+ return true;
41
+ }
42
+ }
43
+ return false;
44
+ }
45
+ isVariableRelatedToVariable(variableA, variableB) {
46
+ const optionA = this.references.relations.variables[variableA].parents[0];
47
+ const optionB = this.references.relations.variables[variableB].parents[0];
48
+ const OptionBParents = this.references.relations.options[optionB].parents;
49
+ const OptionBRoot = OptionBParents[OptionBParents.length - 1];
50
+ const OptionBRootRelations = OptionBRoot
51
+ ? this.references.relations.options[OptionBRoot]
52
+ : undefined;
53
+ if (OptionBRootRelations) {
54
+ if (optionA === optionB ||
55
+ OptionBRootRelations.children.options.includes(optionA) ||
56
+ OptionBRootRelations.dependants.includes(optionA)) {
57
+ return true;
58
+ }
59
+ }
60
+ return false;
61
+ }
62
+ executeCondition(condition, id, index, conditionType) {
63
+ const memoizedKey = this.buildMemoizeKey(condition, id, index, conditionType);
64
+ if (this.executeConditionMemo[memoizedKey])
65
+ return this.executeConditionMemo[memoizedKey];
66
+ let currentData;
67
+ if (conditionType === "options" || conditionType === "optionValidator") {
68
+ currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, id, index).getData();
69
+ }
70
+ else if (conditionType === "variables" ||
71
+ conditionType === "prefillers" ||
72
+ conditionType === "variableValidator") {
73
+ const dataId = this.references.relations.variables[id].parents[0];
74
+ currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, dataId, index).getData();
75
+ }
76
+ else if (conditionType === "sections" || conditionType === "documents") {
77
+ currentData = new DataPopulator_1.default(this, this.references, this.ovc, condition.dataMap, id, index).getData();
78
+ }
79
+ const result = (0, lplogic_1.default)(condition.conditions, currentData);
80
+ this.executeConditionMemo[memoizedKey] = result;
81
+ return result;
82
+ }
83
+ }
84
+ exports.default = ConditionsRunner;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
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
+ const ConditionsRunner_1 = __importDefault(require("./ConditionsRunner"));
7
+ const inputs_type_json_1 = __importDefault(require("./fixtures/inputs-type.json"));
8
+ const references_json_1 = __importDefault(require("./fixtures/references.json"));
9
+ describe("ConditionsRunner class", () => {
10
+ it("should not raise error when instanciating class", () => {
11
+ expect(new ConditionsRunner_1.default(references_json_1.default, inputs_type_json_1.default)).toBeDefined();
12
+ });
13
+ });
@@ -0,0 +1,29 @@
1
+ import { Types } from "@legalplace/referencesparser";
2
+ import type InputsType from "@legalplace/types/dist/inputs";
3
+ import ConditionsRunner from "./ConditionsRunner";
4
+ import { ConditionData, ConditionDataMap } from "./types/conditions.d";
5
+ declare class DataPopulator {
6
+ private references;
7
+ private ovc;
8
+ private conditionsRunner;
9
+ private id?;
10
+ private index?;
11
+ private dataMap;
12
+ private data;
13
+ constructor(conditionsRunner: ConditionsRunner, references: Types.ReferencesType, ovc: InputsType, dataMap: ConditionDataMap, id?: number, index?: number);
14
+ getData(): Readonly<ConditionData>;
15
+ private populateConditions;
16
+ private getOptionConditions;
17
+ private getVariablesConditions;
18
+ private populateVariables;
19
+ private getVariableValues;
20
+ private getVariableRootParent;
21
+ private cleanArrayFromNullValues;
22
+ populateOptions(): void;
23
+ private getOptionInputs;
24
+ private getOptionRootParent;
25
+ private optionConditionValue;
26
+ private variableConditionValue;
27
+ private sectionConditionValue;
28
+ }
29
+ export default DataPopulator;
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class DataPopulator {
4
+ constructor(conditionsRunner, references, ovc, dataMap, id, index) {
5
+ this.data = {
6
+ o: {},
7
+ v: {},
8
+ c: {
9
+ o: {},
10
+ s: {},
11
+ v: {},
12
+ },
13
+ };
14
+ this.id = id;
15
+ this.ovc = ovc;
16
+ this.index = index;
17
+ this.dataMap = dataMap;
18
+ this.references = references;
19
+ this.conditionsRunner = conditionsRunner;
20
+ this.populateVariables();
21
+ this.populateOptions();
22
+ this.populateConditions();
23
+ }
24
+ getData() {
25
+ return this.data;
26
+ }
27
+ populateConditions() {
28
+ for (let i = 0; i < this.dataMap.c.s.length; i += 1) {
29
+ const sectionId = this.dataMap.c.s[i];
30
+ const sectionCondition = this.sectionConditionValue(sectionId);
31
+ this.data.c.s[sectionId] = [sectionCondition !== false];
32
+ }
33
+ for (let i = 0; i < this.dataMap.c.o.length; i += 1) {
34
+ const optionId = this.dataMap.c.o[i];
35
+ const optionCondition = [...this.getOptionConditions(optionId)];
36
+ this.data.c.o[optionId] =
37
+ optionCondition === undefined ? [false] : optionCondition;
38
+ }
39
+ for (let i = 0; i < this.dataMap.c.v.length; i += 1) {
40
+ const variableId = this.dataMap.c.v[i];
41
+ const variableCondition = [...this.getVariablesConditions(variableId)];
42
+ this.data.c.v[variableId] =
43
+ variableCondition === undefined ? [false] : variableCondition;
44
+ }
45
+ }
46
+ getOptionConditions(optionId) {
47
+ const conditions = this.optionConditionValue(optionId);
48
+ if (conditions === undefined)
49
+ return [true];
50
+ if (this.id === undefined || this.index === undefined) {
51
+ return conditions;
52
+ }
53
+ const optionRootParent = this.getOptionRootParent(optionId);
54
+ const optionRootRelations = this.references.relations.options[optionRootParent];
55
+ if (optionRootRelations.children.options.includes(this.id) ||
56
+ optionRootRelations.dependants.includes(this.id) ||
57
+ this.id === optionRootParent) {
58
+ if (conditions !== undefined && conditions[this.index] === false)
59
+ return [false];
60
+ return [conditions[this.index]];
61
+ }
62
+ return conditions;
63
+ }
64
+ getVariablesConditions(variableId) {
65
+ const conditions = this.variableConditionValue(variableId);
66
+ if (conditions === undefined)
67
+ return [true];
68
+ if (this.id === undefined || this.index === undefined) {
69
+ return conditions;
70
+ }
71
+ const variableRootParent = this.getVariableRootParent(variableId);
72
+ const variableRootRelations = this.references.relations.options[variableRootParent];
73
+ if (variableRootRelations.children.options.includes(this.id) ||
74
+ variableRootRelations.dependants.includes(this.id) ||
75
+ this.id === variableRootParent) {
76
+ return [conditions[this.index]];
77
+ }
78
+ return conditions;
79
+ }
80
+ populateVariables() {
81
+ for (let i = 0; i < this.dataMap.v.length; i += 1) {
82
+ const variableId = this.dataMap.v[i];
83
+ const variableReference = this.references.variables[variableId];
84
+ let values = this.getVariableValues(variableId);
85
+ const parseNumber = (v) => {
86
+ const result = parseFloat(v);
87
+ return Number.isNaN(result) ? "" : result;
88
+ };
89
+ if (variableReference.type === "number") {
90
+ values = values.map((v) => typeof v === "number" ? v : parseNumber(v));
91
+ }
92
+ this.data.v[variableId] = [...values];
93
+ }
94
+ }
95
+ getVariableValues(variableId) {
96
+ const value = this.ovc.variables[variableId];
97
+ if (this.id === undefined || this.index === undefined) {
98
+ return value;
99
+ }
100
+ const variableRootParent = this.getVariableRootParent(variableId);
101
+ const variableRootRelations = this.references.relations.options[variableRootParent];
102
+ if (variableRootRelations.children.options.includes(this.id) ||
103
+ variableRootRelations.dependants.includes(this.id) ||
104
+ this.id === variableRootParent) {
105
+ return [value[this.index]];
106
+ }
107
+ return this.cleanArrayFromNullValues([...value]);
108
+ }
109
+ getVariableRootParent(variableId) {
110
+ const variableParents = this.references.relations.variables[variableId].parents;
111
+ return variableParents[variableParents.length - 1];
112
+ }
113
+ cleanArrayFromNullValues(arr) {
114
+ return arr.map((v) => (v === null ? "" : v));
115
+ }
116
+ populateOptions() {
117
+ for (let i = 0; i < this.dataMap.o.length; i += 1) {
118
+ const optionId = this.dataMap.o[i];
119
+ const inputs = this.getOptionInputs(optionId);
120
+ this.data.o[optionId] = [...inputs];
121
+ }
122
+ }
123
+ getOptionInputs(optionId) {
124
+ const inputs = this.ovc.options[optionId];
125
+ if (this.id === undefined || this.index === undefined) {
126
+ return inputs;
127
+ }
128
+ const optionRootParent = this.getOptionRootParent(optionId);
129
+ const optionRootRelations = this.references.relations.options[optionRootParent];
130
+ if (optionRootRelations.children.options.includes(this.id) ||
131
+ optionRootRelations.dependants.includes(this.id) ||
132
+ this.id === optionRootParent) {
133
+ return [inputs[this.index]];
134
+ }
135
+ return inputs;
136
+ }
137
+ getOptionRootParent(optionId) {
138
+ const optionParents = this.references.relations.options[optionId].parents;
139
+ return optionParents.length > 0
140
+ ? optionParents[optionParents.length - 1]
141
+ : optionId;
142
+ }
143
+ optionConditionValue(optionId) {
144
+ const conditions = this.references.conditions.options[optionId];
145
+ if (conditions === undefined)
146
+ return undefined;
147
+ const inputs = this.ovc.options[optionId];
148
+ return inputs.map((input, index) => this.conditionsRunner.executeCondition(conditions, optionId, index, "options"));
149
+ }
150
+ variableConditionValue(variableId) {
151
+ const conditions = this.references.conditions.variables[variableId];
152
+ if (conditions === undefined)
153
+ return undefined;
154
+ const inputs = this.ovc.variables[variableId];
155
+ const result = inputs.map((input, index) => this.conditionsRunner.executeCondition(conditions, variableId, index, "variables"));
156
+ return result;
157
+ }
158
+ sectionConditionValue(sectionId) {
159
+ const conditions = this.references.conditions.sections.main[sectionId];
160
+ if (conditions === undefined)
161
+ return undefined;
162
+ return this.conditionsRunner.executeCondition(conditions, 0, 0, "sections");
163
+ }
164
+ }
165
+ exports.default = DataPopulator;