forked from HL7-DaVinci/crd-request-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildScript.2022071.js
More file actions
320 lines (282 loc) · 11.3 KB
/
buildScript.2022071.js
File metadata and controls
320 lines (282 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* 2022071 NCPDP SCRIPT Support */
import { getDrugCodeableConceptFromMedicationRequest } from './fhir';
function xmlAddTextNode(xmlDoc, parent, sectionName, value) {
var section = xmlDoc.createElement(sectionName);
var textNode = xmlDoc.createTextNode(value);
section.appendChild(textNode);
parent.appendChild(section);
}
function buildNewRxName(doc, nameResource) {
var names = doc.createElement('Names');
var name = doc.createElement('Name');
xmlAddTextNode(doc, name, 'LastName', nameResource.family);
xmlAddTextNode(doc, name, 'FirstName', nameResource.given[0]);
names.appendChild(name);
return names;
}
function buildNewRxAddress(doc, addressResource) {
// console.log(addressResource);
var address = doc.createElement('Address');
xmlAddTextNode(doc, address, 'AddressLine1', addressResource.line[0]);
xmlAddTextNode(doc, address, 'City', addressResource.city);
xmlAddTextNode(doc, address, 'StateProvince', addressResource.state);
xmlAddTextNode(doc, address, 'PostalCode', addressResource.postalCode);
xmlAddTextNode(doc, address, 'Country', 'US'); // assume US for now
return address;
}
function buildNewRxPatient(doc, patientResource) {
// console.log(patientResource);
var patient = doc.createElement('Patient');
var humanPatient = doc.createElement('HumanPatient');
// Patient Name
const patientNameResource = patientResource.name[0];
humanPatient.appendChild(buildNewRxName(doc, patientNameResource));
// Patient Gender and Sex
var genderAndSex = doc.createElement('GenderAndSex');
var gender = 'U'; // unknown
var patientResourceGender = patientResource.gender.toLowerCase();
if (patientResourceGender === 'male') {
gender = 'M'; // male
} else if (patientResourceGender === 'female') {
gender = 'F'; // female
} else if (patientResourceGender === 'other') {
gender = 'N'; // non-binary
}
xmlAddTextNode(doc, genderAndSex, 'AdministrativeGender', gender);
humanPatient.appendChild(genderAndSex);
// Patient Birth Date
var dateOfBirth = doc.createElement('DateOfBirth');
xmlAddTextNode(doc, dateOfBirth, 'Date', patientResource.birthDate);
humanPatient.appendChild(dateOfBirth);
// Patient Address
const patientAddressResource = patientResource.address[0];
humanPatient.appendChild(buildNewRxAddress(doc, patientAddressResource));
patient.appendChild(humanPatient);
return patient;
}
function buildNewRxPrescriber(doc, practitionerResource) {
// console.log(practitionerResource);
var prescriber = doc.createElement('Prescriber');
var nonVeterinarian = doc.createElement('NonVeterinarian');
// Prescriber Identifier
for (let i = 0; i < practitionerResource.identifier.length; i++) {
let id = practitionerResource.identifier[i];
if (id.system && id.system.includes('us-npi')) {
var identification = doc.createElement('Identification');
xmlAddTextNode(doc, identification, 'NPI', id.value);
nonVeterinarian.appendChild(identification);
}
}
// Prescriber Name
const practitionerNameResource = practitionerResource.name[0];
nonVeterinarian.appendChild(buildNewRxName(doc, practitionerNameResource));
// Prescriber Address
const practitionerAddressResource = practitionerResource.address[0];
nonVeterinarian.appendChild(buildNewRxAddress(doc, practitionerAddressResource));
// Prescriber Phone Number and Email
var communicationNumbers = doc.createElement('CommunicationNumbers');
for (let i = 0; i < practitionerResource.telecom.length; i++) {
const telecom = practitionerResource.telecom[i];
if (telecom.system === 'phone') {
var primaryTelephone = doc.createElement('PrimaryTelephone');
xmlAddTextNode(doc, primaryTelephone, 'Number', telecom.value);
communicationNumbers.appendChild(primaryTelephone);
} else if (telecom.system === 'email') {
xmlAddTextNode(doc, communicationNumbers, 'ElectronicMail', telecom.value);
}
}
nonVeterinarian.appendChild(communicationNumbers);
prescriber.appendChild(nonVeterinarian);
return prescriber;
}
function quantityUnitOfMeasureFromDrugFormCode(dispenseRequest) {
// Switch on Orderable Drug Form codes from:
// https://terminology.hl7.org/5.0.0/CodeSystem-v3-orderableDrugForm.html
// Return NCPDP QuantityUnitOfMeasure
if (
dispenseRequest.quantity.system.toLowerCase().endsWith('v3-orderableDrugForm'.toLowerCase())
) {
// is a subset of the codes, not a complete list
switch (dispenseRequest.quantity.code.toUpperCase()) {
case 'APPFUL': // Applicatorful
case 'FOAMAPL': // Foam with Applicator
case 'VAGFOAMAPL': // Vaginal Foam with Applicator
case 'VAGCRMAPL': // Vaginal Cream with Applicator
case 'OINTAPL': // Ointment with Applicator
case 'VAGOINTAPL': // Vaginal Ointment with Applicator
case 'GELAPL': // Gel with Applicator
case 'VGELAPL': // Vaginal Gel with Applicator
return 'C62412'; // Applicator
//case "":
// return "C54564" // Blister
case 'CAPLET': // Caplet
return 'C64696'; // Caplet
case 'CAP': // Capsule
return 'C48480'; // Capsule
//case "":
// return "C64933" // Each
//case "":
// return "C53499" // Film
//case "":
// return "C48155" // Gram
case 'GUM': // Chewing Gum
return 'C69124'; // Gum
//case "":
// return "C48499" // Implant
//case "":
// return "C62276" // Insert
//case "":
// return "C48504" // Kit
//case "":
// return "C120263" // Lancet
case 'ORTROCHE': // Lozenge/Oral Troche
return 'C48506'; // Lozenge
//case "":
// return "C28254" // Milliliter
//case "":
// return "C48521" // Packet
case 'PAD': // Pad
case 'MEDPAD': // Medicated Pad
return 'C65032'; // Pad
case 'PATCH': // Patch
case 'TPATCH': // Transdermal Patch
case 'TPATH16': // 16 Hour Transdermal Patch
case 'TPATH24': // 24 Hour Transdermal Patch
case 'TPATH2WK': // Biweekly Transdermal Patch
case 'TPATH72': // 72 Hour Transdermal Patch
case 'TPATHWK': // Weekly Hour Transdermal Patch
return 'C48524'; // Patch
//case "":
// return "C120216" // Pen Needle
//case "":
// return "C62609" // Ring
// case "":
// return "C53502" // Sponge
//case "":
// return "C53503" // Stick
//case "":
// return "C48538" // Strip
case 'SUPP': // Suppository
case 'RECSUPP': // Rectal Suppository
case 'URETHSUPP': // Urethral Suppository
case 'VAGSUPP': // Vaginal Suppository
return 'C48539'; // Suppository
case 'SWAB': // Swab
case 'MEDSWAB': // Medicated Swab
return 'C53504'; // Swab
case 'TAB': // Tablet
case 'ORTAB': // Oral Tablet
case 'BUCTAB': // Buccal Tablet
case 'SRBUCTAB': // Sustained Release Buccal Tablet
case 'CHEWTAB': // Chewable Tablet
case 'CPTAB': // Coated Particles Tablet
case 'DISINTTAB': // Disintegrating Tablet
case 'DRTAB': // Delayed Release Tablet
case 'ECTAB': // Enteric Coated Tablet
case 'ERECTTAB': // Extended Release Enteric Coated Tablet
case 'ERTAB': // Extended Release Tablet
case 'ERTAB12': // 12 Hour Extended Release Tablet
case 'ERTAB24': // 24 Hour Extended Release Tablet
case 'SLTAB': // Sublingual Tablet
case 'VAGTAB': // Vaginal Tablet
return 'C48542'; // Tablet
//case "":
// return "C48548" // Troche
case 'WAFER': // Wafer
return 'C48552'; // Wafer
default:
return 'C38046'; // Unspecified
}
}
return 'C38046'; // unspecified
}
function buildNewRxMedication(doc, medicationRequestResource) {
// console.log(medicationRequestResource);
var medicationPrescribed = doc.createElement('MedicationPrescribed');
// Medication Product
var product = doc.createElement('Product');
var drugCoded = doc.createElement('DrugCoded');
// loop through the coding values and find the ndc code and the rxnorm code
let medicationCodingList =
getDrugCodeableConceptFromMedicationRequest(medicationRequestResource)?.coding;
for (let i = 0; i < medicationCodingList.length; i++) {
const coding = medicationCodingList[i];
const system = coding.system.toLowerCase();
if (system.endsWith('ndc')) {
// Medication NDC
xmlAddTextNode(doc, medicationPrescribed, 'DrugDescription', coding.display);
xmlAddTextNode(doc, drugCoded, 'NDC', coding.code); // 10-digit number
}
}
product.appendChild(drugCoded);
medicationPrescribed.appendChild(product);
// Medication Quantity
const dispenseRequest = medicationRequestResource.dispenseRequest;
var quantity = doc.createElement('Quantity');
xmlAddTextNode(doc, quantity, 'Value', dispenseRequest.quantity.value);
xmlAddTextNode(doc, quantity, 'CodeListQualifier', 38); // Original Quantity
var quantityUnitOfMeasure = doc.createElement('QuantityUnitOfMeasure');
xmlAddTextNode(
doc,
quantityUnitOfMeasure,
'Code',
quantityUnitOfMeasureFromDrugFormCode(dispenseRequest)
);
quantity.appendChild(quantityUnitOfMeasure);
medicationPrescribed.appendChild(quantity);
// Medication Written Date
var writtenDate = doc.createElement('WrittenDate');
xmlAddTextNode(doc, writtenDate, 'Date', medicationRequestResource.authoredOn);
medicationPrescribed.appendChild(writtenDate);
// Medication Substitutions (0 - None)
var substitutions = doc.createElement('Substitutions');
xmlAddTextNode(doc, substitutions, 'Substitutions', 0);
medicationPrescribed.appendChild(substitutions);
// Medication NumberOfRefills (0 - None)
xmlAddTextNode(
doc,
medicationPrescribed,
'NumberOfRefills',
dispenseRequest.numberOfRepeatsAllowed
);
// Medication Sig
var sig = doc.createElement('Sig');
xmlAddTextNode(doc, sig, 'SigText', medicationRequestResource.dosageInstruction[0].text);
medicationPrescribed.appendChild(sig);
// Medication REMS
// A - Prescriber has checked REMS and the prescriber's actions have been completed.
// B - Prescriber has checked REMS and the prescriber's actions are not yet completed.
// N - Prescriber has not checked REMS.
xmlAddTextNode(doc, medicationPrescribed, 'PrescriberCheckedREMS', 'B');
return medicationPrescribed;
}
export default function buildNewRxRequest(
patientResource,
practitionerResource,
medicationRequestResource
) {
// console.log(medicationRequestResource);
var doc = document.implementation.createDocument('', '', null);
var message = doc.createElement('Message');
// Header
var header = doc.createElement('Header');
// generate the message id (just get the milliseconds since epoch and use that)
const d1 = new Date();
const messageIdValue = d1.getTime();
// console.log(messageIdValue);
xmlAddTextNode(doc, header, 'MessageID', messageIdValue);
message.appendChild(header);
// Body
var body = doc.createElement('Body');
var newRx = doc.createElement('NewRx');
// Patient
newRx.appendChild(buildNewRxPatient(doc, patientResource));
// Prescriber
newRx.appendChild(buildNewRxPrescriber(doc, practitionerResource));
// Medication
newRx.appendChild(buildNewRxMedication(doc, medicationRequestResource));
body.appendChild(newRx);
message.appendChild(body);
doc.appendChild(message);
return doc;
}