@legalplace/prerenderx 2.2.5 → 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.
package/README.md CHANGED
@@ -14,7 +14,55 @@ const ovc = {options: {}, variables: {}}; // Your OVC
14
14
 
15
15
  const prerender = new Prerender(modelV3, ovc);
16
16
 
17
- const documents = prerender.getDocuments();
17
+ // Getting documents index
18
+ const documentsIndex = prerender.getDocumentsIndex();
19
+
20
+ // Getting all documents
21
+ const documents = Object.keys(documentsIndexes).map((hash) => prerender.getDocument(hash))
22
+ ```
23
+
24
+ # Return formats
25
+ *Prerender.getDocument* returns two different types:
26
+
27
+ * In case document is an output it returns a *plaintext*
28
+ * In case document is a pdf form it returns an object with this definition:
29
+ ```
30
+ {
31
+ id: string
32
+ inputs: {
33
+ // Key is input's id
34
+ // Value is input's value
35
+ [key: string]: string
36
+ }
37
+ boxes: {
38
+ // Key is box's id
39
+ // Value is box's value
40
+ [key: string]: string
41
+ }
42
+ }
43
+ ```
44
+
45
+ # How to determine whether a document is an output or a pdf form
46
+ *Note*
47
+ * Outputs should be sent to the worker to generate DOCX & PDF files
48
+ * Pdf forms should be filled with pdfjson and the original pdf
49
+
50
+ ```javascript
51
+ import { Prerender } from 'prerenderx';
52
+
53
+ const model = {}; // Your v3 (non final) model
54
+ const ovc = {options: {}, variables: {}}; // Your OVC
55
+
56
+ const prerender = new Prerender(modelV3, ovc);
57
+
58
+ // Getting documents index
59
+ const documentsIndex = prerender.getDocumentsIndex();
60
+
61
+ // Extracting PDF Forms documents only
62
+ const pdfFormsDocuments = documentsIndex.filter((documentDescription) => Prerender.isPdfForm(documentDescription))
63
+
64
+ // Extracting outputs documents only
65
+ const outputDocuments = documentsIndex.filter((documentDescription) => Prerender.isOutput(documentDescription))
18
66
  ```
19
67
 
20
68
  Variable `documents` will now contain an object containing all documents and their respective outputs
@@ -6,8 +6,6 @@ declare class DocumentRender {
6
6
  references: Types.ReferencesType;
7
7
  conditions: ConditionsRunner;
8
8
  ovc: OVC_TYPE;
9
- documents: Record<string, string[]>;
10
- documentsNamesHash: Record<string, string>;
11
9
  private NumAuto;
12
10
  constructor(references: Types.ReferencesType, conditions: ConditionsRunner, ovc: OVC_TYPE, numauto: NumAuto);
13
11
  renderDocument(documentName: string): string;
@@ -6,8 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var SectionRender_1 = __importDefault(require("./SectionRender"));
7
7
  var DocumentRender = (function () {
8
8
  function DocumentRender(references, conditions, ovc, numauto) {
9
- this.documents = {};
10
- this.documentsNamesHash = {};
11
9
  this.references = references;
12
10
  this.conditions = conditions;
13
11
  this.ovc = ovc;
@@ -0,0 +1,13 @@
1
+ import FillPdfFormBase from './misc/FillPdfFormBase';
2
+ declare class FillPdfForm extends FillPdfFormBase {
3
+ fillDocument(documentName: string): {
4
+ id: string;
5
+ inputs: Record<string, string>;
6
+ boxes: Record<string, string>;
7
+ };
8
+ private getInputValue;
9
+ private getBoxValue;
10
+ private getVariableValue;
11
+ private getOptionValue;
12
+ }
13
+ export default FillPdfForm;
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __importDefault = (this && this.__importDefault) || function (mod) {
16
+ return (mod && mod.__esModule) ? mod : { "default": mod };
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ var FillPdfFormBase_1 = __importDefault(require("./misc/FillPdfFormBase"));
20
+ var FillPdfForm = (function (_super) {
21
+ __extends(FillPdfForm, _super);
22
+ function FillPdfForm() {
23
+ return _super !== null && _super.apply(this, arguments) || this;
24
+ }
25
+ FillPdfForm.prototype.fillDocument = function (documentName) {
26
+ var _this = this;
27
+ var result = {
28
+ id: '',
29
+ inputs: {},
30
+ boxes: {}
31
+ };
32
+ var document = this.references.documents[documentName];
33
+ var pdf = document.pdf;
34
+ if (typeof pdf === 'undefined')
35
+ return result;
36
+ var form = pdf.form;
37
+ result.id = pdf.id;
38
+ form.inputs.forEach(function (input) {
39
+ var id = "Page" + input.page + "[0]." + input.name;
40
+ result.inputs[id] = _this.getInputValue(input);
41
+ });
42
+ form.boxes.forEach(function (box) {
43
+ var id = "Page" + box.page + "[0]." + box.name;
44
+ result.boxes[id] = _this.getBoxValue(box);
45
+ });
46
+ return result;
47
+ };
48
+ FillPdfForm.prototype.getInputValue = function (input) {
49
+ for (var i = 0; i < input.variables.length; i += 1) {
50
+ var variable = input.variables[i];
51
+ var currentValue = this.getVariableValue(variable.id);
52
+ if (currentValue !== false) {
53
+ var dateRegex = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
54
+ console.log('Value for', variable.id, currentValue, dateRegex.test(currentValue));
55
+ if (variable.date && dateRegex.test(currentValue)) {
56
+ switch (variable.date) {
57
+ case 'DD':
58
+ console.log('Value s', variable.id, input.name, currentValue.replace(dateRegex, '$1'), dateRegex.test(currentValue));
59
+ return currentValue.replace(dateRegex, '$1');
60
+ case 'MM':
61
+ return currentValue.replace(dateRegex, '$2');
62
+ case 'YYYY':
63
+ return currentValue.replace(dateRegex, '$3');
64
+ default:
65
+ }
66
+ }
67
+ var hourRegex = /^([0-9]{2}:[0-9]{2})$/;
68
+ if (variable.hour && hourRegex.test(currentValue)) {
69
+ switch (variable.hour) {
70
+ case 'HH':
71
+ return currentValue.replace(hourRegex, '$1');
72
+ case 'MM':
73
+ return currentValue.replace(hourRegex, '$2');
74
+ default:
75
+ }
76
+ }
77
+ var emailRegex = /^(.*)@(.*)$/;
78
+ if (variable.email && emailRegex.test(currentValue)) {
79
+ switch (variable.email) {
80
+ case 'ADDRESS':
81
+ return currentValue.split('@')[0];
82
+ case 'PROVIDER':
83
+ return currentValue.split('@')[1];
84
+ default:
85
+ }
86
+ }
87
+ if (variable.fragment && typeof variable.fragment.from === 'number' && typeof variable.fragment.to === 'number') {
88
+ var _a = variable.fragment, from = _a.from, to = _a.to;
89
+ return currentValue.slice(from, to);
90
+ }
91
+ return currentValue;
92
+ }
93
+ }
94
+ return '';
95
+ };
96
+ FillPdfForm.prototype.getBoxValue = function (box) {
97
+ for (var i = 0; i < box.options.length; i += 1) {
98
+ var option = box.options[i];
99
+ var currentValue = this.getOptionValue(option.id);
100
+ if (currentValue === true) {
101
+ return box.value;
102
+ }
103
+ }
104
+ return '';
105
+ };
106
+ FillPdfForm.prototype.getVariableValue = function (variableId) {
107
+ var _a;
108
+ if (this.isVariableDisplayed(variableId, 0) === false)
109
+ return false;
110
+ return (((_a = this.ovc.variables[variableId]) === null || _a === void 0 ? void 0 : _a[0]) || '').toString();
111
+ };
112
+ FillPdfForm.prototype.getOptionValue = function (optionId) {
113
+ var _a;
114
+ if (this.isOptionDisplayed(optionId, 0) === false)
115
+ return false;
116
+ return ((_a = this.ovc.options[optionId]) === null || _a === void 0 ? void 0 : _a[0]) === true;
117
+ };
118
+ return FillPdfForm;
119
+ }(FillPdfFormBase_1.default));
120
+ exports.default = FillPdfForm;
@@ -1,23 +1,35 @@
1
1
  import { ModelV3 } from '@legalplace/models-v3-types';
2
2
  import OVC_TYPE from './ovc.type';
3
3
  import { OldOVC } from './OvcConverter';
4
+ declare type DocumentOutputIndex = {
5
+ name: string;
6
+ pdf: boolean;
7
+ docx: boolean;
8
+ };
9
+ declare type DocumentPdfFormIndex = {
10
+ name: string;
11
+ form: boolean;
12
+ };
13
+ declare type FDFType = {
14
+ inputs: Record<string, string>;
15
+ boxes: Record<string, string>;
16
+ };
4
17
  declare class Prerender {
5
18
  private model;
6
19
  private references;
7
20
  private ovc;
8
21
  private conditions;
9
22
  private documentRender;
23
+ private pdfFiller;
10
24
  private NumAuto;
11
25
  private documentsIndex;
12
26
  private renderedDocuments;
13
27
  constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC);
14
- getDocumentsIndex(): Record<string, {
15
- name: string;
16
- pdf: boolean;
17
- docx: boolean;
18
- }>;
28
+ getDocumentsIndex(): Record<string, DocumentOutputIndex | DocumentPdfFormIndex>;
29
+ getDocument(documentNameHash: string): string | FDFType;
19
30
  private generateDocumentNamesHash;
20
31
  private generateAllDocuments;
21
- getDocument(documentNameHash: string): string;
32
+ static isPdfForm(index: DocumentOutputIndex | DocumentPdfFormIndex): index is DocumentPdfFormIndex;
33
+ static isOutput(index: DocumentOutputIndex | DocumentPdfFormIndex): index is DocumentOutputIndex;
22
34
  }
23
35
  export default Prerender;
@@ -10,6 +10,7 @@ var ConditionsRunner_1 = __importDefault(require("./ConditionsRunner"));
10
10
  var OvcConverter_1 = __importDefault(require("./OvcConverter"));
11
11
  var NumAuto_1 = __importDefault(require("./NumAuto"));
12
12
  var InputsInitiator_1 = __importDefault(require("./InputsInitiator"));
13
+ var FillPdfForm_1 = __importDefault(require("./FillPdfForm"));
13
14
  var Prerender = (function () {
14
15
  function Prerender(model, ovc) {
15
16
  this.documentsIndex = {};
@@ -21,15 +22,22 @@ var Prerender = (function () {
21
22
  var inputs = new InputsInitiator_1.default(this.references, this.ovc).getInputs();
22
23
  this.conditions = new ConditionsRunner_1.default(this.references, inputs);
23
24
  this.documentRender = new DocumentRender_1.default(this.references, this.conditions, inputs, this.NumAuto);
25
+ this.pdfFiller = new FillPdfForm_1.default(this.references, this.conditions, inputs);
24
26
  this.generateDocumentNamesHash();
25
27
  this.generateAllDocuments();
26
28
  }
27
29
  Prerender.prototype.getDocumentsIndex = function () {
28
30
  return this.documentsIndex;
29
31
  };
32
+ Prerender.prototype.getDocument = function (documentNameHash) {
33
+ return this.renderedDocuments[documentNameHash];
34
+ };
30
35
  Prerender.prototype.generateDocumentNamesHash = function () {
31
36
  var _this = this;
32
37
  Object.keys(this.model.documents).forEach(function (documentName) {
38
+ var _a, _b;
39
+ var document = _this.model.documents[documentName];
40
+ var documentParams = _this.model.documents[documentName].params;
33
41
  var conditionObject = _this.references.conditions.documents[documentName];
34
42
  var condition = conditionObject === undefined ? true : _this.conditions.executeCondition(conditionObject, 0, 0, 'documents');
35
43
  if (condition === true) {
@@ -37,22 +45,39 @@ var Prerender = (function () {
37
45
  .createHash('sha1')
38
46
  .update(documentName)
39
47
  .digest('hex');
40
- _this.documentsIndex[hash] = {
41
- name: documentName,
42
- pdf: true,
43
- docx: true
44
- };
48
+ if (typeof document.pdf === 'object' && typeof document.pdf.id === 'string' && typeof document.pdf.form === 'object') {
49
+ _this.documentsIndex[hash] = {
50
+ name: documentName,
51
+ form: true
52
+ };
53
+ }
54
+ else {
55
+ _this.documentsIndex[hash] = {
56
+ name: documentName,
57
+ pdf: ((_a = documentParams === null || documentParams === void 0 ? void 0 : documentParams.formats) === null || _a === void 0 ? void 0 : _a.pdf) === undefined ? true : documentParams.formats.pdf,
58
+ docx: ((_b = documentParams === null || documentParams === void 0 ? void 0 : documentParams.formats) === null || _b === void 0 ? void 0 : _b.docx) === undefined ? true : documentParams.formats.docx
59
+ };
60
+ }
45
61
  }
46
62
  });
47
63
  };
48
64
  Prerender.prototype.generateAllDocuments = function () {
49
65
  var _this = this;
50
66
  Object.keys(this.documentsIndex).forEach(function (documentNameHash) {
51
- _this.renderedDocuments[documentNameHash] = _this.documentRender.renderDocument(_this.documentsIndex[documentNameHash].name);
67
+ var index = _this.documentsIndex[documentNameHash];
68
+ if (Prerender.isPdfForm(index)) {
69
+ _this.renderedDocuments[documentNameHash] = _this.pdfFiller.fillDocument(_this.documentsIndex[documentNameHash].name);
70
+ }
71
+ else {
72
+ _this.renderedDocuments[documentNameHash] = _this.documentRender.renderDocument(_this.documentsIndex[documentNameHash].name);
73
+ }
52
74
  });
53
75
  };
54
- Prerender.prototype.getDocument = function (documentNameHash) {
55
- return this.renderedDocuments[documentNameHash];
76
+ Prerender.isPdfForm = function (index) {
77
+ return Object.prototype.hasOwnProperty.call(index, 'form');
78
+ };
79
+ Prerender.isOutput = function (index) {
80
+ return !Object.prototype.hasOwnProperty.call(index, 'form');
56
81
  };
57
82
  return Prerender;
58
83
  }());
@@ -0,0 +1,16 @@
1
+ import { Types } from '@legalplace/referencesparser';
2
+ import ConditionsRunner from '../ConditionsRunner';
3
+ import OVC_TYPE from '../ovc.type';
4
+ declare class FillPdfFormBase {
5
+ references: Types.ReferencesType;
6
+ conditions: ConditionsRunner;
7
+ ovc: OVC_TYPE;
8
+ constructor(references: Types.ReferencesType, conditions: ConditionsRunner, ovc: OVC_TYPE);
9
+ isVariableDisplayed(id: number, index: number): boolean;
10
+ isOptionDisplayed(id: number, index: number): boolean;
11
+ private getVariableCondition;
12
+ private getOptionCondition;
13
+ private getSectionCondition;
14
+ private getOptionParentSection;
15
+ }
16
+ export default FillPdfFormBase;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
3
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
5
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6
+ r[k] = a[j];
7
+ return r;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ var FillPdfFormBase = (function () {
11
+ function FillPdfFormBase(references, conditions, ovc) {
12
+ this.references = references;
13
+ this.conditions = conditions;
14
+ this.ovc = ovc;
15
+ }
16
+ FillPdfFormBase.prototype.isVariableDisplayed = function (id, index) {
17
+ var _a;
18
+ var variableCondition = this.getVariableCondition(id, index);
19
+ var variableParents = ((_a = this.references.relations.variables[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
20
+ var parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index);
21
+ return [variableCondition, parentOptionIsDisplayed].filter(function (c) { return c !== true; }).length === 0;
22
+ };
23
+ FillPdfFormBase.prototype.isOptionDisplayed = function (id, index) {
24
+ var _this = this;
25
+ var _a;
26
+ var optionCondition = this.getOptionCondition(id, index);
27
+ var optionParents = ((_a = this.references.relations.options[id]) === null || _a === void 0 ? void 0 : _a.parents) || [];
28
+ var parentsConditions = optionParents.map(function (optionId) {
29
+ return _this.getOptionCondition(optionId, index) !== false;
30
+ });
31
+ var parentsInputs = optionParents.map(function (optionId) {
32
+ return _this.ovc.options[optionId][index];
33
+ });
34
+ var parentSectionId = this.getOptionParentSection(id);
35
+ var parentSectionCondition = this.getSectionCondition(parentSectionId) !== false;
36
+ return __spreadArrays([optionCondition, parentSectionCondition], parentsConditions, parentsInputs).filter(function (c) { return c !== true; }).length === 0;
37
+ };
38
+ FillPdfFormBase.prototype.getVariableCondition = function (id, index) {
39
+ var conditionObject = this.references.conditions.variables[id];
40
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, index, 'variables');
41
+ };
42
+ FillPdfFormBase.prototype.getOptionCondition = function (id, index) {
43
+ var conditionObject = this.references.conditions.options[id];
44
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, index, 'options');
45
+ };
46
+ FillPdfFormBase.prototype.getSectionCondition = function (id) {
47
+ var conditionObject = this.references.conditions.sections.main[id];
48
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, 0, 'sections');
49
+ };
50
+ FillPdfFormBase.prototype.getOptionParentSection = function (id) {
51
+ var parents = this.references.relations.options[id].parents;
52
+ var rootId = parents.length > 0 ? parents[parents.length - 1] : id;
53
+ var sections = Object.values(this.references.sections.main);
54
+ for (var i = 0; i < sections.length; i += 1) {
55
+ if (sections[i].options.includes(rootId))
56
+ return sections[i].id;
57
+ }
58
+ throw new Error("Cannot find parent section for option " + id + " (Root option id: " + rootId + ")");
59
+ };
60
+ return FillPdfFormBase;
61
+ }());
62
+ exports.default = FillPdfFormBase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/prerenderx",
3
- "version": "2.2.5",
3
+ "version": "2.3.0",
4
4
  "description": "PrerenderX",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://git.legalplace.eu/legalplace/prerenderx",
@@ -15,8 +15,8 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@legalplace/lplogic": "^2.0.0",
18
- "@legalplace/models-v3-types": "^3.1.1",
19
- "@legalplace/referencesparser": "^1.2.0",
18
+ "@legalplace/models-v3-types": "^3.4.47",
19
+ "@legalplace/referencesparser": "^1.4.1",
20
20
  "crypto": "^1.0.1",
21
21
  "typescript": "3.9.3"
22
22
  },
@@ -11,10 +11,6 @@ class DocumentRender {
11
11
 
12
12
  ovc: OVC_TYPE
13
13
 
14
- documents: Record<string, string[]> = {}
15
-
16
- documentsNamesHash: Record<string, string> = {}
17
-
18
14
  private NumAuto: NumAuto
19
15
 
20
16
  constructor(references: Types.ReferencesType, conditions: ConditionsRunner, ovc: OVC_TYPE, numauto: NumAuto) {
@@ -0,0 +1,144 @@
1
+ import { FDFBoxesV3, FDFInputsV3 } from '@legalplace/models-v3-types'
2
+ import FillPdfFormBase from './misc/FillPdfFormBase'
3
+
4
+ class FillPdfForm extends FillPdfFormBase {
5
+ public fillDocument(documentName: string) {
6
+ const result: {
7
+ id: string
8
+ inputs: Record<string, string>
9
+ boxes: Record<string, string>
10
+ } = {
11
+ id: '',
12
+ inputs: {},
13
+ boxes: {}
14
+ }
15
+
16
+ // Getting document
17
+ const document = this.references.documents[documentName]
18
+ const { pdf } = document
19
+ if (typeof pdf === 'undefined') return result
20
+ const { form } = pdf
21
+
22
+ // Setting pdf ID
23
+ result.id = pdf.id
24
+
25
+ // Filling inputs
26
+ form.inputs.forEach(input => {
27
+ const id = `Page${input.page}[0].${input.name}`
28
+ result.inputs[id] = this.getInputValue(input)
29
+ })
30
+
31
+ // Filling boxes
32
+ form.boxes.forEach(box => {
33
+ const id = `Page${box.page}[0].${box.name}`
34
+ result.boxes[id] = this.getBoxValue(box)
35
+ })
36
+
37
+ return result
38
+ }
39
+
40
+ /**
41
+ * Returns an input value
42
+ * @param input Input
43
+ */
44
+ private getInputValue(input: FDFInputsV3): string {
45
+ for (let i = 0; i < input.variables.length; i += 1) {
46
+ const variable = input.variables[i]
47
+ const currentValue = this.getVariableValue(variable.id)
48
+
49
+ if (currentValue !== false) {
50
+ // Date values
51
+ const dateRegex = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/
52
+ console.log('Value for', variable.id, currentValue, dateRegex.test(currentValue))
53
+ if (variable.date && dateRegex.test(currentValue)) {
54
+ switch (variable.date) {
55
+ case 'DD':
56
+ console.log('Value s', variable.id, input.name, currentValue.replace(dateRegex, '$1'), dateRegex.test(currentValue))
57
+ return currentValue.replace(dateRegex, '$1')
58
+ case 'MM':
59
+ return currentValue.replace(dateRegex, '$2')
60
+ case 'YYYY':
61
+ return currentValue.replace(dateRegex, '$3')
62
+ default:
63
+ }
64
+ }
65
+
66
+ // Hour values
67
+ const hourRegex = /^([0-9]{2}:[0-9]{2})$/
68
+ if (variable.hour && hourRegex.test(currentValue)) {
69
+ switch (variable.hour) {
70
+ case 'HH':
71
+ return currentValue.replace(hourRegex, '$1')
72
+ case 'MM':
73
+ return currentValue.replace(hourRegex, '$2')
74
+ default:
75
+ }
76
+ }
77
+
78
+ // Email values
79
+ const emailRegex = /^(.*)@(.*)$/
80
+ if (variable.email && emailRegex.test(currentValue)) {
81
+ switch (variable.email) {
82
+ case 'ADDRESS':
83
+ return currentValue.split('@')[0]
84
+ case 'PROVIDER':
85
+ return currentValue.split('@')[1]
86
+ default:
87
+ }
88
+ }
89
+
90
+ // Fragment values
91
+ if (variable.fragment && typeof variable.fragment.from === 'number' && typeof variable.fragment.to === 'number') {
92
+ const { from, to } = variable.fragment
93
+ return currentValue.slice(from, to)
94
+ }
95
+
96
+ // Return value
97
+ return currentValue
98
+ }
99
+ }
100
+
101
+ return ''
102
+ }
103
+
104
+ /**
105
+ * Return a box value
106
+ * @param box Box
107
+ */
108
+ private getBoxValue(box: FDFBoxesV3): string {
109
+ for (let i = 0; i < box.options.length; i += 1) {
110
+ const option = box.options[i]
111
+ const currentValue = this.getOptionValue(option.id)
112
+
113
+ if (currentValue === true) {
114
+ return box.value
115
+ }
116
+ }
117
+
118
+ return ''
119
+ }
120
+
121
+ /**
122
+ * Returns a variable's value
123
+ */
124
+ private getVariableValue(variableId: number) {
125
+ // Checking if variable is displayed
126
+ if (this.isVariableDisplayed(variableId, 0) === false) return false
127
+
128
+ // Returning variable's value
129
+ return (this.ovc.variables[variableId]?.[0] || '').toString()
130
+ }
131
+
132
+ /**
133
+ * Returns a option's value
134
+ */
135
+ private getOptionValue(optionId: number) {
136
+ // Checking if option is displayed
137
+ if (this.isOptionDisplayed(optionId, 0) === false) return false
138
+
139
+ // Returning option's value
140
+ return this.ovc.options[optionId]?.[0] === true
141
+ }
142
+ }
143
+
144
+ export default FillPdfForm
@@ -7,6 +7,24 @@ import OVC_TYPE from './ovc.type'
7
7
  import OvcConverter, { OldOVC } from './OvcConverter'
8
8
  import NumAuto from './NumAuto'
9
9
  import IntputsInitiator from './InputsInitiator'
10
+ import FillPdfForm from './FillPdfForm'
11
+
12
+ type DocumentOutputIndex = {
13
+ name: string
14
+ pdf: boolean
15
+ docx: boolean
16
+ }
17
+ type DocumentPdfFormIndex = {
18
+ name: string
19
+ form: boolean
20
+ }
21
+
22
+ type DocumentIndex = Record<string, DocumentOutputIndex | DocumentPdfFormIndex>
23
+
24
+ type FDFType = {
25
+ inputs: Record<string, string>
26
+ boxes: Record<string, string>
27
+ }
10
28
 
11
29
  class Prerender {
12
30
  private model: ModelV3
@@ -19,18 +37,13 @@ class Prerender {
19
37
 
20
38
  private documentRender: DocumentRender
21
39
 
40
+ private pdfFiller: FillPdfForm
41
+
22
42
  private NumAuto: NumAuto
23
43
 
24
- private documentsIndex: Record<
25
- string,
26
- {
27
- name: string
28
- pdf: boolean
29
- docx: boolean
30
- }
31
- > = {}
44
+ private documentsIndex: DocumentIndex = {}
32
45
 
33
- private renderedDocuments: Record<string, string> = {}
46
+ private renderedDocuments: Record<string, string | FDFType> = {}
34
47
 
35
48
  constructor(model: ModelV3, ovc: OVC_TYPE | OldOVC) {
36
49
  this.model = model
@@ -53,6 +66,9 @@ class Prerender {
53
66
  // Rendering documents
54
67
  this.documentRender = new DocumentRender(this.references, this.conditions, inputs, this.NumAuto)
55
68
 
69
+ // Filling pdf documents
70
+ this.pdfFiller = new FillPdfForm(this.references, this.conditions, inputs)
71
+
56
72
  this.generateDocumentNamesHash()
57
73
  this.generateAllDocuments()
58
74
  }
@@ -61,8 +77,14 @@ class Prerender {
61
77
  return this.documentsIndex
62
78
  }
63
79
 
80
+ public getDocument(documentNameHash: string) {
81
+ return this.renderedDocuments[documentNameHash]
82
+ }
83
+
64
84
  private generateDocumentNamesHash() {
65
85
  Object.keys(this.model.documents).forEach(documentName => {
86
+ const document = this.model.documents[documentName]
87
+ const documentParams = this.model.documents[documentName].params
66
88
  const conditionObject = this.references.conditions.documents[documentName]
67
89
  const condition = conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, 0, 0, 'documents')
68
90
 
@@ -74,10 +96,17 @@ class Prerender {
74
96
  .digest('hex')
75
97
 
76
98
  // Referencing in index
77
- this.documentsIndex[hash] = {
78
- name: documentName,
79
- pdf: true,
80
- docx: true
99
+ if (typeof document.pdf === 'object' && typeof document.pdf.id === 'string' && typeof document.pdf.form === 'object') {
100
+ this.documentsIndex[hash] = {
101
+ name: documentName,
102
+ form: true
103
+ }
104
+ } else {
105
+ this.documentsIndex[hash] = {
106
+ name: documentName,
107
+ pdf: documentParams?.formats?.pdf === undefined ? true : documentParams.formats.pdf,
108
+ docx: documentParams?.formats?.docx === undefined ? true : documentParams.formats.docx
109
+ }
81
110
  }
82
111
  }
83
112
  })
@@ -85,12 +114,21 @@ class Prerender {
85
114
 
86
115
  private generateAllDocuments() {
87
116
  Object.keys(this.documentsIndex).forEach(documentNameHash => {
88
- this.renderedDocuments[documentNameHash] = this.documentRender.renderDocument(this.documentsIndex[documentNameHash].name)
117
+ const index = this.documentsIndex[documentNameHash]
118
+ if (Prerender.isPdfForm(index)) {
119
+ this.renderedDocuments[documentNameHash] = this.pdfFiller.fillDocument(this.documentsIndex[documentNameHash].name)
120
+ } else {
121
+ this.renderedDocuments[documentNameHash] = this.documentRender.renderDocument(this.documentsIndex[documentNameHash].name)
122
+ }
89
123
  })
90
124
  }
91
125
 
92
- public getDocument(documentNameHash: string) {
93
- return this.renderedDocuments[documentNameHash]
126
+ static isPdfForm(index: DocumentOutputIndex | DocumentPdfFormIndex): index is DocumentPdfFormIndex {
127
+ return Object.prototype.hasOwnProperty.call(index, 'form')
128
+ }
129
+
130
+ static isOutput(index: DocumentOutputIndex | DocumentPdfFormIndex): index is DocumentOutputIndex {
131
+ return !Object.prototype.hasOwnProperty.call(index, 'form')
94
132
  }
95
133
  }
96
134
 
@@ -0,0 +1,103 @@
1
+ import { Types } from '@legalplace/referencesparser'
2
+ import ConditionsRunner from '../ConditionsRunner'
3
+ import OVC_TYPE from '../ovc.type'
4
+
5
+ class FillPdfFormBase {
6
+ references: Types.ReferencesType
7
+
8
+ conditions: ConditionsRunner
9
+
10
+ ovc: OVC_TYPE
11
+
12
+ constructor(references: Types.ReferencesType, conditions: ConditionsRunner, ovc: OVC_TYPE) {
13
+ this.references = references
14
+ this.conditions = conditions
15
+ this.ovc = ovc
16
+ }
17
+
18
+ /**
19
+ * Checks whether a variable is displayed at a given index
20
+ * @param id Variable's ID
21
+ * @param index Index
22
+ */
23
+ isVariableDisplayed(id: number, index: number) {
24
+ // Getting variable's conditions & executing it if any
25
+ const variableCondition = this.getVariableCondition(id, index)
26
+ const variableParents = this.references.relations.variables[id]?.parents || []
27
+ const parentOptionIsDisplayed = this.isOptionDisplayed(variableParents[0], index)
28
+
29
+ return [variableCondition, parentOptionIsDisplayed].filter(c => c !== true).length === 0
30
+ }
31
+
32
+ /**
33
+ * Checks whether an option is displayed at a given index
34
+ * @param id Option's ID
35
+ * @param index Index
36
+ */
37
+ isOptionDisplayed(id: number, index: number) {
38
+ // Getting variable's conditions & executing it if any
39
+ const optionCondition = this.getOptionCondition(id, index)
40
+ const optionParents = this.references.relations.options[id]?.parents || []
41
+ const parentsConditions = optionParents.map(optionId => {
42
+ return this.getOptionCondition(optionId, index) !== false
43
+ })
44
+ const parentsInputs = optionParents.map(optionId => {
45
+ return this.ovc.options[optionId][index]
46
+ })
47
+ const parentSectionId = this.getOptionParentSection(id)
48
+ const parentSectionCondition = this.getSectionCondition(parentSectionId) !== false
49
+
50
+ return [optionCondition, parentSectionCondition, ...parentsConditions, ...parentsInputs].filter(c => c !== true).length === 0
51
+ }
52
+
53
+ /**
54
+ * Returns a variable's conditions
55
+ * @param id Variable's id
56
+ * @param index Variable's index
57
+ */
58
+ private getVariableCondition(id: number, index: number) {
59
+ const conditionObject = this.references.conditions.variables[id]
60
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, index, 'variables')
61
+ }
62
+
63
+ /**
64
+ * Returns an option's conditions
65
+ * @param id Option's id
66
+ * @param index Option's index
67
+ */
68
+ private getOptionCondition(id: number, index: number) {
69
+ const conditionObject = this.references.conditions.options[id]
70
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, index, 'options')
71
+ }
72
+
73
+ /**
74
+ * Returns a section's conditions
75
+ * @param id Section's id
76
+ */
77
+ private getSectionCondition(id: number) {
78
+ const conditionObject = this.references.conditions.sections.main[id]
79
+ return conditionObject === undefined ? true : this.conditions.executeCondition(conditionObject, id, 0, 'sections')
80
+ }
81
+
82
+ /**
83
+ * Returns option's parent section
84
+ * @param id Option's id
85
+ */
86
+ private getOptionParentSection(id: number) {
87
+ const { parents } = this.references.relations.options[id]
88
+
89
+ // Getting root option id
90
+ const rootId = parents.length > 0 ? parents[parents.length - 1] : id
91
+
92
+ // Looking for section
93
+ const sections = Object.values(this.references.sections.main)
94
+
95
+ for (let i = 0; i < sections.length; i += 1) {
96
+ if (sections[i].options.includes(rootId)) return sections[i].id
97
+ }
98
+
99
+ throw new Error(`Cannot find parent section for option ${id} (Root option id: ${rootId})`)
100
+ }
101
+ }
102
+
103
+ export default FillPdfFormBase