Skip to content

Commit

Permalink
Updated create and update method of serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Aug 11, 2022
1 parent 4ab7f35 commit 6e0161b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
14 changes: 0 additions & 14 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ def check_external_entry(self, srf_id):

def create(self, validated_data):
with transaction.atomic():
medical_history = validated_data.pop("medical_history", [])
meta_info = validated_data.pop("meta_info", {})
contacted_patients = validated_data.pop("contacted_patients", [])

Expand Down Expand Up @@ -253,12 +252,6 @@ def create(self, validated_data):

validated_data["created_by"] = self.context["request"].user
patient = super().create(validated_data)
diseases = []

for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

if meta_info:
meta_info_obj = PatientMetaInfo.objects.create(**meta_info)
Expand Down Expand Up @@ -286,7 +279,6 @@ def create(self, validated_data):

def update(self, instance, validated_data):
with transaction.atomic():
medical_history = validated_data.pop("medical_history", [])
meta_info = validated_data.pop("meta_info", {})
contacted_patients = validated_data.pop("contacted_patients", [])

Expand All @@ -302,12 +294,6 @@ def update(self, instance, validated_data):
self.check_external_entry(validated_data["srf_id"])

patient = super().update(instance, validated_data)
Disease.objects.filter(patient=patient).update(deleted=True)
diseases = []
for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

if meta_info:
for key, value in meta_info.items():
Expand Down
18 changes: 18 additions & 0 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def update(self, instance, validated_data):
)
patient.save()

medical_history = validated_data.pop("medical_history", [])

Disease.objects.filter(patient=patient).update(deleted=True)
diseases = []

for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

validated_data["last_updated_by_telemedicine"] = (
self.context["request"].user == instance.assigned_to
)
Expand Down Expand Up @@ -209,6 +219,14 @@ def create(self, validated_data):
if validated_data["is_kasp"]:
validated_data["kasp_enabled_date"] = localtime(now())

medical_history = validated_data.pop("medical_history", [])
diseases = []

for disease in medical_history:
diseases.append(Disease(patient=patient, **disease))
if diseases:
Disease.objects.bulk_create(diseases, ignore_conflicts=True)

bed = validated_data.pop("bed", None)

validated_data["facility_id"] = validated_data[
Expand Down

0 comments on commit 6e0161b

Please sign in to comment.