diff --git a/care/facility/migrations/0414_remove_bed_old_name.py b/care/facility/migrations/0414_remove_bed_old_name.py new file mode 100644 index 0000000000..64a03775e8 --- /dev/null +++ b/care/facility/migrations/0414_remove_bed_old_name.py @@ -0,0 +1,16 @@ +# Generated by Django 4.2.10 on 2024-02-14 11:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0413_eventtype_patientconsultationevent_and_more"), + ] + + operations = [ + migrations.RemoveField( + model_name="bed", + name="old_name", + ), + ] diff --git a/care/facility/models/bed.py b/care/facility/models/bed.py index bca71296de..d701dba41a 100644 --- a/care/facility/models/bed.py +++ b/care/facility/models/bed.py @@ -4,6 +4,7 @@ However this is an addon feature and is not required for the regular patient flow, Leaving scope to build rooms and wards to being even more organization. """ + from django.core.exceptions import ValidationError from django.db import models from django.db.models import JSONField @@ -17,7 +18,6 @@ class Bed(BaseModel): name = models.CharField(max_length=1024) - old_name = models.CharField(max_length=1024, null=True, blank=True) description = models.TextField(default="", blank=True) bed_type = models.IntegerField( choices=BedTypeChoices, default=BedType.REGULAR.value @@ -49,7 +49,7 @@ def __str__(self): def validate(self) -> None: if ( - Bed.objects.filter(location=self.location, name=self.name) + Bed.objects.filter(location=self.location, name__iexact=self.name) .exclude(pk=self.pk) .exists() ): diff --git a/care/facility/tests/test_bed_create.py b/care/facility/tests/test_bed_create.py index b9d2dedcfc..26a18a69be 100644 --- a/care/facility/tests/test_bed_create.py +++ b/care/facility/tests/test_bed_create.py @@ -33,6 +33,36 @@ def test_create(self): sample_data["number_of_beds"], ) + def test_create_with_same_name(self): + sample_data = { + "bed_type": "REGULAR", + "description": "Testing creation of beds.", + "facility": self.facility.external_id, + "location": self.asset_location.external_id, + "name": "Test Bed", + "number_of_beds": 1, + } + response = self.client.post("/api/v1/bed/", sample_data, format="json") + self.assertIs(response.status_code, status.HTTP_201_CREATED) + + response = self.client.post("/api/v1/bed/", sample_data, format="json") + self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual( + response.json()["name"], + ["Bed with same name already exists in location."], + ) + + # validate case insensitive + sample_data["name"] = "test bed" + response = self.client.post("/api/v1/bed/", sample_data, format="json") + self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual( + response.json()["name"], + ["Bed with same name already exists in location."], + ) + + self.assertEqual(Bed.objects.filter(facility=self.facility).count(), 1) + class MultipleBedTest(TestUtils, APITestCase): @classmethod