Skip to content

Commit

Permalink
add field readmission + custom migration (#1572)
Browse files Browse the repository at this point in the history
* add field readmission + custom migration

* rebase migration

* fix rebase migration
  • Loading branch information
rithviknishad authored Sep 11, 2023
1 parent cbc0603 commit fe28fc1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
13 changes: 12 additions & 1 deletion care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Meta:
"last_edited_by",
"created_by",
"kasp_enabled_date",
"is_readmission",
"deprecated_verified_by",
)
exclude = ("deleted", "external_id")
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
),
]
1 change: 1 addition & 0 deletions care/facility/models/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe28fc1

Please sign in to comment.