@legalplace/prerenderx 2.0.1 → 2.1.1

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,17 +1,12 @@
1
1
  {
2
2
  "eslint.enable": true,
3
- "eslint.autoFixOnSave": true,
4
3
  "editor.tabSize": 2,
5
- "eslint.validate": [
6
- "javascript",
7
- {
8
- "autoFix": true,
9
- "language": "typescript"
10
- }
11
- ],
12
4
  "eslint.options": {
13
5
  "extensions": [".ts", ".js"]
14
6
  },
7
+ "editor.codeActionsOnSave": {
8
+ "source.fixAll.eslint": true
9
+ },
15
10
  "files.autoSave": "off",
16
11
  "editor.formatOnSave": false,
17
12
  "editor.formatOnType": true,
@@ -0,0 +1,21 @@
1
+ import { Types } from "@legalplace/referencesparser";
2
+ declare type References = Types.ReferencesType;
3
+ declare type OVC_TYPE = {
4
+ options: {
5
+ [key: string]: boolean[];
6
+ };
7
+ variables: {
8
+ [key: string]: (number | string)[];
9
+ };
10
+ };
11
+ declare class IntputsInitiator {
12
+ private inputs;
13
+ private references;
14
+ private ovc;
15
+ constructor(references: References, ovc: OVC_TYPE | null);
16
+ getInputs(): OVC_TYPE;
17
+ private setMultiples;
18
+ private initOptionsInputs;
19
+ private initVariablesInputs;
20
+ }
21
+ export default IntputsInitiator;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var IntputsInitiator = (function () {
4
+ function IntputsInitiator(references, ovc) {
5
+ this.inputs = {
6
+ options: {},
7
+ variables: {}
8
+ };
9
+ this.references = references;
10
+ this.ovc = ovc;
11
+ this.setMultiples();
12
+ this.initOptionsInputs();
13
+ this.initVariablesInputs();
14
+ }
15
+ IntputsInitiator.prototype.getInputs = function () {
16
+ return this.inputs;
17
+ };
18
+ IntputsInitiator.prototype.setMultiples = function () {
19
+ var _this = this;
20
+ if (this.ovc === null)
21
+ return;
22
+ Object.keys(this.ovc.options).forEach(function (id) {
23
+ if (_this.ovc === null)
24
+ return;
25
+ var inputs = _this.ovc.options[id];
26
+ if (inputs.length > 1) {
27
+ var parentsTree = _this.references.relations.options[id].parents;
28
+ var rootOptionId = parentsTree.length > 0 ? parentsTree[parentsTree.length - 1] : id;
29
+ if (!_this.ovc.options[rootOptionId]) {
30
+ var rootReference = _this.references.options[rootOptionId];
31
+ if (rootReference.meta.type === "static") {
32
+ _this.ovc.options[rootOptionId] = new Array(inputs.length).fill(true);
33
+ }
34
+ }
35
+ }
36
+ });
37
+ };
38
+ IntputsInitiator.prototype.initOptionsInputs = function () {
39
+ var optionsIds = Object.keys(this.references.options);
40
+ for (var i = 0; i < optionsIds.length; i += 1) {
41
+ var optionId = optionsIds[i];
42
+ var option = this.references.options[optionId];
43
+ var values = [false];
44
+ if (this.ovc &&
45
+ Object.prototype.hasOwnProperty.call(this.ovc.options, optionId)) {
46
+ values = this.ovc.options[optionId];
47
+ }
48
+ else if ([
49
+ "static",
50
+ "hidden",
51
+ "brut-text",
52
+ "repeated",
53
+ "box",
54
+ "separator"
55
+ ].includes(option.meta.type)) {
56
+ values = [true];
57
+ }
58
+ this.inputs.options[optionId] = values;
59
+ }
60
+ };
61
+ IntputsInitiator.prototype.initVariablesInputs = function () {
62
+ var variablesIds = Object.keys(this.references.variables);
63
+ for (var i = 0; i < variablesIds.length; i += 1) {
64
+ var variableId = variablesIds[i];
65
+ var values = [""];
66
+ if (this.ovc &&
67
+ Object.prototype.hasOwnProperty.call(this.ovc.variables, variableId)) {
68
+ values = this.ovc.variables[variableId];
69
+ }
70
+ else {
71
+ var defaultValue = "";
72
+ var prefillings = this.references.variables[variableId].prefillings;
73
+ if (prefillings &&
74
+ Array.isArray(prefillings) &&
75
+ prefillings.length > 0) {
76
+ var noConditionsPrefills = prefillings.filter(function (prefill) {
77
+ return typeof prefill.conditions === "undefined" ||
78
+ Object.keys(prefill.conditions).length === 0;
79
+ });
80
+ defaultValue =
81
+ noConditionsPrefills.length > 0
82
+ ? noConditionsPrefills[0].value
83
+ : "";
84
+ }
85
+ values = [defaultValue];
86
+ }
87
+ this.inputs.variables[variableId] = values;
88
+ }
89
+ };
90
+ return IntputsInitiator;
91
+ }());
92
+ exports.default = IntputsInitiator;
@@ -0,0 +1,19 @@
1
+ import OVC_TYPE from "./ovc.type";
2
+ export interface OldOVC {
3
+ o: {
4
+ [k: string]: string | number | (string | number)[];
5
+ };
6
+ v: {
7
+ [k: string]: string | number | (string | number)[];
8
+ };
9
+ c: {
10
+ [k: string]: boolean | boolean[];
11
+ };
12
+ }
13
+ declare class OvcConverter {
14
+ convertToOptionsVariables(ovc: OldOVC): OVC_TYPE;
15
+ isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC;
16
+ isOptionsVariables(_obj: OVC_TYPE | OldOVC): boolean;
17
+ }
18
+ declare const _default: OvcConverter;
19
+ export default _default;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var OvcConverter = (function () {
4
+ function OvcConverter() {
5
+ }
6
+ OvcConverter.prototype.convertToOptionsVariables = function (ovc) {
7
+ var inputs = {
8
+ options: {},
9
+ variables: {}
10
+ };
11
+ if (typeof ovc !== "object")
12
+ return inputs;
13
+ if (typeof ovc.o === "object") {
14
+ Object.keys(ovc.o).forEach(function (id) {
15
+ var currentOption = ovc.o[id];
16
+ if (Array.isArray(currentOption)) {
17
+ var occurences_1 = currentOption.length;
18
+ currentOption.forEach(function (childId, index) {
19
+ if (typeof childId === "string" && childId.trim().length === 0)
20
+ return;
21
+ if (!Object.prototype.hasOwnProperty.call(inputs.options, childId)) {
22
+ inputs.options[childId] = new Array(occurences_1).fill(false);
23
+ }
24
+ inputs.options[childId][index] = true;
25
+ });
26
+ }
27
+ else {
28
+ if (typeof currentOption === "string" &&
29
+ currentOption.trim().length === 0)
30
+ return;
31
+ inputs.options[currentOption] = [true];
32
+ }
33
+ });
34
+ }
35
+ if (typeof ovc.v === "object") {
36
+ Object.keys(ovc.v).forEach(function (id) {
37
+ var currentVariable = ovc.v[id];
38
+ if (Array.isArray(currentVariable)) {
39
+ inputs.variables[id] = currentVariable;
40
+ }
41
+ else {
42
+ inputs.variables[id] = [currentVariable];
43
+ }
44
+ });
45
+ }
46
+ if (typeof ovc.c === "object") {
47
+ Object.keys(ovc.c).forEach(function (id) {
48
+ var currentCheckbox = ovc.c[id];
49
+ if (typeof id === "string" && id.trim().length === 0)
50
+ return;
51
+ if (Array.isArray(currentCheckbox)) {
52
+ inputs.options[id] = currentCheckbox;
53
+ }
54
+ else {
55
+ inputs.options[id] = [currentCheckbox];
56
+ }
57
+ });
58
+ }
59
+ return inputs;
60
+ };
61
+ OvcConverter.prototype.isOvc = function (_obj) {
62
+ if (typeof _obj !== "object" || _obj === null)
63
+ return false;
64
+ var ovcKeys = ["o", "v", "c"];
65
+ var foundKeys = Object.keys(_obj)
66
+ .map(function (key) { return (ovcKeys.includes(key) ? 1 : 0); })
67
+ .reduce(function (a, b) { return a + b; }, 0);
68
+ return foundKeys > 1;
69
+ };
70
+ OvcConverter.prototype.isOptionsVariables = function (_obj) {
71
+ if (typeof _obj !== "object" || _obj === null)
72
+ return false;
73
+ var optionsVariablesKeys = ["options", "variables"];
74
+ var foundKeys = Object.keys(_obj)
75
+ .map(function (key) { return (optionsVariablesKeys.includes(key) ? 1 : 0); })
76
+ .reduce(function (a, b) { return a + b; }, 0);
77
+ return foundKeys === 2;
78
+ };
79
+ return OvcConverter;
80
+ }());
81
+ exports.default = new OvcConverter();
@@ -1,5 +1,6 @@
1
1
  import { ModelV3 } from "@legalplace/models-v3-types";
2
2
  import OVC_TYPE from "./ovc.type";
3
+ import { OldOVC } from "./OvcConverter";
3
4
  declare class Prerender {
4
5
  private model;
5
6
  private references;
@@ -9,7 +10,7 @@ declare class Prerender {
9
10
  private NumAuto;
10
11
  private documentsIndex;
11
12
  private renderedDocuments;
12
- constructor(model: ModelV3, ovc: OVC_TYPE);
13
+ constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC);
13
14
  getDocumentsIndex(): Record<string, {
14
15
  name: string;
15
16
  pdf: boolean;
@@ -7,17 +7,22 @@ var referencesparser_1 = require("@legalplace/referencesparser");
7
7
  var crypto_1 = __importDefault(require("crypto"));
8
8
  var DocumentRender_1 = __importDefault(require("./DocumentRender"));
9
9
  var ConditionsRunner_1 = __importDefault(require("./ConditionsRunner"));
10
+ var OvcConverter_1 = __importDefault(require("./OvcConverter"));
10
11
  var NumAuto_1 = __importDefault(require("./NumAuto"));
12
+ var InputsInitiator_1 = __importDefault(require("./InputsInitiator"));
11
13
  var Prerender = (function () {
12
14
  function Prerender(model, ovc) {
13
15
  this.documentsIndex = {};
14
16
  this.renderedDocuments = {};
15
17
  this.model = model;
16
- this.ovc = ovc;
18
+ this.ovc = OvcConverter_1.default.isOvc(ovc)
19
+ ? OvcConverter_1.default.convertToOptionsVariables(ovc)
20
+ : ovc;
17
21
  this.NumAuto = new NumAuto_1.default();
18
22
  this.references = new referencesparser_1.ReferencesParser(model).getReferences();
19
- this.conditions = new ConditionsRunner_1.default(this.references, this.ovc);
20
- this.documentRender = new DocumentRender_1.default(this.references, this.conditions, this.ovc, this.NumAuto);
23
+ var inputs = new InputsInitiator_1.default(this.references, this.ovc).getInputs();
24
+ this.conditions = new ConditionsRunner_1.default(this.references, inputs);
25
+ this.documentRender = new DocumentRender_1.default(this.references, this.conditions, inputs, this.NumAuto);
21
26
  this.generateDocumentNamesHash();
22
27
  this.generateAllDocuments();
23
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/prerenderx",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "PrerenderX",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://git.legalplace.eu/legalplace/prerenderx",
@@ -0,0 +1,146 @@
1
+ import { Types } from "@legalplace/referencesparser";
2
+
3
+ type References = Types.ReferencesType;
4
+
5
+ type OVC_TYPE = {
6
+ options: {
7
+ [key: string]: boolean[];
8
+ };
9
+ variables: {
10
+ [key: string]: (number | string)[];
11
+ };
12
+ };
13
+
14
+ class IntputsInitiator {
15
+ private inputs: OVC_TYPE = {
16
+ options: {},
17
+ variables: {}
18
+ };
19
+
20
+ private references: References;
21
+
22
+ private ovc: OVC_TYPE | null;
23
+
24
+ /**
25
+ * InitalConditions Constructor
26
+ * @param references References object
27
+ */
28
+ constructor(references: References, ovc: OVC_TYPE | null) {
29
+ this.references = references;
30
+ this.ovc = ovc;
31
+
32
+ // Setting multiples
33
+ this.setMultiples();
34
+
35
+ // Initiating inputs
36
+ this.initOptionsInputs();
37
+ this.initVariablesInputs();
38
+ }
39
+
40
+ public getInputs() {
41
+ return this.inputs;
42
+ }
43
+
44
+ private setMultiples() {
45
+ if (this.ovc === null) return;
46
+ Object.keys(this.ovc.options).forEach(id => {
47
+ if (this.ovc === null) return;
48
+ const inputs = this.ovc.options[id];
49
+
50
+ if (inputs.length > 1) {
51
+ // Getting parents
52
+ const parentsTree = this.references.relations.options[id].parents;
53
+
54
+ // root parent
55
+ const rootOptionId =
56
+ parentsTree.length > 0 ? parentsTree[parentsTree.length - 1] : id;
57
+
58
+ if (!this.ovc.options[rootOptionId]) {
59
+ // root Option reference
60
+ const rootReference = this.references.options[rootOptionId];
61
+ if (rootReference.meta.type === "static") {
62
+ this.ovc.options[rootOptionId] = new Array(inputs.length).fill(
63
+ true
64
+ );
65
+ }
66
+ }
67
+ }
68
+ });
69
+ }
70
+
71
+ /**
72
+ * Initates options inputs
73
+ */
74
+ private initOptionsInputs() {
75
+ // Options
76
+ const optionsIds = Object.keys(this.references.options);
77
+ for (let i = 0; i < optionsIds.length; i += 1) {
78
+ const optionId = optionsIds[i];
79
+ const option = this.references.options[optionId];
80
+
81
+ let values = [false];
82
+ if (
83
+ this.ovc &&
84
+ Object.prototype.hasOwnProperty.call(this.ovc.options, optionId)
85
+ ) {
86
+ values = this.ovc.options[optionId];
87
+ } else if (
88
+ [
89
+ "static",
90
+ "hidden",
91
+ "brut-text",
92
+ "repeated",
93
+ "box",
94
+ "separator"
95
+ ].includes(option.meta.type)
96
+ ) {
97
+ values = [true];
98
+ }
99
+
100
+ this.inputs.options[optionId] = values;
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Initates variables inputs
106
+ */
107
+ private initVariablesInputs() {
108
+ const variablesIds = Object.keys(this.references.variables);
109
+ for (let i = 0; i < variablesIds.length; i += 1) {
110
+ const variableId = variablesIds[i];
111
+
112
+ let values: (string | number)[] = [""];
113
+ if (
114
+ this.ovc &&
115
+ Object.prototype.hasOwnProperty.call(this.ovc.variables, variableId)
116
+ ) {
117
+ values = this.ovc.variables[variableId];
118
+ } else {
119
+ let defaultValue = "";
120
+
121
+ // Checking for first prefill without condition
122
+ const { prefillings } = this.references.variables[variableId];
123
+ if (
124
+ prefillings &&
125
+ Array.isArray(prefillings) &&
126
+ prefillings.length > 0
127
+ ) {
128
+ const noConditionsPrefills = prefillings.filter(
129
+ prefill =>
130
+ typeof prefill.conditions === "undefined" ||
131
+ Object.keys(prefill.conditions).length === 0
132
+ );
133
+ defaultValue =
134
+ noConditionsPrefills.length > 0
135
+ ? noConditionsPrefills[0].value
136
+ : "";
137
+ }
138
+ values = [defaultValue];
139
+ }
140
+
141
+ this.inputs.variables[variableId] = values;
142
+ }
143
+ }
144
+ }
145
+
146
+ export default IntputsInitiator;
@@ -0,0 +1,107 @@
1
+ import OVC_TYPE from "./ovc.type";
2
+
3
+ export interface OldOVC {
4
+ o: {
5
+ [k: string]: string | number | (string | number)[];
6
+ };
7
+ v: {
8
+ [k: string]: string | number | (string | number)[];
9
+ };
10
+ c: {
11
+ [k: string]: boolean | boolean[];
12
+ };
13
+ }
14
+
15
+ class OvcConverter {
16
+ public convertToOptionsVariables(ovc: OldOVC) {
17
+ const inputs: OVC_TYPE = {
18
+ options: {},
19
+ variables: {}
20
+ };
21
+
22
+ if (typeof ovc !== "object") return inputs;
23
+
24
+ // Reading options
25
+ if (typeof ovc.o === "object") {
26
+ Object.keys(ovc.o).forEach(id => {
27
+ const currentOption = ovc.o[id];
28
+
29
+ // If it's a multiple
30
+ if (Array.isArray(currentOption)) {
31
+ const occurences = currentOption.length;
32
+ currentOption.forEach((childId, index) => {
33
+ if (typeof childId === "string" && childId.trim().length === 0)
34
+ return;
35
+
36
+ // Creating input if it doesn't exist
37
+ if (
38
+ !Object.prototype.hasOwnProperty.call(inputs.options, childId)
39
+ ) {
40
+ inputs.options[childId] = new Array(occurences).fill(false);
41
+ }
42
+
43
+ // Setting current index to true
44
+ inputs.options[childId][index] = true;
45
+ });
46
+ } else {
47
+ if (
48
+ typeof currentOption === "string" &&
49
+ currentOption.trim().length === 0
50
+ )
51
+ return;
52
+ inputs.options[currentOption] = [true];
53
+ }
54
+ });
55
+ }
56
+
57
+ // Reading variables
58
+ if (typeof ovc.v === "object") {
59
+ Object.keys(ovc.v).forEach(id => {
60
+ const currentVariable = ovc.v[id];
61
+
62
+ // If it's a multiple
63
+ if (Array.isArray(currentVariable)) {
64
+ inputs.variables[id] = currentVariable;
65
+ } else {
66
+ inputs.variables[id] = [currentVariable];
67
+ }
68
+ });
69
+ }
70
+
71
+ // Reading checkboxes
72
+ if (typeof ovc.c === "object") {
73
+ Object.keys(ovc.c).forEach(id => {
74
+ const currentCheckbox = ovc.c[id];
75
+ if (typeof id === "string" && id.trim().length === 0) return;
76
+ // If it's a multiple
77
+ if (Array.isArray(currentCheckbox)) {
78
+ inputs.options[id] = currentCheckbox;
79
+ } else {
80
+ inputs.options[id] = [currentCheckbox];
81
+ }
82
+ });
83
+ }
84
+
85
+ return inputs;
86
+ }
87
+
88
+ public isOvc(_obj: OVC_TYPE | OldOVC): _obj is OldOVC {
89
+ if (typeof _obj !== "object" || _obj === null) return false;
90
+ const ovcKeys = ["o", "v", "c"];
91
+ const foundKeys = Object.keys(_obj)
92
+ .map((key): number => (ovcKeys.includes(key) ? 1 : 0))
93
+ .reduce((a, b) => a + b, 0);
94
+ return foundKeys > 1;
95
+ }
96
+
97
+ public isOptionsVariables(_obj: OVC_TYPE | OldOVC) {
98
+ if (typeof _obj !== "object" || _obj === null) return false;
99
+ const optionsVariablesKeys = ["options", "variables"];
100
+ const foundKeys = Object.keys(_obj)
101
+ .map((key): number => (optionsVariablesKeys.includes(key) ? 1 : 0))
102
+ .reduce((a, b) => a + b, 0);
103
+ return foundKeys === 2;
104
+ }
105
+ }
106
+
107
+ export default new OvcConverter();
@@ -4,7 +4,9 @@ import crypto from "crypto";
4
4
  import DocumentRender from "./DocumentRender";
5
5
  import ConditionsRunner from "./ConditionsRunner";
6
6
  import OVC_TYPE from "./ovc.type";
7
+ import OvcConverter, { OldOVC } from "./OvcConverter";
7
8
  import NumAuto from "./NumAuto";
9
+ import IntputsInitiator from "./InputsInitiator";
8
10
 
9
11
  class Prerender {
10
12
  private model: ModelV3;
@@ -30,22 +32,31 @@ class Prerender {
30
32
 
31
33
  private renderedDocuments: Record<string, string> = {};
32
34
 
33
- constructor(model: ModelV3, ovc: OVC_TYPE) {
35
+ constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC) {
34
36
  this.model = model;
35
- this.ovc = ovc;
37
+
38
+ // Converting OVC if needed
39
+ this.ovc = OvcConverter.isOvc(ovc)
40
+ ? OvcConverter.convertToOptionsVariables(ovc)
41
+ : ovc;
42
+
43
+ // Initiating num auto
36
44
  this.NumAuto = new NumAuto();
37
45
 
38
- // @ts-ignore
46
+ // Parsing references
39
47
  this.references = new ReferencesParser(model).getReferences();
40
48
 
49
+ // Initiating inputs
50
+ const inputs = new IntputsInitiator(this.references, this.ovc).getInputs();
51
+
41
52
  // Initiating ConditionsRunner
42
- this.conditions = new ConditionsRunner(this.references, this.ovc);
53
+ this.conditions = new ConditionsRunner(this.references, inputs);
43
54
 
44
55
  // Rendering documents
45
56
  this.documentRender = new DocumentRender(
46
57
  this.references,
47
58
  this.conditions,
48
- this.ovc,
59
+ inputs,
49
60
  this.NumAuto
50
61
  );
51
62