Skip to content

Commit

Permalink
Create better patient retrieve API
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Dec 28, 2024
1 parent 12b0539 commit bfb5e55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions care/emr/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from django_filters.rest_framework import DjangoFilterBackend
from pydantic import BaseModel
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response

from care.emr.api.viewsets.base import EMRModelViewSet
from care.emr.models.patient import Patient
from care.emr.resources.patient.spec import (
PatientCreateSpec,
PatientListSpec,
PatientPartialSpec,
PatientRetrieveSpec,
)
from care.security.authorization import AuthorizationController
Expand Down Expand Up @@ -60,7 +62,20 @@ def search(self, request, *args, **kwargs):
queryset = (queryset.filter(name__icontains=request_data.name))[
:max_page_size
]
data = [
self.get_read_pydantic_model().serialize(obj).to_json() for obj in queryset
]
data = [PatientPartialSpec.serialize(obj).to_json() for obj in queryset]
return Response({"results": data})

class SearchRetrieveRequestSpec(BaseModel):
phone_number: str
year_of_birth: int
partial_id: str

@action(detail=False, methods=["POST"])
def search_retrieve(self, request, *args, **kwargs):
request_data = self.SearchRetrieveRequestSpec(**request.data)
queryset = Patient.objects.filter(phone_number=request_data.phone_number)
queryset = queryset.filter(year_of_birth=request_data.year_of_birth)
for patient in queryset:
if str(patient.external_id)[:5] == request_data.partial_id:
return Response(PatientRetrieveSpec.serialize(patient).to_json())
raise PermissionDenied("No valid patients found")
17 changes: 17 additions & 0 deletions care/emr/resources/patient/spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import uuid
from enum import Enum

from django.utils import timezone
Expand All @@ -22,6 +23,7 @@ class BloodGroupChoices(str, Enum):
O_positive = "O_positive"
unknown = "unknown"


class GenderChoices(str, Enum):
male = "male"
female = "female"
Expand Down Expand Up @@ -82,6 +84,21 @@ def perform_extra_serialization(cls, mapping, obj):
mapping["id"] = obj.external_id


class PatientPartialSpec(EMRResource):
__model__ = Patient

id: UUID4 | None = None
name: str
gender: GenderChoices
phone_number: str
partial_id: str

@classmethod
def perform_extra_serialization(cls, mapping, obj):
mapping["partial_id"] = str(obj.external_id)[:5]
mapping["id"] = str(uuid.uuid4())


class PatientRetrieveSpec(PatientListSpec):
geo_organization: dict = {}

Expand Down

0 comments on commit bfb5e55

Please sign in to comment.