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

fix: consultation specific doctor notes #1285

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FacilitySerializer,
)
from care.facility.api.serializers.patient_consultation import (
PatientConsultationIDSerializer,
PatientConsultationSerializer,
)
from care.facility.models import (
Expand Down Expand Up @@ -73,7 +74,8 @@ class PatientListSerializer(serializers.ModelSerializer):

blood_group = ChoiceField(choices=BLOOD_GROUP_CHOICES, required=True)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
choices=DISEASE_STATUS_CHOICES,
default=DiseaseStatusEnum.SUSPECTED.value,
)
source = ChoiceField(choices=PatientRegistration.SourceChoices)

Expand Down Expand Up @@ -181,7 +183,8 @@ class Meta:
default=PatientRegistration.SourceEnum.CARE.value,
)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
choices=DISEASE_STATUS_CHOICES,
default=DiseaseStatusEnum.SUSPECTED.value,
)

meta_info = PatientMetaInfoSerializer(required=False, allow_null=True)
Expand All @@ -198,7 +201,9 @@ class Meta:
last_edited = UserBaseMinimumSerializer(read_only=True)
created_by = UserBaseMinimumSerializer(read_only=True)
vaccine_name = serializers.ChoiceField(
choices=PatientRegistration.vaccineChoices, required=False, allow_null=True
choices=PatientRegistration.vaccineChoices,
required=False,
allow_null=True,
)

assigned_to_object = UserBaseMinimumSerializer(source="assigned_to", read_only=True)
Expand All @@ -218,7 +223,11 @@ class Meta:
"external_id",
)
include = ("contacted_patients",)
read_only = TIMESTAMP_FIELDS + ("last_edited", "created_by", "is_active")
read_only = TIMESTAMP_FIELDS + (
"last_edited",
"created_by",
"is_active",
)

# def get_last_consultation(self, obj):
# last_consultation = PatientConsultation.objects.filter(patient=obj).last()
Expand Down Expand Up @@ -454,6 +463,7 @@ def save(self, **kwargs):

class PatientNotesSerializer(serializers.ModelSerializer):
facility = FacilityBasicInfoSerializer(read_only=True)
consultation = PatientConsultationIDSerializer(read_only=True)
created_by_object = UserBaseMinimumSerializer(source="created_by", read_only=True)

def validate_empty_values(self, data):
Expand All @@ -463,5 +473,11 @@ def validate_empty_values(self, data):

class Meta:
model = PatientNotes
fields = ("note", "facility", "created_by_object", "created_date")
fields = (
"note",
"facility",
"consultation",
"created_by_object",
"created_date",
)
read_only_fields = ("created_date",)
11 changes: 11 additions & 0 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ def list(self, request, *args, **kwargs):
return super(PatientSearchViewSet, self).list(request, *args, **kwargs)


class PatientNotesFilterSet(filters.FilterSet):
consultation = filters.CharFilter(field_name="consultation__external_id")


class PatientNotesViewSet(
ListModelMixin, RetrieveModelMixin, CreateModelMixin, GenericViewSet
):
Expand All @@ -599,6 +603,8 @@ class PatientNotesViewSet(
)
serializer_class = PatientNotesSerializer
permission_classes = (IsAuthenticated, DRYPermissions)
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = PatientNotesFilterSet

def get_queryset(self):
user = self.request.user
Expand Down Expand Up @@ -629,8 +635,13 @@ def perform_create(self, serializer):
raise ValidationError(
{"patient": "Only active patients data can be updated"}
)
consultation = None
if self.request.data.get("consultation"):
consultation = patient.last_consultation

return serializer.save(
facility=patient.facility,
patient=patient,
consultation=consultation,
created_by=self.request.user,
)
33 changes: 33 additions & 0 deletions care/facility/migrations/0373_patientnotes_consultation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.2 on 2023-07-19 11:47

import django.db.models.deletion
from django.db import migrations, models


def forwards_func(apps, schema_editor):
patient_notes_model = apps.get_model("facility", "PatientNotes")
patient_notes = patient_notes_model.objects.all()
for patient_note in patient_notes:
if patient_note.patient.last_consultation:
patient_note.consultation = patient_note.patient.last_consultation
patient_note.save()


class Migration(migrations.Migration):
dependencies = [
("facility", "0372_assetavailabilityrecord"),
]

operations = [
migrations.AddField(
model_name="patientnotes",
name="consultation",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="facility.patientconsultation",
),
),
migrations.RunPython(forwards_func),
]
8 changes: 7 additions & 1 deletion care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ class TestTypeEnum(enum.Enum):
)

action = models.IntegerField(
choices=ActionChoices, blank=True, null=True, default=ActionEnum.NO_ACTION.value
choices=ActionChoices,
blank=True,
null=True,
default=ActionEnum.NO_ACTION.value,
)
review_time = models.DateTimeField(
null=True, blank=True, verbose_name="Patient's next review time"
Expand Down Expand Up @@ -674,6 +677,9 @@ class PatientNotes(FacilityBaseModel, PatientRelatedPermissionMixin):
patient = models.ForeignKey(
PatientRegistration, on_delete=models.PROTECT, null=False, blank=False
)
consultation = models.ForeignKey(
PatientConsultation, on_delete=models.PROTECT, null=True, blank=True
)
facility = models.ForeignKey(
Facility, on_delete=models.PROTECT, null=False, blank=False
)
Expand Down
16 changes: 4 additions & 12 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from enum import Enum

from rest_framework import status
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework_simplejwt.tokens import RefreshToken

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


class ExpectedPatientNoteKeys(Enum):
Expand All @@ -26,6 +24,7 @@ class ExpectedFacilityKeys(Enum):
DISTRICT_OBJECT = "district_object"
STATE_OBJECT = "state_object"
FACILITY_TYPE = "facility_type"
CONSULTATION = "consultation"
READ_COVER_IMAGE_URL = "read_cover_image_url"
FEATURES = "features"
PATIENT_COUNT = "patient_count"
Expand Down Expand Up @@ -74,20 +73,13 @@ class ExpectedCreatedByObjectKeys(Enum):
HOME_FACILITY = "home_facility"


class PatientNotesTestCase(TestBase, TestClassMixin, APITestCase):
class PatientNotesTestCase(TestClassMixin):
asset_id = None

def setUp(self):
self.factory = APIRequestFactory()
state = self.create_state()
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.patient = self.create_patient(district=district.id)
facility = self.create_facility(district=self.district, user=self.users[0])

self.patient = self.create_patient(district=self.district.id)
self.patient_note = self.create_patient_note(
patient=self.patient, facility=facility
)
Expand Down
2 changes: 1 addition & 1 deletion data/medibase.json
Original file line number Diff line number Diff line change
Expand Up @@ -1144305,4 +1144305,4 @@
"cims_class_link": "/india/drug/search?q=drugs+for+erectile+dysfunction&mtype=brand&code=10d",
"cims_class": "Drugs for Erectile Dysfunction",
"atc_classification": "G04BE03 - sildenafil ; Belongs to the class of drugs used in erectile dysfunction."
}]
}]
Loading