Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created October 3, 2024 06:39
Show Gist options
  • Save ir4y/a82bcda4d7b9b12ebc3400b6fa3dd5ac to your computer and use it in GitHub Desktop.
Save ir4y/a82bcda4d7b9b12ebc3400b6fa3dd5ac to your computer and use it in GitHub Desktop.
Example of complex fhirpath usage
import { ColumnType } from 'antd/es/table';
import { Text } from '@beda.software/emr/components';
import { Appointment, ServiceRequest } from 'fhir/r4b';
import fhirpath from 'fhirpath';
import { AsJson } from '../../components/AsJson';
import { TableData } from './types';
import { WasUpdated } from '../../components/WasUpdated';
import { Status } from '../../components/Status';
export const appointmentStatuses = [
{
text: 'Pending',
value: 'pending',
},
{
text: 'Booked',
value: 'booked',
},
{
text: 'Fulfilled',
value: 'fulfilled',
},
{
text: 'Cancel',
value: 'cancelled',
},
];
function g(appointment: Appointment, exp: string) {
const res = fhirpath.evaluate(appointment, exp, { Appointment: appointment });
if (res.length === 0) {
return "-";
} if (res.length === 1 && typeof res[0] === 'string') {
return res[0];
} else {
return <AsJson data={res} />;
}
}
const with_status = (actorPath: string, _statusPath?: string) => (_text: any, { appointment }: TableData) => {
let actor = g(appointment, actorPath);
if (typeof actor === 'string') {
// Handle Anaesthetist ono line display
actor = actor.split(',')[0];
}
/* const status = g(appointment, statusPath); */
return (<>
{actor}
</>)
}
export const columns: Array<ColumnType<TableData>> = [
{
title: 'Updated',
key: 'updated',
render: (_text: any, { appointment, wasUpdated }: TableData) => {
const rawLastUpdated = appointment.meta?.lastUpdated;
const date = rawLastUpdated ? new Date(rawLastUpdated) : '';
return (
<>
{`${date.toLocaleString()}`} {wasUpdated ? <WasUpdated>New updates</WasUpdated> : ''}
</>
);
},
},
{
title: 'Booking status',
key: 'status',
render: (_text: any, { appointment }: TableData) => {
return <Status>{appointment.status}</Status>;
}
},
{
title: 'Hospital',
key: 'Hospital',
render: with_status(
"Appointment.participant.where('22232009' in type.coding.code).actor.display",
"Appointment.participant.where('22232009' in type.coding.code).status"),
},
{
title: 'VMO',
key: 'practitioner-primary',
render: with_status(
"Appointment.participant.where('PPRF' in type.coding.code).actor.display",
"Appointment.participant.where('PPRF' in type.coding.code).status"),
},
{
title: 'Assistant',
key: 'practitioner-secondary',
render: with_status(
"Appointment.participant.where('SPRF' in type.coding.code).actor.display",
"Appointment.participant.where('SPRF' in type.coding.code).status"),
},
{
title: 'Anaesthetist',
key: 'anesthesiologist',
render: with_status(
"Appointment.participant.where('88189002' in type.coding.code).actor.display",
"Appointment.participant.where('88189002' in type.coding.code).status"),
},
{
title: 'Patient',
key: 'patient',
render: with_status(
"Appointment.participant.where('116154003' in type.coding.code).actor.display",
"Appointment.participant.where('116154003' in type.coding.code).status"),
},
{
title: 'Procedure',
key: 'service',
render: (_text: any, { appointment, bundle }: TableData) => {
const serviceRequestId = fhirpath.evaluate(appointment, "Appointment.basedOn.where(reference.split('/')[0] = 'ServiceRequest').reference.split('/')[1]")[0]
const sr: ServiceRequest = fhirpath.evaluate(bundle, `Bundle.entry.resource.where(id='${serviceRequestId}')`)[0];
const mainProcedure = sr.code?.text ?? 'Unknown';
const secondaryProcedure = fhirpath.evaluate(appointment, "Appointment.contained.where(resourceType='ServiceRequest' and id = %Appointment.supportingInformation.where(type='ServiceRequest').reference.substring(1,36)).code.text", { Appointment: appointment });
return <><Text strong>{mainProcedure}</Text><br />{secondaryProcedure}</>
},
},
{
title: 'Procedure Date & Time',
key: 'date',
render: (_text: any, { appointment, bundle }: TableData) => {
const serviceRequestId = fhirpath.evaluate(appointment, "Appointment.basedOn.where(reference.split('/')[0] = 'ServiceRequest').reference.split('/')[1]")[0]
const sr: ServiceRequest = fhirpath.evaluate(bundle, `Bundle.entry.resource.where(id='${serviceRequestId}')`)[0];
return new Date(sr.occurrenceDateTime!).toLocaleString();
},
sorter: () => 0,
},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment