Skip to content

Commit

Permalink
Made doctor role as static attribute (#1507)
Browse files Browse the repository at this point in the history
* made doctor role as static attribute

* renamed create_by_local_user field and added tests

* added comments

* update migrations

Signed-off-by: Aakash Singh <[email protected]>

---------

Signed-off-by: Aakash Singh <[email protected]>
Co-authored-by: Vignesh Hari <[email protected]>
Co-authored-by: Aakash Singh <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2023
1 parent 9519d70 commit cd124c7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
18 changes: 16 additions & 2 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,34 @@ 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():
raise serializers.ValidationError({"note": ["Note cannot be empty"]})
return super().validate_empty_values(data)

def create(self, validated_data):
user_type = User.REVERSE_TYPE_MAP[validated_data["created_by"].user_type]
# If the user is a doctor and the note is being created in the home facility
# then the user type is doctor else it is a remote specialist
if user_type == "Doctor":
if validated_data["created_by"].home_facility == validated_data["facility"]:
validated_data["user_type"] = "Doctor"
else:
validated_data["user_type"] = "RemoteSpecialist"
else:
# If the user is not a doctor then the user type is the same as the user type
validated_data["user_type"] = user_type

return super().create(validated_data)

class Meta:
model = PatientNotes
fields = (
"note",
"facility",
"created_by_object",
"created_by_local_user",
"user_type",
"created_date",
)
read_only_fields = ("created_date",)
12 changes: 1 addition & 11 deletions 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 BooleanField, Case, F, Value, When
from django.db.models import Case, 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 @@ -607,16 +607,6 @@ 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 Down
20 changes: 20 additions & 0 deletions care/facility/migrations/0380_patientnotes_user_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-08-22 07:32

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"facility",
"0379_rename_prescribed_medication_patientconsultation_treatment_plan",
),
]

operations = [
migrations.AddField(
model_name="patientnotes",
name="user_type",
field=models.CharField(default="", max_length=25),
),
]
1 change: 1 addition & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ class PatientNotes(FacilityBaseModel, PatientRelatedPermissionMixin):
facility = models.ForeignKey(
Facility, on_delete=models.PROTECT, null=False, blank=False
)
user_type = models.CharField(max_length=25, default="")
created_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
Expand Down
35 changes: 22 additions & 13 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework_simplejwt.tokens import RefreshToken

from care.facility.models import User
from care.facility.tests.mixins import TestClassMixin
from care.utils.tests.test_base import TestBase

Expand All @@ -15,7 +16,7 @@ class ExpectedPatientNoteKeys(Enum):
FACILITY = "facility"
CREATED_BY_OBJECT = "created_by_object"
CREATED_DATE = "created_date"
CREATED_BY_LOCAL_USER = "created_by_local_user"
USER_TYPE = "user_type"


class ExpectedFacilityKeys(Enum):
Expand Down Expand Up @@ -85,24 +86,32 @@ def setUp(self):
district = self.create_district(state=state)

# 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 = self.create_user(
district=district,
username="test user",
user_type=User.TYPE_VALUE_MAP["Doctor"],
)
facility = self.create_facility(district=district)
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 = self.create_user(
district=district,
username="test user 2",
user_type=User.TYPE_VALUE_MAP["Doctor"],
)
facility2 = self.create_facility(district=district)
self.user2.home_facility = facility2
self.user2.save()

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

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

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

Expand All @@ -117,11 +126,11 @@ def test_patient_notes(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIsInstance(response.json()["results"], list)

# Test created_by_local_user field if user is not from same facility as patient
# Test user_type field if user is not from same facility as patient
data2 = response.json()["results"][0]

created_by_local_user_content2 = data2["created_by_local_user"]
self.assertEqual(created_by_local_user_content2, False)
user_type_content2 = data2["user_type"]
self.assertEqual(user_type_content2, "RemoteSpecialist")

# Ensure only necessary data is being sent and no extra data
data = response.json()["results"][1]
Expand All @@ -130,9 +139,9 @@ def test_patient_notes(self):
data.keys(), [item.value for item in ExpectedPatientNoteKeys]
)

created_by_local_user_content = data["created_by_local_user"]
user_type_content = data["user_type"]

self.assertEqual(created_by_local_user_content, True)
self.assertEqual(user_type_content, "Doctor")

facility_content = data["facility"]

Expand Down
21 changes: 14 additions & 7 deletions care/utils/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytz import unicode
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework_simplejwt.tokens import RefreshToken

from care.facility.models import (
CATEGORY_CHOICES,
Expand All @@ -19,7 +20,6 @@
Facility,
LocalBody,
PatientConsultation,
PatientNotes,
PatientRegistration,
User,
)
Expand Down Expand Up @@ -102,7 +102,7 @@ def create_facility(
return f

@classmethod
def create_patient(cls, **kwargs):
def create_patient(cls, facility=None, **kwargs):
patient_data = cls.get_patient_data().copy()
patient_data.update(kwargs)

Expand All @@ -112,7 +112,7 @@ def create_patient(cls, **kwargs):

patient_data.update(
{
"facility": cls.facility,
"facility": facility or cls.facility,
"district_id": district_id,
"state_id": state_id,
"disease_status": getattr(
Expand Down Expand Up @@ -439,12 +439,19 @@ def create_consultation(
return PatientConsultation.objects.create(**data)

def create_patient_note(
self, patient=None, facility=None, note="Patient is doing find", **kwargs
self, patient=None, note="Patient is doing find", created_by=None, **kwargs
):
data = {
"patient": patient or self.patient,
"facility": facility or self.facility,
"facility": patient.facility or self.facility,
"note": note,
}
data.update(kwargs)
return PatientNotes.objects.create(**data)

patientId = patient.external_id

refresh_token = RefreshToken.for_user(created_by)
self.client.credentials(
HTTP_AUTHORIZATION=f"Bearer {refresh_token.access_token}"
)

self.client.post(f"/api/v1/patient/{patientId}/notes/", data=data)

0 comments on commit cd124c7

Please sign in to comment.