Skip to content

Commit

Permalink
Limit minimum encounter date to 2020-01-01 (#1862)
Browse files Browse the repository at this point in the history
limit min encounter date to 2020-01-01
  • Loading branch information
sainak authored Feb 10, 2024
1 parent 640ede4 commit b26c849
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
13 changes: 12 additions & 1 deletion care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."}
Expand Down
15 changes: 15 additions & 0 deletions care/facility/tests/test_patient_consultation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import base64
import json
from datetime import timedelta
from datetime import datetime, timedelta
from pathlib import Path

import environ
Expand Down Expand Up @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions docs/django-configuration/configuration.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit b26c849

Please sign in to comment.