@legalplace/tagextractor 2.0.0 → 2.2.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,47 +1,56 @@
1
- import { ModelV3 } from '@legalplace/models-v3-types'
2
- import { ReferencesParser, Types } from '@legalplace/referencesparser'
3
- import { ReferencesType } from '@legalplace/referencesparser/dist/libs/References.type'
4
- import ConditionsRunner from '@legalplace/conditions-runner'
5
- import OvcConverter from '@legalplace/ovc-converter'
6
- import InputsType from '@legalplace/types/dist/inputs'
7
- import OvcType from '@legalplace/types/dist/ovc'
8
- import TagsType from '../types/tags.type'
1
+ import { ModelV3 } from "@legalplace/models-v3-types";
2
+ import { ReferencesParser, Types } from "@legalplace/referencesparser";
3
+ import { ReferencesType } from "@legalplace/referencesparser/dist/libs/References.type";
4
+ import ConditionsRunner from "@legalplace/conditions-runner";
5
+ import OvcConverter from "@legalplace/ovc-converter";
6
+ import InputsType from "@legalplace/types/dist/inputs";
7
+ import OvcType from "@legalplace/types/dist/ovc";
8
+ import TagsType from "../types/tags.type";
9
9
 
10
10
  class TagExtractor {
11
- private references: Types.ReferencesType
11
+ private references: Types.ReferencesType;
12
12
 
13
- private inputs: InputsType
13
+ private inputs: InputsType;
14
14
 
15
- private conditionsRunner: ConditionsRunner
15
+ private conditionsRunner: ConditionsRunner;
16
16
 
17
17
  private tags: TagsType = {
18
18
  options: {},
19
19
  variables: {}
20
- }
20
+ };
21
21
 
22
- private isReferencesType = (_obj: ModelV3 | ReferencesType): _obj is ReferencesType => {
22
+ private isReferencesType = (
23
+ _obj: ModelV3 | ReferencesType
24
+ ): _obj is ReferencesType => {
23
25
  // We check if the object contains one of the fields only contained in ReferencesType
24
- return Object.keys(_obj).some(key => key === 'boxes')
25
- }
26
+ return Object.keys(_obj).some(key => key === "boxes");
27
+ };
26
28
 
27
- constructor(modelOrReferences: ModelV3 | ReferencesType, ovc: OvcType | InputsType) {
28
- this.references = this.isReferencesType(modelOrReferences) ? modelOrReferences : new ReferencesParser(modelOrReferences).references
29
+ constructor(
30
+ modelOrReferences: ModelV3 | ReferencesType,
31
+ ovc: OvcType | InputsType
32
+ ) {
33
+ this.references = this.isReferencesType(modelOrReferences)
34
+ ? modelOrReferences
35
+ : new ReferencesParser(modelOrReferences).references;
29
36
 
30
37
  // Converting inputs if needed
31
- this.inputs = OvcConverter.isOptionsVariables(ovc) ? ovc : OvcConverter.convertToOptionsVariables(ovc, this.references)
38
+ this.inputs = OvcConverter.isOptionsVariables(ovc)
39
+ ? ovc
40
+ : OvcConverter.convertToOptionsVariables(ovc, this.references);
32
41
 
33
42
  // Initiaing Conditions runner
34
- this.conditionsRunner = new ConditionsRunner(this.references, this.inputs)
43
+ this.conditionsRunner = new ConditionsRunner(this.references, this.inputs);
35
44
 
36
45
  // Extracting tags from options
37
- this.extractFromOptions()
46
+ this.extractFromOptions();
38
47
 
39
48
  // Extracting tags from variables
40
- this.extractFromVariables()
49
+ this.extractFromVariables();
41
50
  }
42
51
 
43
52
  public get getTags() {
44
- return this.tags
53
+ return this.tags;
45
54
  }
46
55
 
47
56
  /**
@@ -49,12 +58,13 @@ class TagExtractor {
49
58
  */
50
59
  private extractFromOptions() {
51
60
  Object.keys(this.references.options).forEach(id => {
52
- const { tags, label } = this.references.options[id].meta
61
+ const { tags, label } = this.references.options[id].meta;
53
62
 
54
63
  if (Array.isArray(tags) && tags.length > 0 && this.inputs.options[id]) {
55
64
  this.inputs.options[id].forEach((v, index) => {
56
65
  tags.forEach(tag => {
57
- if (this.tags.options[tag] === undefined) this.tags.options[tag] = []
66
+ if (this.tags.options[tag] === undefined)
67
+ this.tags.options[tag] = [];
58
68
  this.tags.options[tag].push({
59
69
  id: parseInt(id, 10),
60
70
  index,
@@ -62,11 +72,11 @@ class TagExtractor {
62
72
  condition: this.getOptionCondition(parseInt(id, 10), index),
63
73
  visible: this.isOptionDisplayed(parseInt(id, 10), index),
64
74
  value: this.inputs.options[id][index]
65
- })
66
- })
67
- })
75
+ });
76
+ });
77
+ });
68
78
  }
69
- })
79
+ });
70
80
  }
71
81
 
72
82
  /**
@@ -74,24 +84,26 @@ class TagExtractor {
74
84
  */
75
85
  private extractFromVariables() {
76
86
  Object.keys(this.references.variables).forEach(id => {
77
- const { tags, label } = this.references.variables[id]
87
+ const { tags, label } = this.references.variables[id];
78
88
 
79
89
  if (Array.isArray(tags) && tags.length > 0 && this.inputs.variables[id]) {
80
90
  this.inputs.variables[id].forEach((v, index) => {
81
91
  tags.forEach(tag => {
82
- if (this.tags.variables[tag] === undefined) this.tags.variables[tag] = []
92
+ if (this.tags.variables[tag] === undefined)
93
+ this.tags.variables[tag] = [];
83
94
  this.tags.variables[tag].push({
84
95
  id: parseInt(id, 10),
85
96
  index,
86
97
  label,
87
- condition: this.getVariableCondition(parseInt(id, 10), index) !== false,
98
+ condition:
99
+ this.getVariableCondition(parseInt(id, 10), index) !== false,
88
100
  visible: this.isVariableDisplayed(parseInt(id, 10), index),
89
101
  value: this.inputs.variables[id][index]
90
- })
91
- })
92
- })
102
+ });
103
+ });
104
+ });
93
105
  }
94
- })
106
+ });
95
107
  }
96
108
 
97
109
  /**
@@ -101,12 +113,23 @@ class TagExtractor {
101
113
  */
102
114
  isVariableDisplayed(id: number, index: number) {
103
115
  // Getting variable's conditions & executing it if any
104
- const variableCondition = this.getVariableCondition(id, index)
105
- const variableParents = this.references.relations.variables[id]?.parents || []
106
- const parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index)
107
- const parentOptionIsTruethful = this.inputs.options[variableParents[0]]?.[index] === true
108
-
109
- return [variableCondition, parentOptionIsDisplayed, parentOptionIsTruethful].filter(c => c !== true).length === 0
116
+ const variableCondition = this.getVariableCondition(id, index);
117
+ const variableParents =
118
+ this.references.relations.variables[id]?.parents || [];
119
+ const parentOptionIsDisplayed = this.isOptionDisplayed(
120
+ variableParents[0],
121
+ index
122
+ );
123
+ const parentOptionIsTruethful =
124
+ this.inputs.options[variableParents[0]]?.[index] === true;
125
+
126
+ return (
127
+ [
128
+ variableCondition,
129
+ parentOptionIsDisplayed,
130
+ parentOptionIsTruethful
131
+ ].filter(c => c !== true).length === 0
132
+ );
110
133
  }
111
134
 
112
135
  /**
@@ -116,18 +139,29 @@ class TagExtractor {
116
139
  */
117
140
  isOptionDisplayed(id: number, index: number) {
118
141
  // Getting variable's conditions & executing it if any
119
- const optionCondition = this.getOptionCondition(id, index)
120
- const optionParents = this.references.relations.options[id]?.parents || []
142
+ const optionCondition = this.getOptionCondition(id, index);
143
+ const optionParents = this.references.relations.options[id]?.parents || [];
121
144
  const parentsConditions = optionParents.map(optionId => {
122
- return this.getOptionCondition(optionId, index) !== false
123
- })
145
+ return this.getOptionCondition(optionId, index) !== false;
146
+ });
124
147
  const parentsInputs = optionParents.map(optionId => {
125
- return this.inputs.options[optionId][index]
126
- })
127
- const parentSectionId = this.getOptionParentSection(id)
128
- const parentSectionCondition = this.getSectionCondition(parentSectionId) !== false
129
-
130
- return [optionCondition, parentSectionCondition, ...parentsConditions, ...parentsInputs].filter(c => c !== true).length === 0
148
+ // If the parent option is not already defined we set the displayed parameter to false
149
+ return this.inputs.options[optionId]
150
+ ? this.inputs.options[optionId][index]
151
+ : false;
152
+ });
153
+ const parentSectionId = this.getOptionParentSection(id);
154
+ const parentSectionCondition =
155
+ this.getSectionCondition(parentSectionId) !== false;
156
+
157
+ return (
158
+ [
159
+ optionCondition,
160
+ parentSectionCondition,
161
+ ...parentsConditions,
162
+ ...parentsInputs
163
+ ].filter(c => c !== true).length === 0
164
+ );
131
165
  }
132
166
 
133
167
  /**
@@ -136,8 +170,15 @@ class TagExtractor {
136
170
  * @param index Variable's index
137
171
  */
138
172
  private getVariableCondition(id: number, index: number) {
139
- const conditionObject = this.references.conditions.variables[id]
140
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables')
173
+ const conditionObject = this.references.conditions.variables[id];
174
+ return conditionObject === undefined
175
+ ? true
176
+ : this.conditionsRunner.executeCondition(
177
+ conditionObject,
178
+ id,
179
+ index,
180
+ "variables"
181
+ );
141
182
  }
142
183
 
143
184
  /**
@@ -146,8 +187,15 @@ class TagExtractor {
146
187
  * @param index Option's index
147
188
  */
148
189
  private getOptionCondition(id: number, index: number) {
149
- const conditionObject = this.references.conditions.options[id]
150
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options')
190
+ const conditionObject = this.references.conditions.options[id];
191
+ return conditionObject === undefined
192
+ ? true
193
+ : this.conditionsRunner.executeCondition(
194
+ conditionObject,
195
+ id,
196
+ index,
197
+ "options"
198
+ );
151
199
  }
152
200
 
153
201
  /**
@@ -155,8 +203,15 @@ class TagExtractor {
155
203
  * @param id Section's id
156
204
  */
157
205
  private getSectionCondition(id: number) {
158
- const conditionObject = this.references.conditions.sections.main[id]
159
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections')
206
+ const conditionObject = this.references.conditions.sections.main[id];
207
+ return conditionObject === undefined
208
+ ? true
209
+ : this.conditionsRunner.executeCondition(
210
+ conditionObject,
211
+ id,
212
+ 0,
213
+ "sections"
214
+ );
160
215
  }
161
216
 
162
217
  /**
@@ -164,20 +219,22 @@ class TagExtractor {
164
219
  * @param id Option's id
165
220
  */
166
221
  private getOptionParentSection(id: number) {
167
- const { parents } = this.references.relations.options[id]
222
+ const { parents } = this.references.relations.options[id];
168
223
 
169
224
  // Getting root option id
170
- const rootId = parents.length > 0 ? parents[parents.length - 1] : id
225
+ const rootId = parents.length > 0 ? parents[parents.length - 1] : id;
171
226
 
172
227
  // Looking for section
173
- const sections = Object.values(this.references.sections.main)
228
+ const sections = Object.values(this.references.sections.main);
174
229
 
175
230
  for (let i = 0; i < sections.length; i += 1) {
176
- if (sections[i].options.includes(rootId)) return sections[i].id
231
+ if (sections[i].options.includes(rootId)) return sections[i].id;
177
232
  }
178
233
 
179
- throw new Error(`Cannot find parent section for option ${id} (Root option id: ${rootId})`)
234
+ throw new Error(
235
+ `Cannot find parent section for option ${id} (Root option id: ${rootId})`
236
+ );
180
237
  }
181
238
  }
182
239
 
183
- export default TagExtractor
240
+ export default TagExtractor;
@@ -0,0 +1,60 @@
1
+ {
2
+ "options": {
3
+ "1": [
4
+ true
5
+ ],
6
+ "2": [
7
+ true
8
+ ],
9
+ "5": [
10
+ true
11
+ ],
12
+ "6": [
13
+ true
14
+ ],
15
+ "7": [
16
+ true
17
+ ],
18
+ "8": [
19
+ false
20
+ ],
21
+ "10": [
22
+ false
23
+ ],
24
+ "11": [
25
+ true
26
+ ],
27
+ "12": [
28
+ true
29
+ ],
30
+ "13": [
31
+ false
32
+ ],
33
+ "14": [
34
+ false
35
+ ],
36
+ "15": [
37
+ false
38
+ ],
39
+ "16": [
40
+ true
41
+ ],
42
+ "18": [
43
+ false
44
+ ],
45
+ "24": [
46
+ true
47
+ ]
48
+ },
49
+ "variables": {
50
+ "1": [
51
+ "Company"
52
+ ],
53
+ "2": [
54
+ ""
55
+ ],
56
+ "5": [
57
+ "Non merci"
58
+ ]
59
+ }
60
+ }
@@ -0,0 +1,291 @@
1
+ {
2
+ "documents": {
3
+ "main": {
4
+ "name": "main",
5
+ "sections": [
6
+ {
7
+ "id": 1,
8
+ "label": "Première section",
9
+ "options": [
10
+ 1,
11
+ 2,
12
+ 5,
13
+ 6,
14
+ 7,
15
+ 11
16
+ ]
17
+ },
18
+ {
19
+ "id": 2,
20
+ "label": "Deuxième section ",
21
+ "options": [
22
+ 15,
23
+ 16
24
+ ],
25
+ "grantLevel": "ADMIN"
26
+ }
27
+ ]
28
+ }
29
+ },
30
+ "options": {
31
+ "1": {
32
+ "meta": {
33
+ "type": "hidden",
34
+ "label": "Text de la question",
35
+ "output": "<p></p>",
36
+ "step": "*",
37
+ "id": 1
38
+ },
39
+ "variables": [],
40
+ "options": []
41
+ },
42
+ "2": {
43
+ "meta": {
44
+ "type": "box",
45
+ "id": 2,
46
+ "step": "*",
47
+ "box": {
48
+ "type": "light",
49
+ "content": "<p>Ceci est du texte de présentation de la première section</p>"
50
+ },
51
+ "label": ""
52
+ },
53
+ "options": [],
54
+ "variables": []
55
+ },
56
+ "5": {
57
+ "meta": {
58
+ "type": "checkbox",
59
+ "id": 5,
60
+ "step": "*",
61
+ "label": "J'ai déjà choisi le nom de mon entreprise (conditionne la question sur le nom de l'entreprise)",
62
+ "conditions": {
63
+ "and": [
64
+ {
65
+ "selected": [
66
+ {
67
+ "var": "o.12"
68
+ }
69
+ ]
70
+ }
71
+ ]
72
+ }
73
+ },
74
+ "options": [],
75
+ "variables": []
76
+ },
77
+ "6": {
78
+ "meta": {
79
+ "type": "static",
80
+ "id": 6,
81
+ "label": "Quel est le nom de votre entreprise ?",
82
+ "step": "*",
83
+ "conditions": {
84
+ "and": [
85
+ {
86
+ "selected": [
87
+ {
88
+ "var": "o.5"
89
+ }
90
+ ]
91
+ }
92
+ ]
93
+ },
94
+ "multiple": {
95
+ "enabled": false
96
+ },
97
+ "style": "full"
98
+ },
99
+ "options": [
100
+ 18,
101
+ 24
102
+ ],
103
+ "variables": [
104
+ 1
105
+ ]
106
+ },
107
+ "7": {
108
+ "meta": {
109
+ "type": "static",
110
+ "id": 7,
111
+ "label": "Connaissiez-vous déjà LegalPlace ?",
112
+ "step": "*",
113
+ "hidden": false,
114
+ "tags": [
115
+ "LP_fan"
116
+ ]
117
+ },
118
+ "options": [
119
+ 8,
120
+ 10
121
+ ],
122
+ "variables": []
123
+ },
124
+ "8": {
125
+ "meta": {
126
+ "type": "radio",
127
+ "id": 8,
128
+ "step": "*",
129
+ "label": "Oui j'adore"
130
+ },
131
+ "options": [],
132
+ "variables": []
133
+ },
134
+ "10": {
135
+ "meta": {
136
+ "type": "radio",
137
+ "id": 10,
138
+ "step": "*",
139
+ "label": "Non"
140
+ },
141
+ "options": [],
142
+ "variables": []
143
+ },
144
+ "11": {
145
+ "meta": {
146
+ "type": "static",
147
+ "id": 11,
148
+ "label": "Quel type d'entreprise ? ",
149
+ "step": "*"
150
+ },
151
+ "options": [
152
+ 12,
153
+ 13,
154
+ 14
155
+ ],
156
+ "variables": []
157
+ },
158
+ "12": {
159
+ "meta": {
160
+ "type": "radio",
161
+ "id": 12,
162
+ "step": "*",
163
+ "label": "SAS"
164
+ },
165
+ "options": [],
166
+ "variables": []
167
+ },
168
+ "13": {
169
+ "meta": {
170
+ "type": "radio",
171
+ "id": 13,
172
+ "step": "*",
173
+ "label": "SASU"
174
+ },
175
+ "options": [],
176
+ "variables": []
177
+ },
178
+ "14": {
179
+ "meta": {
180
+ "type": "radio",
181
+ "id": 14,
182
+ "step": "*",
183
+ "label": "Autre"
184
+ },
185
+ "options": [],
186
+ "variables": []
187
+ },
188
+ "15": {
189
+ "meta": {
190
+ "type": "checkbox",
191
+ "id": 15,
192
+ "step": "*",
193
+ "label": "Je souhaite avoir des questions supplémentaires"
194
+ },
195
+ "options": [],
196
+ "variables": []
197
+ },
198
+ "16": {
199
+ "meta": {
200
+ "type": "static",
201
+ "id": 16,
202
+ "label": "Choisissez le jour de la semaine",
203
+ "step": "*",
204
+ "conditions": {
205
+ "and": [
206
+ {
207
+ "selected": [
208
+ {
209
+ "var": "o.15"
210
+ }
211
+ ]
212
+ }
213
+ ]
214
+ }
215
+ },
216
+ "options": [],
217
+ "variables": [
218
+ 2
219
+ ]
220
+ },
221
+ "18": {
222
+ "meta": {
223
+ "type": "checkbox",
224
+ "id": 18,
225
+ "step": "*",
226
+ "label": "Orange"
227
+ },
228
+ "options": [],
229
+ "variables": []
230
+ },
231
+ "24": {
232
+ "meta": {
233
+ "type": "static",
234
+ "id": 24,
235
+ "label": "Voulez-vous changer de nom ? ",
236
+ "step": "*"
237
+ },
238
+ "options": [],
239
+ "variables": [
240
+ 5
241
+ ]
242
+ }
243
+ },
244
+ "variables": {
245
+ "1": {
246
+ "type": "text",
247
+ "id": 1,
248
+ "label": "Nom de l'entreprise",
249
+ "step": "*",
250
+ "mandatory": false,
251
+ "placeholder": "",
252
+ "conditions": {
253
+ "and": [
254
+ {
255
+ "selected": [
256
+ {
257
+ "var": "o.8"
258
+ }
259
+ ]
260
+ }
261
+ ]
262
+ },
263
+ "tags": [
264
+ "Company_name"
265
+ ],
266
+ "hidden": false
267
+ },
268
+ "2": {
269
+ "type": "list",
270
+ "id": 2,
271
+ "label": "Dropdown",
272
+ "step": "*",
273
+ "selectValues": [
274
+ "Lundi",
275
+ "Mardi",
276
+ "Mercredi",
277
+ "Jeudi",
278
+ "Vendredi"
279
+ ]
280
+ },
281
+ "5": {
282
+ "type": "text",
283
+ "id": 5,
284
+ "label": "Field",
285
+ "step": "*",
286
+ "mandatory": false,
287
+ "placeholder": ""
288
+ }
289
+ },
290
+ "customization": {}
291
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "o": {
3
+ "11": 12
4
+ },
5
+ "v": {
6
+ "1": "Company",
7
+ "2": "",
8
+ "5": "Non merci"
9
+ },
10
+ "c": {
11
+ "5": true,
12
+ "15": false,
13
+ "18": false
14
+ }
15
+ }