From 1de5ef7c23e6c6c8adc9fe7d4ea24230d8e52c64 Mon Sep 17 00:00:00 2001 From: Rithvik Nishad Date: Tue, 8 Aug 2023 18:17:50 +0000 Subject: [PATCH] Pop `death_datetime` and `death_confirmed_doctor` if discharge reason other than EXPIRED (#1519) * pop `death_datetime` and `death_confirmed_doctor` if discharge reason is not expired * add tests --- .../api/serializers/patient_consultation.py | 4 +++ .../migrations/0376_auto_20230807_2148.py | 28 +++++++++++++++++++ .../tests/test_patient_consultation_api.py | 18 ++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 care/facility/migrations/0376_auto_20230807_2148.py diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index 0c785232ca..38d5908a8a 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -430,6 +430,10 @@ class Meta: ) def validate(self, attrs): + if attrs.get("discharge_reason") != "EXP": + attrs.pop("death_datetime", None) + attrs.pop("death_confirmed_doctor", None) + if attrs.get("discharge_reason") == "EXP": if not attrs.get("death_datetime"): raise ValidationError({"death_datetime": "This field is required"}) diff --git a/care/facility/migrations/0376_auto_20230807_2148.py b/care/facility/migrations/0376_auto_20230807_2148.py new file mode 100644 index 0000000000..20d850db47 --- /dev/null +++ b/care/facility/migrations/0376_auto_20230807_2148.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.2 on 2023-08-07 16:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0375_alter_dailyround_resp"), + ] + + def reset_expired_fields_for_non_expired_discharged_consultations( + apps, schema_editor + ): + PatientConsultation = apps.get_model("facility", "PatientConsultation") + PatientConsultation.objects.exclude( + discharge_reason__isnull=True, + discharge_reason="EXP", + ).update( + death_datetime=None, + death_confirmed_doctor=None, + ) + + operations = [ + migrations.RunPython( + reset_expired_fields_for_non_expired_discharged_consultations, + reverse_code=migrations.RunPython.noop, + ), + ] diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index a3993f6c22..fc5f21c945 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -189,6 +189,24 @@ def test_discharge_as_expired_after_admission(self): ) self.assertEqual(res.status_code, status.HTTP_200_OK) + def test_discharge_as_recovered_with_expired_fields(self): + consultation = self.create_admission_consultation( + suggestion="A", + admission_date=make_aware(datetime.datetime(2023, 4, 1, 15, 30, 00)), + ) + res = self.discharge( + consultation, + discharge_reason="REC", + discharge_date="2023-04-02T15:30:00Z", + discharge_notes="Discharge as recovered with expired fields", + death_datetime="2023-04-02T15:30:00Z", + death_confirmed_doctor="Dr. Test", + ) + self.assertEqual(res.status_code, status.HTTP_200_OK) + consultation.refresh_from_db() + self.assertIsNone(consultation.death_datetime) + self.assertIsNot(consultation.death_confirmed_doctor, "Dr. Test") + def test_referred_to_external_null(self): consultation = self.create_admission_consultation( suggestion="A",