diff --git a/care/facility/api/serializers/facility_capacity.py b/care/facility/api/serializers/facility_capacity.py index 887f386235..4db943c69c 100644 --- a/care/facility/api/serializers/facility_capacity.py +++ b/care/facility/api/serializers/facility_capacity.py @@ -1,12 +1,14 @@ from rest_framework import serializers from care.facility.api.serializers import TIMESTAMP_FIELDS -from care.facility.models import ROOM_TYPES, FacilityCapacity +from care.facility.models import FacilityCapacity, RoomType from config.serializers import ChoiceField class FacilityCapacitySerializer(serializers.ModelSerializer): - room_type_text = ChoiceField(choices=ROOM_TYPES, read_only=True, source="room_type") + room_type_text = ChoiceField( + choices=RoomType.choices, read_only=True, source="room_type" + ) id = serializers.UUIDField(source="external_id", read_only=True) def validate(self, data): diff --git a/care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py b/care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py new file mode 100644 index 0000000000..80bb7eb767 --- /dev/null +++ b/care/facility/migrations/0464_alter_facilitycapacity_room_type_and_more.py @@ -0,0 +1,90 @@ +# Generated by Django 4.2.2 on 2024-09-02 09:42 + +from django.db import migrations, models + +from care.facility.models import RoomType + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0463_patientnotes_reply_to"), + ] + + def migrate_room_type(apps, schema_editor): + FacilityCapacity = apps.get_model("facility", "FacilityCapacity") + + room_type_migration_map = { + 1: RoomType.GENERAL_BED, # General Bed + 10: RoomType.ICU_BED, # ICU + 20: RoomType.ICU_BED, # Ventilator + 30: RoomType.GENERAL_BED, # Covid Beds + 100: RoomType.ICU_BED, # Covid Ventilators + 110: RoomType.ICU_BED, # Covid ICU + 120: RoomType.OXYGEN_BED, # Covid Oxygen beds + 150: RoomType.OXYGEN_BED, # Oxygen beds + 0: RoomType.OTHER, # Total + 2: RoomType.OTHER, # Hostel + 3: RoomType.ISOLATION_BED, # Single Room with Attached Bathroom + 40: RoomType.GENERAL_BED, # KASP Beds + 50: RoomType.ICU_BED, # KASP ICU beds + 60: RoomType.OXYGEN_BED, # KASP Oxygen beds + 70: RoomType.ICU_BED, # KASP Ventilator beds + } + + merged_facility_capacities = {} + + for old_type, new_type in room_type_migration_map.items(): + facility_capacities = FacilityCapacity.objects.filter(room_type=old_type) + + for facility_capacity in facility_capacities: + key = (facility_capacity.facility.external_id, new_type) + + if key not in merged_facility_capacities: + merged_facility_capacities[key] = { + "facility": facility_capacity.facility, + "room_type": new_type, + "total_capacity": facility_capacity.total_capacity, + "current_capacity": facility_capacity.current_capacity, + } + else: + merged_facility_capacities[key]["total_capacity"] += ( + facility_capacity.total_capacity + ) + merged_facility_capacities[key]["current_capacity"] += ( + facility_capacity.current_capacity + ) + + facility_capacity.delete() + + for data in merged_facility_capacities.values(): + FacilityCapacity.objects.create(**data) + + operations = [ + migrations.RunPython(migrate_room_type, migrations.RunPython.noop), + migrations.AlterField( + model_name="facilitycapacity", + name="room_type", + field=models.IntegerField( + choices=[ + (100, "ICU Bed"), + (200, "Ordinary Bed"), + (300, "Oxygen Bed"), + (400, "Isolation Bed"), + (500, "Others"), + ] + ), + ), + migrations.AlterField( + model_name="historicalfacilitycapacity", + name="room_type", + field=models.IntegerField( + choices=[ + (100, "ICU Bed"), + (200, "Ordinary Bed"), + (300, "Oxygen Bed"), + (400, "Isolation Bed"), + (500, "Others"), + ] + ), + ), + ] diff --git a/care/facility/models/facility.py b/care/facility/models/facility.py index 209dbc3869..aa929acc7c 100644 --- a/care/facility/models/facility.py +++ b/care/facility/models/facility.py @@ -43,6 +43,15 @@ (70, "KASP Ventilator beds"), ] + +class RoomType(models.IntegerChoices): + ICU_BED = 100, "ICU Bed" + GENERAL_BED = 200, "Ordinary Bed" + OXYGEN_BED = 300, "Oxygen Bed" + ISOLATION_BED = 400, "Isolation Bed" + OTHER = 500, "Others" + + # to be removed in further PR FEATURE_CHOICES = [ (1, "CT Scan Facility"), @@ -426,7 +435,7 @@ class FacilityCapacity(FacilityBaseModel, FacilityRelatedPermissionMixin): facility = models.ForeignKey( "Facility", on_delete=models.CASCADE, null=False, blank=False ) - room_type = models.IntegerField(choices=ROOM_TYPES) + room_type = models.IntegerField(choices=RoomType.choices) total_capacity = models.IntegerField(default=0, validators=[MinValueValidator(0)]) current_capacity = models.IntegerField(default=0, validators=[MinValueValidator(0)]) @@ -462,7 +471,7 @@ def __str__(self): return ( str(self.facility) + " " - + REVERSE_ROOM_TYPES[self.room_type] + + RoomType(self.room_type).label + " " + str(self.total_capacity) ) diff --git a/data/dummy/facility.json b/data/dummy/facility.json index b33eaf7ad8..cea1986085 100644 --- a/data/dummy/facility.json +++ b/data/dummy/facility.json @@ -792,7 +792,7 @@ "modified_date": "2022-09-27T07:00:19.399Z", "deleted": false, "facility": 1, - "room_type": 150, + "room_type": 300, "total_capacity": 1000, "current_capacity": 20 } @@ -806,7 +806,7 @@ "modified_date": "2022-09-27T07:16:52.525Z", "deleted": false, "facility": 2, - "room_type": 150, + "room_type": 300, "total_capacity": 20, "current_capacity": 1 } @@ -820,7 +820,7 @@ "modified_date": "2023-09-15T06:12:31.548Z", "deleted": false, "facility": 4, - "room_type": 150, + "room_type": 300, "total_capacity": 12, "current_capacity": 2 }