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

chore: Add total doctors count to hospital doctor list response #2629

Closed
7 changes: 7 additions & 0 deletions care/facility/api/viewsets/hospital_doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.generics import get_object_or_404
from rest_framework.mixins import ListModelMixin
from rest_framework.permissions import IsAuthenticated
from django.db.models import Sum

from care.facility.api.serializers.hospital_doctor import HospitalDoctorSerializer
from care.facility.api.viewsets import FacilityBaseViewset
Expand Down Expand Up @@ -42,5 +43,11 @@ def get_facility(self):
facility_qs.filter(users__id__exact=self.request.user.id)
return get_object_or_404(facility_qs)

def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
total_doctors = self.get_queryset().aggregate(total_doctors=Sum('count'))['total_doctors']
response.data["total_doctors"] = total_doctors
return response

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle potential None value from aggregation

The aggregation might return None if there are no records, which could lead to unexpected behavior in the frontend.

Here's a slightly more robust implementation:

     def list(self, request, *args, **kwargs):
         response = super().list(request, *args, **kwargs)
-        total_doctors = self.get_queryset().aggregate(total_doctors=Sum("count"))["total_doctors"]
-        response.data["total_doctors"] = total_doctors
+        total_doctors = self.get_queryset().aggregate(total_doctors=Sum("count"))["total_doctors"]
+        response.data["total_doctors"] = total_doctors or 0
         return response
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
total_doctors = self.get_queryset().aggregate(total_doctors=Sum('count'))['total_doctors']
response.data["total_doctors"] = total_doctors
return response
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
total_doctors = self.get_queryset().aggregate(total_doctors=Sum("count"))["total_doctors"]
response.data["total_doctors"] = total_doctors or 0
return response
🧰 Tools
🪛 Ruff (0.8.0)

48-48: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)


48-48: Single quotes found but double quotes preferred

Replace single quotes with double quotes

(Q000)

def perform_create(self, serializer):
serializer.save(facility=self.get_facility())