diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index e5281bd55f..ae2771b35c 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -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( @@ -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: diff --git a/care/facility/migrations/0385_alter_patientconsultation_verified_by.py b/care/facility/migrations/0385_alter_patientconsultation_verified_by.py new file mode 100644 index 0000000000..f6fc284274 --- /dev/null +++ b/care/facility/migrations/0385_alter_patientconsultation_verified_by.py @@ -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, + ), + ), + ] diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index 75f588d1f9..382499996a 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -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( diff --git a/care/facility/tests/test_patient_consultation_api.py b/care/facility/tests/test_patient_consultation_api.py index c98ed783ac..5e11c5743c 100644 --- a/care/facility/tests/test_patient_consultation_api.py +++ b/care/facility/tests/test_patient_consultation_api.py @@ -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)