Skip to content

Commit

Permalink
Merge pull request #2293 from coronasafe/fix-6022
Browse files Browse the repository at this point in the history
Block expired patients from being transfered
  • Loading branch information
vigneshhari authored Jul 13, 2024
2 parents f89e730 + f23128d commit 5aca29e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ class Meta:
"facility",
"allow_transfer",
"is_active",
"is_expired",
)


Expand Down
8 changes: 8 additions & 0 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,14 @@ def transfer(self, request, *args, **kwargs):
patient = PatientRegistration.objects.get(external_id=kwargs["external_id"])
facility = Facility.objects.get(external_id=request.data["facility"])

if patient.is_expired:
return Response(
{
"Patient": "Patient transfer cannot be completed because the patient is expired"
},
status=status.HTTP_406_NOT_ACCEPTABLE,
)

if patient.is_active and facility == patient.facility:
return Response(
{
Expand Down
4 changes: 4 additions & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ class TestTypeEnum(enum.Enum):

objects = BaseManager()

@property
def is_expired(self) -> bool:
return self.death_datetime is not None

def __str__(self):
return f"{self.name} - {self.year_of_birth} - {self.get_gender_display()}"

Expand Down
25 changes: 25 additions & 0 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,31 @@ def test_transfer_with_active_consultation_same_facility(self):
"Patient transfer cannot be completed because the patient has an active consultation in the same facility",
)

def test_transfer_with_expired_patient(self):
# Mocking discharged as expired
self.consultation.new_discharge_reason = NewDischargeReasonEnum.EXPIRED
self.consultation.death_datetime = now()
self.consultation.save()

# Set the patient's facility to allow transfers
self.patient.allow_transfer = True
self.patient.save()

# Ensure transfer fails if the patient has an active consultation
self.client.force_authenticate(user=self.super_user)
response = self.client.post(
f"/api/v1/patient/{self.patient.external_id}/transfer/",
{
"year_of_birth": 1992,
"facility": self.facility.external_id,
},
)
self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE)
self.assertEqual(
response.data["Patient"],
"Patient transfer cannot be completed because the patient is expired",
)

def test_transfer_disallowed_by_facility(self):
# Set the patient's facility to disallow transfers
self.patient.allow_transfer = False
Expand Down

0 comments on commit 5aca29e

Please sign in to comment.