Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/services/guidanceresponse.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FhirUtilities } from '../fhir/utilities';
import { GuidanceResponseUtilities } from '../fhir/guidanceResponseUtilities';
import GuidanceResponseModel from '../lib/schemas/resources/GuidanceResponse';
import { Parameters, Medication, Patient } from 'fhir/r4';
import { Parameters, Medication, Patient, MedicationRequest, FhirResource } from 'fhir/r4';
import { getCaseInfo } from '../lib/etasu';

module.exports.searchById = async (args: any) => {
Expand All @@ -17,14 +17,37 @@ module.exports.create = async (args: any, req: any) => {
return await FhirUtilities.store(resource, GuidanceResponseModel, base_version);
};

const getMedicationCode = (medication: Medication | undefined) => {
const getMedicationCode = (
medication: Medication | MedicationRequest | undefined
): string | undefined => {
// grab the medication drug code from the Medication resource
let drugCode = null;
medication?.code?.coding?.forEach(medCode => {
if (medCode?.system?.endsWith('rxnorm')) {
drugCode = medCode?.code;
let drugCode;
if (medication?.resourceType == 'Medication') {
medication?.code?.coding?.forEach(medCode => {
if (medCode?.system?.endsWith('rxnorm')) {
drugCode = medCode?.code;
}
});
} else {
if (medication?.medicationCodeableConcept) {
medication?.medicationCodeableConcept?.coding?.forEach(medCode => {
if (medCode.system?.endsWith('rxnorm')) {
drugCode = medCode.code;
}
});
} else if (medication?.medicationReference) {
const ref = medication.medicationReference.reference;
if (ref?.startsWith('#')) {
const containedRef = ref.slice(1);
const match = medication.contained?.find(res => {
return res.id === containedRef;
});
if (match?.resourceType === 'Medication') {
return getMedicationCode(match);
}
}
}
});
}
return drugCode;
};

Expand All @@ -33,12 +56,16 @@ module.exports.remsEtasu = async (args: any, context: any, logger: any) => {

const parameters: Parameters = args?.resource;
let patient: Patient | undefined;
let medication: Medication | undefined;
let medication: Medication | MedicationRequest | undefined;

parameters?.parameter?.forEach(param => {
if (param?.name === 'patient' && param?.resource?.resourceType === 'Patient') {
patient = param.resource;
} else if (param?.name === 'medication' && param?.resource?.resourceType === 'Medication') {
} else if (
param?.name === 'medication' &&
(param?.resource?.resourceType === 'Medication' ||
param.resource?.resourceType === 'MedicationRequest')
) {
medication = param.resource;
}
});
Expand Down