Skip to content

Commit

Permalink
fixed validation in medication request
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar committed Dec 31, 2024
1 parent 6a7ce7e commit e843369
Showing 1 changed file with 68 additions and 4 deletions.
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 e843369

Please sign in to comment.