From 3bd5e4e048dd3b7cc7fba53d9b903dfca345ef54 Mon Sep 17 00:00:00 2001 From: Rithvik Nishad Date: Wed, 23 Oct 2024 20:02:21 +0530 Subject: [PATCH] fixes validation preventing linking multiple cameras to a bed (#2559) --- care/facility/api/serializers/bed.py | 17 ++++++----- care/facility/tests/test_asset_bed_api.py | 36 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/care/facility/api/serializers/bed.py b/care/facility/api/serializers/bed.py index 508c2f9619..031d2a68c1 100644 --- a/care/facility/api/serializers/bed.py +++ b/care/facility/api/serializers/bed.py @@ -111,6 +111,10 @@ def validate(self, attrs): not facilities.filter(id=asset.current_location.facility.id).exists() ) or (not facilities.filter(id=bed.facility.id).exists()): raise PermissionError + if AssetBed.objects.filter(asset=asset, bed=bed).exists(): + raise ValidationError( + {"non_field_errors": "Asset is already linked to bed"} + ) if asset.asset_class not in [ AssetClasses.HL7MONITOR.name, AssetClasses.ONVIF.name, @@ -123,18 +127,15 @@ def validate(self, attrs): {"asset": "Should be in the same facility as the bed"} ) if ( - asset.asset_class - in [ - AssetClasses.HL7MONITOR.name, - AssetClasses.ONVIF.name, - ] + asset.asset_class == AssetClasses.HL7MONITOR.name + and AssetBed.objects.filter( + bed=bed, asset__asset_class=asset.asset_class + ).exists() ) and AssetBed.objects.filter( bed=bed, asset__asset_class=asset.asset_class ).exists(): raise ValidationError( - { - "asset": "Bed is already in use by another asset of the same class" - } + {"asset": "Another HL7 Monitor is already linked to this bed."} ) else: raise ValidationError( diff --git a/care/facility/tests/test_asset_bed_api.py b/care/facility/tests/test_asset_bed_api.py index d22aae9bfd..4ed81a36b8 100644 --- a/care/facility/tests/test_asset_bed_api.py +++ b/care/facility/tests/test_asset_bed_api.py @@ -21,9 +21,21 @@ def setUpTestData(cls): ) cls.asset_location = cls.create_asset_location(cls.facility) cls.asset = cls.create_asset(cls.asset_location) + cls.monitor_asset_1 = cls.create_asset( + cls.asset_location, asset_class=AssetClasses.HL7MONITOR.name + ) + cls.monitor_asset_2 = cls.create_asset( + cls.asset_location, asset_class=AssetClasses.HL7MONITOR.name + ) cls.camera_asset = cls.create_asset( cls.asset_location, asset_class=AssetClasses.ONVIF.name ) + cls.camera_asset_1 = cls.create_asset( + cls.asset_location, asset_class=AssetClasses.ONVIF.name, name="Camera 1" + ) + cls.camera_asset_2 = cls.create_asset( + cls.asset_location, asset_class=AssetClasses.ONVIF.name, name="Camera 2" + ) cls.bed = cls.create_bed(cls.facility, cls.asset_location) def test_link_disallowed_asset_class_asset_to_bed(self): @@ -49,6 +61,30 @@ def test_link_asset_to_bed_and_attempt_duplicate_linking(self): self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data["count"], 1) + def test_linking_multiple_cameras_to_a_bed(self): + data = { + "asset": self.camera_asset_1.external_id, + "bed": self.bed.external_id, + } + res = self.client.post("/api/v1/assetbed/", data) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + # Attempt linking another camera to same bed. + data["asset"] = self.camera_asset_2.external_id + res = self.client.post("/api/v1/assetbed/", data) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + + def test_linking_multiple_hl7_monitors_to_a_bed(self): + data = { + "asset": self.monitor_asset_1.external_id, + "bed": self.bed.external_id, + } + res = self.client.post("/api/v1/assetbed/", data) + self.assertEqual(res.status_code, status.HTTP_201_CREATED) + # Attempt linking another hl7 monitor to same bed. + data["asset"] = self.monitor_asset_2.external_id + res = self.client.post("/api/v1/assetbed/", data) + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) + class AssetBedCameraPresetViewSetTestCase(TestUtils, APITestCase): @classmethod