Skip to content

Commit 40f75a5

Browse files
authored
Merge pull request #124 from mcode/patient-csv-util
Add helper for parsing patient CSV ids with BOMs
2 parents d3a5a83 + df91c74 commit 40f75a5

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed

src/cli/app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const parse = require('csv-parse/lib/sync');
21
const fs = require('fs');
32
const path = require('path');
43
const moment = require('moment');
@@ -7,6 +6,7 @@ const { RunInstanceLogger } = require('./RunInstanceLogger');
76
const { sendEmailNotification, zipErrors } = require('./emailNotifications');
87
const { extractDataForPatients } = require('./mcodeExtraction');
98
const { maskMRN } = require('../helpers/patientUtils');
9+
const { parsePatientIds } = require('../helpers/appUtils');
1010

1111
function getConfig(pathToConfig) {
1212
// Checks pathToConfig points to valid JSON file
@@ -82,8 +82,7 @@ async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, d
8282
await mcodeClient.init();
8383

8484
// Parse CSV for list of patient mrns
85-
const patientIdsCsvPath = path.resolve(config.patientIdCsvPath);
86-
const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), { columns: true }).map((row) => row.mrn);
85+
const patientIds = parsePatientIds(config.patientIdCsvPath);
8786

8887
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
8988
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);

src/helpers/appUtils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const parse = require('csv-parse/lib/sync');
4+
5+
/**
6+
* Parses a provided CSV with MRN column into string array of IDs
7+
*
8+
* @param {string} pathToCSV filePath to the CSV content to be parsed to get IDs
9+
* @returns array of parsed IDs from the CSV
10+
*/
11+
function parsePatientIds(pathToCSV) {
12+
// Parse CSV for list of patient IDs
13+
const patientIdsCsvPath = path.resolve(pathToCSV);
14+
const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), {
15+
columns: (header) => header.map((column) => column.toLowerCase()),
16+
bom: true,
17+
}).map((row) => {
18+
if (!row.mrn) {
19+
throw new Error(`${pathToCSV} has no "mrn" column`);
20+
}
21+
22+
return row.mrn;
23+
});
24+
25+
return patientIds;
26+
}
27+
28+
module.exports = {
29+
parsePatientIds,
30+
};

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ const { getDiseaseStatusCode, getDiseaseStatusEvidenceCode, mEpochToDate } = req
6666
const { formatDate, formatDateTime } = require('./helpers/dateUtils');
6767
const { lowercaseLookupQuery, createLowercaseLookup, createInvertedLookup } = require('./helpers/lookupUtils');
6868
const { getConditionEntriesFromContext, getConditionsFromContext, getEncountersFromContext, getPatientFromContext } = require('./helpers/contextUtils');
69+
const { parsePatientIds } = require('./helpers/appUtils');
6970

7071
module.exports = {
7172
// CLI Related utilities
72-
mcodeApp,
7373
RunInstanceLogger,
7474
extractDataForPatients,
75+
mcodeApp,
76+
parsePatientIds,
7577
sendEmailNotification,
7678
zipErrors,
7779
// Extractors and Clients

test/helpers/appUtils.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const path = require('path');
2+
const { parsePatientIds } = require('../../src/helpers/appUtils');
3+
4+
const MOCK_VALID_ID_CSV = path.join(__dirname, './fixtures/valid-mrns.csv');
5+
6+
// Has no MRN column
7+
const MOCK_INVALID_ID_CSV = path.join(__dirname, './fixtures/invalid-mrns.csv');
8+
9+
describe('appUtils', () => {
10+
describe('parsePatientIds', () => {
11+
test('valid path should parse content', () => {
12+
const expectedIds = ['123', '456', '789'];
13+
const ids = parsePatientIds(MOCK_VALID_ID_CSV);
14+
15+
// Should get every MRN
16+
expect(ids).toHaveLength(expectedIds.length);
17+
expect(ids).toEqual(expectedIds);
18+
});
19+
20+
test('invalid path should throw error', () => {
21+
expect(() => parsePatientIds(MOCK_INVALID_ID_CSV)).toThrowError();
22+
});
23+
});
24+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
not-mrn
2+
123
3+
456
4+
789

test/helpers/fixtures/valid-mrns.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mrn
2+
123
3+
456
4+
789

0 commit comments

Comments
 (0)