diff --git a/care/facility/api/serializers/patient.py b/care/facility/api/serializers/patient.py index e94fa0d25c..b38cc60993 100644 --- a/care/facility/api/serializers/patient.py +++ b/care/facility/api/serializers/patient.py @@ -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 = ( @@ -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 ( diff --git a/care/facility/models/tests/test_patient.py b/care/facility/models/tests/test_patient.py index d00b3e8d93..00f7e5e7df 100644 --- a/care/facility/models/tests/test_patient.py +++ b/care/facility/models/tests/test_patient.py @@ -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 @@ -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)