Skip to content

Commit

Permalink
Updated Vaccination field of health details API
Browse files Browse the repository at this point in the history
* Added vaccination in health details model as jsonfield
* Added schema for vaccintion field
* Removed vaccine model and its references
  • Loading branch information
aeswibon committed Oct 5, 2022
1 parent f018999 commit 9fb47a3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 202 deletions.
55 changes: 16 additions & 39 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from care.facility.models.bed import Bed, ConsultationBed
from care.facility.models.notification import Notification
from care.facility.models.patient import PatientHealthDetails, Vaccine
from care.facility.models.patient import PatientHealthDetails
from care.facility.models.patient_base import (
DISCHARGE_REASON_CHOICES,
SYMPTOM_CHOICES,
Expand Down Expand Up @@ -84,7 +84,9 @@ class PatientConsultationSerializer(serializers.ModelSerializer):
created_by = UserBaseMinimumSerializer(read_only=True)
last_daily_round = DailyRoundSerializer(read_only=True)
health_details = ExternalIdSerializerField(
queryset=PatientHealthDetails.objects.all(), required=False
queryset=PatientHealthDetails.objects.all(),
source="last_health_details",
required=False,
)

health_details_object = PatientHealthDetailsSerializer(
Expand Down Expand Up @@ -144,7 +146,6 @@ def validate_bed_number(self, bed_number):
return bed_number

def update(self, instance, validated_data):

instance.last_edited_by = self.context["request"].user

if instance.discharge_date:
Expand Down Expand Up @@ -185,9 +186,12 @@ def update(self, instance, validated_data):
consultation = super().update(instance, validated_data)

try:
vaccination_history = health_details_data.pop("vaccination_history", [])
last_health_details_object = PatientHealthDetails.objects.get(
consultation=consultation
)

serializer = PatientHealthDetailsSerializer(
last_health_details_object,
data={
"patient": consultation.patient.external_id.hex,
"facility": consultation.facility.external_id.hex,
Expand All @@ -201,18 +205,6 @@ def update(self, instance, validated_data):
serializer.is_valid(raise_exception=True)
serializer.save()

health_details = PatientHealthDetails.objects.filter(
consultation=consultation
).latest("external_id")

consultation.health_details = health_details

vaccines = []
for vaccine in vaccination_history:
vaccines.append(Vaccine(health_details=health_details, **vaccine))
if vaccines:
Vaccine.objects.bulk_create(vaccines, ignore_conflicts=True)

except KeyError:
pass

Expand Down Expand Up @@ -292,8 +284,6 @@ def create(self, validated_data):
consultation.save()

try:
vaccination_history = health_details_data.pop("vaccination_history", [])

serializer = PatientHealthDetailsSerializer(
data={
"patient": consultation.patient.external_id.hex,
Expand All @@ -308,18 +298,6 @@ def create(self, validated_data):
serializer.is_valid(raise_exception=True)
serializer.save()

health_details = PatientHealthDetails.objects.filter(
consultation=consultation
).latest("external_id")

consultation.health_details = health_details

vaccines = []
for vaccine in vaccination_history:
vaccines.append(Vaccine(health_details=health_details, **vaccine))
if vaccines:
Vaccine.objects.bulk_create(vaccines, ignore_conflicts=True)

except KeyError as error:
if consultation.patient.last_consultation is None:
raise ValidationError(
Expand All @@ -330,11 +308,10 @@ def create(self, validated_data):
}
) from error

else:
consultation.last_health_details = (
consultation.patient.last_consultation.last_health_details
)
consultation.save(update_fields=["last_health_details"])
consultation.last_health_details = (
consultation.patient.last_consultation.last_health_details
)
consultation.save(update_fields=["last_health_details"])

if bed:
consultation_bed = ConsultationBed(
Expand Down Expand Up @@ -431,27 +408,27 @@ def validate(self, attrs):
for diagnosis in validated["icd11_diagnoses"]:
try:
ICDDiseases.by.id[diagnosis]
except BaseException:
except BaseException as error:
raise ValidationError(
{
"icd11_diagnoses": [
f"{diagnosis} is not a valid ICD 11 Diagnosis ID"
]
}
)
) from error

if "icd11_provisional_diagnoses" in validated:
for diagnosis in validated["icd11_provisional_diagnoses"]:
try:
ICDDiseases.by.id[diagnosis]
except BaseException:
except BaseException as error:
raise ValidationError(
{
"icd11_provisional_diagnoses": [
f"{diagnosis} is not a valid ICD 11 Diagnosis ID"
]
}
)
) from error

return validated

Expand Down
21 changes: 1 addition & 20 deletions care/facility/api/serializers/patient_health_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@
PatientRegistration,
)
from care.facility.models.facility import Facility
from care.facility.models.patient import Vaccine
from care.facility.models.patient_base import BLOOD_GROUP_CHOICES, VACCINE_CHOICES
from care.facility.models.patient_base import BLOOD_GROUP_CHOICES
from care.utils.queryset.facility import get_home_facility_queryset
from care.utils.serializer.external_id_field import ExternalIdSerializerField


class VaccinationHistorySerializer(serializers.ModelSerializer):
vaccine = serializers.ChoiceField(choices=VACCINE_CHOICES)
doses = serializers.IntegerField(required=False, default=0)
last_vaccinated_date = serializers.DateField()

class Meta:
model = Vaccine
fields = (
"vaccine",
"doses",
"last_vaccinated_date",
)


class PatientHealthDetailsSerializer(serializers.ModelSerializer):

id = serializers.CharField(source="external_id", read_only=True)
Expand All @@ -42,10 +27,6 @@ class PatientHealthDetailsSerializer(serializers.ModelSerializer):
)
blood_group = ChoiceField(choices=BLOOD_GROUP_CHOICES, required=True)

vaccination_history = serializers.ListSerializer(
child=VaccinationHistorySerializer(), required=False
)

class Meta:
model = PatientHealthDetails
exclude = ("deleted", "external_id")
Expand Down
44 changes: 44 additions & 0 deletions care/facility/migrations/0322_patienthealthdetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import django.db.models.deletion
import django.core.validators
import care.facility.models.mixins.permissions.patient
import django.contrib.postgres.fields.jsonb
import care.utils.models.validators


def populate_data(apps, schema_editor):
Expand Down Expand Up @@ -45,6 +47,13 @@ def populate_data(apps, schema_editor):
has_allergy=has_allergy,
allergies=allergies,
blood_group=blood_group,
vaccination_history=[
{
"vaccine": patient.vaccine_name,
"doses": patient.number_of_doses,
"last_vaccinated_date": patient.last_vaccinated_date,
}
],
)

health_details_objs.append(health_details_obj)
Expand Down Expand Up @@ -187,6 +196,41 @@ class Migration(migrations.Migration):
verbose_name="Patient's Height in CM",
),
),
(
"vaccination_history",
django.contrib.postgres.fields.jsonb.JSONField(
default=list,
validators=[
care.utils.models.validators.JSONFieldSchemaValidator(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"items": [
{
"additionalProperties": False,
"properties": {
"doses": {
"default": 0,
"type": "number",
},
"last_vaccinated_date": {
"type": "string"
},
"vaccine": {"type": "string"},
},
"required": [
"vaccine",
"doses",
"last_vaccinated_date",
],
"type": "object",
}
],
"type": "array",
}
)
],
),
),
],
options={
"abstract": False,
Expand Down
103 changes: 0 additions & 103 deletions care/facility/migrations/0323_vaccine_model.py

This file was deleted.

23 changes: 22 additions & 1 deletion care/facility/models/json_schema/consultation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from care.facility.models.json_schema.common import DATETIME_REGEX

LINES_CATHETERS = {
"$schema": f"http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": [
{
Expand All @@ -17,3 +17,24 @@
}
],
}

VACCINATION = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"vaccine": {"type": "string"},
"doses": {"type": "number", "default": 0},
"last_vaccinated_date": {"type": "string"},
},
"additionalProperties": False,
"required": [
"vaccine",
"doses",
"last_vaccinated_date",
],
}
],
}
Loading

0 comments on commit 9fb47a3

Please sign in to comment.