diff --git a/src/application/app.js b/src/application/app.js index fcb206e3..da3b4c8f 100644 --- a/src/application/app.js +++ b/src/application/app.js @@ -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 @@ -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 diff --git a/src/cli/cli.js b/src/cli/cli.js index d957c9e9..f21cf982 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -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'); @@ -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'; diff --git a/src/helpers/configValidator.js b/src/helpers/configUtils.js similarity index 78% rename from src/helpers/configValidator.js rename to src/helpers/configUtils.js index 3bac2236..e0494cde 100644 --- a/src/helpers/configValidator.js +++ b/src/helpers/configUtils.js @@ -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); @@ -34,5 +46,6 @@ function validateConfig(config) { } module.exports = { + getConfig, validateConfig, }; diff --git a/src/index.js b/src/index.js index db3791d1..6160db71 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -76,6 +77,8 @@ module.exports = { parsePatientIds, sendEmailNotification, zipErrors, + getConfig, + validateConfig, // Extractors and Clients BaseClient, BaseFHIRExtractor, diff --git a/test/application/app.test.js b/test/application/app.test.js index 86897761..b42b22de 100644 --- a/test/application/app.test.js +++ b/test/application/app.test.js @@ -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.', () => { diff --git a/test/helpers/configValidator.test.js b/test/helpers/configUtils.test.js similarity index 69% rename from test/helpers/configValidator.test.js rename to test/helpers/configUtils.test.js index 456783a7..7e111fe7 100644 --- a/test/helpers/configValidator.test.js +++ b/test/helpers/configUtils.test.js @@ -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: '' }; diff --git a/test/application/fixtures/test-config.json b/test/helpers/fixtures/test-config.json similarity index 100% rename from test/application/fixtures/test-config.json rename to test/helpers/fixtures/test-config.json