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

Add age validation in PatientDetailSerializer #1929

Merged
merged 13 commits into from
Apr 22, 2024
13 changes: 13 additions & 0 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class Meta:
)
abha_number_object = AbhaNumberSerializer(source="abha_number", read_only=True)

date_of_birth = serializers.DateField(required=False, allow_null=True)
year_of_birth = serializers.IntegerField(default=0)

class Meta:
model = PatientRegistration
exclude = (
Expand Down Expand Up @@ -251,6 +254,16 @@ def validate_countries_travelled(self, value):
value = [value]
return value

def validate_date_of_birth(self, value):
if value and value > now().date():
raise serializers.ValidationError("Enter a valid DOB such that age > 0")
return value

def validate_year_of_birth(self, value):
if value and value > now().year:
raise serializers.ValidationError("Enter a valid year of birth")
return value

def validate(self, attrs):
validated = super().validate(attrs)
if not self.partial and not (
Expand Down
40 changes: 40 additions & 0 deletions care/facility/models/tests/test_patient.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from datetime import timedelta

from django.utils.timezone import now
from rest_framework import status
from rest_framework.test import APITestCase

from care.facility.models import DiseaseStatusEnum
Expand All @@ -23,3 +27,39 @@ def test_disease_state_recovery_is_aliased_to_recovered(self):
patient.refresh_from_db()

self.assertEqual(patient.disease_status, DiseaseStatusEnum.RECOVERED.value)

def test_date_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"date_of_birth": now().date() + timedelta(days=365),
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("date_of_birth", response.data)

def test_year_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"year_of_birth": now().year + 1,
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("year_of_birth", response.data)
Loading