Skip to content

Commit 774d1bb

Browse files
authored
add error handling around ncpdp script (#181)
1 parent 39a7fc3 commit 774d1bb

2 files changed

Lines changed: 104 additions & 91 deletions

File tree

src/util/buildScript.2017071.js

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@ import { getDrugCodeableConceptFromMedicationRequest } from './fhir';
44

55
var SCRIPT_VERSION = '20170715';
66

7+
function safeText(value) {
8+
if (value === null || value === undefined) return '';
9+
return String(value);
10+
}
11+
712
function xmlAddTextNode(xmlDoc, parent, sectionName, value) {
813
var section = xmlDoc.createElement(sectionName);
9-
var textNode = xmlDoc.createTextNode(value);
14+
var textNode = xmlDoc.createTextNode(safeText(value));
1015
section.appendChild(textNode);
1116
parent.appendChild(section);
1217
}
1318

1419
function xmlAddTextNodeWithAttribute(xmlDoc, parent, sectionName, value, attrName, attrValue) {
1520
var section = xmlDoc.createElement(sectionName);
1621
section.setAttribute(attrName, attrValue);
17-
var textNode = xmlDoc.createTextNode(value);
22+
var textNode = xmlDoc.createTextNode(safeText(value));
1823
section.appendChild(textNode);
1924
parent.appendChild(section);
2025
}
2126

2227
function buildNewRxName(doc, nameResource) {
2328
var name = doc.createElement('Name');
24-
xmlAddTextNode(doc, name, 'LastName', nameResource.family);
25-
xmlAddTextNode(doc, name, 'FirstName', nameResource.given[0]);
29+
xmlAddTextNode(doc, name, 'LastName', nameResource?.family);
30+
xmlAddTextNode(doc, name, 'FirstName', nameResource?.given?.[0]);
2631
return name;
2732
}
2833

2934
function buildNewRxAddress(doc, addressResource) {
3035
var address = doc.createElement('Address');
31-
xmlAddTextNode(doc, address, 'AddressLine1', addressResource.line[0]);
32-
xmlAddTextNode(doc, address, 'City', addressResource.city);
33-
xmlAddTextNode(doc, address, 'StateProvince', addressResource.state);
34-
xmlAddTextNode(doc, address, 'PostalCode', addressResource.postalCode);
36+
xmlAddTextNode(doc, address, 'AddressLine1', addressResource?.line?.[0]);
37+
xmlAddTextNode(doc, address, 'City', addressResource?.city);
38+
xmlAddTextNode(doc, address, 'StateProvince', addressResource?.state);
39+
xmlAddTextNode(doc, address, 'PostalCode', addressResource?.postalCode);
3540
xmlAddTextNode(doc, address, 'Country', 'US'); // assume US for now
3641
return address;
3742
}
@@ -41,12 +46,12 @@ function buildNewRxPatient(doc, patientResource) {
4146
var humanPatient = doc.createElement('HumanPatient');
4247

4348
// Patient Name
44-
const patientNameResource = patientResource.name[0];
45-
humanPatient.appendChild(buildNewRxName(doc, patientNameResource));
49+
const patientNameResource = patientResource?.name?.[0];
50+
humanPatient.appendChild(buildNewRxName(doc, patientNameResource ?? {}));
4651

4752
// Patient Gender and Sex
4853
var gender = 'U'; // unknown
49-
var patientResourceGender = patientResource.gender.toLowerCase();
54+
var patientResourceGender = patientResource?.gender?.toLowerCase() ?? '';
5055
if (patientResourceGender === 'male') {
5156
gender = 'M'; // male
5257
} else if (patientResourceGender === 'female') {
@@ -58,22 +63,23 @@ function buildNewRxPatient(doc, patientResource) {
5863

5964
// Patient Birth Date
6065
var dateOfBirth = doc.createElement('DateOfBirth');
61-
xmlAddTextNode(doc, dateOfBirth, 'Date', patientResource.birthDate);
66+
xmlAddTextNode(doc, dateOfBirth, 'Date', patientResource?.birthDate);
6267
humanPatient.appendChild(dateOfBirth);
6368

6469
// Patient Address
65-
const patientAddressResource = patientResource.address[0];
66-
humanPatient.appendChild(buildNewRxAddress(doc, patientAddressResource));
70+
const patientAddressResource = patientResource?.address?.[0];
71+
humanPatient.appendChild(buildNewRxAddress(doc, patientAddressResource ?? {}));
6772

6873
patient.appendChild(humanPatient);
6974
return patient;
7075
}
7176

7277
function getPractitionerNpi(practitionerResource) {
73-
for (let i = 0; i < practitionerResource.identifier.length; i++) {
74-
let id = practitionerResource.identifier[i];
75-
if (id.system && id.system.includes('us-npi')) {
76-
return id.value;
78+
const identifiers = practitionerResource?.identifier ?? [];
79+
for (let i = 0; i < identifiers.length; i++) {
80+
let id = identifiers[i];
81+
if (id?.system && id.system.includes('us-npi')) {
82+
return id.value ?? null;
7783
}
7884
}
7985
return null;
@@ -91,22 +97,23 @@ function buildNewRxPrescriber(doc, practitionerResource, npi) {
9197
}
9298

9399
// Prescriber Name
94-
const practitionerNameResource = practitionerResource.name[0];
95-
nonVeterinarian.appendChild(buildNewRxName(doc, practitionerNameResource));
100+
const practitionerNameResource = practitionerResource?.name?.[0];
101+
nonVeterinarian.appendChild(buildNewRxName(doc, practitionerNameResource ?? {}));
96102

97103
// Prescriber Address
98-
const practitionerAddressResource = practitionerResource.address[0];
99-
nonVeterinarian.appendChild(buildNewRxAddress(doc, practitionerAddressResource));
104+
const practitionerAddressResource = practitionerResource?.address?.[0];
105+
nonVeterinarian.appendChild(buildNewRxAddress(doc, practitionerAddressResource ?? {}));
100106

101107
// Prescriber Phone Number and Email
102108
const communicationNumbers = doc.createElement('CommunicationNumbers');
103-
for (let i = 0; i < practitionerResource.telecom.length; i++) {
104-
const telecom = practitionerResource.telecom[i];
105-
if (telecom.system === 'phone') {
109+
const telecomList = practitionerResource?.telecom ?? [];
110+
for (let i = 0; i < telecomList.length; i++) {
111+
const telecom = telecomList[i];
112+
if (telecom?.system === 'phone') {
106113
const primaryTelephone = doc.createElement('PrimaryTelephone');
107114
xmlAddTextNode(doc, primaryTelephone, 'Number', telecom.value);
108115
communicationNumbers.appendChild(primaryTelephone);
109-
} else if (telecom.system === 'email') {
116+
} else if (telecom?.system === 'email') {
110117
xmlAddTextNode(doc, communicationNumbers, 'ElectronicMail', telecom.value);
111118
}
112119
}
@@ -225,16 +232,18 @@ function buildNewRxMedication(doc, medicationRequestResource) {
225232
var drugCoded = doc.createElement('DrugCoded');
226233

227234
// loop through the coding values and find the ndc code and the rxnorm code
228-
let medicationCodingList =
229-
getDrugCodeableConceptFromMedicationRequest(medicationRequestResource)?.coding;
230235

231-
var drugDisplay = 'undefined';
236+
const medicationCodingList =
237+
getDrugCodeableConceptFromMedicationRequest(medicationRequestResource)?.coding ?? [];
238+
239+
var drugDisplay = '';
232240
for (let i = 0; i < medicationCodingList.length; i++) {
233241
const coding = medicationCodingList[i];
234-
const system = coding.system.toLowerCase();
242+
const system = coding?.system?.toLowerCase() ?? '';
235243

236244
// get the display from first drug coding that contains a display value
237-
if (coding.display && drugDisplay == 'undefined') {
245+
246+
if (coding?.display && !drugDisplay) {
238247
drugDisplay = coding.display;
239248
}
240249

@@ -253,25 +262,23 @@ function buildNewRxMedication(doc, medicationRequestResource) {
253262
medicationPrescribed.appendChild(drugCoded);
254263

255264
// Medication Quantity
256-
console.log(medicationRequestResource);
257-
const dispenseRequest = medicationRequestResource.dispenseRequest;
258-
var quantity = doc.createElement('Quantity');
259-
xmlAddTextNode(doc, quantity, 'Value', dispenseRequest?.quantity?.value ? dispenseRequest?.quantity?.value : '');
260-
xmlAddTextNode(doc, quantity, 'CodeListQualifier', 38); // Original Quantity
261-
var quantityUnitOfMeasure = doc.createElement('QuantityUnitOfMeasure');
262-
xmlAddTextNode(
263-
doc,
264-
quantityUnitOfMeasure,
265-
'Code',
266-
quantityUnitOfMeasureFromDrugFormCode(dispenseRequest ? dispenseRequest : {})
267-
);
268-
quantity.appendChild(quantityUnitOfMeasure);
269-
medicationPrescribed.appendChild(quantity);
270-
265+
const dispenseRequest = medicationRequestResource?.dispenseRequest;
266+
var quantity = doc.createElement('Quantity');
267+
xmlAddTextNode(doc, quantity, 'Value', dispenseRequest?.quantity?.value);
268+
xmlAddTextNode(doc, quantity, 'CodeListQualifier', 38); // Original Quantity
269+
var quantityUnitOfMeasure = doc.createElement('QuantityUnitOfMeasure');
270+
xmlAddTextNode(
271+
doc,
272+
quantityUnitOfMeasure,
273+
'Code',
274+
quantityUnitOfMeasureFromDrugFormCode(dispenseRequest ?? {})
275+
);
276+
quantity.appendChild(quantityUnitOfMeasure);
277+
medicationPrescribed.appendChild(quantity);
271278

272279
// Medication Written Date
273280
var writtenDate = doc.createElement('WrittenDate');
274-
xmlAddTextNode(doc, writtenDate, 'Date', medicationRequestResource.authoredOn);
281+
xmlAddTextNode(doc, writtenDate, 'Date', medicationRequestResource?.authoredOn);
275282
medicationPrescribed.appendChild(writtenDate);
276283

277284
// Medication Substitutions (0 - None)
@@ -282,17 +289,15 @@ function buildNewRxMedication(doc, medicationRequestResource) {
282289
doc,
283290
medicationPrescribed,
284291
'NumberOfRefills',
285-
dispenseRequest?.numberOfRepeatsAllowed ? dispenseRequest?.numberOfRepeatsAllowed : ''
292+
dispenseRequest?.numberOfRepeatsAllowed
286293
);
287294

288-
289295
// Medication Sig
290-
var dosageInstruction = medicationRequestResource?.dosageInstruction;
291-
var dosageText = dosageInstruction ? dosageInstruction [0]?.text : ''
292-
var sig = doc.createElement('Sig');
293-
xmlAddTextNode(doc, sig, 'SigText', dosageText);
294-
medicationPrescribed.appendChild(sig);
295-
296+
const dosageInstruction = medicationRequestResource?.dosageInstruction;
297+
const dosageText = dosageInstruction?.[0]?.text;
298+
var sig = doc.createElement('Sig');
299+
xmlAddTextNode(doc, sig, 'SigText', dosageText);
300+
medicationPrescribed.appendChild(sig);
296301

297302
// Medication REMS
298303
// A - Prescriber has checked REMS and the prescriber's actions have been completed.

0 commit comments

Comments
 (0)