diff --git a/care/emr/api/viewsets/scheduling/booking.py b/care/emr/api/viewsets/scheduling/booking.py index 08dba5f1a1..c86fa53d2a 100644 --- a/care/emr/api/viewsets/scheduling/booking.py +++ b/care/emr/api/viewsets/scheduling/booking.py @@ -1,4 +1,4 @@ -from django_filters import CharFilter, FilterSet, UUIDFilter +from django_filters import CharFilter, DateFilter, FilterSet, UUIDFilter from django_filters.rest_framework import DjangoFilterBackend from rest_framework.decorators import action from rest_framework.response import Response @@ -16,14 +16,26 @@ TokenBookingUpdateSpec, ) from care.emr.resources.user.spec import UserSpec -from care.facility.models import Facility, FacilityUser +from care.facility.models import Facility, FacilityOrganizationUser class TokenBookingFilters(FilterSet): status = CharFilter(field_name="status") + date = DateFilter(field_name="token_slot__start_datetime__date") slot = UUIDFilter(field_name="token_slot__external_id") + resource = UUIDFilter(method="filter_by_resource") patient = UUIDFilter(field_name="patient__external_id") + def filter_by_resource(self, queryset, name, value): + if not value: + return queryset + resource = SchedulableUserResource.objects.filter( + resource__external_id=value + ).first() + if not resource: + return queryset.none() + return queryset.filter(token_slot__resource=resource) + class TokenBookingViewSet( EMRRetrieveMixin, EMRUpdateMixin, EMRListMixin, EMRDeleteMixin, EMRBaseViewSet @@ -58,11 +70,11 @@ def get_queryset(self): @action(detail=False, methods=["GET"]) def available_doctors(self, request, *args, **kwargs): facility = Facility.objects.get(external_id=self.kwargs["facility_external_id"]) - facility_users = FacilityUser.objects.filter( + facility_users = FacilityOrganizationUser.objects.filter( + organization__facility=facility, user_id__in=SchedulableUserResource.objects.filter( facility=facility ).values("resource_id"), - facility=facility, ) return Response(