forked from adobe/dynamic-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
280 lines (223 loc) · 7.66 KB
/
server.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
// Express, Async, Fetch, & Body Parser
const express = require('express');
const async = require('express-async-await');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
// Form Data, Multer, & Uploads
const FormData = require('form-data');
const fs = require('fs');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
// HTTPS & Path
const path = require('path');
// js-yaml
const yaml = require('js-yaml');
const config = yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'config', 'config.yaml'), 'utf-8'));
// Main App
const app = express();
// Configuration
var host = config.server.host.replace(/\/$/, '');
var endpoint = config.server.endpoint;
var url = host + endpoint;
var port = process.env.PORT || config.port || 80;
var integration_key = null;
// Configuration
if ('enterprise' in config && config.enterprise && 'integration' in config.enterprise && config.enterprise.integration) {
integration_key = config.enterprise.integration;
} else {
integration_key = process.env.INTEGRATION_KEY;
}
function checkMultiKeys(obj) {
return Object.keys(obj).includes("esignadmin");
}
function getIntegrationKey(req) {
var integrationKey = null;
if(checkMultiKeys(integration_key)){
integrationKey = integration_key[req.header("workflow-service-user")];
} else{
integrationKey = integration_key;
}
return integrationKey;
}
function getHeader(req){
//'Authorization': 'Bearer '+ integration_key,
var headers = {
'Authorization': 'Bearer '+ getIntegrationKey(req),
'Accept': 'application/json',
'Content-Type': 'application/json'
}
if (config['features']['x-api-user']) {
headers['x-api-user'] = config['features']['x-api-user'];
}
return headers;
}
let emailRegex = config['features']['email_regex'];
let emailErrorMessage = config['features']['email_error_message'];
app.use(express.static(__dirname + '/static'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Get features from config files
app.get('/features', function (req, res) {
res.json(config['features']);
});
// Get index.html page from server
app.get('/', function (req, res) {
if(checkMultiKeys(integration_key)){
res.sendFile(__dirname + '/static/router.html');
} else {
res.sendFile(__dirname + '/static/views/index.html');
}
});
// GET /workflows
app.get('/api/getWorkflows', async function (req, res, next) {
console.log("Route matches '/api/getWorkflows'");
//console.log(req);
//console.log(req.header);
//console.log(JSON.stringify(req.headers));
console.log(req.header("workflow-service-user"));
var workflow_service_user = req.header("workflow-service-user");
function getWorkflows() {
/***
* This function makes a request to get workflows
*/
const endpoint = '/workflows';
return fetch(url + endpoint, {
method: 'GET',
headers: getHeader(req)
});
}
const workflow_list = await getWorkflows();
const data = await workflow_list.json();
res.json(data['userWorkflowList']);
});
// GET /workflows/{workflowId}
app.get('/api/getWorkflowById/:id', async function (req, res,next) {
function getWorkflowById() {
/***
* This function makes a request to get workflow by ID
*/
const endpoint = '/workflows/' + req.params.id;
return fetch(url + endpoint, {
method: 'GET',
headers: getHeader(req)
});
}
const workflow_by_id = await getWorkflowById();
const data = await workflow_by_id.json();
res.json(data);
});
// POST /workflows/{workflowId}/agreements
app.post('/api/postAgreement/:id', async function (req, res, next) {
function postAgreement() {
/***
* This function post agreements
*/
const endpoint = '/agreements/';
return fetch(url + endpoint, {
method: 'POST',
headers: getHeader(req),
body: JSON.stringify(req.body)
});
}
let okToSubmit = true;
for (let o = 0; req.body.participantSetsInfo.length > o; o++) {
console.log(req.body.participantSetsInfo[o].memberInfos);
for (let i = 0; req.body.participantSetsInfo[o].memberInfos.length > i; i++) {
if (!req.body.participantSetsInfo[o].memberInfos[i].email.match(emailRegex)) {
okToSubmit = false;
}
}
}
let data;
if (okToSubmit) {
const api_response = await postAgreement();
data = await api_response.json();
} else {
data = { code: 'MISC_ERROR', message: emailErrorMessage };
}
res.json(data);
});
// GET /agreements/{agreementId}/signingUrls
app.get('/api/getSigningUrls/:id', async function (req, res, next) {
async function getSigningUrls(count = 0) {
/***
* This function gets URL for the e-sign page for the current signer(s) of an agreement.
*/
const endpoint = '/agreements/' + req.params.id + '/signingUrls';
const sign_in_response = await fetch(url + endpoint, {
method: 'GET',
headers: getHeader(req)
});
const sign_in_data = await sign_in_response.json();
// Look for times to retry and default to 15, if not found
const retries = 'sign_now_retries' in config['features'] ? config['features']['sign_now_retries'] : 600;
if (sign_in_data.code === 'AGREEMENT_NOT_EXPOSED' || sign_in_data.code === 'BAD_REQUEST') {
// retry for n times with 1s delay
if (count >= retries) {
return sign_in_data;
} else {
await new Promise(done => setTimeout(() => done(), 1000));
count++;
return await getSigningUrls(count);
}
}
return sign_in_data;
}
const data = await getSigningUrls();
res.json(data);
});
// POST /transientDocuments
app.post('/api/postTransient', upload.single('myfile'), async function (req, res, next) {
function postTransient() {
/***
* This functions post transient
*/
const endpoint = '/transientDocuments';
let newHeader = { ...getHeader(req) };
delete newHeader['Accept'];
delete newHeader['Content-Type'];
return fetch(url + endpoint, {
method: 'POST',
headers: newHeader,
body: form
});
}
// Create FormData
var form = new FormData();
form.append('File-Name', req.file.originalname);
form.append('Mime-Type', req.file.mimetype);
form.append('File', fs.createReadStream(req.file.path));
const api_response = await postTransient();
const data = await api_response.json();
// Delete uploaded doc after transient call
fs.unlink(req.file.path, function (err) {
if (err) return console.log(err);
});
res.json(data);
});
// Get index.html page from server
app.get('/:area', function (req, res) {
console.log("Route matches '/:area' (" + req.params.area + ")");
res.render('index', {area: req.params.area});
//res.sendFile(__dirname + '/static/' + req.params.area + '.html');
});
app.get('/testing/:area', function (req, res) {
console.log("Route matches '/testing/:area' (" + req.params.area + ")");
res.render('index', {area: req.params.area});
// res.sendFile(__dirname + '/static/' + req.params.area + '.html');
});
app.disable('etag');
app.set('view engine', 'ejs');
app.listen(port, () => console.log(`Server started on port ${port}`));