-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.js
291 lines (241 loc) · 8.09 KB
/
handler.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
281
282
283
284
285
286
287
288
289
290
291
/* eslint-disable dot-notation */
const uniqid = require('uniqid');
const ApiError = require('./lib/ApiError');
const authorize = require('./lib/authorizer');
const { upload, store } = require('./lib/uploader');
const { callTextractAsync, fetchAndStoreOutput, getStored } = require('./lib/textractCallerAsync');
const callTextractSync = require('./lib/textractCallerSync');
const mapTextractOutput = require('./lib/textractMapper');
const processDocument = require('./lib/documentProcessor');
const validateProcessed = require('./lib/processedValidator');
const normalizeValidated = require('./lib/validatedNormalizator');
const { saveResult, retrieveResult } = require('./lib/resultHandler');
const respond = require('./lib/responder');
const postExtraction = async (dataExtracted, requestId, debug) => {
// map raw extracted output
const dataMapped = mapTextractOutput(dataExtracted);
if (debug) {
console.log('dataMapped', dataMapped);
await store.set(`${requestId}/_1_mapped.json`, JSON.stringify(dataMapped));
}
// augment and select data for known documentTypes
const dataProcessed = processDocument(dataMapped.keyValues, dataMapped.rawText);
if (debug) {
await store.set(`${requestId}/_2_processed.json`, JSON.stringify(dataProcessed));
}
// validate processed data
const dataValidated = validateProcessed(dataProcessed.extracted);
if (debug) {
await store.set(`${requestId}/_3_validated.json`, JSON.stringify(dataValidated));
}
// normalize values formatting
const dataNormalized = normalizeValidated(dataValidated, dataProcessed.normalizer);
if (debug) {
await store.set(`${requestId}/_4_normalized.json`, JSON.stringify(dataNormalized));
// output mapped data on debug
dataNormalized.Mapped = dataMapped;
}
// save normalized data
await saveResult(dataNormalized, requestId);
return dataNormalized;
};
module.exports.process = async (event) => {
let debug = false;
let response = [];
const metadata = {};
let resumeAsync = false;
let object = {};
let extracted = {};
let normalized = {};
const qs = event.queryStringParameters || {};
debug = Object.hasOwnProperty.call(qs, 'debug');
if (event.Records && event.Records.length && event.Records[0].Sns) {
// Resume async execution of the process (invoked from SNS notification event)
// after Textract async PDF analysis is finished
const snsData = JSON.parse(event.Records[0].Sns.Message);
resumeAsync = { req: snsData.JobTag, job: snsData.JobId };
}
const requestId = resumeAsync ? resumeAsync.req : `${new Date().getTime()}_${uniqid()}`;
metadata['RequestId'] = requestId;
try {
if (!resumeAsync) {
authorize(event);
console.log('STARTED requestId', requestId, {
BUCKET: process.env.BUCKET,
REGION: process.env.REGION,
SNS_TOPIC_ARN: process.env.SNS_TOPIC_ARN,
TEXTRACT_ROLE_ARN: process.env.TEXTRACT_ROLE_ARN,
});
if (debug) {
console.log('DEBUG mode');
// save full event for debugging
await store.set(`${requestId}/event.json`, JSON.stringify(event));
}
// validate and save the file
object = await upload(event, requestId);
if (object.type === 'application/pdf') {
// perform OCR on PDF file *async execution to be continued
await callTextractAsync(object.key, requestId);
// Textract job started, return PENDING as an ERROR
throw new ApiError('Accepted', 202);
} else {
// perform OCR on IMAGE file *sync execution
extracted = await callTextractSync(object.key);
normalized = await postExtraction(extracted, requestId, debug);
metadata['Status'] = 'SUCCEEDED';
response = [200, normalized];
}
} else {
// continued async execution
console.log('Resuming async execution', requestId);
// fetch output from Textract API
console.log('Fetching Textract output', resumeAsync.job);
extracted = await fetchAndStoreOutput(resumeAsync.job, requestId);
// save full Textract output
console.log('ENDED requestId', requestId);
return respond([200, {}]);
}
} catch (e) {
console.error(e); // log all catched errors
// respond the request with a registered ERROR
switch (e.statusCode) {
// Textract job started
case 202:
metadata['Status'] = 'PENDING';
response = [e.statusCode, {}];
break;
case 400: // Bad Request
response = [e.statusCode, {
Message: 'Bad Request',
}];
break;
case 401: // Unauthorized
case 413: // Payload Too Large
case 415: // Unsupported Media Type
case 422: // Unprocessable Entity
case 501: // Not Implemented
response = [e.statusCode, {
Message: e.message,
}];
break;
default:
metadata['Status'] = 'FAILED';
response = [500, {
Message: 'Internal Server Error',
}];
}
console.log('ERROR requestId', requestId);
}
if (debug) {
// add more data for debugging
metadata.Debug = {
Object: object,
};
if (response[1].Mapped) {
// mapped is available in normalized, move to Debug node
metadata.Debug.Mapped = response[1].Mapped;
delete response[1].Mapped;
}
if (object.type !== 'application/pdf') {
metadata.Debug.Extracted = extracted;
metadata.Debug.Normalized = normalized;
}
}
// add metadata to response
response[1] = {
...response[1],
...metadata,
};
console.log('ENDED requestId', requestId);
return respond(response);
};
module.exports.retrieve = async (event) => {
let debug = false;
let response = [];
const metadata = {};
let extracted;
let normalized;
const qs = event.queryStringParameters || {};
debug = Object.hasOwnProperty.call(qs, 'debug');
const params = event.pathParameters || {};
const { requestId } = params;
if (!requestId) {
return respond([400, {
Message: 'Bad Request',
}]);
}
metadata['RequestId'] = requestId;
try {
console.log('STARTED requestId', requestId, {
BUCKET: process.env.BUCKET,
REGION: process.env.REGION,
});
if (debug) {
console.log('DEBUG mode');
}
authorize(event);
const result = await retrieveResult(requestId);
if (!result) {
// a result still needs to be processed
extracted = await getStored(requestId);
// run process with stored extracted data
normalized = await postExtraction(extracted, requestId, debug);
metadata['Status'] = 'SUCCEEDED';
response = [200, normalized];
} else {
// already processed, return found result
metadata['Status'] = 'SUCCEEDED';
response = [200, result];
}
} catch (e) {
console.error(e);
// respond the request with a registered ERROR
switch (e.statusCode) {
// Textract job is still running
case 202:
metadata['Status'] = 'PENDING';
response = [e.statusCode, {}];
break;
// requestId does not exist
case 404: // Not Found
metadata['Status'] = 'NOT_FOUND';
response = [e.statusCode, {}];
break;
// postExtraction failed
case 401: // Unauthorized
response = [e.statusCode, {
Message: 'Missing Authentication Token',
}];
break;
case 422: // Unprocessable Entity
case 501: // Not Implemented
response = [e.statusCode, {
Message: e.message,
}];
break;
default:
metadata['Status'] = 'FAILED';
response = [500, {
Message: 'Internal Server Error',
}];
}
}
if (debug) {
// add more data for debugging
metadata.Debug = {};
if (response[1].Mapped) {
// mapped is available in normalized, move to Debug node
metadata.Debug.Mapped = response[1].Mapped;
delete response[1].Mapped;
}
metadata.Debug.Extracted = extracted;
metadata.Debug.Normalized = normalized;
}
// add metadata to response
response[1] = {
...response[1],
...metadata,
};
console.log('ENDED requestId', requestId);
return respond(response);
};