Skip to content

Commit

Permalink
Implement Location Filtering in Patient Consultations (#1679)
Browse files Browse the repository at this point in the history
* Add location filtering to patient consultations

* fix location filter
  • Loading branch information
Ashesh3 authored Oct 30, 2023
1 parent 5428d66 commit 92b99fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def filter_by_category(self, queryset, name, value):
last_consultation_medico_legal_case = filters.BooleanFilter(
field_name="last_consultation__medico_legal_case"
)
last_consultation_current_bed__location = filters.UUIDFilter(
field_name="last_consultation__current_bed__bed__location__external_id"
)

def filter_by_bed_type(self, queryset, name, value):
if not value:
Expand Down
18 changes: 17 additions & 1 deletion care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def setUpTestData(cls):
cls.local_body = cls.create_local_body(cls.district)
cls.super_user = cls.create_super_user("su", cls.district)
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
cls.location = cls.create_asset_location(cls.facility)
cls.user = cls.create_user(
"doctor1", cls.district, home_facility=cls.facility, user_type=15
)
Expand All @@ -217,13 +218,28 @@ def setUpTestData(cls):
suggestion="A",
admission_date=now(),
)
cls.bed = cls.create_bed(cls.facility, cls.location)
cls.consultation_bed = cls.create_consultation_bed(cls.consultation, cls.bed)
cls.consultation.current_bed = cls.consultation_bed
cls.consultation.save()
cls.patient.last_consultation = cls.consultation
cls.patient.save()

def test_filter_by_patient_no(self):
self.client.force_authenticate(user=self.user)
response = self.client.get("/api/v1/patient/?patient_no=IP5678")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"][0]["id"], str(self.patient.external_id)
)

def test_filter_by_location(self):
self.client.force_authenticate(user=self.user)
response = self.client.get(
f"/api/v1/patient/?facility={self.facility.external_id}&location={self.location.external_id}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"][0]["id"], str(self.patient.external_id)
Expand Down
28 changes: 28 additions & 0 deletions care/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
User,
)
from care.facility.models.asset import Asset, AssetLocation
from care.facility.models.bed import Bed, ConsultationBed
from care.facility.models.facility import FacilityUser
from care.users.models import District, State

Expand Down Expand Up @@ -352,6 +353,33 @@ def create_asset(cls, location: AssetLocation, **kwargs) -> Asset:
data.update(kwargs)
return Asset.objects.create(**data)

@classmethod
def create_bed(cls, facility: Facility, location: AssetLocation, **kwargs):
data = {
"bed_type": 1,
"description": "Sample bed",
"facility": facility,
"location": location,
"name": "Test Bed",
}
data.update(kwargs)
return Bed.objects.create(**data)

@classmethod
def create_consultation_bed(
cls,
consultation: PatientConsultation,
bed: Bed,
**kwargs,
):
data = {
"bed": bed,
"consultation": consultation,
"start_date": make_aware(datetime(2020, 4, 1, 15, 30)),
}
data.update(kwargs)
return ConsultationBed.objects.create(**data)

@classmethod
def clone_object(cls, obj, save=True):
new_obj = obj._meta.model.objects.get(pk=obj.id)
Expand Down

0 comments on commit 92b99fd

Please sign in to comment.