diff --git a/src/cli/app.js b/src/cli/app.js index 28ba6b0f..7ac4b60a 100644 --- a/src/cli/app.js +++ b/src/cli/app.js @@ -1,4 +1,3 @@ -const parse = require('csv-parse/lib/sync'); const fs = require('fs'); const path = require('path'); const moment = require('moment'); @@ -7,6 +6,7 @@ const { RunInstanceLogger } = require('./RunInstanceLogger'); const { sendEmailNotification, zipErrors } = require('./emailNotifications'); const { extractDataForPatients } = require('./mcodeExtraction'); const { maskMRN } = require('../helpers/patientUtils'); +const { parsePatientIds } = require('../helpers/appUtils'); function getConfig(pathToConfig) { // Checks pathToConfig points to valid JSON file @@ -82,8 +82,7 @@ async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, d await mcodeClient.init(); // Parse CSV for list of patient mrns - const patientIdsCsvPath = path.resolve(config.patientIdCsvPath); - const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), { columns: true }).map((row) => row.mrn); + const patientIds = parsePatientIds(config.patientIdCsvPath); // Get RunInstanceLogger for recording new runs and inferring dates from previous runs const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs); diff --git a/src/helpers/appUtils.js b/src/helpers/appUtils.js new file mode 100644 index 00000000..f5438cad --- /dev/null +++ b/src/helpers/appUtils.js @@ -0,0 +1,30 @@ +const fs = require('fs'); +const path = require('path'); +const parse = require('csv-parse/lib/sync'); + +/** + * Parses a provided CSV with MRN column into string array of IDs + * + * @param {string} pathToCSV filePath to the CSV content to be parsed to get IDs + * @returns array of parsed IDs from the CSV + */ +function parsePatientIds(pathToCSV) { + // Parse CSV for list of patient IDs + const patientIdsCsvPath = path.resolve(pathToCSV); + const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), { + columns: (header) => header.map((column) => column.toLowerCase()), + bom: true, + }).map((row) => { + if (!row.mrn) { + throw new Error(`${pathToCSV} has no "mrn" column`); + } + + return row.mrn; + }); + + return patientIds; +} + +module.exports = { + parsePatientIds, +}; diff --git a/src/index.js b/src/index.js index 243f6a25..5c862ee7 100644 --- a/src/index.js +++ b/src/index.js @@ -66,12 +66,14 @@ const { getDiseaseStatusCode, getDiseaseStatusEvidenceCode, mEpochToDate } = req 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'); module.exports = { // CLI Related utilities - mcodeApp, RunInstanceLogger, extractDataForPatients, + mcodeApp, + parsePatientIds, sendEmailNotification, zipErrors, // Extractors and Clients diff --git a/test/helpers/appUtils.test.js b/test/helpers/appUtils.test.js new file mode 100644 index 00000000..20478496 --- /dev/null +++ b/test/helpers/appUtils.test.js @@ -0,0 +1,24 @@ +const path = require('path'); +const { parsePatientIds } = require('../../src/helpers/appUtils'); + +const MOCK_VALID_ID_CSV = path.join(__dirname, './fixtures/valid-mrns.csv'); + +// Has no MRN column +const MOCK_INVALID_ID_CSV = path.join(__dirname, './fixtures/invalid-mrns.csv'); + +describe('appUtils', () => { + describe('parsePatientIds', () => { + test('valid path should parse content', () => { + const expectedIds = ['123', '456', '789']; + const ids = parsePatientIds(MOCK_VALID_ID_CSV); + + // Should get every MRN + expect(ids).toHaveLength(expectedIds.length); + expect(ids).toEqual(expectedIds); + }); + + test('invalid path should throw error', () => { + expect(() => parsePatientIds(MOCK_INVALID_ID_CSV)).toThrowError(); + }); + }); +}); diff --git a/test/helpers/fixtures/invalid-mrns.csv b/test/helpers/fixtures/invalid-mrns.csv new file mode 100644 index 00000000..18b28856 --- /dev/null +++ b/test/helpers/fixtures/invalid-mrns.csv @@ -0,0 +1,4 @@ +not-mrn +123 +456 +789 diff --git a/test/helpers/fixtures/valid-mrns.csv b/test/helpers/fixtures/valid-mrns.csv new file mode 100644 index 00000000..e48fff3b --- /dev/null +++ b/test/helpers/fixtures/valid-mrns.csv @@ -0,0 +1,4 @@ +mrn +123 +456 +789