Skip to content

Commit

Permalink
fix(asset_location): added duty_staff endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Nov 4, 2023
1 parent 1a232d7 commit 1a88ef9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
44 changes: 43 additions & 1 deletion care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
StatusChoices,
UserDefaultAssetLocation,
)
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.users.api.serializers.user import (
UserAssignedSerializer,
UserBaseMinimumSerializer,
)
from care.users.models import User
from care.utils.assetintegration.hl7monitor import HL7MonitorAsset
from care.utils.assetintegration.onvif import OnvifAsset
from care.utils.assetintegration.ventilator import VentilatorAsset
Expand All @@ -41,6 +45,12 @@ 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()
if not value:
Expand Down Expand Up @@ -76,6 +86,38 @@ class Meta:
read_only_fields = TIMESTAMP_FIELDS


class AssetLocationDutyStaffSerializer(ModelSerializer):
duty_staff_objects = UserAssignedSerializer(
many=True,
read_only=True,
source="duty_staff",
)

class Meta:
model = AssetLocation
fields = (
"duty_staff_objects",
"duty_staff",
)

def validate(self, attrs):
duty_staff = attrs.get("duty_staff", [])
if not duty_staff:
raise ValidationError({"duty_staff": "Duty staff cannot be empty"})

duty_staff_ids = [user.id for user in duty_staff]
facility_id = self.instance.facility.id

users = User.objects.filter(id__in=duty_staff_ids)
for user in users:
if user.home_facility.id != facility_id:
raise ValidationError(
{"duty_staff": "Only Home Facility Doctors and Staffs are allowed"}
)

return super().validate(attrs)


class AssetBareMinimumSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)

Expand Down
22 changes: 22 additions & 0 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from care.facility.api.serializers.asset import (
AssetAvailabilitySerializer,
AssetLocationDutyStaffSerializer,
AssetLocationSerializer,
AssetSerializer,
AssetServiceSerializer,
Expand Down Expand Up @@ -86,6 +87,11 @@ class AssetLocationViewSet(
filter_backends = (drf_filters.SearchFilter,)
search_fields = ["name"]

def get_serializer_class(self):
if self.action == "duty_staff":
return AssetLocationDutyStaffSerializer
return super().get_serializer_class()

def get_serializer_context(self):
facility = self.get_facility()
context = super().get_serializer_context()
Expand Down Expand Up @@ -118,6 +124,22 @@ def get_facility(self):
def perform_create(self, serializer):
serializer.save(facility=self.get_facility())

@extend_schema(tags=["asset_location"])
@action(methods=["POST"], detail=True)
def duty_staff(self, request, facility_external_id, external_id):
"""
Endpoint for assigning staffs to asset location
"""

asset = self.get_object()
serializer = AssetLocationDutyStaffSerializer(asset, data=request.data)
serializer.is_valid(raise_exception=True)

staffs = serializer.validated_data["duty_staff"]
asset.duty_staff.set(staffs)

return Response(self.get_serializer(asset).data)


class AssetFilter(filters.FilterSet):
facility = filters.UUIDFilter(field_name="current_location__facility__external_id")
Expand Down
3 changes: 3 additions & 0 deletions care/facility/api/viewsets/facility_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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.UUIDFilter(
field_name="home_facility__external_id", lookup_expr="exact"
)

class Meta:
model = User
Expand Down
19 changes: 19 additions & 0 deletions care/facility/migrations/0393_assetlocation_duty_staff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.5 on 2023-10-26 14:45

from django.conf import settings
from django.db import migrations, models


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

operations = [
migrations.AddField(
model_name="assetlocation",
name="duty_staff",
field=models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL),
),
]
5 changes: 5 additions & 0 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class RoomType(enum.Enum):
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
28 changes: 28 additions & 0 deletions care/facility/tests/test_asset_location_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,31 @@ def test_create_asset_location_invalid_middleware(self):
self.assertEqual(
response.data["middleware_address"][0].code, "invalid_domain_name"
)

def test_assign_duty_staff(self):
# creating sample doctor and staff

created_users = []
user_data = [
("doctor1", 15),
("staff1", 10),
]

for user_name, user_type in user_data:
created_user = self.create_user(
user_name,
self.district,
home_facility=self.facility,
user_type=user_type,
)
created_users.append(created_user)

data = {"duty_staff": [user.id for user in created_users]}

response = self.client.post(
f"/api/v1/facility/{self.facility.external_id}/asset_location/{self.asset_location.external_id}/duty_staff/",
data,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()["duty_staff_objects"]), 2)

0 comments on commit 1a88ef9

Please sign in to comment.