-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (147 loc) · 5.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const fs = require('fs'),
path = require('path');
const { client, log, lib, gray } = require('@kai-raschke/prodig-external-deps')();
const { Variables } = require('camunda-external-task-client-js');
const pdfFiller = require('pdffiller');
const formPath = "forms",
outputPath = "tmp";
//existsOrCreate output path
if (!fs.existsSync(outputPath)){
fs.mkdirSync(outputPath);
}
let main = async function({ task, taskService }) {
await taskService.extendLock(task, process.env.LOCK_DURATION); //in case PDF creation takes longer
let variablesIn = task.variables.getAll();
variablesIn.processInstanceId = task.processInstanceId;
variablesIn.transform = variablesIn.transform || '';
variablesIn.output = variablesIn.output || '';
const processVariables = new Variables();
const processDefinitionKey = task.processDefinitionKey;
//Delete 'null' values in process variables; pdffiller cannot work when leaving them in the object
Object.entries(variablesIn).forEach(([key, val]) => {
if (val == null) delete variablesIn[key];
});
if(!variablesIn.formName){
await taskService.handleFailure(task, "An error occured - strange ...");
}
if(!variablesIn.identifier){
await taskService.handleFailure(task, "An error occured - strange ...");
}
variablesIn.inboxTask = variablesIn.inboxTask || 'Task_DocInbox'; //Set default task for barcode scan inbox
let tempOutputFile = path.join(__dirname, outputPath, `${variablesIn.outputName}-${variablesIn.bKey}-temp.pdf`);
let outputFile = path.join(__dirname, outputPath, `${variablesIn.outputName}-${variablesIn.bKey}.pdf`);
let tempFiles = [];
try{
// file goes to (outputPath, `${outputName}-${bKey}-temp.pdf`)
await createPdf(variablesIn.formName, tempOutputFile, variablesIn);
if(!fs.existsSync(tempOutputFile)){
throw "File was not created.";
}
tempFiles.push(tempOutputFile);
} catch(ex){
console.error(ex);
await taskService.handleFailure(task, {
errorMessage: "An error occurred while creating the document.",
errorDetails: `${ex.errno}; ${ex.code}; ${ex.syscall}`,
retries: 0,
retryTimeout: 1000
});
}
//Do all transformations on newly created pdf with given transform variables from bpmn
let transforms = variablesIn.transform.split(',');
for(let i = -1; ++i < transforms.length;){
let transform = transforms[i];
transform = transform.trim();
try{
let module = require(`./transform/${transform}`);
try{
tempOutputFile = await module(tempOutputFile, variablesIn);
} catch(ex){
await taskService.handleFailure(task, "An error occurred while transforming the document.");
}
tempFiles.push(tempOutputFile);
}
catch(ex){
console.error(ex);
}
}
fs.copyFileSync(tempOutputFile, outputFile);
tempFiles.push(outputFile);
//Execute all outputs with transformed (or not) pdf given by input parameters from bpmn
let outputs = variablesIn.output.split(',');
for(let i = -1; ++i < outputs.length;){
let output = outputs[i];
output = output.trim();
try{
let module = require(`./output/${output}`);
try{
await module(outputFile, variablesIn, processVariables);
} catch(ex){
await taskService.handleFailure(task, "An error occurred while executing the output of the document.");
}
}
catch(ex){
console.error(ex);
}
}
if(process.env.NODE_ENV !== 'development')
removeTempFiles(tempFiles);
await taskService.complete(task, processVariables);
};
let createPdf = async function(template, output, data){
return new Promise(async (res,rej) => {
data = data || {};
try{
//Erstellt die vorausgefüllte Form
await fillForm(path.join(__dirname, formPath, `${template}.pdf`), output, data);
res();
}
catch(ex){
rej(ex);
}
});
};
let removeTempFiles = function(tempFiles){
try{
for(let i = -1; ++i < tempFiles.length;){
let tempFile = tempFiles[i];
fs.unlinkSync(tempFile);
}
} catch(ex){ console.log(ex); }
};
let fillForm = async function(source, destination, data, flatten){
return new Promise((res, rej) => {
try{
//flatten = true;
if(!flatten){
if(process.env.NODE_ENV === "development"){
console.log(`Creating ${source} in ${destination} with data ${data}`);
}
pdfFiller.fillFormWithFlatten( source, destination, data, false, function(err) {
if (err) rej(err);
res();
});
}
else{
pdfFiller.fillForm( source, destination, data, function(err) {
if (err) rej(err);
res();
});
}
}
catch(ex){ console.error("FEHLER 2", ex); }
});
};
(function start(){
try {
client.subscribe(process.env.TOPIC, main);
client.start();
if(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'){
//allow testing Camunda external task through http interface if in dev mode
require('./mock').start(main);
}
} catch (e) {
console.error(e);//log.error(e);
}
})();