Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add consultation_filed filter in PatientFilterSet #1789

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.contrib.postgres.search import TrigramSimilarity
from django.db import models
from django.db.models import Case, When
from django.db.models import Case, F, When
from django.db.models.query_utils import Q
from django_filters import rest_framework as filters
from djqscsv import render_to_csv_response
Expand Down Expand Up @@ -95,6 +95,21 @@ class PatientFilterSet(filters.FilterSet):
method="filter_by_category",
choices=CATEGORY_CHOICES,
)
consultation_filed = filters.BooleanFilter(method="filter_consultation_filed")

def filter_consultation_filed(self, queryset, name, value):
consultation_condition = Q(last_consultation__isnull=True) | (
Q(last_consultation__discharge_date__isnull=False) & Q(is_active=True)
)

if value:
return queryset.filter(
last_consultation__facility__id=F("facility__id")
).exclude(consultation_condition)
else:
return queryset.exclude(
last_consultation__facility__id=F("facility__id")
).filter(consultation_condition)

def filter_by_category(self, queryset, name, value):
if value:
Expand Down
12 changes: 12 additions & 0 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ def test_filter_by_location(self):
response.data["results"][0]["id"], str(self.patient.external_id)
)

def test_filter_by_consultation_filed(self):
self.client.force_authenticate(user=self.user)
response_truthy = self.client.get("/api/v1/patient/?consultation_filed=true")
self.assertEqual(response_truthy.status_code, status.HTTP_200_OK)
self.assertEqual(response_truthy.data["count"], 1)
self.assertEqual(
response_truthy.data["results"][0]["id"], str(self.patient.external_id)
)
response_falsy = self.client.get("/api/v1/patient/?consultation_filed=false")
self.assertEqual(response_falsy.status_code, status.HTTP_200_OK)
self.assertEqual(response_falsy.data["count"], 0)


class PatientTransferTestCase(TestUtils, APITestCase):
@classmethod
Expand Down
Loading