@@ -2,7 +2,7 @@ const path = require('path');
2
2
const _ = require ( 'lodash' ) ;
3
3
const { Extractor } = require ( './Extractor' ) ;
4
4
const { CSVModule } = require ( '../modules' ) ;
5
- const { formatDate, formatDateTime } = require ( '../helpers/dateUtils' ) ;
5
+ const { formatDate } = require ( '../helpers/dateUtils' ) ;
6
6
const { generateMcodeResources } = require ( '../templates' ) ;
7
7
const { getEmptyBundle } = require ( '../helpers/fhirUtils' ) ;
8
8
const logger = require ( '../helpers/logger' ) ;
@@ -11,39 +11,54 @@ const logger = require('../helpers/logger');
11
11
function formatData ( tpcData ) {
12
12
logger . debug ( 'Reformatting treatment plan change data from CSV into template format' ) ;
13
13
14
+ // Nothing to format in empty array
15
+ if ( _ . isEmpty ( tpcData ) ) {
16
+ return [ ] ;
17
+ }
18
+
19
+ // Newly combined data has mrn and list of reviews to map to an extension
20
+ const combinedFormat = { mrn : tpcData [ 0 ] . mrn , reviews : [ ] } ;
21
+
14
22
// If there are multiple entries, combine them into one object with multiple reviews
15
- const combinedData = _ . reduce ( tpcData , ( res , n ) => {
16
- if ( ! n . mrn || ! n . dateOfCarePlan || ! n . changed ) {
23
+ const combinedData = _ . reduce ( tpcData , ( res , currentDataEntry ) => {
24
+ const {
25
+ mrn, dateOfCarePlan, changed, reasonCode, reasonDisplayText,
26
+ } = currentDataEntry ;
27
+
28
+ if ( ! mrn || ! dateOfCarePlan || ! changed ) {
17
29
throw new Error ( 'Treatment Plan Change Data missing an expected property: mrn, dateOfCarePlan, changed are required' ) ;
18
30
}
19
31
20
32
// reasonCode is required if changed flag is true
21
- if ( n . changed === 'true' && ! n . reasonCode ) {
33
+ if ( changed === 'true' && ! reasonCode ) {
22
34
throw new Error ( 'reasonCode is required when changed flag is true' ) ;
23
35
}
24
36
25
- if ( ! res . mrn ) res . mrn = n . mrn ;
26
- ( res . reviews || ( res . reviews = [ ] ) ) . push ( {
27
- dateOfCarePlan : n . dateOfCarePlan ,
28
- reasonCode : n . reasonCode ,
29
- changed : n . changed ,
37
+ res . reviews . push ( {
38
+ dateOfCarePlan ,
39
+ reasonCode ,
40
+ reasonDisplayText ,
41
+ changed,
30
42
} ) ;
31
43
return res ;
32
- } , { } ) ;
44
+ } , combinedFormat ) ;
33
45
34
46
// Format each entry in the reviews array
35
- combinedData . reviews = combinedData . reviews . map ( ( reviews ) => {
36
- const { dateOfCarePlan, changed, reasonCode } = reviews ;
47
+ combinedData . reviews = combinedData . reviews . map ( ( review ) => {
48
+ const { dateOfCarePlan, changed, reasonCode, reasonDisplayText } = review ;
37
49
38
50
const formattedData = {
39
51
effectiveDate : formatDate ( dateOfCarePlan ) ,
40
- effectiveDateTime : formatDateTime ( dateOfCarePlan ) ,
41
52
hasChanged : changed ,
42
53
} ;
43
54
44
55
// Add reasonCode to formattedData if available
45
56
if ( reasonCode ) {
46
57
formattedData . reasonCode = reasonCode ;
58
+
59
+ if ( reasonDisplayText ) {
60
+ formattedData . reasonDisplayText = reasonDisplayText ;
61
+ }
47
62
}
48
63
49
64
return formattedData ;
0 commit comments