diff --git a/src/application/app.js b/src/application/app.js index 5cf49533..26357fae 100644 --- a/src/application/app.js +++ b/src/application/app.js @@ -30,7 +30,8 @@ async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug, await mcodeClient.init(); // Parse CSV for list of patient mrns - const patientIds = parsePatientIds(config.patientIdCsvPath); + const dataDirectory = config.commonExtractorArgs && config.commonExtractorArgs.dataDirectory; + const patientIds = parsePatientIds(config.patientIdCsvPath, dataDirectory); // 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 index 7ab38a6c..b0a78d17 100644 --- a/src/helpers/appUtils.js +++ b/src/helpers/appUtils.js @@ -1,25 +1,40 @@ const fs = require('fs'); const path = require('path'); const { csvParse } = require('./csvParsingUtils'); +const logger = require('./logger'); + +/** + * Loads the patientIdCSV data from disk, with some helpful hints logged in case of failure + * + * @returns file corresponding to the patient data + */ +function getPatientIdCSVData(patientIdCsvPath, dataDirectory) { + try { + const patientIdsCsvPath = path.resolve(patientIdCsvPath); + return fs.readFileSync(patientIdsCsvPath, 'utf8'); + } catch (e) { + if (dataDirectory) { + logger.error(`Could not resolve ${patientIdCsvPath}; even with a dataDirectory, the config.patientIdCsvPath variable needs to be a resolvable path to the patientID file on disk.`); + } + throw e; + } +} /** * 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 + * @param {string} patientIdCsvPath filePath to the CSV content to be parsed to get IDs + * @param {string} dataDirectory optional argument for if a dataDirectory was specified by the config * @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 = csvParse(fs.readFileSync(patientIdsCsvPath, 'utf8')).map((row) => { +function parsePatientIds(patientIdCsvPath, dataDirectory) { + const csvData = getPatientIdCSVData(patientIdCsvPath, dataDirectory); + return csvParse(csvData).map((row) => { if (!row.mrn) { - throw new Error(`${pathToCSV} has no "mrn" column`); + throw new Error(`${patientIdCsvPath} has no "mrn" column`); } - return row.mrn; }); - - return patientIds; } module.exports = {