Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds doctor and staff fields in location model #1264

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
StatusChoices,
UserDefaultAssetLocation,
)
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.users.api.serializers.user import (
UserAssignedSerializer,
UserBaseMinimumSerializer,
)
from care.utils.assetintegration.hl7monitor import HL7MonitorAsset
from care.utils.assetintegration.onvif import OnvifAsset
from care.utils.assetintegration.ventilator import VentilatorAsset
Expand All @@ -40,6 +43,11 @@
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 +73,14 @@ def validate(self, data):
"name": "Asset location with this name and facility already exists."
}
)

if "duty_staff" in data:
duty_staffs_objects = len(data["duty_staff"])
if duty_staffs_objects > 3:
raise ValidationError(
{"duty_staff": "Only 3 duty staffs can be assigned"}
)

return data

class Meta:
Expand Down
1 change: 1 addition & 0 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
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,
),
),
]
12 changes: 12 additions & 0 deletions care/facility/migrations/0393_merge_20231020_1752.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.5 on 2023-10-20 12:22

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("facility", "0389_add_duty_staff"),
("facility", "0392_alter_dailyround_consciousness_level"),
]

operations = []
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]
)