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

Removed redundant room types in FacilityCapacity #2417

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions care/facility/api/serializers/facility_capacity.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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", "0453_merge_20240824_2040"),
]

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.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"),
]
),
),
]
13 changes: 11 additions & 2 deletions care/facility/models/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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"),
Expand Down Expand Up @@ -374,7 +383,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)])

Expand Down Expand Up @@ -410,7 +419,7 @@ def __str__(self):
return (
str(self.facility)
+ " "
+ REVERSE_ROOM_TYPES[self.room_type]
+ RoomType(self.room_type).label
+ " "
+ str(self.total_capacity)
)
Expand Down
8 changes: 4 additions & 4 deletions data/dummy/facility.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,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
}
Expand All @@ -788,7 +788,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
}
Expand All @@ -802,7 +802,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
}
Expand All @@ -816,7 +816,7 @@
"modified_date": "2023-09-15T06:12:50.165Z",
"deleted": false,
"facility": 3,
"room_type": 20,
"room_type": 100,
"total_capacity": 31,
"current_capacity": 2
}
Expand Down