diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index bc86b8515e..726facf027 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -106,6 +106,8 @@ class PatientConsultationSerializer(serializers.ModelSerializer): read_only=True ) + medico_legal_case = serializers.BooleanField(default=False, required=False) + def get_discharge_prescription(self, consultation): return Prescription.objects.filter( consultation=consultation, @@ -153,9 +155,14 @@ def update(self, instance, validated_data): instance.last_edited_by = self.context["request"].user if instance.discharge_date: - raise ValidationError( - {"consultation": ["Discharged Consultation data cannot be updated"]} - ) + if "medico_legal_case" not in validated_data: + raise ValidationError( + {"consultation": ["Discharged Consultation data cannot be updated"]} + ) + else: + instance.medico_legal_case = validated_data.pop("medico_legal_case") + instance.save() + return instance if instance.suggestion == SuggestionChoices.OP: instance.discharge_date = localtime(now()) diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py index 0a502dc06a..2fe2e08954 100644 --- a/care/facility/api/viewsets/patient.py +++ b/care/facility/api/viewsets/patient.py @@ -149,6 +149,9 @@ def filter_by_category(self, queryset, name, value): last_consultation_admitted_bed_type_list = MultiSelectFilter( method="filter_by_bed_type", ) + last_consultation_medico_legal_case = filters.BooleanFilter( + field_name="last_consultation__medico_legal_case" + ) def filter_by_bed_type(self, queryset, name, value): if not value: diff --git a/care/facility/migrations/0390_patientconsultation_medico_legal_case.py b/care/facility/migrations/0390_patientconsultation_medico_legal_case.py new file mode 100644 index 0000000000..1e90f034ba --- /dev/null +++ b/care/facility/migrations/0390_patientconsultation_medico_legal_case.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.6 on 2023-10-11 06:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0389_merge_20231005_2247"), + ] + + operations = [ + migrations.AddField( + model_name="patientconsultation", + name="medico_legal_case", + field=models.BooleanField(default=False), + ), + ] diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index c43f6fd5da..6707e967a5 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -143,6 +143,8 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): related_name="patient_assigned_to", ) + medico_legal_case = models.BooleanField(default=False) + deprecated_verified_by = models.TextField(default="", null=True, blank=True) verified_by = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, blank=True diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index 4330ebb170..2ca154bfe5 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -256,3 +256,54 @@ def test_referred_to_external_valid_value(self): referred_to_external=referred_to_external, ) self.assertEqual(res.status_code, status.HTTP_200_OK) + + def test_medico_legal_case(self): + consultation = self.create_admission_consultation( + medico_legal_case=True, + ) + url = self.get_url(consultation) + + data = self.client.get(url) + self.assertEqual(data.status_code, status.HTTP_200_OK) + self.assertEqual(data.data["medico_legal_case"], True) + + # Test Patch + response = self.update_consultation(consultation, medico_legal_case=False) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["medico_legal_case"], False) + + # Test Patch after discharge + response = self.discharge( + consultation, + discharge_reason="REC", + discharge_date="2023-07-01T12:00:00Z", + discharge_notes="Discharged with valid referred_to_external", + medico_legal_case=False, + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = self.client.get(url) + self.assertEqual(data.status_code, status.HTTP_200_OK) + self.assertEqual(data.data["medico_legal_case"], False) + + response = self.update_consultation(consultation, medico_legal_case=True) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["medico_legal_case"], True) + + def test_update_consultation_after_discharge(self): + consultation = self.create_admission_consultation( + suggestion="A", + admission_date=make_aware(datetime.datetime(2020, 4, 1, 15, 30, 00)), + ) + res = self.discharge( + consultation, + discharge_reason="REC", + discharge_date="2020-04-02T15:30:00Z", + discharge_notes="Discharge as recovered after admission before future", + ) + self.assertEqual(res.status_code, status.HTTP_200_OK) + + res = self.update_consultation( + consultation, symptoms=[1, 2], category="MILD", suggestion="A" + ) + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)