Skip to content

Commit

Permalink
feat(assetLocation): added remove duty_staff endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhiuday authored and aeswibon committed Nov 27, 2023
1 parent ed31800 commit 9b32b4b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
26 changes: 22 additions & 4 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def perform_create(self, serializer):

@extend_schema(tags=["asset_location"])
@action(methods=["POST"], detail=True)
def duty_staff(self, request, facility_external_id, external_id):
def add_duty_staff(self, request, facility_external_id, external_id):
"""
Endpoint for assigning staffs to asset location
"""
Expand All @@ -139,9 +139,11 @@ def duty_staff(self, request, facility_external_id, external_id):
{"duty_staff": "Only Home Facility Doctors ayynd Staffs are allowed"}
)

AssetLocationDutyStaff.objects.filter(asset_location=asset).update(deleted=True)

users = User.objects.filter(id__in=duty_staff)
users = User.objects.filter(id__in=duty_staff).exclude(
id__in=AssetLocationDutyStaff.objects.filter(
asset_location=asset, user__id__in=duty_staff
).values_list("user__id", flat=True)
)
with transaction.atomic():
duty_staff_objects = []
for user in users:
Expand All @@ -157,6 +159,22 @@ def duty_staff(self, request, facility_external_id, external_id):

return Response(status=status.HTTP_201_CREATED)

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

asset: AssetLocation = self.get_object()
duty_staff = request.data.get("duty_staff", [])

AssetLocationDutyStaff.objects.filter(
asset_location=asset, user__id__in=duty_staff
).update(deleted=True)

return Response(status=status.HTTP_204_NO_CONTENT)


class AssetFilter(filters.FilterSet):
facility = filters.UUIDFilter(field_name="current_location__facility__external_id")
Expand Down
12 changes: 12 additions & 0 deletions care/facility/migrations/0397_merge_20231127_1259.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.5 on 2023-11-27 07:29

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("facility", "0395_assetlocationdutystaff_assetlocation_users"),
("facility", "0396_merge_20231122_0240"),
]

operations = []
36 changes: 35 additions & 1 deletion care/facility/tests/test_asset_location_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,42 @@ def test_assign_duty_staff(self):
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/",
f"/api/v1/facility/{self.facility.external_id}/asset_location/{self.asset_location.external_id}/add_duty_staff/",
data,
)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_remove_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}/add_duty_staff/",
data,
)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

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

self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

0 comments on commit 9b32b4b

Please sign in to comment.