-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (59 loc) · 2.89 KB
/
Copy pathapp.js
File metadata and controls
70 lines (59 loc) · 2.89 KB
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
const moment = require('moment');
const logger = require('../helpers/logger');
const { RunInstanceLogger } = require('./tools/RunInstanceLogger');
const { sendEmailNotification, zipErrors } = require('./tools/emailNotifications');
const { extractDataForPatients } = require('./tools/mcodeExtraction');
const { parsePatientIds } = require('../helpers/appUtils');
const { validateConfig } = require('../helpers/configUtils');
function checkInputAndConfig(config, fromDate, toDate) {
// Check input args and needed config variables based on client being used
validateConfig(config);
// Check if `fromDate` is a valid date
if (fromDate && !moment(fromDate).isValid()) {
throw new Error('-f/--from-date is not a valid date.');
}
// Check if `toDate` is a valid date
if (toDate && !moment(toDate).isValid()) {
throw new Error('-t/--to-date is not a valid date.');
}
}
async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug, allEntries) {
logger.level = debug ? 'debug' : 'info';
checkInputAndConfig(config, fromDate, toDate);
// Create and initialize client
const mcodeClient = new Client(config);
await mcodeClient.init();
// Parse CSV for list of patient mrns
const dataDirectory = config.commonExtractorArgs && config.commonExtractorArgs.dataDirectory;
const parserOptions = config.commonExtractorArgs && config.commonExtractorArgs.csvParse && config.commonExtractorArgs.csvParse.options ? config.commonExtractorArgs.csvParse.options : {};
const patientIds = parsePatientIds(config.patientIdCsvPath, dataDirectory, parserOptions);
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);
const effectiveFromDate = allEntries ? null : runLogger.getEffectiveFromDate(fromDate);
const effectiveToDate = allEntries ? null : toDate;
// Extract the data
logger.info(`Extracting data for ${patientIds.length} patients`);
const { extractedData, successfulExtraction, totalExtractionErrors } = await extractDataForPatients(patientIds, mcodeClient, effectiveFromDate, effectiveToDate);
// If we have notification information, send an emailNotification
const { notificationInfo } = config;
if (notificationInfo && Object.keys(notificationInfo).length > 0) {
const notificationErrors = zipErrors(totalExtractionErrors);
try {
await sendEmailNotification(notificationInfo, notificationErrors, debug);
} catch (e) {
logger.error(e.message);
}
}
// A run is successful and should be logged when both extraction finishes without fatal errors
// and messages are posted without fatal errors
if (!allEntries && effectiveFromDate) {
const successCondition = successfulExtraction;
if (successCondition) {
runLogger.addRun(effectiveFromDate, effectiveToDate);
}
}
return extractedData;
}
module.exports = {
mcodeApp,
};