Skip to content

Commit

Permalink
add created_by_local_user_content field for patients notes (#1445)
Browse files Browse the repository at this point in the history
* replace id with external_id for home_facility in UserBaseMinimumSerializer

* lint code base

* Used ExternalIdSerializerField for external_id

* added created_by_local_user field

* updated tests for patient note

* added test for user from different facility

---------

Co-authored-by: Vignesh Hari <[email protected]>
Co-authored-by: Aakash Singh <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2023
1 parent 81c838e commit 5275faf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
9 changes: 8 additions & 1 deletion care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def save(self, **kwargs):
class PatientNotesSerializer(serializers.ModelSerializer):
facility = FacilityBasicInfoSerializer(read_only=True)
created_by_object = UserBaseMinimumSerializer(source="created_by", read_only=True)
created_by_local_user = serializers.BooleanField(read_only=True)

def validate_empty_values(self, data):
if not data.get("note", "").strip():
Expand All @@ -463,5 +464,11 @@ def validate_empty_values(self, data):

class Meta:
model = PatientNotes
fields = ("note", "facility", "created_by_object", "created_date")
fields = (
"note",
"facility",
"created_by_object",
"created_by_local_user",
"created_date",
)
read_only_fields = ("created_date",)
13 changes: 12 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 BooleanField, Case, F, Value, 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 @@ -595,6 +595,16 @@ class PatientNotesViewSet(
queryset = (
PatientNotes.objects.all()
.select_related("facility", "patient", "created_by")
.annotate(
created_by_local_user=Case(
When(
created_by__home_facility__external_id=F("facility__external_id"),
then=Value(True),
),
default=Value(False),
output_field=BooleanField(),
)
)
.order_by("-created_date")
)
serializer_class = PatientNotesSerializer
Expand All @@ -617,6 +627,7 @@ def get_queryset(self):
q_filters |= Q(patient__last_consultation__assigned_to=user)
q_filters |= Q(patient__assigned_to=user)
queryset = queryset.filter(q_filters)

return queryset

def perform_create(self, serializer):
Expand Down
29 changes: 25 additions & 4 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ExpectedPatientNoteKeys(Enum):
FACILITY = "facility"
CREATED_BY_OBJECT = "created_by_object"
CREATED_DATE = "created_date"
CREATED_BY_LOCAL_USER = "created_by_local_user"


class ExpectedFacilityKeys(Enum):
Expand Down Expand Up @@ -71,7 +72,6 @@ class ExpectedCreatedByObjectKeys(Enum):
LAST_NAME = "last_name"
USER_TYPE = "user_type"
LAST_LOGIN = "last_login"
HOME_FACILITY = "home_facility"


class PatientNotesTestCase(TestBase, TestClassMixin, APITestCase):
Expand All @@ -85,11 +85,23 @@ def setUp(self):
# Create users and facility
self.user = self.create_user(district=district, username="test user")
facility = self.create_facility(district=district, user=self.user)
self.user.home_facility = facility
self.user.save()

# Create another user from different facility
self.user2 = self.create_user(district=district, username="test user 2")
facility2 = self.create_facility(district=district, user=self.user2)
self.user2.home_facility = facility2
self.user2.save()

self.patient = self.create_patient(district=district.id)

self.patient_note = self.create_patient_note(
patient=self.patient, facility=facility
patient=self.patient, facility=facility, created_by=self.user
)

self.patient_note2 = self.create_patient_note(
patient=self.patient, facility=facility, created_by=self.user2
)

refresh_token = RefreshToken.for_user(self.user)
Expand All @@ -103,14 +115,23 @@ def test_patient_notes(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIsInstance(response.json()["results"], list)

# Ensure only necessary data is being sent and no extra data
# Test created_by_local_user field if user is not from same facility as patient
data2 = response.json()["results"][0]

data = response.json()["results"][0]
created_by_local_user_content2 = data2["created_by_local_user"]
self.assertEqual(created_by_local_user_content2, False)

# Ensure only necessary data is being sent and no extra data
data = response.json()["results"][1]

self.assertCountEqual(
data.keys(), [item.value for item in ExpectedPatientNoteKeys]
)

created_by_local_user_content = data["created_by_local_user"]

self.assertEqual(created_by_local_user_content, True)

facility_content = data["facility"]

if facility_content is not None:
Expand Down
1 change: 0 additions & 1 deletion care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ class Meta:
"last_name",
"user_type",
"last_login",
"home_facility",
)


Expand Down

0 comments on commit 5275faf

Please sign in to comment.