From 85b416a392ae04cd3c7105f9fdee61b2bedeed67 Mon Sep 17 00:00:00 2001 From: Nithin Date: Wed, 21 Aug 2024 00:44:47 +0530 Subject: [PATCH 1/3] Adds validation for max dosage in PRN prescription #8324 --- src/Components/Medicine/validators.ts | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Components/Medicine/validators.ts b/src/Components/Medicine/validators.ts index a0103d11241..2ff36b33f4d 100644 --- a/src/Components/Medicine/validators.ts +++ b/src/Components/Medicine/validators.ts @@ -6,27 +6,51 @@ import { Prescription } from "./models"; export const PrescriptionFormValidator = () => { return (form: Prescription): FormErrors => { const errors: Partial> = {}; + 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 = "Unit must be the same as target dosage's unit"; + errors.target_dosage = "Unit must be the same as base dosage's unit"; } - } 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 = + "Max dosage in 24 hours must be greater than or equal to base dosage"; + } + } 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 => { const errors = PrescriptionFormValidator()(form); @@ -66,10 +90,6 @@ 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); From fac73c424cb76f7be542772c546287f2cbf51434 Mon Sep 17 00:00:00 2001 From: Nithin Date: Sat, 24 Aug 2024 18:19:43 +0530 Subject: [PATCH 2/3] localization for error messages --- src/Components/Medicine/validators.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Medicine/validators.ts b/src/Components/Medicine/validators.ts index 2ff36b33f4d..34b4e573a94 100644 --- a/src/Components/Medicine/validators.ts +++ b/src/Components/Medicine/validators.ts @@ -18,8 +18,8 @@ export const PrescriptionFormValidator = () => { form.target_dosage && form.base_dosage.split(" ")[1] !== form.target_dosage.split(" ")[1] ) { - errors.base_dosage = "Unit must be the same as target dosage's unit"; - errors.target_dosage = "Unit must be the same as base dosage's unit"; + errors.base_dosage = t("unit_must_be_same_as_target_dosage_unit"); + errors.target_dosage = t("unit_must_be_same_as_base_dosage_unit"); } } else { errors.base_dosage = RequiredFieldValidator()(form.base_dosage); From a50f7b11e9642e14a769b83dd443deda28e48f57 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 2 Sep 2024 13:18:23 +0530 Subject: [PATCH 3/3] update i18n's --- src/Components/Medicine/validators.ts | 13 ++++++------- src/Locale/en/Common.json | 3 ++- src/Locale/en/Medicine.json | 7 +++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Components/Medicine/validators.ts b/src/Components/Medicine/validators.ts index 34b4e573a94..c1cbe560ae9 100644 --- a/src/Components/Medicine/validators.ts +++ b/src/Components/Medicine/validators.ts @@ -18,8 +18,8 @@ export const PrescriptionFormValidator = () => { form.target_dosage && form.base_dosage.split(" ")[1] !== form.target_dosage.split(" ")[1] ) { - errors.base_dosage = t("unit_must_be_same_as_target_dosage_unit"); - errors.target_dosage = t("unit_must_be_same_as_base_dosage_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); @@ -36,8 +36,7 @@ export const PrescriptionFormValidator = () => { maxDosageValue && baseDosageValue > maxDosageValue ) { - errors.max_dosage = - "Max dosage in 24 hours must be greater than or equal to base dosage"; + errors.max_dosage = t("max_dosage_in_24hrs_gte_base_dosage_error"); } } else { errors.frequency = RequiredFieldValidator()(form.frequency); @@ -56,7 +55,7 @@ export const EditPrescriptionFormValidator = (old: Prescription) => { const errors = PrescriptionFormValidator()(form); if (comparePrescriptions(old, form)) { - errors.$all = "No changes made"; + errors.$all = t("no_changes_made"); } return errors; @@ -97,13 +96,13 @@ export const AdministrationDosageValidator = ( 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"); } } }; diff --git a/src/Locale/en/Common.json b/src/Locale/en/Common.json index 62437fcf1ed..c5d6d76564e 100644 --- a/src/Locale/en/Common.json +++ b/src/Locale/en/Common.json @@ -191,5 +191,6 @@ "discard": "Discard", "live": "Live", "discharged": "Discharged", - "archived": "Archived" + "archived": "Archived", + "no_changes_made": "No changes made" } \ No newline at end of file diff --git a/src/Locale/en/Medicine.json b/src/Locale/en/Medicine.json index 3e861f2e486..df0467de7b6 100644 --- a/src/Locale/en/Medicine.json +++ b/src/Locale/en/Medicine.json @@ -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" +} \ No newline at end of file