diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py index 2fe2e08954..6caaafaa42 100644 --- a/care/facility/api/viewsets/patient.py +++ b/care/facility/api/viewsets/patient.py @@ -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: diff --git a/care/facility/tests/test_patient_api.py b/care/facility/tests/test_patient_api.py index b4885b9fe8..793308dcb4 100644 --- a/care/facility/tests/test_patient_api.py +++ b/care/facility/tests/test_patient_api.py @@ -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 ) @@ -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) diff --git a/care/utils/tests/test_utils.py b/care/utils/tests/test_utils.py index d2b19705d1..cd95e02299 100644 --- a/care/utils/tests/test_utils.py +++ b/care/utils/tests/test_utils.py @@ -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 @@ -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)