From 3e20678953d45fe8095b5454f19a0181dec8a81c Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Tue, 31 Dec 2024 17:36:35 +0530 Subject: [PATCH 1/3] fixed encounter validation in medicine administration spec --- care/emr/resources/medication/administration/spec.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/care/emr/resources/medication/administration/spec.py b/care/emr/resources/medication/administration/spec.py index 69af878154..6ed0006eb6 100644 --- a/care/emr/resources/medication/administration/spec.py +++ b/care/emr/resources/medication/administration/spec.py @@ -4,6 +4,7 @@ from pydantic import UUID4, BaseModel, Field, field_validator from care.emr.fhir.schema.base import Coding, Quantity +from care.emr.models.encounter import Encounter from care.emr.models.medication_administration import MedicationAdministration from care.emr.models.medication_request import MedicationRequest from care.emr.registries.care_valueset.care_valueset import validate_valueset @@ -182,7 +183,7 @@ class MedicationAdministrationSpec(BaseMedicationAdministrationSpec): @field_validator("encounter") @classmethod def validate_encounter_exists(cls, encounter): - if not PatientConsultation.objects.filter(external_id=encounter).exists(): + if not Encounter.objects.filter(external_id=encounter).exists(): err = "Encounter not found" raise ValueError(err) return encounter From 6a7ce7ee778caa290a6d3968b9e820b52500ee08 Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Tue, 31 Dec 2024 18:00:04 +0530 Subject: [PATCH 2/3] use correct model to serialize --- care/emr/resources/medication/administration/spec.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/care/emr/resources/medication/administration/spec.py b/care/emr/resources/medication/administration/spec.py index 6ed0006eb6..8a269ffc1e 100644 --- a/care/emr/resources/medication/administration/spec.py +++ b/care/emr/resources/medication/administration/spec.py @@ -16,7 +16,6 @@ from care.emr.resources.medication.valueset.medication import CARE_MEDICATION_VALUESET from care.emr.resources.medication.valueset.route import CARE_ROUTE_VALUESET from care.emr.resources.user.spec import UserSpec -from care.facility.models.patient_consultation import PatientConsultation from care.users.models import User @@ -216,7 +215,7 @@ def validate_status_reason(cls, code): def perform_extra_deserialization(self, is_update, obj): if not is_update: - obj.encounter = PatientConsultation.objects.get( + obj.encounter = Encounter.objects.get( external_id=self.encounter ) # Needs more validation obj.patient = obj.encounter.patient From e84336966a56a55678e4dcc5036c345d1ad9f475 Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Tue, 31 Dec 2024 19:26:27 +0530 Subject: [PATCH 3/3] fixed validation in medication request --- care/emr/resources/medication/request/spec.py | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/care/emr/resources/medication/request/spec.py b/care/emr/resources/medication/request/spec.py index bd8cf547dc..3cc2f24e4d 100644 --- a/care/emr/resources/medication/request/spec.py +++ b/care/emr/resources/medication/request/spec.py @@ -6,6 +6,7 @@ from care.emr.fhir.schema.base import Coding from care.emr.models.encounter import Encounter from care.emr.models.medication_request import MedicationRequest +from care.emr.registries.care_valueset.care_valueset import validate_valueset from care.emr.resources.base import EMRResource from care.emr.resources.medication.valueset.additional_instruction import ( CARE_ADDITIONAL_INSTRUCTION_VALUESET, @@ -127,18 +128,72 @@ class DosageInstruction(BaseModel): timing: Timing | None = None as_needed_boolean: bool | None = None as_needed_for: Coding | None = Field( - json_schema_extra={"slug": CARE_AS_NEEDED_REASON_VALUESET.slug} + None, json_schema_extra={"slug": CARE_AS_NEEDED_REASON_VALUESET.slug} ) site: Coding | None = Field( - json_schema_extra={"slug": CARE_BODY_SITE_VALUESET.slug} + None, json_schema_extra={"slug": CARE_BODY_SITE_VALUESET.slug} + ) + route: Coding | None = Field( + None, json_schema_extra={"slug": CARE_ROUTE_VALUESET.slug} ) - route: Coding | None = Field(json_schema_extra={"slug": CARE_ROUTE_VALUESET.slug}) method: Coding | None = Field( - json_schema_extra={"slug": CARE_ADMINISTRATION_METHOD_VALUESET.slug} + None, json_schema_extra={"slug": CARE_ADMINISTRATION_METHOD_VALUESET.slug} ) dose_and_rate: DoseAndRate | None = None max_dose_per_period: DoseRange | None = None + @field_validator("additional_instruction") + @classmethod + def validate_additional_instruction(cls, codes): + return [ + validate_valueset( + "additional_instruction", + cls.model_fields["additional_instruction"].json_schema_extra["slug"], + code, + ) + for code in codes + ] + + @field_validator("as_needed_for") + @classmethod + def validate_as_needed_for(cls, code, values): + if values.data.get("as_needed_boolean", False) and not code: + err = "as_needed_for is required when as_needed_boolean is True" + raise ValueError(err) + + return validate_valueset( + "as_needed_for", + cls.model_fields["as_needed_for"].json_schema_extra["slug"], + code, + ) + + @field_validator("site") + @classmethod + def validate_site(cls, code): + return validate_valueset( + "site", + cls.model_fields["site"].json_schema_extra["slug"], + code, + ) + + @field_validator("route") + @classmethod + def validate_route(cls, code): + return validate_valueset( + "route", + cls.model_fields["route"].json_schema_extra["slug"], + code, + ) + + @field_validator("method") + @classmethod + def validate_method(cls, code): + return validate_valueset( + "method", + cls.model_fields["method"].json_schema_extra["slug"], + code, + ) + class BaseMedicationRequestSpec(EMRResource): __model__ = MedicationRequest @@ -197,6 +252,15 @@ def validate_encounter_exists(cls, encounter): raise ValueError(err) return encounter + @field_validator("medication") + @classmethod + def validate_medication(cls, code): + return validate_valueset( + "medication", + cls.model_fields["medication"].json_schema_extra["slug"], + code, + ) + def perform_extra_deserialization(self, is_update, obj): if not is_update: obj.encounter = Encounter.objects.get(