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

PatientConsultation: Skip verified_by required validation if suggestion is DD #1586

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading