Skip to content

Commit

Permalink
fix(location): added doctor and staff field in location model
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Oct 20, 2023
1 parent 0daef7f commit 67c76af
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
12 changes: 10 additions & 2 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@
StatusChoices,
UserDefaultAssetLocation,
)
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.utils.assetintegration.hl7monitor import HL7MonitorAsset
from care.utils.assetintegration.onvif import OnvifAsset
from care.utils.assetintegration.ventilator import VentilatorAsset
from care.users.api.serializers.user import (
UserAssignedSerializer,
UserBaseMinimumSerializer,
)
from care.utils.queryset.facility import get_facility_queryset
from config.serializers import ChoiceField
from config.validators import MiddlewareDomainAddressValidator


class AssetLocationSerializer(ModelSerializer):
facility = FacilityBareMinimumSerializer(read_only=True)
id = UUIDField(source="external_id", read_only=True)
duty_staff_objects = UserAssignedSerializer(
many=True,
read_only=True,
source="duty_staff",
)

def validate_middleware_address(self, value):
value = (value or "").strip()
Expand All @@ -65,6 +72,7 @@ def validate(self, data):
"name": "Asset location with this name and facility already exists."
}
)

return data

class Meta:
Expand Down
5 changes: 3 additions & 2 deletions care/facility/api/viewsets/facility_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UserFilter(filters.FilterSet):
choices=[(key, key) for key in User.TYPE_VALUE_MAP],
coerce=lambda role: User.TYPE_VALUE_MAP[role],
)
home_facility = filters.CharFilter(field_name="home_facility__external_id")

class Meta:
model = User
Expand Down Expand Up @@ -49,5 +50,5 @@ def get_queryset(self):
queryset=UserSkill.objects.filter(skill__deleted=False),
),
)
except Facility.DoesNotExist:
raise ValidationError({"Facility": "Facility not found"})
except Exception as e:
raise ValidationError({"Facility": "Facility not found"}) from e
20 changes: 20 additions & 0 deletions care/facility/migrations/0389_add_duty_staff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("facility", "0388_goal_goalentry_goalproperty_goalpropertyentry"),
]

operations = [
migrations.AddField(
model_name="assetlocation",
name="duty_staff",
field=models.ManyToManyField(
blank=True,
to=settings.AUTH_USER_MODEL,
),
),
]
14 changes: 12 additions & 2 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class RoomType(enum.Enum):
facility = models.ForeignKey(
Facility, on_delete=models.PROTECT, null=False, blank=False
)
duty_staff = models.ManyToManyField(
User,
blank=True,
)

middleware_address = models.CharField(
null=True, blank=True, default=None, max_length=200
Expand Down Expand Up @@ -79,7 +83,11 @@ class Asset(BaseModel):
choices=AssetTypeChoices, default=AssetType.INTERNAL.value
)
asset_class = models.CharField(
choices=AssetClassChoices, default=None, null=True, blank=True, max_length=20
choices=AssetClassChoices,
default=None,
null=True,
blank=True,
max_length=20,
)
status = models.IntegerField(choices=StatusChoices, default=Status.ACTIVE.value)
current_location = models.ForeignKey(
Expand All @@ -90,7 +98,9 @@ class Asset(BaseModel):
serial_number = models.CharField(max_length=1024, blank=True, null=True)
warranty_details = models.TextField(null=True, blank=True, default="") # Deprecated
meta = JSONField(
default=dict, blank=True, validators=[JSONFieldSchemaValidator(ASSET_META)]
default=dict,
blank=True,
validators=[JSONFieldSchemaValidator(ASSET_META)],
)
# Vendor Details
vendor_name = models.CharField(max_length=1024, blank=True, null=True)
Expand Down
60 changes: 55 additions & 5 deletions care/facility/tests/test_asset_location_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
Test cases for AssetLocationViewSet
"""
from rest_framework import status
from rest_framework.test import APITestCase

from care.users.models import User
from care.utils.tests.test_utils import TestUtils


Expand All @@ -15,12 +19,34 @@ def setUpTestData(cls) -> None:
cls.asset_location = cls.create_asset_location(cls.facility)
cls.user = cls.create_user("staff", cls.district, home_facility=cls.facility)

def get_detail_duty_staff_representation(self, obj=None) -> dict:
"""
Returns duty staff representation
"""
return {
"id": obj.id,
"username": obj.username,
"first_name": obj.first_name,
"last_name": obj.last_name,
"email": obj.email,
"user_type": User.REVERSE_TYPE_MAP[obj.user_type],
}

def get_base_url(self, asset_id=None) -> str:
"""
Returns base url for AssetLocationViewSet
"""
if asset_id is not None:
return f"/api/v1/facility/{self.facility.external_id}/asset_location/{asset_id}/"
return f"/api/v1/facility/{self.facility.external_id}/asset_location/"

def test_list_asset_locations(self):
response = self.client.get(
f"/api/v1/facility/{self.facility.external_id}/asset_location/"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertContains(response, self.asset_location.external_id)
"""
Test list asset locations
"""
res = self.client.get(self.get_base_url())
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertContains(res, self.asset_location.external_id)

def test_retrieve_asset_location(self):
response = self.client.get(
Expand Down Expand Up @@ -75,3 +101,27 @@ def test_create_asset_location_invalid_middleware(self):
self.assertEqual(
response.data["middleware_address"][0].code, "invalid_domain_name"
)

def test_duty_staff_location(self):
"""
Test duty staff location
"""

# adding doctors
doctor = self.create_user(
username="doctor",
district=self.district,
local_body=self.local_body,
home_facility=self.facility,
user_type=15,
)
asset = self.create_asset_location(self.facility)
asset.duty_staff.set([doctor])
asset.save()
res = self.client.get(self.get_base_url(asset.external_id))
self.assertEqual(res.status_code, status.HTTP_200_OK)
duty_staff_objects = res.json()["duty_staff_objects"]
self.assertEqual(len(duty_staff_objects), 1)
self.assertDictContainsSubset(
self.get_detail_duty_staff_representation(doctor), duty_staff_objects[0]
)

0 comments on commit 67c76af

Please sign in to comment.