Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge ip op numbers #1546

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ class PatientFilterSet(filters.FilterSet):
emergency_phone_number = filters.CharFilter(field_name="emergency_phone_number")
allow_transfer = filters.BooleanFilter(field_name="allow_transfer")
name = filters.CharFilter(field_name="name", lookup_expr="icontains")
ip_no = filters.CharFilter(
field_name="last_consultation__ip_no", lookup_expr="icontains"
patient_no = filters.CharFilter(
field_name="last_consultation__patient_no", lookup_expr="icontains"
)
op_no = filters.CharFilter(
field_name="last_consultation__op_no", lookup_expr="icontains"
)
ip_or_op_no = filters.CharFilter(method="filter_by_ip_or_op_no")
gender = filters.NumberFilter(field_name="gender")
age = filters.NumberFilter(field_name="age")
age_min = filters.NumberFilter(field_name="age", lookup_expr="gte")
Expand All @@ -98,14 +94,6 @@ class PatientFilterSet(filters.FilterSet):
choices=CATEGORY_CHOICES,
)

def filter_by_ip_or_op_no(self, queryset, name, value):
if value:
queryset = queryset.filter(
Q(last_consultation__ip_no__icontains=value)
| Q(last_consultation__op_no__icontains=value)
)
return queryset

def filter_by_category(self, queryset, name, value):
if value:
queryset = queryset.filter(
Expand Down
60 changes: 60 additions & 0 deletions care/facility/migrations/0381_merge_ip_op_no.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 4.2.2 on 2023-08-22 11:54

from django.core.paginator import Paginator
from django.db import migrations
from django.db.models import CharField, Q


def merge_ip_op_no(apps, schema_editor):
PatientConsultation = apps.get_model("facility", "PatientConsultation")

patients = (
PatientConsultation.objects.filter(Q(op_no__isnull=False))
.exclude(op_no="")
.only("op_no", "patient_no")
.order_by("id")
)

paginator = Paginator(patients, 1000)
for page_number in paginator.page_range:
bulk = []
for patient in paginator.page(page_number).object_list:
if not patient.op_no.strip():
continue
if patient.patient_no:
patient.patient_no = f"{patient.patient_no} | {patient.op_no}"
else:
patient.patient_no = patient.op_no
bulk.append(patient)

PatientConsultation.objects.bulk_update(bulk, ["patient_no"])


class Migration(migrations.Migration):
dependencies = [
(
"facility",
"0380_patientnotes_user_type",
),
]

operations = [
migrations.RenameField(
model_name="patientconsultation",
old_name="ip_no",
new_name="patient_no",
),
migrations.RunPython(merge_ip_op_no, migrations.RunPython.noop),
migrations.RemoveField(model_name="patientconsultation", name="op_no"),
migrations.AlterField(
model_name="patientconsultation",
name="patient_no",
field=CharField(
blank=True,
default="",
help_text="Patient's unique number in the facility. IP number for inpatients and OP number for outpatients.",
max_length=100,
null=True,
),
),
]
12 changes: 10 additions & 2 deletions care/facility/models/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin):
related_name="consultations",
)

ip_no = models.CharField(max_length=100, default="", null=True, blank=True)
op_no = models.CharField(max_length=100, default="", null=True, blank=True)
patient_no = models.CharField(
max_length=100,
default="",
null=True,
blank=True,
help_text=(
"Patient's unique number in the facility. "
"IP number for inpatients and OP number for outpatients."
),
)

facility = models.ForeignKey(
"Facility", on_delete=models.CASCADE, related_name="consultations"
Expand Down
22 changes: 3 additions & 19 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def setUp(self):

self.patient = self.create_patient(district=district.id, created_by=self.user)
self.consultation = self.create_consultation(
op_no="OP1234",
ip_no="IP5678",
patient_no="IP5678",
patient=self.patient,
facility=facility,
created_by=self.user,
Expand All @@ -227,23 +226,8 @@ def setUp(self):
HTTP_AUTHORIZATION=f"Bearer {refresh_token.access_token}"
)

def test_filter_by_op_no(self):
response = self.client.get("/api/v1/patient/?op_no=OP1234")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"][0]["id"], str(self.patient.external_id)
)

def test_filter_by_ip_OR_op_no(self):
response = self.client.get("/api/v1/patient/?ip_or_op_no=IP5678")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"][0]["id"], str(self.patient.external_id)
)

response = self.client.get("/api/v1/patient/?ip_or_op_no=OP1234")
def test_filter_by_patient_no(self):
response = self.client.get("/api/v1/patient/?patient_no=IP5678")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
Expand Down
8 changes: 7 additions & 1 deletion care/templates/reports/patient_discharge_summary_pdf.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ <h3 class="text-lg leading-6 font-medium text-gray-900">
{% endif %}
<div class="flex gap-6">
<p>
<span class="mt-1 text-sm leading-5 text-gray-500">IP No:</span> {{consultation.ip_no}}
<span class="mt-1 text-sm leading-5 text-gray-500">
{% if consultation.suggestion == 'A' %}
IP No:
{% else %}
OP No:
{% endif %}
</span> {{consultation.patient_no}}
</p>
<p>
<span class="text-sm leading-5 font-medium text-gray-500">Weight: </span>
Expand Down
2 changes: 1 addition & 1 deletion data/dummy/facility.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"modified_date": "2022-09-27T07:20:40.123Z",
"deleted": false,
"patient": 1,
"ip_no": "88.99.44.66",
"patient_no": "88.99.44.66",
"facility": 1,
"diagnosis": "",
"icd11_provisional_diagnoses": "[]",
Expand Down