Skip to content

Commit

Permalink
Adds validation for max dosage in PRN prescription ohcnetwork#8324 (o…
Browse files Browse the repository at this point in the history
…hcnetwork#8378)

Co-authored-by: rithviknishad <[email protected]>
  • Loading branch information
Nithin9585 and rithviknishad authored Sep 2, 2024
1 parent 3c0d9ed commit 6245493
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
43 changes: 31 additions & 12 deletions src/Components/Medicine/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,56 @@ import { Prescription } from "./models";
export const PrescriptionFormValidator = () => {
return (form: Prescription): FormErrors<Prescription> => {
const errors: Partial<Record<keyof Prescription, FieldError>> = {};

errors.medicine_object = RequiredFieldValidator()(form.medicine_object);

if (form.dosage_type === "TITRATED") {
errors.base_dosage = RequiredFieldValidator()(form.base_dosage);
errors.target_dosage = RequiredFieldValidator()(form.target_dosage);

if (
form.base_dosage &&
form.target_dosage &&
form.base_dosage.split(" ")[1] !== form.target_dosage.split(" ")[1]
) {
errors.base_dosage = "Unit must be same as target dosage's unit";
errors.target_dosage = "Unit must be same as base dosage's unit";
errors.base_dosage = t("inconsistent_dosage_units_error");
errors.target_dosage = t("inconsistent_dosage_units_error");
}
} else errors.base_dosage = RequiredFieldValidator()(form.base_dosage);
if (form.dosage_type === "PRN")
} else {
errors.base_dosage = RequiredFieldValidator()(form.base_dosage);
}

if (form.dosage_type === "PRN") {
errors.indicator = RequiredFieldValidator()(form.indicator);
if (form.dosage_type !== "PRN")

const baseDosageValue = getDosageValue(form.base_dosage);
const maxDosageValue = getDosageValue(form.max_dosage);

if (
baseDosageValue &&
maxDosageValue &&
baseDosageValue > maxDosageValue
) {
errors.max_dosage = t("max_dosage_in_24hrs_gte_base_dosage_error");
}
} else {
errors.frequency = RequiredFieldValidator()(form.frequency);
}

return errors;
};
};

const getDosageValue = (dosage: string | undefined) => {
return dosage ? Number(dosage.split(" ")[0]) : undefined;
};

export const EditPrescriptionFormValidator = (old: Prescription) => {
return (form: Prescription): FormErrors<Prescription> => {
const errors = PrescriptionFormValidator()(form);

if (comparePrescriptions(old, form)) {
errors.$all = "No changes made";
errors.$all = t("no_changes_made");
}

return errors;
Expand Down Expand Up @@ -66,24 +89,20 @@ export const AdministrationDosageValidator = (
target_dosage: Prescription["target_dosage"],
) => {
return (value: Prescription["base_dosage"]) => {
const getDosageValue = (dosage: string | undefined) => {
return dosage ? Number(dosage.split(" ")[0]) : undefined;
};

const valueDosage = getDosageValue(value);
const baseDosage = getDosageValue(base_dosage);
const targetDosage = getDosageValue(target_dosage);

if (!valueDosage) return t("field_required");

if (value?.split(" ")[1] !== base_dosage?.split(" ")[1])
return "Unit must be the same as start and target dosage's unit";
return t("inconsistent_dosage_units_error");

if (baseDosage && targetDosage) {
const [min, max] = [baseDosage, targetDosage].sort((a, b) => a - b);

if (!(min <= valueDosage && valueDosage <= max)) {
return "Dosage should be between start and target dosage";
return t("administration_dosage_range_error");
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/Locale/en/Common.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,6 @@
"discard": "Discard",
"live": "Live",
"discharged": "Discharged",
"archived": "Archived"
"archived": "Archived",
"no_changes_made": "No changes made"
}
7 changes: 5 additions & 2 deletions src/Locale/en/Medicine.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
"PRESCRIPTION_FREQUENCY_QID": "6th hourly",
"PRESCRIPTION_FREQUENCY_Q4H": "4th hourly",
"PRESCRIPTION_FREQUENCY_QOD": "Alternate day",
"PRESCRIPTION_FREQUENCY_QWK": "Once a week"
}
"PRESCRIPTION_FREQUENCY_QWK": "Once a week",
"inconsistent_dosage_units_error": "Dosage units must be same",
"max_dosage_in_24hrs_gte_base_dosage_error": "Max. dosage in 24 hours must be greater than or equal to base dosage",
"administration_dosage_range_error": "Dosage should be between start and target dosage"
}

0 comments on commit 6245493

Please sign in to comment.