Skip to content

Commit

Permalink
Merge pull request #59 from mcode/medication-reference
Browse files Browse the repository at this point in the history
Support medicationReference within MedicationRequests
  • Loading branch information
smalho01 authored Dec 1, 2023
2 parents 01e1860 + 760b6c7 commit 23029d1
Show file tree
Hide file tree
Showing 10 changed files with 23,346 additions and 14,994 deletions.
38,213 changes: 23,236 additions & 14,977 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/views/Patient/MedReqDropDown/MedReqDropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import example from '../../../cds-hooks/prefetch/exampleHookService.json'; // TO
import { hydrate } from '../../../cds-hooks/prefetch/PrefetchHydrator';
import { Hook, Card as HooksCard, OrderSelectHook } from '../../../cds-hooks/resources/HookTypes';
import OrderSelect from '../../../cds-hooks/resources/OrderSelect';
import { getDrugCodeFromMedicationRequest } from '../../Questionnaire/questionnaireUtil';
import './MedReqDropDown.css';
import * as env from 'env-var';
import { MedicationBundle, submitToREMS } from '../PatientView';
Expand Down Expand Up @@ -123,7 +124,7 @@ function MedReqDropDown({
useEffect(() => {
if (selectedMedicationCard) {
const medName =
selectedMedicationCard?.medicationCodeableConcept?.coding?.[0].display?.split(' ')[0];
getDrugCodeFromMedicationRequest(selectedMedicationCard)?.display?.split(' ')[0];
if (medName) {
setMedicationName(medName);
}
Expand Down Expand Up @@ -208,7 +209,7 @@ function MedReqDropDown({
{medication ? (
medication.data.map(medications => (
<MenuItem key={medications.id} value={medications.id}>
{medications.medicationCodeableConcept?.coding?.[0].display}
{getDrugCodeFromMedicationRequest(medications)?.display}
</MenuItem>
))
) : (
Expand All @@ -223,7 +224,7 @@ function MedReqDropDown({
<Grid item container>
<Grid item xs={10} sm={11}>
<Typography bgcolor="text.secondary" color="white" textAlign="center">
Code: {selectedMedicationCard?.medicationCodeableConcept?.coding?.[0].code}
Code: {getDrugCodeFromMedicationRequest(selectedMedicationCard)?.code}
</Typography>
<Typography
bgcolor="text.disabled"
Expand All @@ -239,7 +240,7 @@ function MedReqDropDown({
color="white"
textAlign="center"
>
{selectedMedicationCard?.medicationCodeableConcept?.coding?.[0].display}
{getDrugCodeFromMedicationRequest(selectedMedicationCard)?.display}
</Typography>
</Grid>
<Grid
Expand Down
6 changes: 5 additions & 1 deletion src/views/Patient/MedReqDropDown/etasuStatus/EtasuStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import RemsMetEtasuResponse from './RemsMetEtasuResponse';
import MetRequirements from './MetRequirements';
import * as env from 'env-var';
import './EtasuStatus.css';
import { getDrugCodeFromMedicationRequest } from '../../../Questionnaire/questionnaireUtil';

interface EtasuStatusProps {
patient: Patient | null;
Expand All @@ -41,7 +42,10 @@ const EtasuStatus = (props: EtasuStatusProps) => {
const patientFirstName = props.patient?.name?.at(0)?.given?.at(0);
const patientLastName = props.patient?.name?.at(0)?.family;
const patientDOB = props.patient?.birthDate;
const drugCode = props.medication?.medicationCodeableConcept?.coding?.at(0)?.code;
let drugCode = undefined;
if (props.medication) {
drugCode = getDrugCodeFromMedicationRequest(props.medication)?.code;
}
console.log(
'refreshEtasuBundle: ' +
patientFirstName +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useState, useEffect } from 'react';

import './PharmacyStatus.css';
import DoctorOrder from './DoctorOrder';
import { getDrugCodeableConceptFromMedicationRequest } from '../../../Questionnaire/questionnaireUtil';
import * as env from 'env-var';

interface PharmacyStatusProps {
Expand All @@ -32,7 +33,11 @@ const PharmacyStatus = (props: PharmacyStatusProps) => {
const patientLastName = props.patient?.name?.at(0)?.family;
const patientDOB = props.patient?.birthDate;
const rxDate = props.medication?.authoredOn;
const drugNames = props.medication?.medicationCodeableConcept?.coding?.at(0)?.display;
let drugCodeableConcept = undefined;
if (props.medication) {
drugCodeableConcept = getDrugCodeableConceptFromMedicationRequest(props.medication);
}
const drugNames = drugCodeableConcept?.coding?.at(0)?.display;
console.log(
'refreshPharmacyBundle: ' +
patientFirstName +
Expand All @@ -45,7 +50,7 @@ const PharmacyStatus = (props: PharmacyStatusProps) => {
' - ' +
drugNames
);
const ndcDrugCoding = props.medication?.medicationCodeableConcept?.coding?.find(
const ndcDrugCoding = drugCodeableConcept?.coding?.find(
({ system }) => system === 'http://hl7.org/fhir/sid/ndc'
);
let queryString: string =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Patient,
Practitioner
} from 'fhir/r4';
import { getDrugCodeableConceptFromMedicationRequest } from '../../../Questionnaire/questionnaireUtil';

function xmlAddTextNode(
xmlDoc: XMLDocument,
Expand Down Expand Up @@ -237,7 +238,8 @@ function buildNewRxMedication(doc: XMLDocument, medicationRequestResource: Medic
const drugCoded = doc.createElement('DrugCoded');

// loop through the coding values and find the ndc code and the rxnorm code
const medicationCodingList = medicationRequestResource.medicationCodeableConcept?.coding;
const medicationCodingList =
getDrugCodeableConceptFromMedicationRequest(medicationRequestResource)?.coding;
if (medicationCodingList) {
for (let i = 0; i < medicationCodingList.length; i++) {
const coding = medicationCodingList[i];
Expand Down
46 changes: 43 additions & 3 deletions src/views/Patient/PatientView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ interface PatientViewProps {

export interface MedicationBundle {
data: MedicationRequest[];
reference: Patient;

// This is a json object with the key of each element matching the
// contained FHIR resource
references: any;
}

//CDS-Hook Request to REMS-Admin for cards
Expand Down Expand Up @@ -104,7 +107,8 @@ function PatientView(props: PatientViewProps) {
{
patient: 'Patient/{{context.patientId}}',
practitioner: 'Practitioner/{{context.userId}}',
medicationRequests: 'MedicationRequest?subject={{context.patientId}}'
medicationRequests:
'MedicationRequest?subject={{context.patientId}}&_include=MedicationRequest:medication'
},
tempHook
).then(() => {
Expand All @@ -125,11 +129,47 @@ function PatientView(props: PatientViewProps) {
const getMedicationRequest = () => {
client
.request(`MedicationRequest?subject=Patient/${client.patient.id}`, {
resolveReferences: ['subject', 'performer'],
resolveReferences: ['subject', 'performer', 'medicationReference'],
graph: false,
flat: true
})
.then((result: MedicationBundle) => {
result.data.forEach(e => {
if (e?.resourceType === 'MedicationRequest') {
if (e?.medicationReference) {
const medicationReference = e?.medicationReference?.reference;

if (medicationReference) {
// find the matching medication in the references
const medication = result?.references?.[medicationReference];

if (medication) {
const code = medication?.code?.coding?.[0];

if (code) {
// add the reference as a contained resource to the request
if (!e?.contained) {
e.contained = [];
e.contained.push(medication);
} else {
// only add to contained if not already in there
let found = false;
e?.contained.forEach(c => {
if (c.id === medication.id) {
found = true;
}
});
if (!found) {
e?.contained.push(medication);
}
}
}
}
}
}
}
});

setMedication(result);
});
};
Expand Down
5 changes: 3 additions & 2 deletions src/views/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
buildNextQuestionRequest,
findValueByPrefix,
retrieveQuestions,
searchQuestionnaire
searchQuestionnaire,
getDrugCodeableConceptFromMedicationRequest
} from './questionnaireUtil';
import './QuestionnaireForm.css';
import { Button, Typography } from '@mui/material';
Expand Down Expand Up @@ -879,7 +880,7 @@ export function QuestionnaireForm(props: QuestionnaireProps) {
} else if (request.resourceType == 'ServiceRequest') {
c = request.code;
} else if (request.resourceType == 'MedicationRequest') {
c = request.medicationCodeableConcept;
c = getDrugCodeableConceptFromMedicationRequest(request);
}
}
console.log('getCode(): ' + request?.resourceType + ': ');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useState } from 'react';
import ResourceEntry from './ResourceEntry';
import './RemsInterface.css';
import { getDrugCodeableConceptFromMedicationRequest } from '../../questionnaireUtil';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import Paper from '@mui/material/Paper';
import Button from '@mui/material/Button';
Expand Down Expand Up @@ -128,8 +129,9 @@ export default function RemsInterface(props: RemsInterfaceProps) {
const potentialPatient = getResource(props.specialtyRxBundle, patientReference);
if (potentialPrescription && potentialPatient) {
const prescription = potentialPrescription as MedicationRequest;
const simpleDrugName =
prescription.medicationCodeableConcept?.coding?.[0].display?.split(' ')[0];
const medicationCodeableConcept =
getDrugCodeableConceptFromMedicationRequest(prescription);
const simpleDrugName = medicationCodeableConcept?.coding?.[0].display?.split(' ')[0];
const rxDate = prescription.authoredOn;
const patient = potentialPatient as Patient;
const patientFirstName = patient.name?.[0].given?.[0];
Expand Down
2 changes: 1 addition & 1 deletion src/views/Questionnaire/fetchArtifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function fetchArtifactsOperation(
completeOperation(orderResource);
});
} else {
const orderResource = JSON.parse(order.replace(/\\/g, ''));
const orderResource = JSON.parse(order);
completeOperation(orderResource);
}
});
Expand Down
40 changes: 39 additions & 1 deletion src/views/Questionnaire/questionnaireUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export function getAppContext(appContextString: string) {
temp[0] === 'coverage' ||
temp[0] === 'response'
) {
appContext[temp[0]] = temp[1];
const index = temp[0];
// remove the index
temp.shift();
appContext[index] = temp.join('=');
}
});
return appContext;
Expand Down Expand Up @@ -151,6 +154,41 @@ export function buildFhirUrl(reference: string, fhirPrefix: string, fhirVersion:
}
}

/*
* Retrieve the CodeableConcept for the medication from the medicationCodeableConcept if available.
* Read CodeableConcept from contained Medication matching the medicationReference otherwise.
*/
export function getDrugCodeableConceptFromMedicationRequest(medicationRequest: MedicationRequest) {
if (medicationRequest) {
if (medicationRequest?.medicationCodeableConcept) {
console.log('Get Medication code from CodeableConcept');
return medicationRequest?.medicationCodeableConcept;
} else if (medicationRequest?.medicationReference) {
const reference = medicationRequest?.medicationReference;
let coding = undefined;
medicationRequest?.contained?.every(e => {
if (e.resourceType + '/' + e.id === reference.reference) {
if (e.resourceType === 'Medication') {
console.log('Get Medication code from contained resource');
coding = e.code;
}
}
});
return coding;
}
}
return undefined;
}

/*
* Retrieve the coding for the medication from the medicationCodeableConcept if available.
* Read coding from contained Medication matching the medicationReference otherwise.
*/
export function getDrugCodeFromMedicationRequest(medicationRequest: MedicationRequest) {
const codeableConcept = getDrugCodeableConceptFromMedicationRequest(medicationRequest);
return codeableConcept?.coding?.[0];
}

export function fetchFhirVersion(fhirServer: string) {
return new Promise<string>(function (resolve, reject) {
console.log('fetchFhirVersion from ' + fhirServer);
Expand Down

0 comments on commit 23029d1

Please sign in to comment.