Skip to content

Commit

Permalink
fix(asset): updated to field level validation
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Nov 8, 2023
1 parent 1a88ef9 commit 94fcef3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core.cache import cache
from django.db import transaction
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils.timezone import now
from rest_framework import serializers
Expand Down Expand Up @@ -100,22 +101,23 @@ class Meta:
"duty_staff",
)

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

Check warning on line 106 in care/facility/api/serializers/asset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/asset.py#L106

Added line #L106 was not covered by tests

duty_staff_ids = [user.id for user in duty_staff]
duty_staff_ids = [user.id for user in value]
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"}
)
count = User.objects.filter(
Q(home_facility__id=facility_id) & Q(id__in=duty_staff_ids)
).count()

return super().validate(attrs)
if count != len(duty_staff_ids):
raise ValidationError(

Check warning on line 116 in care/facility/api/serializers/asset.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/serializers/asset.py#L116

Added line #L116 was not covered by tests
{"duty_staff": "Only Home Facility Doctors and Staffs are allowed"}
)

return value


class AssetBareMinimumSerializer(ModelSerializer):
Expand Down

0 comments on commit 94fcef3

Please sign in to comment.