-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (93 loc) · 3.39 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
/* eslint-disable consistent-return, no-console */
'use strict';
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, json } = format;
const { Consumer } = require('sqs-consumer');
const SubmitCase = require('./models/i-casework');
const GetCase = require('./models/i-casework-getcase');
const config = require('./config');
const logger = createLogger({
format: combine(
timestamp(),
json()
),
transports: [
new transports.Console({level: 'info',
handleExceptions: true
})]
});
let db;
if (config.audit) {
db = require('./db');
}
const logError = (caseErrorMessageWithIDs, errorType, err) => {
logger.log('error', caseErrorMessageWithIDs);
logger.log('error', `${errorType} submission failed: ${err.status || '5xx'} - ${err}`);
if (err.headers) {
logger.log('error', err.headers['x-application-error-code']);
logger.log('error', err.headers['x-application-error-info']);
}
};
const submitAudit = (type, opts) => {
if (!config.audit) {
return Promise.resolve();
}
return db(type).insert(opts).catch(e => {
throw new Error(`Audit Error - ${e.message || e}`);
});
};
const handleError = async (caseID, externalID, requestType, reject, err) => {
const id = caseID || 'N/A (Failed To Send)';
if (!err.message.includes('Audit Error')) {
logError(`${requestType} request: Case externalID ${externalID} - iCasework Case ID ${id}`, 'Casework', err);
}
try {
await submitAudit('resolver', { success: false, caseID, externalID });
return reject(err);
} catch (auditErr) {
logError(`iCasework Case ID ${id} - ExternalId ${externalID}`, 'Audit', auditErr);
return reject(auditErr);
}
};
const resolver = Consumer.create({
queueUrl: config.aws.sqs,
handleMessage: async message => {
return new Promise(async (resolve, reject) => {
const getCase = new GetCase(JSON.parse(message.Body));
const submitCase = new SubmitCase(JSON.parse(message.Body));
const externalID = submitCase.get('ExternalId');
let caseID;
let requestType = 'N/A';
try {
requestType = 'GET';
const getCaseResponse = await getCase.fetch();
const isCaseFound = (getCaseResponse.exists ? 'found' : 'not found');
logger.info({ externalID, message: 'Casework GET request successful. externalId:' +
`${externalID} was ${isCaseFound}`});
caseID = getCaseResponse.caseId;
if (!getCaseResponse.exists) {
requestType = 'CREATECASE';
const data = await submitCase.save();
caseID = data.createcaseresponse.caseid;
logger.info({ caseID, externalID, message: 'Casework submission successful' });
await submitAudit('resolver', { success: true, caseID, externalID });
return resolve();
}
logger.info({ externalID, message: `Case already submitted with iCasework Case ID ${caseID}` });
await submitAudit('duplicates', { caseID, externalID });
await submitAudit('resolver', { success: true, caseID, externalID });
return resolve();
} catch (e) {
return handleError(caseID, externalID, requestType, reject, e);
}
});
}
});
resolver.on('error', err => {
console.error(err.message);
});
resolver.on('processing_error', err => {
console.error(err.message);
});
resolver.start();
logger.info(`Resolver is listening for messages from: ${config.aws.sqs}`);