diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py index f55a60030a..634035ab13 100644 --- a/care/facility/api/viewsets/patient.py +++ b/care/facility/api/viewsets/patient.py @@ -18,6 +18,7 @@ ) from django.db.models.functions import Coalesce, ExtractDay, Now from django.db.models.query import QuerySet +from django.utils import timezone from django_filters import rest_framework as filters from djqscsv import render_to_csv_response from drf_spectacular.utils import extend_schema, extend_schema_view @@ -237,6 +238,20 @@ def filter_bed_not_null(self, queryset, name, value): last_consultation__discharge_date__isnull=True, ) + def filter_by_review_missed(self, queryset, name, value): + if isinstance(value, bool): + if value: + queryset = queryset.filter( + (Q(review_time__isnull=False) & Q(review_time__lt=timezone.now())) + ) + else: + queryset = queryset.filter( + Q(review_time__isnull=True) | Q(review_time__gt=timezone.now()) + ) + return queryset + + review_missed = filters.BooleanFilter(method="filter_by_review_missed") + # Filter consultations by ICD-11 Diagnoses diagnoses = MultiSelectFilter(method="filter_by_diagnoses") diagnoses_unconfirmed = MultiSelectFilter(method="filter_by_diagnoses") diff --git a/care/facility/tests/test_patient_api.py b/care/facility/tests/test_patient_api.py index 01b017c34d..d86f7fdd20 100644 --- a/care/facility/tests/test_patient_api.py +++ b/care/facility/tests/test_patient_api.py @@ -409,6 +409,21 @@ def test_filter_by_diagnoses_confirmed(self): ) self.assertNotContains(res, self.patient.external_id) + def test_filter_by_review_missed(self): + self.client.force_authenticate(user=self.user) + res = self.client.get(self.get_base_url() + "?review_missed=true") + self.assertEqual(res.status_code, status.HTTP_200_OK) + for patient in res.json()["results"]: + self.assertLess(patient["review_time"], now()) + + res = self.client.get(self.get_base_url() + "?review_missed=false") + self.assertEqual(res.status_code, status.HTTP_200_OK) + for patient in res.json()["results"]: + if patient["review_time"]: + self.assertGreaterEqual(patient["review_time"], now()) + else: + self.assertIsNone(patient["review_time"]) + class PatientTransferTestCase(TestUtils, APITestCase): @classmethod