Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions src/application/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
const fs = require('fs');
const path = require('path');
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/configValidator');

function getConfig(pathToConfig) {
// Checks pathToConfig points to valid JSON file
const fullPath = path.resolve(pathToConfig);
try {
return JSON.parse(fs.readFileSync(fullPath));
} catch (err) {
throw new Error(`The provided filepath to a configuration file ${pathToConfig}, full path ${fullPath} did not point to a valid JSON file.`);
}
}
const { validateConfig } = require('../helpers/configUtils');

function checkInputAndConfig(config, fromDate, toDate) {
// Check input args and needed config variables based on client being used
Expand All @@ -33,9 +21,8 @@ function checkInputAndConfig(config, fromDate, toDate) {
}
}

async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, debug, allEntries) {
async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug, allEntries) {
logger.level = debug ? 'debug' : 'info';
const config = getConfig(pathToConfig);
checkInputAndConfig(config, fromDate, toDate);

// Create and initialize client
Expand Down
4 changes: 3 additions & 1 deletion src/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const program = require('commander');
const { MCODEClient } = require('../client/MCODEClient');
const logger = require('../helpers/logger');
const { mcodeApp } = require('../application');
const { getConfig } = require('../helpers/configUtils');

const defaultPathToConfig = path.join('config', 'csv.config.json');
const defaultPathToRunLogs = path.join('logs', 'run-logs.json');
Expand All @@ -27,7 +28,8 @@ const allEntries = !entriesFilter;

async function runApp() {
try {
const extractedData = await mcodeApp(MCODEClient, fromDate, toDate, configFilepath, runLogFilepath, debug, allEntries);
const config = getConfig(configFilepath);
const extractedData = await mcodeApp(MCODEClient, fromDate, toDate, config, runLogFilepath, debug, allEntries);

// Finally, save the data to disk
const outputPath = './output';
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/configValidator.js → src/helpers/configUtils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const metaSchema = require('ajv/lib/refs/json-schema-draft-06.json');
const logger = require('./logger');
const configSchema = require('./schemas/config.schema.json');

function getConfig(pathToConfig) {
// Checks pathToConfig points to valid JSON file
const fullPath = path.resolve(pathToConfig);
try {
return JSON.parse(fs.readFileSync(fullPath));
} catch (err) {
throw new Error(`The provided filepath to a configuration file ${pathToConfig}, full path ${fullPath} did not point to a valid JSON file.`);
}
}

const ajv = new Ajv({ logger: false, allErrors: true });
ajv.addMetaSchema(metaSchema);

Expand Down Expand Up @@ -34,5 +46,6 @@ function validateConfig(config) {
}

module.exports = {
getConfig,
validateConfig,
};
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const { formatDate, formatDateTime } = require('./helpers/dateUtils');
const { lowercaseLookupQuery, createLowercaseLookup, createInvertedLookup } = require('./helpers/lookupUtils');
const { getConditionEntriesFromContext, getConditionsFromContext, getEncountersFromContext, getPatientFromContext } = require('./helpers/contextUtils');
const { parsePatientIds } = require('./helpers/appUtils');
const { getConfig, validateConfig } = require('./helpers/configUtils');

module.exports = {
// CLI Related utilities
Expand All @@ -76,6 +77,8 @@ module.exports = {
parsePatientIds,
sendEmailNotification,
zipErrors,
getConfig,
validateConfig,
// Extractors and Clients
BaseClient,
BaseFHIRExtractor,
Expand Down
15 changes: 0 additions & 15 deletions test/application/app.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
const rewire = require('rewire');
const testConfig = require('./fixtures/test-config.json');

const app = rewire('../../src/application/app.js');
const getConfig = app.__get__('getConfig');
const checkInputAndConfig = app.__get__('checkInputAndConfig');

describe('App Tests', () => {
describe('getConfig', () => {
const pathToConfig = 'test/application/fixtures/test-config.json';

it('should throw error when pathToConfig does not point to valid JSON file.', () => {
expect(() => getConfig()).toThrowError();
});

it('should return test config', () => {
const config = getConfig(pathToConfig);
expect(config).toEqual(testConfig);
});
});

describe('checkInputAndConfig', () => {
const config = { patientIdCsvPath: '', extractors: [] };
it('should throw error when fromDate is invalid.', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const { validateConfig } = require('../../src/helpers/configValidator.js');
const { validateConfig, getConfig } = require('../../src/helpers/configUtils.js');
const testConfig = require('./fixtures/test-config.json');

describe('getConfig', () => {
const pathToConfig = 'test/helpers/fixtures/test-config.json';

it('should throw error when pathToConfig does not point to valid JSON file.', () => {
expect(() => getConfig()).toThrowError();
});

it('should return test config', () => {
const config = getConfig(pathToConfig);
expect(config).toEqual(testConfig);
});
});

describe('validateConfig', () => {
const missingPropertyConfig = { patientIdCsvPath: '' };
Expand Down