Skip to content

Commit

Permalink
PatientConsultation: Skip verified_by required validation if `sugge…
Browse files Browse the repository at this point in the history
…stion` is DD (#1586)

* skip required validation if `suggestion` is DD

* set blank=True

* fix test
  • Loading branch information
rithviknishad authored Sep 8, 2023
1 parent 2641851 commit 0d82471
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
36 changes: 25 additions & 11 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PatientConsultationSerializer(serializers.ModelSerializer):

verified_by_object = UserBaseMinimumSerializer(source="verified_by", read_only=True)
verified_by = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), required=True, allow_null=False
queryset=User.objects.all(), required=False, allow_null=True
)

discharge_reason = serializers.ChoiceField(
Expand Down Expand Up @@ -318,19 +318,33 @@ def validate(self, attrs):
validated = super().validate(attrs)
# TODO Add Bed Authorisation Validation

if not validated["verified_by"].user_type == User.TYPE_VALUE_MAP["Doctor"]:
raise ValidationError("Only Doctors can verify a Consultation")

facility = (
self.instance and self.instance.facility or validated["patient"].facility
)
if (
validated["verified_by"].home_facility
and validated["verified_by"].home_facility != facility
"suggestion" in validated
and validated["suggestion"] != SuggestionChoices.DD
):
raise ValidationError(
"Home Facility of the Doctor must be the same as the Consultation Facility"
if "verified_by" not in validated:
raise ValidationError(
{
"verified_by": [
"This field is required as the suggestion is not 'Declared Death'"
]
}
)
if not validated["verified_by"].user_type == User.TYPE_VALUE_MAP["Doctor"]:
raise ValidationError("Only Doctors can verify a Consultation")

facility = (
self.instance
and self.instance.facility
or validated["patient"].facility
)
if (
validated["verified_by"].home_facility
and validated["verified_by"].home_facility != facility
):
raise ValidationError(
"Home Facility of the Doctor must be the same as the Consultation Facility"
)

if "suggestion" in validated:
if validated["suggestion"] is SuggestionChoices.R:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.2 on 2023-09-08 15:23

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("facility", "0384_patientconsultation_verified_by"),
]

operations = [
migrations.AlterField(
model_name="patientconsultation",
name="verified_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
]
2 changes: 1 addition & 1 deletion care/facility/models/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class PatientConsultation(PatientBaseModel, PatientRelatedPermissionMixin):

deprecated_verified_by = models.TextField(default="", null=True, blank=True)
verified_by = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, blank=False
User, on_delete=models.SET_NULL, null=True, blank=True
)

created_by = models.ForeignKey(
Expand Down
2 changes: 1 addition & 1 deletion care/facility/tests/test_patient_consultation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def discharge(self, consultation, **kwargs):

def test_create_consultation_verified_by_invalid_user(self):
res = self.update_consultation(
self.consultation, verified_by=self.state_admin.id
self.consultation, verified_by=self.state_admin.id, suggestion="A"
)
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down

0 comments on commit 0d82471

Please sign in to comment.