Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip creating event group if all fields are null #2134

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions care/facility/events/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider making the check against None instead?
What if any of the vital is 0 (falsy)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense, will update it

continue
PatientConsultationEvent.objects.select_for_update().filter(
consultation_id=consultation_id,
event_type=group_id,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
Loading