From 5be2e008a1a8c5b6f11ec74e96176741651b48bb Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Mon, 13 May 2024 19:01:25 +0530 Subject: [PATCH] Skip creating event group if all fields are null (#2134) --- care/facility/events/handler.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/care/facility/events/handler.py b/care/facility/events/handler.py index a78513d185..4fac4efe03 100644 --- a/care/facility/events/handler.py +++ b/care/facility/events/handler.py @@ -58,6 +58,15 @@ def create_consultation_event_entry( ).values_list("id", "fields") for group_id, group_fields in groups: if set(group_fields) & fields_to_store: + value = {} + for field in group_fields: + try: + value[field] = data[field] + except KeyError: + value[field] = getattr(object_instance, field, None) + # if all values in the group are Falsy, skip creating the event for this group + if all(not v for v in value.values()): + continue PatientConsultationEvent.objects.select_for_update().filter( consultation_id=consultation_id, event_type=group_id, @@ -66,12 +75,6 @@ def create_consultation_event_entry( object_id=object_instance.id, created_date__lt=created_date, ).update(is_latest=False) - value = {} - for field in group_fields: - try: - value[field] = data[field] - except KeyError: - value[field] = getattr(object_instance, field, None) batch.append( PatientConsultationEvent( consultation_id=consultation_id, @@ -99,7 +102,7 @@ def create_consultation_events( objects: list | QuerySet | Model, caused_by: int, created_date: datetime = None, - old: Model = None, + old: Model | None = None, ): if created_date is None: created_date = now()