From fe28fc16d4e2f231a61f27a5e273d92c6490eb98 Mon Sep 17 00:00:00 2001 From: Rithvik Nishad Date: Mon, 11 Sep 2023 20:30:08 +0530 Subject: [PATCH] add field readmission + custom migration (#1572) * add field readmission + custom migration * rebase migration * fix rebase migration --- .../api/serializers/patient_consultation.py | 13 ++++- ...0385_patientconsultation_is_readmission.py | 48 +++++++++++++++++++ care/facility/models/patient_consultation.py | 1 + 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 care/facility/migrations/0385_patientconsultation_is_readmission.py diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index ae2771b35c..bc86b8515e 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -136,6 +136,7 @@ class Meta: "last_edited_by", "created_by", "kasp_enabled_date", + "is_readmission", "deprecated_verified_by", ) exclude = ("deleted", "external_id") @@ -262,6 +263,17 @@ def create(self, validated_data): consultation = super().create(validated_data) consultation.created_by = self.context["request"].user consultation.last_edited_by = self.context["request"].user + patient = consultation.patient + last_consultation = patient.last_consultation + if ( + last_consultation + and consultation.suggestion == SuggestionChoices.A + and last_consultation.suggestion == SuggestionChoices.A + and last_consultation.discharge_date + and last_consultation.discharge_date + timedelta(days=30) + > consultation.admission_date + ): + consultation.is_readmission = True consultation.save() if bed and consultation.suggestion == SuggestionChoices.A: @@ -274,7 +286,6 @@ def create(self, validated_data): consultation.current_bed = consultation_bed consultation.save(update_fields=["current_bed"]) - patient = consultation.patient if consultation.suggestion == SuggestionChoices.OP: consultation.discharge_date = localtime(now()) consultation.save() diff --git a/care/facility/migrations/0385_patientconsultation_is_readmission.py b/care/facility/migrations/0385_patientconsultation_is_readmission.py new file mode 100644 index 0000000000..1155c09110 --- /dev/null +++ b/care/facility/migrations/0385_patientconsultation_is_readmission.py @@ -0,0 +1,48 @@ +# Generated by Django 4.2.2 on 2023-09-01 16:22 + +from datetime import timedelta + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0384_patientconsultation_verified_by"), + ] + + def fill_is_readmission(apps, schema_editor): + PatientConsultation = apps.get_model("facility", "PatientConsultation") + + bulk_update_list = [] + qs = PatientConsultation.objects.filter( + suggestion="A", admission_date__isnull=False + ) + + def is_readmission(consultation): + # readmission=True if last_consultation.discharge_date is within 30 days of admission_date + return qs.filter( + patient=consultation.patient, + created_date__lt=consultation.created_date, + discharge_date__isnull=False, + discharge_date__gte=consultation.admission_date - timedelta(days=30), + ).exists() + + for consultation in qs: + if is_readmission(consultation): + consultation.is_readmission = True + bulk_update_list.append(consultation) + + PatientConsultation.objects.bulk_update( + bulk_update_list, ["is_readmission"], batch_size=1000 + ) + + operations = [ + migrations.AddField( + model_name="patientconsultation", + name="is_readmission", + field=models.BooleanField(default=False), + ), + migrations.RunPython( + fill_is_readmission, reverse_code=migrations.RunPython.noop + ), + ] diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index 382499996a..c43f6fd5da 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -107,6 +107,7 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): on_delete=models.PROTECT, related_name="referred_patients", ) # Deprecated + is_readmission = models.BooleanField(default=False) referred_to_external = models.TextField(default="", null=True, blank=True) admitted = models.BooleanField(default=False) # Deprecated admission_date = models.DateTimeField(null=True, blank=True) # Deprecated