Skip to content

Commit

Permalink
Pop death_datetime and death_confirmed_doctor if discharge reason…
Browse files Browse the repository at this point in the history
… other than EXPIRED (#1519)

* pop `death_datetime` and `death_confirmed_doctor` if discharge reason is not expired

* add tests
  • Loading branch information
rithviknishad authored Aug 8, 2023
1 parent 1579fe9 commit 1de5ef7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
28 changes: 28 additions & 0 deletions care/facility/migrations/0376_auto_20230807_2148.py
Original file line number Diff line number Diff line change
@@ -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,
),
]
18 changes: 18 additions & 0 deletions care/facility/tests/test_patient_consultation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1de5ef7

Please sign in to comment.