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
5 changes: 2 additions & 3 deletions src/cli/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const parse = require('csv-parse/lib/sync');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/appUtils.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/helpers/appUtils.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
4 changes: 4 additions & 0 deletions test/helpers/fixtures/invalid-mrns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
not-mrn
123
456
789
4 changes: 4 additions & 0 deletions test/helpers/fixtures/valid-mrns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mrn
123
456
789