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 validation for patient transfers #2110

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from django.db import transaction
from django.db.models import Q
from django.utils.timezone import make_aware, now
from django.utils.timezone import make_aware
from rest_framework import serializers

from care.abdm.api.serializers.abhanumber import AbhaNumberSerializer
Expand Down Expand Up @@ -462,16 +462,32 @@ class PatientTransferSerializer(serializers.ModelSerializer):
write_only=True, queryset=Facility.objects.all()
)
patient = serializers.UUIDField(source="external_id", read_only=True)
last_consultation_discharge_date = serializers.DateTimeField(
write_only=True, required=True
)

class Meta:
model = PatientRegistration
fields = ("facility", "year_of_birth", "patient", "facility_object")
fields = (
"facility",
"year_of_birth",
"patient",
"facility_object",
"last_consultation_discharge_date",
)

def validate_year_of_birth(self, value):
if self.instance and self.instance.year_of_birth != value:
raise serializers.ValidationError("Year of birth does not match")
return value

def validate_last_consultation_discharge_date(self, value):
if self.instance and self.instance.last_consultation.encounter_date >= value:
raise serializers.ValidationError(
"Discharge date should be after the last encounter date"
)
return value

def create(self, validated_data):
raise NotImplementedError

Expand All @@ -484,14 +500,16 @@ def update(self, instance, validated_data):
).first()

if consultation:
consultation.discharge_date = now()
consultation.discharge_date = validated_data[
"last_consultation_discharge_date"
]
consultation.new_discharge_reason = NewDischargeReasonEnum.REFERRED
consultation.current_bed = None
consultation.save()

ConsultationBed.objects.filter(
consultation=consultation, end_date__isnull=True
).update(end_date=now())
).update(end_date=validated_data["last_consultation_discharge_date"])

instance.save()
return instance
Expand Down
16 changes: 16 additions & 0 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,22 @@ def validate_encounter_date(self, value):
raise ValidationError(
{"encounter_date": "This field value cannot be in the future."}
)

last_consultation = (
PatientConsultation.objects.filter(
patient=self.validated_data["patient"], discharge_date__isnull=False
)
.order_by("-encounter_date")
.first()
)
if last_consultation and value < last_consultation.encounter_date:
raise ValidationError(
{
"encounter_date": [
"This field value must be greater than the last discharge date."
]
}
)
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
return value

def validate_patient_no(self, value):
Expand Down
Loading