diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index 216871954b..e2a5867a18 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -1,7 +1,8 @@ from datetime import timedelta +from django.conf import settings from django.db import transaction -from django.utils.timezone import localtime, now +from django.utils.timezone import localtime, make_aware, now from rest_framework import serializers from rest_framework.exceptions import ValidationError @@ -47,6 +48,8 @@ from care.utils.serializer.external_id_field import ExternalIdSerializerField from config.serializers import ChoiceField +MIN_ENCOUNTER_DATE = make_aware(settings.MIN_ENCOUNTER_DATE) + class PatientConsultationSerializer(serializers.ModelSerializer): id = serializers.CharField(source="external_id", read_only=True) @@ -474,6 +477,14 @@ def validate_create_diagnoses(self, value): return value def validate_encounter_date(self, value): + if value < MIN_ENCOUNTER_DATE: + raise ValidationError( + { + "encounter_date": [ + f"This field value must be greater than {MIN_ENCOUNTER_DATE.strftime('%Y-%m-%d')}" + ] + } + ) if value > now(): raise ValidationError( {"encounter_date": "This field value cannot be in the future."} diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index 08f649ed2b..9b74338d1a 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -4,6 +4,7 @@ from rest_framework import status from rest_framework.test import APITestCase +from care.facility.api.serializers.patient_consultation import MIN_ENCOUNTER_DATE from care.facility.models.icd11_diagnosis import ( ConditionVerificationStatus, ICD11Diagnosis, @@ -100,6 +101,20 @@ def discharge(self, consultation, **kwargs): f"{self.get_url(consultation)}discharge_patient/", kwargs, "json" ) + def test_encounter_date_less_than_minimum(self): + date = MIN_ENCOUNTER_DATE - datetime.timedelta(days=1) + patient = self.create_patient(self.district, self.facility) + data = self.get_default_data().copy() + data.update( + { + "patient": patient.external_id, + "facility": self.facility.external_id, + "encounter_date": date, + } + ) + res = self.client.post(self.get_url(), data, format="json") + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) + def test_create_consultation_treating_physician_invalid_user(self): consultation = self.create_admission_consultation(suggestion="A") res = self.update_consultation( diff --git a/config/settings/base.py b/config/settings/base.py index 0a487ebf92..09efa1770a 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -4,7 +4,7 @@ import base64 import json -from datetime import timedelta +from datetime import datetime, timedelta from pathlib import Path import environ @@ -571,6 +571,12 @@ # for setting the shifting mode PEACETIME_MODE = env.bool("PEACETIME_MODE", default=True) +MIN_ENCOUNTER_DATE = env( + "MIN_ENCOUNTER_DATE", + cast=lambda d: datetime.strptime(d, "%Y-%m-%d"), + default=datetime(2020, 1, 1), +) + # for exporting csv CSV_REQUEST_PARAMETER = "csv" diff --git a/docs/django-configuration/configuration.rst b/docs/django-configuration/configuration.rst index 52d284041e..1f83b31891 100644 --- a/docs/django-configuration/configuration.rst +++ b/docs/django-configuration/configuration.rst @@ -1,6 +1,11 @@ Environment Variables =============== +``MIN_ENCOUNTER_DATE`` +--------------------- +Default value is `2020-01-01`. This is the minimum date for a possible consultation encounter. +Example: `MIN_ENCOUNTER_DATE=2000-01-01` + ``TASK_SUMMARIZE_TRIAGE`` --------------------- Default value is `True`. If set to `False`, the celery task to summarize triage data will not be executed.