From c2e667b007696049f69ee37754048de57caadffb Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 17 Oct 2023 11:07:58 +0530 Subject: [PATCH 01/10] refactor patient_consultation --- care/abdm/utils/fhir.py | 4 +- .../api/serializers/patient_consultation.py | 97 +++++++++++++++++-- ...deprecated_consultation_status_and_more.py | 72 ++++++++++++++ ...ion_date_and_populate_route_to_facility.py | 78 +++++++++++++++ care/facility/models/patient.py | 10 +- care/facility/models/patient_base.py | 20 +++- care/facility/models/patient_consultation.py | 41 ++++++-- .../tests/test_patient_consultation_api.py | 6 +- .../patient_discharge_summary_pdf.html | 14 +-- 9 files changed, 305 insertions(+), 37 deletions(-) create mode 100644 care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py create mode 100644 care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py diff --git a/care/abdm/utils/fhir.py b/care/abdm/utils/fhir.py index 214257bc02..76429320c8 100644 --- a/care/abdm/utils/fhir.py +++ b/care/abdm/utils/fhir.py @@ -89,8 +89,8 @@ def _practioner(self): id = str(uuid()) name = ( ( - self.consultation.verified_by - and f"{self.consultation.verified_by.first_name} {self.consultation.verified_by.last_name}" + self.consultation.treating_physician + and f"{self.consultation.treating_physician.first_name} {self.consultation.treating_physician.last_name}" ) or self.consultation.deprecated_verified_by or f"{self.consultation.created_by.first_name} {self.consultation.created_by.last_name}" diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index bc86b8515e..22ac069d75 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -7,6 +7,7 @@ from care.abdm.utils.api_call import AbdmGateway from care.facility.api.serializers import TIMESTAMP_FIELDS +from care.facility.api.serializers.asset import AssetLocationSerializer from care.facility.api.serializers.bed import ConsultationBedSerializer from care.facility.api.serializers.daily_round import DailyRoundSerializer from care.facility.api.serializers.facility import FacilityBasicInfoSerializer @@ -18,11 +19,13 @@ Prescription, PrescriptionType, ) +from care.facility.models.asset import AssetLocation from care.facility.models.bed import Bed, ConsultationBed from care.facility.models.notification import Notification from care.facility.models.patient_base import ( DISCHARGE_REASON_CHOICES, SYMPTOM_CHOICES, + RouteToFacility, SuggestionChoices, ) from care.facility.models.patient_consultation import PatientConsultation @@ -63,6 +66,29 @@ class PatientConsultationSerializer(serializers.ModelSerializer): referred_to_external = serializers.CharField( required=False, allow_null=True, allow_blank=True ) + + referred_from_facility_object = FacilityBasicInfoSerializer( + source="referred_from_facility", read_only=True + ) + referred_from_facility = ExternalIdSerializerField( + queryset=Facility.objects.all(), + required=False, + ) + referred_from_facility_external = serializers.CharField( + required=False, allow_null=True, allow_blank=True + ) + referred_by_external = serializers.CharField( + required=False, allow_null=True, allow_blank=True + ) + + transferred_from_location_object = AssetLocationSerializer( + source="transferred_from_location", read_only=True + ) + transferred_from_location = ExternalIdSerializerField( + queryset=AssetLocation.objects.all(), + required=False, + ) + patient = ExternalIdSerializerField(queryset=PatientRegistration.objects.all()) facility = ExternalIdSerializerField(read_only=True) @@ -71,8 +97,10 @@ class PatientConsultationSerializer(serializers.ModelSerializer): queryset=User.objects.all(), required=False, allow_null=True ) - verified_by_object = UserBaseMinimumSerializer(source="verified_by", read_only=True) - verified_by = serializers.PrimaryKeyRelatedField( + treating_physician_object = UserBaseMinimumSerializer( + source="treating_physician", read_only=True + ) + treating_physician = serializers.PrimaryKeyRelatedField( queryset=User.objects.all(), required=False, allow_null=True ) @@ -329,19 +357,74 @@ def validate(self, attrs): validated = super().validate(attrs) # TODO Add Bed Authorisation Validation + if route_to_facility := validated.get("route_to_facility"): + if route_to_facility == RouteToFacility.OUTPATIENT: + validated["icu_admission_date"] = None + validated["transferred_from_location"] = None + validated["referred_from_facility"] = None + validated["referred_from_facility_external"] = "" + validated["referred_by_external"] = "" + + if route_to_facility == RouteToFacility.INTRA_FACILITY_TRANSFER: + validated["referred_from_facility"] = None + validated["referred_from_facility_external"] = "" + validated["referred_by_external"] = "" + + if not validated.get("transferred_from_location"): + raise ValidationError( + { + "transferred_from_location": [ + "This field is required as the patient has been transferred from another location." + ] + } + ) + + if route_to_facility == RouteToFacility.INTER_FACILITY_TRANSFER: + validated["transferred_from_location"] = None + + if not validated.get("referred_from_facility") and not validated.get( + "referred_from_facility_external" + ): + raise ValidationError( + { + "referred_from_facility": [ + "This field is required as the patient has been referred from another facility." + ] + } + ) + + if validated.get("referred_from_facility") and validated.get( + "referred_from_facility_external" + ): + raise ValidationError( + { + "referred_from_facility": [ + "Only one of referred_from_facility and referred_from_facility_external can be set" + ], + "referred_from_facility_external": [ + "Only one of referred_from_facility and referred_from_facility_external can be set" + ], + } + ) + else: + raise ValidationError({"route_to_facility": "This field is required"}) + if ( "suggestion" in validated and validated["suggestion"] != SuggestionChoices.DD ): - if "verified_by" not in validated: + if "treating_physician" not in validated: raise ValidationError( { - "verified_by": [ + "treating_physician": [ "This field is required as the suggestion is not 'Declared Death'" ] } ) - if not validated["verified_by"].user_type == User.TYPE_VALUE_MAP["Doctor"]: + if ( + not validated["treating_physician"].user_type + == User.TYPE_VALUE_MAP["Doctor"] + ): raise ValidationError("Only Doctors can verify a Consultation") facility = ( @@ -350,8 +433,8 @@ def validate(self, attrs): or validated["patient"].facility ) if ( - validated["verified_by"].home_facility - and validated["verified_by"].home_facility != facility + validated["treating_physician"].home_facility + and validated["treating_physician"].home_facility != facility ): raise ValidationError( "Home Facility of the Doctor must be the same as the Consultation Facility" diff --git a/care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py b/care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py new file mode 100644 index 0000000000..dc54705d8e --- /dev/null +++ b/care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py @@ -0,0 +1,72 @@ +# Generated by Django 4.2.5 on 2023-10-16 11:40 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0389_merge_20231005_2247"), + ] + + operations = [ + migrations.RenameField( + model_name="patientconsultation", + old_name="consultation_status", + new_name="deprecated_consultation_status", + ), + migrations.RenameField( + model_name="patientconsultation", + old_name="verified_by", + new_name="treating_physician", + ), + migrations.AddField( + model_name="patientconsultation", + name="referred_from_facility", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="facility.facility", + ), + ), + migrations.AddField( + model_name="patientconsultation", + name="referred_from_facility_external", + field=models.TextField(blank=True, default="", null=True), + ), + migrations.AddField( + model_name="patientconsultation", + name="referred_by_external", + field=models.TextField(blank=True, default="", null=True), + ), + migrations.AddField( + model_name="patientconsultation", + name="route_to_facility", + field=models.SmallIntegerField( + blank=True, + choices=[ + (None, "(Unknown)"), + (10, "Outpatient/Emergency Room"), + (20, "Referred from another facility"), + (30, "Internal Transfer within the facility"), + ], + null=True, + ), + ), + migrations.AddField( + model_name="patientconsultation", + name="transferred_from_location", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="facility.assetlocation", + ), + ), + migrations.AddField( + model_name="patientconsultation", + name="icu_admission_date", + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py b/care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py new file mode 100644 index 0000000000..4feb4c4dcc --- /dev/null +++ b/care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py @@ -0,0 +1,78 @@ +# Generated by Django 4.2.5 on 2023-10-16 11:41 + +import datetime + +from django.db import migrations +from django.db.models import DurationField, ExpressionWrapper, F +from django.db.models.functions import TruncDay +from django.utils import timezone + + +class Migration(migrations.Migration): + dependencies = [ + ( + "facility", + "0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", + ), + ] + + def clean_admission_date(apps, schema_editor): + """ + Clean admission_date field to be 00:00:00 IST + + For example: + + `2023-10-06 06:00:00 +05:30 IST` (`2023-10-06 00:30:00 +00:00 UTC`) would be updated to + `2023-10-06 00:00:00 +05:30 IST` (`2023-10-05 18:30:00 +00:00 UTC`) + + Equivalent to the following SQL: + + ```sql + UPDATE facility_patientconsultation + SET admission_date = + timezone('IST', admission_date) AT TIME ZONE 'UTC' + + (date_trunc('day', timezone('IST', admission_date)) - timezone('IST', admission_date)) + + (interval '-5 hours -30 minutes') + WHERE admission_date IS NOT NULL; + ``` + """ + + current_timezone = timezone.get_current_timezone() + tz_offset = timezone.timedelta( + minutes=current_timezone.utcoffset(datetime.datetime.utcnow()).seconds / 60 + ) + + PatientConsultation = apps.get_model("facility", "PatientConsultation") + PatientConsultation.objects.filter(admission_date__isnull=False).update( + admission_date=ExpressionWrapper( + # Convert the admission_date to UTC by subtracting the current offset + F("admission_date") - tz_offset + + # Get the day part of the admission_date and subtract the actual admission_date from it + (TruncDay(F("admission_date")) - F("admission_date")), + output_field=DurationField(), + ) + ) + + def populate_route_to_facility(apps, schema_editor): + PatientConsultation = apps.get_model("facility", "PatientConsultation") + qs = PatientConsultation.objects.all() + + # Brought Dead/Outpatient -> Outpatient/Emergency Room + qs.filter(deprecated_consultation_status=1).update(route_to_facility=10) + qs.filter(deprecated_consultation_status=5).update(route_to_facility=10) + + # Transferred from Ward/ICU -> Internal Transfer within facility + qs.filter(deprecated_consultation_status=2).update(route_to_facility=30) + qs.filter(deprecated_consultation_status=3).update(route_to_facility=30) + + # Referred from other hospital -> Referred from another facility + qs.filter(deprecated_consultation_status=4).update(route_to_facility=20) + + operations = [ + migrations.RunPython( + clean_admission_date, reverse_code=migrations.RunPython.noop + ), + migrations.RunPython( + populate_route_to_facility, reverse_code=migrations.RunPython.noop + ), + ] diff --git a/care/facility/models/patient.py b/care/facility/models/patient.py index 44fe0ee1a6..3db9650e3b 100644 --- a/care/facility/models/patient.py +++ b/care/facility/models/patient.py @@ -29,8 +29,8 @@ BLOOD_GROUP_CHOICES, DISEASE_STATUS_CHOICES, REVERSE_CATEGORY_CHOICES, - REVERSE_CONSULTATION_STATUS_CHOICES, REVERSE_DISCHARGE_REASON_CHOICES, + REVERSE_ROUTE_TO_FACILITY_CHOICES, ) from care.facility.models.patient_consultation import PatientConsultation from care.facility.static_data.icd11 import ICDDiseases @@ -487,7 +487,7 @@ def save(self, *args, **kwargs) -> None: "created_date": "Date of Registration", "created_date__time": "Time of Registration", # Last Consultation Details - "last_consultation__consultation_status": "Status during consultation", + "last_consultation__route_to_facility": "Route to Facility", "last_consultation__created_date": "Date of first consultation", "last_consultation__created_date__time": "Time of first consultation", "last_consultation__icd11_diagnoses": "Diagnoses", @@ -519,8 +519,8 @@ def format_as_time(time): "last_consultation__icd11_provisional_diagnoses": ( lambda x: ", ".join([ICDDiseases.by.id[id].label.strip() for id in x]) ), - "last_consultation__consultation_status": ( - lambda x: REVERSE_CONSULTATION_STATUS_CHOICES.get(x, "-").replace("_", " ") + "last_consultation__route_to_facility": ( + lambda x: REVERSE_ROUTE_TO_FACILITY_CHOICES.get(x, "-") ), "last_consultation__category": lambda x: REVERSE_CATEGORY_CHOICES.get(x, "-"), "last_consultation__discharge_reason": ( @@ -653,7 +653,7 @@ class FacilityPatientStatsHistory(FacilityBaseModel, FacilityRelatedPermissionMi "facilitypatientstatshistory__num_patients_visited": "Vistited Patients", "facilitypatientstatshistory__num_patients_home_quarantine": "Home Quarantined Patients", "facilitypatientstatshistory__num_patients_isolation": "Patients Isolated", - "facilitypatientstatshistory__num_patient_referred": "Patients Reffered", + "facilitypatientstatshistory__num_patient_referred": "Patients Referred", "facilitypatientstatshistory__num_patient_confirmed_positive": "Patients Confirmed Positive", } diff --git a/care/facility/models/patient_base.py b/care/facility/models/patient_base.py index 250630a998..dce09a74f9 100644 --- a/care/facility/models/patient_base.py +++ b/care/facility/models/patient_base.py @@ -1,6 +1,9 @@ import enum from types import SimpleNamespace +from django.db.models import IntegerChoices +from django.utils.translation import gettext_lazy as _ + def reverse_choices(choices): output = {} @@ -106,7 +109,7 @@ class DiseaseStatusEnum(enum.IntEnum): SuggestionChoices = SimpleNamespace(HI="HI", A="A", R="R", OP="OP", DC="DC", DD="DD") -class ConsultationStatusEnum(enum.Enum): +class DeprecatedConsultationStatusEnum(enum.Enum): UNKNOWN = 0 BROUGHT_DEAD = 1 TRANSFERRED_FROM_WARD = 2 @@ -115,7 +118,17 @@ class ConsultationStatusEnum(enum.Enum): OUT_PATIENT = 5 -ConsultationStatusChoices = [(e.value, e.name) for e in ConsultationStatusEnum] +DeprecatedConsultationStatusChoices = [ + (e.value, e.name) for e in DeprecatedConsultationStatusEnum +] + + +class RouteToFacility(IntegerChoices): + OUTPATIENT = 10, _("Outpatient/Emergency Room") + INTER_FACILITY_TRANSFER = 20, _("Referred from another facility") + INTRA_FACILITY_TRANSFER = 30, _("Internal Transfer within the facility") + + __empty__ = _("(Unknown)") class BedType(enum.Enum): @@ -134,7 +147,6 @@ class BedType(enum.Enum): REVERSE_DISEASE_STATUS_CHOICES = reverse_choices(DISEASE_STATUS_CHOICES) REVERSE_COVID_CATEGORY_CHOICES = reverse_choices(COVID_CATEGORY_CHOICES) # Deprecated REVERSE_CATEGORY_CHOICES = reverse_choices(CATEGORY_CHOICES) -# REVERSE_ADMIT_CHOICES = reverse_choices(ADMIT_CHOICES) REVERSE_BED_TYPE_CHOICES = reverse_choices(BedTypeChoices) -REVERSE_CONSULTATION_STATUS_CHOICES = reverse_choices(ConsultationStatusChoices) +REVERSE_ROUTE_TO_FACILITY_CHOICES = reverse_choices(RouteToFacility.choices) REVERSE_DISCHARGE_REASON_CHOICES = reverse_choices(DISCHARGE_REASON_CHOICES) diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index c43f6fd5da..459e7fb5fc 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -18,8 +18,9 @@ REVERSE_CATEGORY_CHOICES, REVERSE_COVID_CATEGORY_CHOICES, SYMPTOM_CHOICES, - ConsultationStatusChoices, - ConsultationStatusEnum, + DeprecatedConsultationStatusChoices, + DeprecatedConsultationStatusEnum, + RouteToFacility, SuggestionChoices, reverse_choices, ) @@ -95,9 +96,12 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): prescriptions = JSONField(default=dict) # Deprecated procedure = JSONField(default=dict) suggestion = models.CharField(max_length=4, choices=SUGGESTION_CHOICES) - consultation_status = models.IntegerField( - default=ConsultationStatusEnum.UNKNOWN.value, - choices=ConsultationStatusChoices, + deprecated_consultation_status = models.IntegerField( + default=DeprecatedConsultationStatusEnum.UNKNOWN.value, + choices=DeprecatedConsultationStatusChoices, + ) # Deprecated in favour of `route_to_facility` + route_to_facility = models.SmallIntegerField( + choices=RouteToFacility.choices, blank=True, null=True ) review_interval = models.IntegerField(default=-1) referred_to = models.ForeignKey( @@ -106,11 +110,28 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): blank=True, 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) + transferred_from_location = models.ForeignKey( + "AssetLocation", + null=True, + blank=True, + on_delete=models.PROTECT, + ) + referred_from_facility = models.ForeignKey( + "Facility", + null=True, + blank=True, + on_delete=models.PROTECT, + ) + referred_from_facility_external = models.TextField( + default="", null=True, blank=True + ) + referred_by_external = models.TextField(default="", null=True, blank=True) + is_readmission = models.BooleanField(default=False) admitted = models.BooleanField(default=False) # Deprecated admission_date = models.DateTimeField(null=True, blank=True) # Deprecated + icu_admission_date = models.DateTimeField(null=True, blank=True) discharge_date = models.DateTimeField(null=True, blank=True) discharge_reason = models.CharField( choices=DISCHARGE_REASON_CHOICES, @@ -143,8 +164,10 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): related_name="patient_assigned_to", ) - deprecated_verified_by = models.TextField(default="", null=True, blank=True) - verified_by = models.ForeignKey( + deprecated_verified_by = models.TextField( + default="", null=True, blank=True + ) # Deprecated + treating_physician = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, blank=True ) diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index 4330ebb170..d9dc700d76 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -32,7 +32,7 @@ def get_default_data(self): "history_of_present_illness": "history_of_present_illness", "treatment_plan": "treatment_plan", "suggestion": PatientConsultation.SUGGESTION_CHOICES[0][0], - "verified_by": self.doctor.id, + "treating_physician": self.doctor.id, } def get_url(self, consultation=None): @@ -61,13 +61,13 @@ def discharge(self, consultation, **kwargs): f"{self.get_url(consultation)}discharge_patient/", kwargs, "json" ) - def test_create_consultation_verified_by_invalid_user(self): + def test_create_consultation_treating_physician_invalid_user(self): consultation = self.create_admission_consultation( suggestion="A", admission_date=make_aware(datetime.datetime(2020, 4, 1, 15, 30, 00)), ) res = self.update_consultation( - consultation, verified_by=self.doctor.id, suggestion="A" + consultation, treating_physician=self.doctor.id, suggestion="A" ) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/care/templates/reports/patient_discharge_summary_pdf.html b/care/templates/reports/patient_discharge_summary_pdf.html index 2b1ed15156..28f562ee8c 100644 --- a/care/templates/reports/patient_discharge_summary_pdf.html +++ b/care/templates/reports/patient_discharge_summary_pdf.html @@ -71,8 +71,8 @@

- Status during consultation: - {{consultation.get_consultation_status_display|field_name_to_label}} + Route to Facility: + {{consultation.get_route_to_facility_display|field_name_to_label}}

Decision after consultation: @@ -85,7 +85,7 @@

{% elif consultation.suggestion == 'R' %}

- Reffered to: + Referred to: {{consultation.referred_to.name}}

{% elif consultation.suggestion == 'DD' %} @@ -121,7 +121,7 @@

{{consultation.height}} cm

- {% if consultation.consultation_status != 1 %} + {% if consultation.route_to_facility %}

Symptoms at admission: @@ -326,7 +326,7 @@

Ongoing Medication: {{patient.ongoing_medication}}

- {% if consultation.consultation_status != 1 %} + {% if consultation.route_to_facility %}

History of present illness: {{consultation.history_of_present_illness}} @@ -945,8 +945,8 @@

Treating Physician

- {% if consultation.verified_by %} - {{ consultation.verified_by.first_name }} {{ consultation.verified_by.last_name }} + {% if consultation.treating_physician %} + {{ consultation.treating_physician.first_name }} {{ consultation.treating_physician.last_name }} {% else %} - {% endif %} From 67cfdd9bb021957cf11efde19de8406e33dad75c Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 17 Oct 2023 11:22:21 +0530 Subject: [PATCH 02/10] rebase migrations --- ...eprecated_consultation_status_and_more.py} | 24 +++++++++---------- ...on_date_and_populate_route_to_facility.py} | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) rename care/facility/migrations/{0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py => 0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py} (95%) rename care/facility/migrations/{0391_clean_admission_date_and_populate_route_to_facility.py => 0393_clean_admission_date_and_populate_route_to_facility.py} (96%) diff --git a/care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py b/care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py similarity index 95% rename from care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py rename to care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py index dc54705d8e..3ed9d28dcb 100644 --- a/care/facility/migrations/0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py +++ b/care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-16 11:40 +# Generated by Django 4.2.5 on 2023-10-17 05:51 import django.db.models.deletion from django.db import migrations, models @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ("facility", "0389_merge_20231005_2247"), + ("facility", "0391_merge_20231016_1845"), ] operations = [ @@ -20,6 +20,16 @@ class Migration(migrations.Migration): old_name="verified_by", new_name="treating_physician", ), + migrations.AddField( + model_name="patientconsultation", + name="icu_admission_date", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="patientconsultation", + name="referred_by_external", + field=models.TextField(blank=True, default="", null=True), + ), migrations.AddField( model_name="patientconsultation", name="referred_from_facility", @@ -35,11 +45,6 @@ class Migration(migrations.Migration): name="referred_from_facility_external", field=models.TextField(blank=True, default="", null=True), ), - migrations.AddField( - model_name="patientconsultation", - name="referred_by_external", - field=models.TextField(blank=True, default="", null=True), - ), migrations.AddField( model_name="patientconsultation", name="route_to_facility", @@ -64,9 +69,4 @@ class Migration(migrations.Migration): to="facility.assetlocation", ), ), - migrations.AddField( - model_name="patientconsultation", - name="icu_admission_date", - field=models.DateTimeField(blank=True, null=True), - ), ] diff --git a/care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py b/care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py similarity index 96% rename from care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py rename to care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py index 4feb4c4dcc..c450b8461f 100644 --- a/care/facility/migrations/0391_clean_admission_date_and_populate_route_to_facility.py +++ b/care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-16 11:41 +# Generated by Django 4.2.5 on 2023-10-17 05:51 import datetime @@ -12,7 +12,7 @@ class Migration(migrations.Migration): dependencies = [ ( "facility", - "0390_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", + "0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", ), ] From d5307b86025c1cf0789ac98c6513d84430fb5709 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 17 Oct 2023 12:11:47 +0530 Subject: [PATCH 03/10] reorder fields --- care/facility/models/patient_consultation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index 3687fc0219..e95ad50356 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -164,13 +164,14 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin): related_name="patient_assigned_to", ) + medico_legal_case = models.BooleanField(default=False) + deprecated_verified_by = models.TextField( default="", null=True, blank=True ) # Deprecated treating_physician = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, blank=True ) - medico_legal_case = models.BooleanField(default=False) created_by = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, related_name="created_user" From cd7de5c4f8262172d63add5b2923ca3cd1c12079 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 23 Oct 2023 18:59:40 +0530 Subject: [PATCH 04/10] fix existing tests --- .../api/serializers/patient_consultation.py | 104 +++++++++--------- .../tests/test_patient_consultation_api.py | 1 + 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index e2b34e4dfd..693a969e1d 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -250,6 +250,58 @@ def update(self, instance, validated_data): return consultation def create(self, validated_data): + if route_to_facility := validated_data.get("route_to_facility"): + if route_to_facility == RouteToFacility.OUTPATIENT: + validated_data["icu_admission_date"] = None + validated_data["transferred_from_location"] = None + validated_data["referred_from_facility"] = None + validated_data["referred_from_facility_external"] = "" + validated_data["referred_by_external"] = "" + + if route_to_facility == RouteToFacility.INTRA_FACILITY_TRANSFER: + validated_data["referred_from_facility"] = None + validated_data["referred_from_facility_external"] = "" + validated_data["referred_by_external"] = "" + + if not validated_data.get("transferred_from_location"): + raise ValidationError( + { + "transferred_from_location": [ + "This field is required as the patient has been transferred from another location." + ] + } + ) + + if route_to_facility == RouteToFacility.INTER_FACILITY_TRANSFER: + validated_data["transferred_from_location"] = None + + if not validated_data.get( + "referred_from_facility" + ) and not validated_data.get("referred_from_facility_external"): + raise ValidationError( + { + "referred_from_facility": [ + "This field is required as the patient has been referred from another facility." + ] + } + ) + + if validated_data.get("referred_from_facility") and validated_data.get( + "referred_from_facility_external" + ): + raise ValidationError( + { + "referred_from_facility": [ + "Only one of referred_from_facility and referred_from_facility_external can be set" + ], + "referred_from_facility_external": [ + "Only one of referred_from_facility and referred_from_facility_external can be set" + ], + } + ) + else: + raise ValidationError({"route_to_facility": "This field is required"}) + action = -1 review_interval = -1 if "action" in validated_data: @@ -364,58 +416,6 @@ def validate(self, attrs): validated = super().validate(attrs) # TODO Add Bed Authorisation Validation - if route_to_facility := validated.get("route_to_facility"): - if route_to_facility == RouteToFacility.OUTPATIENT: - validated["icu_admission_date"] = None - validated["transferred_from_location"] = None - validated["referred_from_facility"] = None - validated["referred_from_facility_external"] = "" - validated["referred_by_external"] = "" - - if route_to_facility == RouteToFacility.INTRA_FACILITY_TRANSFER: - validated["referred_from_facility"] = None - validated["referred_from_facility_external"] = "" - validated["referred_by_external"] = "" - - if not validated.get("transferred_from_location"): - raise ValidationError( - { - "transferred_from_location": [ - "This field is required as the patient has been transferred from another location." - ] - } - ) - - if route_to_facility == RouteToFacility.INTER_FACILITY_TRANSFER: - validated["transferred_from_location"] = None - - if not validated.get("referred_from_facility") and not validated.get( - "referred_from_facility_external" - ): - raise ValidationError( - { - "referred_from_facility": [ - "This field is required as the patient has been referred from another facility." - ] - } - ) - - if validated.get("referred_from_facility") and validated.get( - "referred_from_facility_external" - ): - raise ValidationError( - { - "referred_from_facility": [ - "Only one of referred_from_facility and referred_from_facility_external can be set" - ], - "referred_from_facility_external": [ - "Only one of referred_from_facility and referred_from_facility_external can be set" - ], - } - ) - else: - raise ValidationError({"route_to_facility": "This field is required"}) - if ( "suggestion" in validated and validated["suggestion"] != SuggestionChoices.DD diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index 6a5a1681f6..eae5ffa660 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -26,6 +26,7 @@ def setUpTestData(cls) -> None: def get_default_data(self): return { + "route_to_facility": 10, "symptoms": [1], "category": CATEGORY_CHOICES[0][0], "examination_details": "examination_details", From f9fe44125ec60bc3e9a4c9d2adcbddc66e429eea Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 23 Oct 2023 19:22:34 +0530 Subject: [PATCH 05/10] add tests --- .../tests/test_patient_consultation_api.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index eae5ffa660..0fe995e525 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -19,6 +19,7 @@ def setUpTestData(cls) -> None: cls.local_body = cls.create_local_body(cls.district) cls.super_user = cls.create_super_user("su", cls.district) cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body) + cls.location = cls.create_asset_location(cls.facility) cls.user = cls.create_user("staff1", cls.district, home_facility=cls.facility) cls.doctor = cls.create_user( "doctor", cls.district, home_facility=cls.facility, user_type=15 @@ -41,6 +42,21 @@ def get_url(self, consultation=None): return f"/api/v1/consultation/{consultation.external_id}/" return "/api/v1/consultation/" + def create_route_to_facility_consultation( + self, patient=None, route_to_facility=10, **kwargs + ): + patient = patient or self.create_patient(self.district, self.facility) + data = self.get_default_data().copy() + kwargs.update( + { + "patient": patient.external_id, + "facility": self.facility.external_id, + "route_to_facility": route_to_facility, + } + ) + data.update(kwargs) + return self.client.post(self.get_url(), data) + def create_admission_consultation(self, patient=None, **kwargs): patient = patient or self.create_patient(self.district, self.facility) data = self.get_default_data().copy() @@ -258,6 +274,40 @@ def test_referred_to_external_valid_value(self): ) self.assertEqual(res.status_code, status.HTTP_200_OK) + def test_route_to_facility_referred_from_facility_empty(self): + res = self.create_route_to_facility_consultation(route_to_facility=20) + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) + + def test_route_to_facility_referred_from_facility_external(self): + res = self.create_route_to_facility_consultation( + route_to_facility=20, referred_from_facility_external="Test" + ) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + + def test_route_to_facility_referred_from_facility(self): + res = self.create_route_to_facility_consultation( + route_to_facility=20, referred_from_facility=self.facility.external_id + ) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + + def test_route_to_facility_referred_from_facility_and_external_together(self): + res = self.create_route_to_facility_consultation( + route_to_facility=20, + referred_from_facility="123", + referred_from_facility_external="Test", + ) + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) + + def test_route_to_facility_transfer_within_facility_empty(self): + res = self.create_route_to_facility_consultation(route_to_facility=30) + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) + + def test_route_to_facility_transfer_within_facility(self): + res = self.create_route_to_facility_consultation( + route_to_facility=30, transferred_from_location=self.location.external_id + ) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + def test_medico_legal_case(self): consultation = self.create_admission_consultation( medico_legal_case=True, From 99f381edb19cc9af7a236bc90603faf762a1a502 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 25 Oct 2023 17:05:05 +0530 Subject: [PATCH 06/10] rebase migrations --- ...ientconsultation_deprecated_consultation_status_and_more.py} | 2 +- ...0394_clean_admission_date_and_populate_route_to_facility.py} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename care/facility/migrations/{0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py => 0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py} (97%) rename care/facility/migrations/{0393_clean_admission_date_and_populate_route_to_facility.py => 0394_clean_admission_date_and_populate_route_to_facility.py} (97%) diff --git a/care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py b/care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py similarity index 97% rename from care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py rename to care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py index 3ed9d28dcb..dff329e111 100644 --- a/care/facility/migrations/0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py +++ b/care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ("facility", "0391_merge_20231016_1845"), + ("facility", "0392_alter_dailyround_consciousness_level"), ] operations = [ diff --git a/care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py b/care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py similarity index 97% rename from care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py rename to care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py index c450b8461f..a88bb8aaf2 100644 --- a/care/facility/migrations/0393_clean_admission_date_and_populate_route_to_facility.py +++ b/care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py @@ -12,7 +12,7 @@ class Migration(migrations.Migration): dependencies = [ ( "facility", - "0392_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", + "0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", ), ] From 86ccb04b46ac4dfcad0dfc5dca67fc1396c62c47 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 14 Nov 2023 11:56:44 +0530 Subject: [PATCH 07/10] Rename and Alter field route to facility instead. Rebase migrations. --- ...onsultation_route_to_facility_and_more.py} | 23 +++-------- ..._patientconsultation_route_to_facility.py} | 38 +++++++++++++------ care/facility/models/patient_base.py | 14 ------- care/facility/models/patient_consultation.py | 6 --- 4 files changed, 32 insertions(+), 49 deletions(-) rename care/facility/migrations/{0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py => 0394_rename_consultation_status_patientconsultation_route_to_facility_and_more.py} (72%) rename care/facility/migrations/{0394_clean_admission_date_and_populate_route_to_facility.py => 0395_alter_patientconsultation_route_to_facility.py} (64%) diff --git a/care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py b/care/facility/migrations/0394_rename_consultation_status_patientconsultation_route_to_facility_and_more.py similarity index 72% rename from care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py rename to care/facility/migrations/0394_rename_consultation_status_patientconsultation_route_to_facility_and_more.py index dff329e111..51c6db6345 100644 --- a/care/facility/migrations/0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more.py +++ b/care/facility/migrations/0394_rename_consultation_status_patientconsultation_route_to_facility_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-10-17 05:51 +# Generated by Django 4.2.5 on 2023-11-14 06:22 import django.db.models.deletion from django.db import migrations, models @@ -6,14 +6,17 @@ class Migration(migrations.Migration): dependencies = [ - ("facility", "0392_alter_dailyround_consciousness_level"), + ( + "facility", + "0393_rename_diagnosis_patientconsultation_deprecated_diagnosis_and_more", + ), ] operations = [ migrations.RenameField( model_name="patientconsultation", old_name="consultation_status", - new_name="deprecated_consultation_status", + new_name="route_to_facility", ), migrations.RenameField( model_name="patientconsultation", @@ -45,20 +48,6 @@ class Migration(migrations.Migration): name="referred_from_facility_external", field=models.TextField(blank=True, default="", null=True), ), - migrations.AddField( - model_name="patientconsultation", - name="route_to_facility", - field=models.SmallIntegerField( - blank=True, - choices=[ - (None, "(Unknown)"), - (10, "Outpatient/Emergency Room"), - (20, "Referred from another facility"), - (30, "Internal Transfer within the facility"), - ], - null=True, - ), - ), migrations.AddField( model_name="patientconsultation", name="transferred_from_location", diff --git a/care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py b/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py similarity index 64% rename from care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py rename to care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py index a88bb8aaf2..261d232d13 100644 --- a/care/facility/migrations/0394_clean_admission_date_and_populate_route_to_facility.py +++ b/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py @@ -1,8 +1,8 @@ -# Generated by Django 4.2.5 on 2023-10-17 05:51 +# Generated by Django 4.2.5 on 2023-11-14 06:23 import datetime -from django.db import migrations +from django.db import migrations, models from django.db.models import DurationField, ExpressionWrapper, F from django.db.models.functions import TruncDay from django.utils import timezone @@ -12,7 +12,7 @@ class Migration(migrations.Migration): dependencies = [ ( "facility", - "0393_rename_consultation_status_patientconsultation_deprecated_consultation_status_and_more", + "0394_rename_consultation_status_patientconsultation_route_to_facility_and_more", ), ] @@ -53,26 +53,40 @@ def clean_admission_date(apps, schema_editor): ) ) - def populate_route_to_facility(apps, schema_editor): + def migrate_route_to_facility(apps, schema_editor): PatientConsultation = apps.get_model("facility", "PatientConsultation") qs = PatientConsultation.objects.all() + # Unknown -> None + qs.filter(route_to_facility=0).update(route_to_facility=None) # Brought Dead/Outpatient -> Outpatient/Emergency Room - qs.filter(deprecated_consultation_status=1).update(route_to_facility=10) - qs.filter(deprecated_consultation_status=5).update(route_to_facility=10) - + qs.filter(route_to_facility=1).update(route_to_facility=10) + qs.filter(route_to_facility=5).update(route_to_facility=10) # Transferred from Ward/ICU -> Internal Transfer within facility - qs.filter(deprecated_consultation_status=2).update(route_to_facility=30) - qs.filter(deprecated_consultation_status=3).update(route_to_facility=30) - + qs.filter(route_to_facility=2).update(route_to_facility=30) + qs.filter(route_to_facility=3).update(route_to_facility=30) # Referred from other hospital -> Referred from another facility - qs.filter(deprecated_consultation_status=4).update(route_to_facility=20) + qs.filter(route_to_facility=4).update(route_to_facility=20) operations = [ migrations.RunPython( clean_admission_date, reverse_code=migrations.RunPython.noop ), + migrations.AlterField( + model_name="patientconsultation", + name="route_to_facility", + field=models.SmallIntegerField( + blank=True, + choices=[ + (None, "(Unknown)"), + (10, "Outpatient/Emergency Room"), + (20, "Referred from another facility"), + (30, "Internal Transfer within the facility"), + ], + null=True, + ), + ), migrations.RunPython( - populate_route_to_facility, reverse_code=migrations.RunPython.noop + migrate_route_to_facility, reverse_code=migrations.RunPython.noop ), ] diff --git a/care/facility/models/patient_base.py b/care/facility/models/patient_base.py index dce09a74f9..200d34014c 100644 --- a/care/facility/models/patient_base.py +++ b/care/facility/models/patient_base.py @@ -109,20 +109,6 @@ class DiseaseStatusEnum(enum.IntEnum): SuggestionChoices = SimpleNamespace(HI="HI", A="A", R="R", OP="OP", DC="DC", DD="DD") -class DeprecatedConsultationStatusEnum(enum.Enum): - UNKNOWN = 0 - BROUGHT_DEAD = 1 - TRANSFERRED_FROM_WARD = 2 - TRANSFERRED_FROM_ICU = 3 - REFERRED_FROM_OTHER_HOSPITAL = 4 - OUT_PATIENT = 5 - - -DeprecatedConsultationStatusChoices = [ - (e.value, e.name) for e in DeprecatedConsultationStatusEnum -] - - class RouteToFacility(IntegerChoices): OUTPATIENT = 10, _("Outpatient/Emergency Room") INTER_FACILITY_TRANSFER = 20, _("Referred from another facility") diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index dfdda9dffc..b5e1bb02b1 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -18,8 +18,6 @@ REVERSE_CATEGORY_CHOICES, REVERSE_COVID_CATEGORY_CHOICES, SYMPTOM_CHOICES, - DeprecatedConsultationStatusChoices, - DeprecatedConsultationStatusEnum, RouteToFacility, SuggestionChoices, reverse_choices, @@ -98,10 +96,6 @@ class PatientConsultation(PatientBaseModel, ConsultationRelatedPermissionMixin): prescriptions = JSONField(default=dict) # Deprecated procedure = JSONField(default=dict) suggestion = models.CharField(max_length=4, choices=SUGGESTION_CHOICES) - deprecated_consultation_status = models.IntegerField( - default=DeprecatedConsultationStatusEnum.UNKNOWN.value, - choices=DeprecatedConsultationStatusChoices, - ) # Deprecated in favour of `route_to_facility` route_to_facility = models.SmallIntegerField( choices=RouteToFacility.choices, blank=True, null=True ) From fbf1232583b367f213de9f9e9553027950ea758e Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 14 Nov 2023 12:13:32 +0530 Subject: [PATCH 08/10] update tests --- care/facility/tests/test_patient_consultation_api.py | 10 ++++++++++ data/dummy/facility.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index df8cbe140a..e6a2026a7e 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -56,6 +56,13 @@ def create_route_to_facility_consultation( "patient": patient.external_id, "facility": self.facility.external_id, "route_to_facility": route_to_facility, + "create_diagnoses": [ + { + "diagnosis": ICD11Diagnosis.objects.first().id, + "is_principal": False, + "verification_status": ConditionVerificationStatus.CONFIRMED, + } + ], } ) data.update(kwargs) @@ -303,12 +310,14 @@ def test_route_to_facility_referred_from_facility_external(self): res = self.create_route_to_facility_consultation( route_to_facility=20, referred_from_facility_external="Test" ) + print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_route_to_facility_referred_from_facility(self): res = self.create_route_to_facility_consultation( route_to_facility=20, referred_from_facility=self.facility.external_id ) + print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_route_to_facility_referred_from_facility_and_external_together(self): @@ -327,6 +336,7 @@ def test_route_to_facility_transfer_within_facility(self): res = self.create_route_to_facility_consultation( route_to_facility=30, transferred_from_location=self.location.external_id ) + print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_medico_legal_case(self): diff --git a/data/dummy/facility.json b/data/dummy/facility.json index ec7d1065ca..6e3b5f4072 100644 --- a/data/dummy/facility.json +++ b/data/dummy/facility.json @@ -1282,7 +1282,7 @@ "prescriptions": {}, "procedure": {}, "suggestion": "R", - "consultation_status": 0, + "route_to_facility": null, "review_interval": -1, "referred_to": 2, "is_readmission": false, From 5ddcbd28477e999de8862658df1a55344cc8baff Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 14 Nov 2023 12:19:27 +0530 Subject: [PATCH 09/10] fix tests, season 2 --- .../tests/test_patient_consultation_api.py | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index e6a2026a7e..e5921283db 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -39,6 +39,13 @@ def get_default_data(self): "treatment_plan": "treatment_plan", "suggestion": PatientConsultation.SUGGESTION_CHOICES[0][0], "treating_physician": self.doctor.id, + "create_diagnoses": [ + { + "diagnosis": ICD11Diagnosis.objects.first().id, + "is_principal": False, + "verification_status": ConditionVerificationStatus.CONFIRMED, + } + ], } def get_url(self, consultation=None): @@ -56,17 +63,10 @@ def create_route_to_facility_consultation( "patient": patient.external_id, "facility": self.facility.external_id, "route_to_facility": route_to_facility, - "create_diagnoses": [ - { - "diagnosis": ICD11Diagnosis.objects.first().id, - "is_principal": False, - "verification_status": ConditionVerificationStatus.CONFIRMED, - } - ], } ) data.update(kwargs) - return self.client.post(self.get_url(), data) + return self.client.post(self.get_url(), data, format="json") def create_admission_consultation(self, patient=None, **kwargs): patient = patient or self.create_patient(self.district, self.facility) @@ -75,13 +75,6 @@ def create_admission_consultation(self, patient=None, **kwargs): { "patient": patient.external_id, "facility": self.facility.external_id, - "create_diagnoses": [ - { - "diagnosis": ICD11Diagnosis.objects.first().id, - "is_principal": False, - "verification_status": ConditionVerificationStatus.CONFIRMED, - } - ], } ) data.update(kwargs) @@ -310,14 +303,12 @@ def test_route_to_facility_referred_from_facility_external(self): res = self.create_route_to_facility_consultation( route_to_facility=20, referred_from_facility_external="Test" ) - print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_route_to_facility_referred_from_facility(self): res = self.create_route_to_facility_consultation( route_to_facility=20, referred_from_facility=self.facility.external_id ) - print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_route_to_facility_referred_from_facility_and_external_together(self): @@ -336,7 +327,6 @@ def test_route_to_facility_transfer_within_facility(self): res = self.create_route_to_facility_consultation( route_to_facility=30, transferred_from_location=self.location.external_id ) - print(res.data) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_medico_legal_case(self): From 031e957bea026ef6369735a1ca249384cced26f2 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Tue, 14 Nov 2023 13:18:41 +0530 Subject: [PATCH 10/10] Apply suggestions based on Code Review --- ...0395_alter_patientconsultation_route_to_facility.py | 10 ++++++---- care/facility/models/patient_base.py | 1 - 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py b/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py index 261d232d13..1d65685adb 100644 --- a/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py +++ b/care/facility/migrations/0395_alter_patientconsultation_route_to_facility.py @@ -60,11 +60,13 @@ def migrate_route_to_facility(apps, schema_editor): # Unknown -> None qs.filter(route_to_facility=0).update(route_to_facility=None) # Brought Dead/Outpatient -> Outpatient/Emergency Room - qs.filter(route_to_facility=1).update(route_to_facility=10) - qs.filter(route_to_facility=5).update(route_to_facility=10) + qs.filter(models.Q(route_to_facility=1) | models.Q(route_to_facility=5)).update( + route_to_facility=10 + ) # Transferred from Ward/ICU -> Internal Transfer within facility - qs.filter(route_to_facility=2).update(route_to_facility=30) - qs.filter(route_to_facility=3).update(route_to_facility=30) + qs.filter(models.Q(route_to_facility=2) | models.Q(route_to_facility=3)).update( + route_to_facility=30 + ) # Referred from other hospital -> Referred from another facility qs.filter(route_to_facility=4).update(route_to_facility=20) diff --git a/care/facility/models/patient_base.py b/care/facility/models/patient_base.py index 200d34014c..0fd2d40eab 100644 --- a/care/facility/models/patient_base.py +++ b/care/facility/models/patient_base.py @@ -113,7 +113,6 @@ class RouteToFacility(IntegerChoices): OUTPATIENT = 10, _("Outpatient/Emergency Room") INTER_FACILITY_TRANSFER = 20, _("Referred from another facility") INTRA_FACILITY_TRANSFER = 30, _("Internal Transfer within the facility") - __empty__ = _("(Unknown)")