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

Medication Request | Convert dosage instruction to array #9665

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/Routers/routes/ConsultationRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import PatientConsentRecords from "@/components/Patient/PatientConsentRecords";

import { AppRoutes } from "@/Routers/AppRouter";
import { EncounterShow } from "@/pages/Encounters/EncounterShow";
import { PrintPrescription } from "@/pages/Encounters/PrintPrescription";

const consultationRoutes: AppRoutes = {
"/facility/:facilityId/encounter/:encounterId/prescriptions": ({
facilityId,
encounterId,
}) => <PrintPrescription facilityId={facilityId} encounterId={encounterId} />,
"/facility/:facilityId/encounter/:encounterId/:tab": ({
facilityId,
encounterId,
Expand Down
10 changes: 5 additions & 5 deletions src/components/Medicine/AdministerMedicine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function AdministerMedicine({ prescription, onClose }: Props) {
const encounterId = useSlug("encounter");
const { patient } = useEncounter();

const doseAndRate = prescription.dosage_instruction.dose_and_rate!;
const doseAndRate = prescription.dosage_instruction[0].dose_and_rate!;
const requiresDosage = doseAndRate.type === "calculated";

const formSchema = z
Expand Down Expand Up @@ -154,10 +154,10 @@ export default function AdministerMedicine({ prescription, onClose }: Props) {
occurrence_period_end: time.toISOString(),
note: values.note,
dosage: {
text: prescription.dosage_instruction.text,
site: prescription.dosage_instruction.site,
route: prescription.dosage_instruction.route,
method: prescription.dosage_instruction.method,
text: prescription.dosage_instruction[0].text,
site: prescription.dosage_instruction[0].site,
route: prescription.dosage_instruction[0].route,
method: prescription.dosage_instruction[0].method,
dose: {
value: doseValue!,
unit: doseUnit!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function MedicineAdministrationTable({
<span className="hidden px-2 text-center text-xs leading-none lg:block">
<p>{t("dosage")} &</p>
<p>
{!prescriptions[0]?.dosage_instruction.as_needed_boolean
{!prescriptions[0]?.dosage_instruction[0].as_needed_boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Check for potential index error when accessing the first dosage instruction

Referencing dosage_instruction[0] could lead to runtime errors if the array is empty. A simple guard check is recommended:

- {!prescriptions[0]?.dosage_instruction[0].as_needed_boolean
+ {!prescriptions[0]?.dosage_instruction?.length &&
+  !prescriptions[0].dosage_instruction[0].as_needed_boolean

Committable suggestion skipped: line range outside the PR's diff.

gigincg marked this conversation as resolved.
Show resolved Hide resolved
? t("frequency")
: t("indicator")}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export default function MedicineAdministrationTableRow({
</span>
)}

{prescription.dosage_instruction.route && (
{prescription.dosage_instruction[0].route && (
<span className="hidden rounded-full border border-blue-500 bg-blue-100 px-1.5 text-xs font-medium text-blue-700 lg:block">
{displayCode(prescription.dosage_instruction.route)}
{displayCode(prescription.dosage_instruction[0].route)}
Comment on lines +148 to +150
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Safely access prescription.dosage_instruction[0].route

As with other components, ensure that the dosage_instruction array is not empty before accessing [0].

- {prescription.dosage_instruction[0].route && (
+ {prescription.dosage_instruction?.length &&
+  prescription.dosage_instruction[0].route && (
     <span> ... </span>
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{prescription.dosage_instruction[0].route && (
<span className="hidden rounded-full border border-blue-500 bg-blue-100 px-1.5 text-xs font-medium text-blue-700 lg:block">
{displayCode(prescription.dosage_instruction.route)}
{displayCode(prescription.dosage_instruction[0].route)}
{prescription.dosage_instruction?.length &&
prescription.dosage_instruction[0].route && (
<span className="hidden rounded-full border border-blue-500 bg-blue-100 px-1.5 text-xs font-medium text-blue-700 lg:block">
{displayCode(prescription.dosage_instruction[0].route)}

</span>
)}
</div>
Expand Down Expand Up @@ -217,7 +217,7 @@ type DosageFrequencyInfoProps = {
export function DosageFrequencyInfo({
prescription,
}: DosageFrequencyInfoProps) {
const dosageInstruction = prescription.dosage_instruction;
const dosageInstruction = prescription.dosage_instruction[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add null-check for dosage_instruction array

In DosageFrequencyInfo, referencing prescription.dosage_instruction[0] directly can cause errors if the array is empty. Consider a guard to avoid potential runtime issues.

- const dosageInstruction = prescription.dosage_instruction[0];
+ const dosageInstruction = prescription.dosage_instruction?.[0];
+ if (!dosageInstruction) {
+   return null; // or a placeholder
+ }

Committable suggestion skipped: line range outside the PR's diff.


return (
<div className="flex justify-center">
Expand Down
21 changes: 11 additions & 10 deletions src/components/Medicine/PrescriptionDetailCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function PrescriptionDetailCard({
{prescription.category === "discharge" &&
`${t("discharge")} `}
{t(
prescription.dosage_instruction.as_needed_boolean
prescription.dosage_instruction[0].as_needed_boolean
? "prn_prescription"
: "prescription",
)}
Expand Down Expand Up @@ -146,30 +146,31 @@ export default function PrescriptionDetailCard({
</div>
</Detail>

{prescription.dosage_instruction.dose_and_rate?.dose_range && (
{prescription.dosage_instruction[0].dose_and_rate?.dose_range && (
<>
<Detail className="col-span-5" label={t("start_dosage")}>
{displayQuantity(
prescription.dosage_instruction.dose_and_rate?.dose_range
prescription.dosage_instruction[0].dose_and_rate?.dose_range
?.low as Quantity,
)}
</Detail>
<Detail className="col-span-5" label={t("target_dosage")}>
{displayQuantity(
prescription.dosage_instruction.dose_and_rate?.dose_range
prescription.dosage_instruction[0].dose_and_rate?.dose_range
?.high as Quantity,
)}
</Detail>
</>
)}

{prescription.dosage_instruction.dose_and_rate?.dose_quantity && (
{prescription.dosage_instruction[0].dose_and_rate
?.dose_quantity && (
<Detail
className="col-span-10 sm:col-span-6 md:col-span-4"
label={t("dosage")}
>
{displayQuantity(
prescription.dosage_instruction.dose_and_rate
prescription.dosage_instruction[0].dose_and_rate
?.dose_quantity as Quantity,
)}
</Detail>
Expand All @@ -179,19 +180,19 @@ export default function PrescriptionDetailCard({
className="col-span-10 break-all sm:col-span-6"
label={t("route")}
>
{displayCode(prescription.dosage_instruction.route)}
{displayCode(prescription.dosage_instruction[0].route)}
</Detail>

{prescription.dosage_instruction.as_needed_boolean ? (
{prescription.dosage_instruction[0].as_needed_boolean ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Validate PRN fields together
When as_needed_boolean is true, as_needed_for should be required. Consider adding validation in the parent form or data model to ensure correct usage of these fields.

Also applies to: 191-191

<Detail
className="col-span-10 md:col-span-4"
label={t("indicator")}
>
{displayCode(prescription.dosage_instruction.as_needed_for)}
{displayCode(prescription.dosage_instruction[0].as_needed_for)}
</Detail>
) : (
<Detail className="col-span-5" label={t("frequency")}>
{displayTiming(prescription.dosage_instruction.timing)}
{displayTiming(prescription.dosage_instruction[0].timing)}
</Detail>
)}

Expand Down
16 changes: 8 additions & 8 deletions src/components/Medicine/PrescriptionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ export default function PrescriptionsTable({
...obj,
medicine: displayCode(obj.medication),
route__pretty:
obj.dosage_instruction.route &&
displayCode(obj.dosage_instruction.route),
obj.dosage_instruction[0].route &&
displayCode(obj.dosage_instruction[0].route),
frequency__pretty:
obj.dosage_instruction.timing &&
displayTiming(obj.dosage_instruction.timing),
obj.dosage_instruction[0].timing &&
displayTiming(obj.dosage_instruction[0].timing),
max_dose_per_period__pretty:
obj.dosage_instruction.max_dose_per_period &&
obj.dosage_instruction[0].max_dose_per_period &&
displayDoseRange(
obj.dosage_instruction.max_dose_per_period,
obj.dosage_instruction[0].max_dose_per_period,
),
indicator:
obj.dosage_instruction.as_needed_for &&
displayCode(obj.dosage_instruction.as_needed_for),
obj.dosage_instruction[0].as_needed_for &&
displayCode(obj.dosage_instruction[0].as_needed_for),
Comment on lines +94 to +106
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Guard against empty dosage_instruction arrays

All these references to obj.dosage_instruction[0] assume at least one element is present. It’s advisable to gracefully handle or fallback if the array is empty. For instance:

- route__pretty:
-   obj.dosage_instruction[0].route &&
-   displayCode(obj.dosage_instruction[0].route),
+ route__pretty:
+   obj.dosage_instruction?.length &&
+   obj.dosage_instruction[0].route &&
+   displayCode(obj.dosage_instruction[0].route),

Apply a similar pattern for the other properties (timing, max_dose_per_period, as_needed_for) to avoid runtime errors.

Committable suggestion skipped: line range outside the PR's diff.

})) || []
}
objectKeys={Object.values(tkeys)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ValueSetSelect from "@/components/Questionnaire/ValueSetSelect";
import {
MEDICATION_REQUEST_INTENT,
MedicationRequest,
MedicationRequestDosageInstruction,
MedicationRequestIntent,
} from "@/types/emr/medicationRequest";
import { Code } from "@/types/questionnaire/code";
Expand All @@ -41,7 +42,7 @@ const MEDICATION_REQUEST_INITIAL_VALUE: MedicationRequest = {
do_not_perform: false,
medication: undefined,
authored_on: new Date().toISOString(),
dosage_instruction: {},
dosage_instruction: [],
};

export function MedicationRequestQuestion({
Expand All @@ -56,7 +57,11 @@ export function MedicationRequestQuestion({
const handleAddMedication = (medication: Code) => {
const newMedications: MedicationRequest[] = [
...medications,
{ ...MEDICATION_REQUEST_INITIAL_VALUE, medication },
{
...MEDICATION_REQUEST_INITIAL_VALUE,
medication,
dosage_instruction: [],
},
];
updateQuestionnaireResponseCB({
...questionnaireResponse,
Expand Down Expand Up @@ -138,12 +143,12 @@ export const MedicationRequestItem: React.FC<{
onRemove?: () => void;
index?: number;
}> = ({ medication, disabled, onUpdate, onRemove, index = 0 }) => {
const dosageInstruction = medication.dosage_instruction;
const dosageInstruction = medication.dosage_instruction[0];
const handleUpdateDosageInstruction = (
updates: Partial<MedicationRequest["dosage_instruction"]>,
updates: Partial<MedicationRequestDosageInstruction>,
) => {
onUpdate?.({
dosage_instruction: { ...dosageInstruction, ...updates },
dosage_instruction: [{ ...dosageInstruction, ...updates }],
});
};

Expand Down Expand Up @@ -198,7 +203,7 @@ export const MedicationRequestItem: React.FC<{
<QuantityInput
units={DOSAGE_UNITS}
quantity={
medication.dosage_instruction?.dose_and_rate?.dose_quantity
medication.dosage_instruction[0]?.dose_and_rate?.dose_quantity
}
onChange={(value) =>
handleUpdateDosageInstruction({
Expand All @@ -212,7 +217,7 @@ export const MedicationRequestItem: React.FC<{
<Label className="mb-1 block text-sm font-medium">Route</Label>
<ValueSetSelect
system="system-route"
value={medication.dosage_instruction?.route}
value={medication.dosage_instruction[0]?.route}
onSelect={(route) => handleUpdateDosageInstruction({ route })}
placeholder="Select route"
disabled={disabled}
Expand All @@ -224,7 +229,7 @@ export const MedicationRequestItem: React.FC<{
</Label>
<ValueSetSelect
system="system-administration-method"
value={medication.dosage_instruction?.method}
value={medication.dosage_instruction[0]?.method}
onSelect={(method) => handleUpdateDosageInstruction({ method })}
placeholder="Select method"
disabled={disabled}
Expand All @@ -238,7 +243,7 @@ export const MedicationRequestItem: React.FC<{
<Label className="mb-1 block text-sm font-medium pr-4">Site</Label>
<ValueSetSelect
system="system-body-site"
value={medication.dosage_instruction?.site}
value={medication.dosage_instruction[0]?.site}
onSelect={(site) => handleUpdateDosageInstruction({ site })}
placeholder="Select site"
disabled={disabled}
Expand All @@ -249,12 +254,14 @@ export const MedicationRequestItem: React.FC<{
<div className="flex items-center gap-2 mt-2">
<Checkbox
id={`prn-checkbox-${medication.medication?.code}`}
checked={medication.dosage_instruction?.as_needed_boolean ?? false}
checked={
medication.dosage_instruction[0]?.as_needed_boolean ?? false
}
onCheckedChange={(checked) =>
handleUpdateDosageInstruction({
as_needed_boolean: !!checked,
as_needed_for: checked
? medication.dosage_instruction?.as_needed_for
? medication.dosage_instruction[0]?.as_needed_for
: undefined,
})
}
Expand All @@ -265,12 +272,12 @@ export const MedicationRequestItem: React.FC<{
</Label>
</div>

{medication.dosage_instruction?.as_needed_boolean ? (
{medication.dosage_instruction[0]?.as_needed_boolean ? (
<div className="flex gap-2">
<div className="flex-1">
<ValueSetSelect
system="system-as-needed-reason"
value={medication.dosage_instruction?.as_needed_for}
value={medication.dosage_instruction[0]?.as_needed_for}
onSelect={(reason) =>
handleUpdateDosageInstruction({ as_needed_for: reason })
}
Expand Down Expand Up @@ -320,13 +327,17 @@ export const MedicationRequestItem: React.FC<{
</Label>
<ValueSetSelect
system="system-additional-instruction"
value={medication.dosage_instruction?.additional_instruction?.[0]}
value={
medication.dosage_instruction[0]?.additional_instruction?.[0]
}
onSelect={(additionalInstruction) =>
onUpdate?.({
dosage_instruction: {
...medication.dosage_instruction,
additional_instruction: [additionalInstruction],
},
dosage_instruction: [
{
...medication.dosage_instruction[0],
additional_instruction: [additionalInstruction],
},
],
})
}
disabled={disabled}
Expand All @@ -343,7 +354,7 @@ export const MedicationRequestItem: React.FC<{
};

const reverseFrequencyOption = (
option: MedicationRequest["dosage_instruction"]["timing"],
option: MedicationRequest["dosage_instruction"][0]["timing"],
) => {
return Object.entries(FREQUENCY_OPTIONS).find(
([, value]) =>
Expand Down Expand Up @@ -395,6 +406,6 @@ const FREQUENCY_OPTIONS = {
string,
{
display: string;
timing: MedicationRequest["dosage_instruction"]["timing"];
timing: MedicationRequest["dosage_instruction"][0]["timing"];
}
>;
Loading
Loading