Skip to content

Commit

Permalink
Updated value of health_details field in consultation API
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Oct 5, 2022
1 parent 9fb47a3 commit d64d76d
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@

class PatientConsultationSerializer(serializers.ModelSerializer):
id = serializers.CharField(source="external_id", read_only=True)
facility_name = serializers.CharField(source="facility.name", read_only=True)
facility_name = serializers.CharField(
source="facility.name", read_only=True
)
suggestion_text = ChoiceField(
choices=PatientConsultation.SUGGESTION_CHOICES,
read_only=True,
Expand All @@ -58,10 +60,14 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
referred_to = ExternalIdSerializerField(
queryset=Facility.objects.all(), required=False
)
patient = ExternalIdSerializerField(queryset=PatientRegistration.objects.all())
patient = ExternalIdSerializerField(
queryset=PatientRegistration.objects.all()
)
facility = ExternalIdSerializerField(read_only=True)

assigned_to_object = UserAssignedSerializer(source="assigned_to", read_only=True)
assigned_to_object = UserAssignedSerializer(
source="assigned_to", read_only=True
)

assigned_to = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), required=False, allow_null=True
Expand All @@ -78,7 +84,9 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
required=False,
)

review_time = serializers.IntegerField(default=-1, write_only=True, required=False)
review_time = serializers.IntegerField(
default=-1, write_only=True, required=False
)

last_edited_by = UserBaseMinimumSerializer(read_only=True)
created_by = UserBaseMinimumSerializer(read_only=True)
Expand Down Expand Up @@ -119,7 +127,9 @@ def get_icd11_diagnoses_objects_by_ids(self, diagnoses_ids):
return diagnosis_objects

def get_icd11_diagnoses_object(self, consultation):
return self.get_icd11_diagnoses_objects_by_ids(consultation.icd11_diagnoses)
return self.get_icd11_diagnoses_objects_by_ids(
consultation.icd11_diagnoses
)

def get_icd11_provisional_diagnoses_object(self, consultation):
return self.get_icd11_diagnoses_objects_by_ids(
Expand Down Expand Up @@ -150,7 +160,11 @@ def update(self, instance, validated_data):

if instance.discharge_date:
raise ValidationError(
{"consultation": ["Discharged Consultation data cannot be updated"]}
{
"consultation": [
"Discharged Consultation data cannot be updated"
]
}
)

if instance.suggestion == SuggestionChoices.OP:
Expand Down Expand Up @@ -209,7 +223,10 @@ def update(self, instance, validated_data):
pass

if "assigned_to" in validated_data:
if validated_data["assigned_to"] != _temp and validated_data["assigned_to"]:
if (
validated_data["assigned_to"] != _temp
and validated_data["assigned_to"]
):
NotificationGenerator(
event=Notification.Event.PATIENT_CONSULTATION_ASSIGNMENT,
caused_by=self.context["request"].user,
Expand Down Expand Up @@ -241,12 +258,16 @@ def create(self, validated_data):

# Authorisation Check

allowed_facilities = get_home_facility_queryset(self.context["request"].user)
allowed_facilities = get_home_facility_queryset(
self.context["request"].user
)
if not allowed_facilities.filter(
id=self.validated_data["patient"].facility.id
).exists():
raise ValidationError(
{"facility": "Consultation creates are only allowed in home facility"}
{
"facility": "Consultation creates are only allowed in home facility"
}
)

# End Authorisation Checks
Expand All @@ -265,7 +286,9 @@ def create(self, validated_data):
if validated_data["patient"].last_consultation:
if not validated_data["patient"].last_consultation.discharge_date:
raise ValidationError(
{"consultation": "Exists please Edit Existing Consultation"}
{
"consultation": "Exists please Edit Existing Consultation"
}
)

if "is_kasp" in validated_data:
Expand Down Expand Up @@ -296,7 +319,8 @@ def create(self, validated_data):
},
)
serializer.is_valid(raise_exception=True)
serializer.save()
health_details = serializer.save()
consultation.health_details = health_details.external_id.hex

except KeyError as error:
if consultation.patient.last_consultation is None:
Expand Down Expand Up @@ -336,7 +360,9 @@ def create(self, validated_data):
if action != -1:
patient.action = action
if review_time > 0:
patient.review_time = localtime(now()) + timedelta(minutes=review_time)
patient.review_time = localtime(now()) + timedelta(
minutes=review_time
)

patient.save()
NotificationGenerator(
Expand Down Expand Up @@ -365,9 +391,9 @@ def validate(self, attrs):
# TODO Add Bed Authorisation Validation

if "suggestion" in validated:
if validated["suggestion"] is SuggestionChoices.R and not validated.get(
"referred_to"
):
if validated[
"suggestion"
] is SuggestionChoices.R and not validated.get("referred_to"):
raise ValidationError(
{
"referred_to": [
Expand Down Expand Up @@ -400,7 +426,11 @@ def validate(self, attrs):
)
if validated["review_time"] <= 0:
raise ValidationError(
{"review_time": ["This field value is must be greater than 0."]}
{
"review_time": [
"This field value is must be greater than 0."
]
}
)
from care.facility.static_data.icd11 import ICDDiseases

Expand Down Expand Up @@ -434,8 +464,12 @@ def validate(self, attrs):


class PatientConsultationIDSerializer(serializers.ModelSerializer):
consultation_id = serializers.UUIDField(source="external_id", read_only=True)
patient_id = serializers.UUIDField(source="patient.external_id", read_only=True)
consultation_id = serializers.UUIDField(
source="external_id", read_only=True
)
patient_id = serializers.UUIDField(
source="patient.external_id", read_only=True
)

class Meta:
model = PatientConsultation
Expand Down

0 comments on commit d64d76d

Please sign in to comment.