@@ -71,84 +71,78 @@ function getEffectiveFromDate(fromDate, runLogger) {
71
71
}
72
72
73
73
async function mcodeApp ( Client , fromDate , toDate , pathToConfig , pathToRunLogs , debug , allEntries ) {
74
- try {
75
- if ( debug ) logger . level = 'debug' ;
76
- // Don't require a run-logs file if we are extracting all-entries. Only required when using --entries-filter.
77
- if ( ! allEntries ) checkLogFile ( pathToRunLogs ) ;
78
- const config = getConfig ( pathToConfig ) ;
79
- checkInputAndConfig ( config , fromDate , toDate ) ;
80
-
81
- // Create and initialize client
82
- const mcodeClient = new Client ( config ) ;
83
- await mcodeClient . init ( ) ;
84
-
85
- // Parse CSV for list of patient mrns
86
- const patientIdsCsvPath = path . resolve ( config . patientIdCsvPath ) ;
87
- const patientIds = parse ( fs . readFileSync ( patientIdsCsvPath , 'utf8' ) , { columns : true , bom : true } ) . map ( ( row ) => row . mrn ) ;
88
-
89
- // Get RunInstanceLogger for recording new runs and inferring dates from previous runs
90
- const runLogger = allEntries ? null : new RunInstanceLogger ( pathToRunLogs ) ;
91
- const effectiveFromDate = allEntries ? null : getEffectiveFromDate ( fromDate , runLogger ) ;
92
- const effectiveToDate = allEntries ? null : toDate ;
93
-
94
- // Extract the data
95
- logger . info ( `Extracting data for ${ patientIds . length } patients` ) ;
96
- const { extractedData, successfulExtraction, totalExtractionErrors } = await extractDataForPatients ( patientIds , mcodeClient , effectiveFromDate , effectiveToDate ) ;
97
-
98
- // If we have notification information, send an emailNotification
99
- const { notificationInfo } = config ;
100
- if ( notificationInfo ) {
101
- const notificationErrors = zipErrors ( totalExtractionErrors ) ;
102
- try {
103
- await sendEmailNotification ( notificationInfo , notificationErrors , debug ) ;
104
- } catch ( e ) {
105
- logger . error ( e . message ) ;
106
- }
74
+ if ( debug ) logger . level = 'debug' ;
75
+ // Don't require a run-logs file if we are extracting all-entries. Only required when using --entries-filter.
76
+ if ( ! allEntries ) checkLogFile ( pathToRunLogs ) ;
77
+ const config = getConfig ( pathToConfig ) ;
78
+ checkInputAndConfig ( config , fromDate , toDate ) ;
79
+
80
+ // Create and initialize client
81
+ const mcodeClient = new Client ( config ) ;
82
+ await mcodeClient . init ( ) ;
83
+
84
+ // 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 ) ;
87
+
88
+ // Get RunInstanceLogger for recording new runs and inferring dates from previous runs
89
+ const runLogger = allEntries ? null : new RunInstanceLogger ( pathToRunLogs ) ;
90
+ const effectiveFromDate = allEntries ? null : getEffectiveFromDate ( fromDate , runLogger ) ;
91
+ const effectiveToDate = allEntries ? null : toDate ;
92
+
93
+ // Extract the data
94
+ logger . info ( `Extracting data for ${ patientIds . length } patients` ) ;
95
+ const { extractedData, successfulExtraction, totalExtractionErrors } = await extractDataForPatients ( patientIds , mcodeClient , effectiveFromDate , effectiveToDate ) ;
96
+
97
+ // If we have notification information, send an emailNotification
98
+ const { notificationInfo } = config ;
99
+ if ( notificationInfo ) {
100
+ const notificationErrors = zipErrors ( totalExtractionErrors ) ;
101
+ try {
102
+ await sendEmailNotification ( notificationInfo , notificationErrors , debug ) ;
103
+ } catch ( e ) {
104
+ logger . error ( e . message ) ;
107
105
}
108
- // A run is successful and should be logged when both extraction finishes without fatal errors
109
- // and messages are posted without fatal errors
110
- if ( ! allEntries && effectiveFromDate ) {
111
- const successCondition = successfulExtraction ;
112
- if ( successCondition ) {
113
- runLogger . addRun ( effectiveFromDate , effectiveToDate ) ;
114
- }
106
+ }
107
+ // A run is successful and should be logged when both extraction finishes without fatal errors
108
+ // and messages are posted without fatal errors
109
+ if ( ! allEntries && effectiveFromDate ) {
110
+ const successCondition = successfulExtraction ;
111
+ if ( successCondition ) {
112
+ runLogger . addRun ( effectiveFromDate , effectiveToDate ) ;
115
113
}
114
+ }
116
115
117
- // check if config specifies that MRN needs to be masked
118
- // if it does need to be masked, mask all references to MRN outside of the patient resource
119
- const patientConfig = config . extractors . find ( ( e ) => e . type === 'CSVPatientExtractor' ) ;
120
- if ( patientConfig && ( 'constructorArgs' in patientConfig && 'mask' in patientConfig . constructorArgs ) ) {
121
- if ( patientConfig . constructorArgs . mask . includes ( 'mrn' ) ) {
122
- extractedData . forEach ( ( bundle , i ) => {
123
- // NOTE: This may fail to mask MRN-related properties on non-patient resources
124
- // Need to investigate further.
125
- try {
126
- maskMRN ( bundle ) ;
127
- } catch ( e ) {
128
- logger . error ( `Bundle ${ i + 1 } : ${ e . message } ` ) ;
129
- }
130
- } ) ;
131
- }
116
+ // check if config specifies that MRN needs to be masked
117
+ // if it does need to be masked, mask all references to MRN outside of the patient resource
118
+ const patientConfig = config . extractors . find ( ( e ) => e . type === 'CSVPatientExtractor' ) ;
119
+ if ( patientConfig && ( 'constructorArgs' in patientConfig && 'mask' in patientConfig . constructorArgs ) ) {
120
+ if ( patientConfig . constructorArgs . mask . includes ( 'mrn' ) ) {
121
+ extractedData . forEach ( ( bundle , i ) => {
122
+ // NOTE: This may fail to mask MRN-related properties on non-patient resources
123
+ // Need to investigate further.
124
+ try {
125
+ maskMRN ( bundle ) ;
126
+ } catch ( e ) {
127
+ logger . error ( `Bundle ${ i + 1 } : ${ e . message } ` ) ;
128
+ }
129
+ } ) ;
132
130
}
131
+ }
133
132
134
- // Finally, save the data to disk
135
- const outputPath = './output' ;
136
- if ( ! fs . existsSync ( outputPath ) ) {
137
- logger . info ( `Creating directory ${ outputPath } ` ) ;
138
- fs . mkdirSync ( outputPath ) ;
139
- }
140
- // For each bundle in our extractedData, write it to our output directory
141
- extractedData . forEach ( ( bundle , i ) => {
142
- const outputFile = path . join ( outputPath , `mcode-extraction-patient-${ i + 1 } .json` ) ;
143
- logger . debug ( `Logging mCODE output to ${ outputFile } ` ) ;
144
- fs . writeFileSync ( outputFile , JSON . stringify ( bundle ) , 'utf8' ) ;
145
- } ) ;
146
- logger . info ( `Successfully logged ${ extractedData . length } mCODE bundle(s) to ${ outputPath } ` ) ;
147
- } catch ( e ) {
148
- logger . error ( e . message ) ;
149
- logger . debug ( e . stack ) ;
150
- process . exit ( 1 ) ;
133
+ // Finally, save the data to disk
134
+ const outputPath = './output' ;
135
+ if ( ! fs . existsSync ( outputPath ) ) {
136
+ logger . info ( `Creating directory ${ outputPath } ` ) ;
137
+ fs . mkdirSync ( outputPath ) ;
151
138
}
139
+ // For each bundle in our extractedData, write it to our output directory
140
+ extractedData . forEach ( ( bundle , i ) => {
141
+ const outputFile = path . join ( outputPath , `mcode-extraction-patient-${ i + 1 } .json` ) ;
142
+ logger . debug ( `Logging mCODE output to ${ outputFile } ` ) ;
143
+ fs . writeFileSync ( outputFile , JSON . stringify ( bundle ) , 'utf8' ) ;
144
+ } ) ;
145
+ logger . info ( `Successfully logged ${ extractedData . length } mCODE bundle(s) to ${ outputPath } ` ) ;
152
146
}
153
147
154
148
module . exports = {
0 commit comments