@legalplace/tagextractor 2.1.0 → 2.3.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,97 +1,108 @@
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 type InputsType from "@legalplace/types/dist/inputs";
7
+ import type 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
- variables: {}
20
- }
19
+ variables: {},
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
+ Object.keys(_obj).some((key) => key === "boxes");
26
27
 
27
- constructor(modelOrReferences: ModelV3 | ReferencesType, ovc: OvcType | InputsType) {
28
- this.references = this.isReferencesType(modelOrReferences) ? modelOrReferences : new ReferencesParser(modelOrReferences).references
28
+ constructor(
29
+ modelOrReferences: ModelV3 | ReferencesType,
30
+ ovc: OvcType | InputsType
31
+ ) {
32
+ this.references = this.isReferencesType(modelOrReferences)
33
+ ? modelOrReferences
34
+ : new ReferencesParser(modelOrReferences).references;
29
35
 
30
36
  // Converting inputs if needed
31
- this.inputs = OvcConverter.isOptionsVariables(ovc) ? ovc : OvcConverter.convertToOptionsVariables(ovc, this.references)
37
+ this.inputs = OvcConverter.isOptionsVariables(ovc)
38
+ ? ovc
39
+ : OvcConverter.convertToOptionsVariables(ovc, this.references);
32
40
 
33
41
  // Initiaing Conditions runner
34
- this.conditionsRunner = new ConditionsRunner(this.references, this.inputs)
42
+ this.conditionsRunner = new ConditionsRunner(this.references, this.inputs);
35
43
 
36
44
  // Extracting tags from options
37
- this.extractFromOptions()
45
+ this.extractFromOptions();
38
46
 
39
47
  // Extracting tags from variables
40
- this.extractFromVariables()
48
+ this.extractFromVariables();
41
49
  }
42
50
 
43
51
  public get getTags() {
44
- return this.tags
52
+ return this.tags;
45
53
  }
46
54
 
47
55
  /**
48
56
  * Reads Options
49
57
  */
50
58
  private extractFromOptions() {
51
- Object.keys(this.references.options).forEach(id => {
52
- const { tags, label } = this.references.options[id].meta
59
+ Object.keys(this.references.options).forEach((id) => {
60
+ const { tags, label } = this.references.options[id].meta;
53
61
 
54
62
  if (Array.isArray(tags) && tags.length > 0 && this.inputs.options[id]) {
55
63
  this.inputs.options[id].forEach((v, index) => {
56
- tags.forEach(tag => {
57
- if (this.tags.options[tag] === undefined) this.tags.options[tag] = []
64
+ tags.forEach((tag) => {
65
+ if (this.tags.options[tag] === undefined)
66
+ this.tags.options[tag] = [];
58
67
  this.tags.options[tag].push({
59
68
  id: parseInt(id, 10),
60
69
  index,
61
70
  label,
62
71
  condition: this.getOptionCondition(parseInt(id, 10), index),
63
72
  visible: this.isOptionDisplayed(parseInt(id, 10), index),
64
- value: this.inputs.options[id][index]
65
- })
66
- })
67
- })
73
+ value: this.inputs.options[id][index],
74
+ });
75
+ });
76
+ });
68
77
  }
69
- })
78
+ });
70
79
  }
71
80
 
72
81
  /**
73
82
  * Reads Variables
74
83
  */
75
84
  private extractFromVariables() {
76
- Object.keys(this.references.variables).forEach(id => {
77
- const { tags, label } = this.references.variables[id]
85
+ Object.keys(this.references.variables).forEach((id) => {
86
+ const { tags, label } = this.references.variables[id];
78
87
 
79
88
  if (Array.isArray(tags) && tags.length > 0 && this.inputs.variables[id]) {
80
89
  this.inputs.variables[id].forEach((v, index) => {
81
- tags.forEach(tag => {
82
- if (this.tags.variables[tag] === undefined) this.tags.variables[tag] = []
90
+ tags.forEach((tag) => {
91
+ if (this.tags.variables[tag] === undefined)
92
+ this.tags.variables[tag] = [];
83
93
  this.tags.variables[tag].push({
84
94
  id: parseInt(id, 10),
85
95
  index,
86
96
  label,
87
- condition: this.getVariableCondition(parseInt(id, 10), index) !== false,
97
+ condition:
98
+ this.getVariableCondition(parseInt(id, 10), index) !== false,
88
99
  visible: this.isVariableDisplayed(parseInt(id, 10), index),
89
- value: this.inputs.variables[id][index]
90
- })
91
- })
92
- })
100
+ value: this.inputs.variables[id][index],
101
+ });
102
+ });
103
+ });
93
104
  }
94
- })
105
+ });
95
106
  }
96
107
 
97
108
  /**
@@ -101,12 +112,23 @@ class TagExtractor {
101
112
  */
102
113
  isVariableDisplayed(id: number, index: number) {
103
114
  // 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
115
+ const variableCondition = this.getVariableCondition(id, index);
116
+ const variableParents =
117
+ this.references.relations.variables[id]?.parents || [];
118
+ const parentOptionIsDisplayed = this.isOptionDisplayed(
119
+ variableParents[0],
120
+ index
121
+ );
122
+ const parentOptionIsTruethful =
123
+ this.inputs.options[variableParents[0]]?.[index] === true;
124
+
125
+ return (
126
+ [
127
+ variableCondition,
128
+ parentOptionIsDisplayed,
129
+ parentOptionIsTruethful,
130
+ ].filter((c) => c !== true).length === 0
131
+ );
110
132
  }
111
133
 
112
134
  /**
@@ -116,19 +138,29 @@ class TagExtractor {
116
138
  */
117
139
  isOptionDisplayed(id: number, index: number) {
118
140
  // Getting variable's conditions & executing it if any
119
- const optionCondition = this.getOptionCondition(id, index)
120
- const optionParents = this.references.relations.options[id]?.parents || []
121
- const parentsConditions = optionParents.map(optionId => {
122
- return this.getOptionCondition(optionId, index) !== false
123
- })
124
- const parentsInputs = optionParents.map(optionId => {
141
+ const optionCondition = this.getOptionCondition(id, index);
142
+ const optionParents = this.references.relations.options[id]?.parents || [];
143
+ const parentsConditions = optionParents.map(
144
+ (optionId) => this.getOptionCondition(optionId, index) !== false
145
+ );
146
+ const parentsInputs = optionParents.map((optionId) =>
125
147
  // If the parent option is not already defined we set the displayed parameter to false
126
- return this.inputs.options[optionId]?.[index] || false
127
- })
128
- const parentSectionId = this.getOptionParentSection(id)
129
- const parentSectionCondition = this.getSectionCondition(parentSectionId) !== false
130
-
131
- return [optionCondition, parentSectionCondition, ...parentsConditions, ...parentsInputs].filter(c => c !== true).length === 0
148
+ this.inputs.options[optionId]
149
+ ? this.inputs.options[optionId][index]
150
+ : false
151
+ );
152
+ const parentSectionId = this.getOptionParentSection(id);
153
+ const parentSectionCondition =
154
+ this.getSectionCondition(parentSectionId) !== false;
155
+
156
+ return (
157
+ [
158
+ optionCondition,
159
+ parentSectionCondition,
160
+ ...parentsConditions,
161
+ ...parentsInputs,
162
+ ].filter((c) => c !== true).length === 0
163
+ );
132
164
  }
133
165
 
134
166
  /**
@@ -137,8 +169,15 @@ class TagExtractor {
137
169
  * @param index Variable's index
138
170
  */
139
171
  private getVariableCondition(id: number, index: number) {
140
- const conditionObject = this.references.conditions.variables[id]
141
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'variables')
172
+ const conditionObject = this.references.conditions.variables[id];
173
+ return conditionObject === undefined
174
+ ? true
175
+ : this.conditionsRunner.executeCondition(
176
+ conditionObject,
177
+ id,
178
+ index,
179
+ "variables"
180
+ );
142
181
  }
143
182
 
144
183
  /**
@@ -147,8 +186,15 @@ class TagExtractor {
147
186
  * @param index Option's index
148
187
  */
149
188
  private getOptionCondition(id: number, index: number) {
150
- const conditionObject = this.references.conditions.options[id]
151
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, index, 'options')
189
+ const conditionObject = this.references.conditions.options[id];
190
+ return conditionObject === undefined
191
+ ? true
192
+ : this.conditionsRunner.executeCondition(
193
+ conditionObject,
194
+ id,
195
+ index,
196
+ "options"
197
+ );
152
198
  }
153
199
 
154
200
  /**
@@ -156,8 +202,15 @@ class TagExtractor {
156
202
  * @param id Section's id
157
203
  */
158
204
  private getSectionCondition(id: number) {
159
- const conditionObject = this.references.conditions.sections.main[id]
160
- return conditionObject === undefined ? true : this.conditionsRunner.executeCondition(conditionObject, id, 0, 'sections')
205
+ const conditionObject = this.references.conditions.sections.main[id];
206
+ return conditionObject === undefined
207
+ ? true
208
+ : this.conditionsRunner.executeCondition(
209
+ conditionObject,
210
+ id,
211
+ 0,
212
+ "sections"
213
+ );
161
214
  }
162
215
 
163
216
  /**
@@ -165,20 +218,22 @@ class TagExtractor {
165
218
  * @param id Option's id
166
219
  */
167
220
  private getOptionParentSection(id: number) {
168
- const { parents } = this.references.relations.options[id]
221
+ const { parents } = this.references.relations.options[id];
169
222
 
170
223
  // Getting root option id
171
- const rootId = parents.length > 0 ? parents[parents.length - 1] : id
224
+ const rootId = parents.length > 0 ? parents[parents.length - 1] : id;
172
225
 
173
226
  // Looking for section
174
- const sections = Object.values(this.references.sections.main)
227
+ const sections = Object.values(this.references.sections.main);
175
228
 
176
229
  for (let i = 0; i < sections.length; i += 1) {
177
- if (sections[i].options.includes(rootId)) return sections[i].id
230
+ if (sections[i].options.includes(rootId)) return sections[i].id;
178
231
  }
179
232
 
180
- throw new Error(`Cannot find parent section for option ${id} (Root option id: ${rootId})`)
233
+ throw new Error(
234
+ `Cannot find parent section for option ${id} (Root option id: ${rootId})`
235
+ );
181
236
  }
182
237
  }
183
238
 
184
- export default TagExtractor
239
+ 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
+ }