Skip to content

Commit

Permalink
Merge pull request #2694 from ohcnetwork/medicine_administration
Browse files Browse the repository at this point in the history
Fixed encounter validation in medicine administration spec
  • Loading branch information
vigneshhari authored Dec 31, 2024
2 parents 363e40d + e843369 commit 7c6dc73
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
6 changes: 3 additions & 3 deletions care/emr/resources/medication/administration/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,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


Expand Down Expand Up @@ -182,7 +182,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
Expand Down Expand Up @@ -215,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
Expand Down
72 changes: 68 additions & 4 deletions care/emr/resources/medication/request/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7c6dc73

Please sign in to comment.