Skip to content

Commit 2d3f905

Browse files
author
Matthew Gramigna
committed
Refactor formatting impl and update tests
1 parent 6647149 commit 2d3f905

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

src/extractors/CSVTreatmentPlanChangeExtractor.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const _ = require('lodash');
33
const { Extractor } = require('./Extractor');
44
const { CSVModule } = require('../modules');
5-
const { formatDate, formatDateTime } = require('../helpers/dateUtils');
5+
const { formatDate } = require('../helpers/dateUtils');
66
const { generateMcodeResources } = require('../templates');
77
const { getEmptyBundle } = require('../helpers/fhirUtils');
88
const logger = require('../helpers/logger');
@@ -11,39 +11,54 @@ const logger = require('../helpers/logger');
1111
function formatData(tpcData) {
1212
logger.debug('Reformatting treatment plan change data from CSV into template format');
1313

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+
1422
// 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) {
1729
throw new Error('Treatment Plan Change Data missing an expected property: mrn, dateOfCarePlan, changed are required');
1830
}
1931

2032
// reasonCode is required if changed flag is true
21-
if (n.changed === 'true' && !n.reasonCode) {
33+
if (changed === 'true' && !reasonCode) {
2234
throw new Error('reasonCode is required when changed flag is true');
2335
}
2436

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,
3042
});
3143
return res;
32-
}, {});
44+
}, combinedFormat);
3345

3446
// 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;
3749

3850
const formattedData = {
3951
effectiveDate: formatDate(dateOfCarePlan),
40-
effectiveDateTime: formatDateTime(dateOfCarePlan),
4152
hasChanged: changed,
4253
};
4354

4455
// Add reasonCode to formattedData if available
4556
if (reasonCode) {
4657
formattedData.reasonCode = reasonCode;
58+
59+
if (reasonDisplayText) {
60+
formattedData.reasonDisplayText = reasonDisplayText;
61+
}
4762
}
4863

4964
return formattedData;

test/extractors/CSVTreatmentPlanChangeExtractor.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ describe('CSVTreatmentPlanChangeExtractor', () => {
6868
reviews: [
6969
{
7070
effectiveDate: '2020-04-15',
71-
effectiveDateTime: '2020-04-15',
7271
hasChanged: 'true',
7372
reasonCode: '281647001',
73+
reasonDisplayText: 'Adverse reaction (disorder)',
7474
},
7575
{
7676
effectiveDate: '2020-04-30',
77-
effectiveDateTime: '2020-04-30',
7877
reasonCode: '405613005',
7978
hasChanged: 'true',
8079
},

test/extractors/fixtures/csv-treatment-plan-change-bundle.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"type": "collection",
44
"entry": [
55
{
6-
"fullUrl": "urn:uuid:a66c9b6d0c06506e28dcaac8fc2ef9126b1039507761c79c6e04f606b5c7b054",
6+
"fullUrl": "urn:uuid:3005b559bb913fdfb23756a442645c6c37bbdafa42ad4be0bd1a6b698274f466",
77
"resource": {
88
"resourceType": "CarePlan",
9-
"id": "a66c9b6d0c06506e28dcaac8fc2ef9126b1039507761c79c6e04f606b5c7b054",
9+
"id": "3005b559bb913fdfb23756a442645c6c37bbdafa42ad4be0bd1a6b698274f466",
1010
"meta": {
1111
"profile": [
1212
"http://mcodeinitiative.org/codex/us/icare/StructureDefinition/icare-care-plan-with-review"
@@ -24,8 +24,9 @@
2424
"url": "CarePlanChangeReason",
2525
"valueCodeableConcept": {
2626
"coding": [
27-
{ "system": "http://snomed.info/sct", "code": "281647001" }
28-
]
27+
{ "system": "http://snomed.info/sct", "code": "281647001", "display": "Adverse reaction (disorder)" }
28+
],
29+
"text": "Adverse reaction (disorder)"
2930
}
3031
},
3132
{ "url": "ReviewDate", "valueDate": "2020-04-15" },

test/extractors/fixtures/csv-treatment-plan-change-module-response.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"mrn": "mrn-1",
44
"dateOfCarePlan": "2020-04-15",
55
"reasonCode": "281647001",
6+
"reasonDisplayText": "Adverse reaction (disorder)",
67
"changed": "true"
78
},
89
{

0 commit comments

Comments
 (0)