Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOMED Hydrating, Filtering, Special MR Generation #1648

Draft
wants to merge 19 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
feat(snomed): medadmin to medstatement
Refs: #1442
  • Loading branch information
jonahkaye committed Apr 22, 2024
commit 960040bb5bec1e395b8f11fd73746c91f07a7175
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const bundleToHtml = (fhirBundle: Bundle): string => {
const {
patient,
medications,
medicationAdministrations,
medicationStatements,
conditions,
allergies,
Expand Down Expand Up @@ -220,7 +219,6 @@ export const bundleToHtml = (fhirBundle: Bundle): string => {
${createMRHeader(patient)}
<div class="divider"></div>
<div id="mr-sections">
${createDischargeMedicationsSection(medications, medicationAdministrations)}
${createMedicationSection(medications, medicationStatements)}
${createConditionSection(conditions, encounters, patient)}
${createAllergySection(allergies)}
Expand Down Expand Up @@ -746,7 +744,7 @@ function createOrganiztionField(
return organization?.name ? `<p>Facility: ${organization.name}</p>` : "";
}

function createDischargeMedicationsSection(
export function createDischargeMedicationsSection(
medications: Medication[],
medicationAdministrations: MedicationAdministration[]
) {
Expand Down Expand Up @@ -827,14 +825,14 @@ export function createMedicationSection(
return dayjs(a.effectivePeriod?.start).isBefore(dayjs(b.effectivePeriod?.start)) ? 1 : -1;
});

const removeDuplicate = uniqWith(medicationsSortedByDate, (a, b) => {
const aDate = dayjs(a.effectivePeriod?.start).format(ISO_DATE);
const bDate = dayjs(b.effectivePeriod?.start).format(ISO_DATE);
// const removeDuplicate = uniqWith(medicationsSortedByDate, (a, b) => {
// const aDate = dayjs(a.effectivePeriod?.start).format(ISO_DATE);
// const bDate = dayjs(b.effectivePeriod?.start).format(ISO_DATE);

return aDate === bDate && a.dosage?.[0]?.text === b.dosage?.[0]?.text;
});
// return aDate === bDate && a.dosage?.[0]?.text === b.dosage?.[0]?.text;
// });

const activeMedications = removeDuplicate.filter(
const activeMedications = medicationsSortedByDate.filter(
medicationStatement => medicationStatement.status === "active"
);

Expand Down Expand Up @@ -876,11 +874,11 @@ function getPeriodDateFromMedicationStatement(
const end = v.effectivePeriod?.end ? dayjs(v.effectivePeriod.end) : undefined;
const yearDiff = start && end ? end.diff(start, "year") : undefined;

if (type === "start" && yearDiff !== undefined && yearDiff < 5) {
if (type === "start" && (yearDiff == undefined || yearDiff < 5)) {
return v.effectivePeriod?.start;
} else if (type === "end" && yearDiff !== undefined && yearDiff < 5) {
} else if (type === "end" && (yearDiff == undefined || yearDiff < 5)) {
return v.effectivePeriod?.end;
} else if (type === "lastSeen" && yearDiff !== undefined && yearDiff > 5) {
} else if (type === "lastSeen" && yearDiff && yearDiff > 5) {
return v.effectivePeriod?.end;
}
return undefined;
Expand Down
74 changes: 74 additions & 0 deletions packages/utils/src/terminology-server/fhir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
MedicationAdministration,
MedicationAdministrationDosage,
MedicationStatement,
Dosage,
} from "@medplum/fhirtypes";

export function convertMedicationAdministrationToStatement(
medAdmin: MedicationAdministration
): MedicationStatement {
const medStatement: MedicationStatement = {
resourceType: "MedicationStatement",
id: medAdmin.id,
meta: medAdmin.meta,
implicitRules: medAdmin.implicitRules,
language: medAdmin.language,
text: medAdmin.text,
contained: medAdmin.contained,
extension: medAdmin.extension,
modifierExtension: medAdmin.modifierExtension,
identifier: medAdmin.identifier,
partOf: medAdmin.partOf,
status: convertStatus(medAdmin.status),
statusReason: medAdmin.statusReason,
category: medAdmin.category,
medicationCodeableConcept: medAdmin.medicationCodeableConcept,
medicationReference: medAdmin.medicationReference,
subject: medAdmin.subject,
context: medAdmin.context,
effectiveDateTime: medAdmin.effectiveDateTime,
effectivePeriod: medAdmin.effectivePeriod,
derivedFrom: [],
reasonCode: medAdmin.reasonCode,
reasonReference: medAdmin.reasonReference,
note: medAdmin.note,
dosage: convertDosage(medAdmin.dosage),
};

return medStatement;
}

function convertStatus(status: MedicationAdministration["status"]): MedicationStatement["status"] {
const statusMapping: { [key: string]: MedicationStatement["status"] } = {
completed: "completed",
"entered-in-error": "entered-in-error",
"in-progress": "active",
stopped: "stopped",
// Add more mappings as required
};

const mappedStatus = status ? statusMapping[status] : "unknown";
return mappedStatus;
}

function convertDosage(dosage: MedicationAdministrationDosage | undefined): Dosage[] {
if (!dosage) return [];

const convertedDosage: Dosage = {
text: dosage.text,
site: dosage.site,
route: dosage.route,
method: dosage.method,
doseAndRate: [
{
doseQuantity: dosage.dose,
rateRatio: dosage.rateRatio,
rateQuantity: dosage.rateQuantity,
},
],
timing: {},
};

return [convertedDosage];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add documentation explaining what it does and how to use it, please.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import daysjs from "dayjs";
import { getCodeDisplay, getCodeDetailsFull } from "./term-server-api";
import { populateHashTableFromCodeDetails, SnomedHierarchyTableEntry } from "./snomed-heirarchies";
import { RemovalStats, createInitialRemovalStats, prettyPrintRemovalStats } from "./stats";
import { convertMedicationAdministrationToStatement } from "./fhir";

async function processDirectoryOrFile(
directoryOrFile: string,
Expand Down Expand Up @@ -269,6 +270,22 @@ async function filterMedicationStatements({
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
}

function convertMedicationAdministrationsToMedicationStatements({
filePath,
}: {
filePath: string;
}) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
const entries = data.bundle ? data.bundle.entry : data.entry;
for (const entry of entries) {
const resource = entry.resource;
if (resource && resource.resourceType === "MedicationAdministration") {
entry.resource = convertMedicationAdministrationToStatement(resource);
}
}
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
}

export async function fullProcessing(directoryPath: string) {
const removalStats = createInitialRemovalStats();

Expand Down Expand Up @@ -323,6 +340,12 @@ export async function fullProcessing(directoryPath: string) {
});
});

await processDirectoryOrFile(directoryPath, async filePath => {
await convertMedicationAdministrationsToMedicationStatements({
filePath,
});
});

prettyPrintRemovalStats(removalStats);
}

Expand Down
Loading