From 18e07b9b23e5c744e55533bb4db761e8978e9f07 Mon Sep 17 00:00:00 2001 From: Martin Riese Date: Tue, 3 Dec 2024 13:48:59 -0600 Subject: [PATCH] Add FF to include more users in conditional alerts * By default if the target of a conditional alert is a location, it is only send to users that have this location as their primary location. * With the new FF enabled all users that are assigned to the location are included no matter, if it is their primary or a secondary location. --- .../scheduling/scheduling_partitioned/models.py | 14 ++++++++++++-- corehq/toggles/__init__.py | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/corehq/messaging/scheduling/scheduling_partitioned/models.py b/corehq/messaging/scheduling/scheduling_partitioned/models.py index 2f1375aba5ae..bfd3851c5354 100644 --- a/corehq/messaging/scheduling/scheduling_partitioned/models.py +++ b/corehq/messaging/scheduling/scheduling_partitioned/models.py @@ -2,9 +2,11 @@ import pytz import sys import uuid + +from corehq import toggles from corehq.apps.casegroups.models import CommCareCaseGroup from corehq.apps.groups.models import Group -from corehq.apps.locations.dbaccessors import get_all_users_by_location +from corehq.apps.locations.dbaccessors import get_all_users_by_location, user_ids_at_locations from corehq.apps.locations.models import SQLLocation from corehq.apps.sms.models import MessagingEvent from corehq.apps.users.cases import get_owner_id, get_wrapped_owner @@ -24,6 +26,7 @@ from datetime import timedelta, date, datetime, time from memoized import memoized from dimagi.utils.couch import get_redis_lock +from dimagi.utils.couch.database import iter_docs from dimagi.utils.modules import to_function from django.db import models from django.conf import settings @@ -191,7 +194,14 @@ def expand_group(group): def expand_location_ids(domain, location_ids): user_ids = set() for location_id in location_ids: - for user in get_all_users_by_location(domain, location_id): + if toggles.INCLUDE_ALL_LOCATIONS.enabled(domain): + user_ids_at_this_location = user_ids_at_locations([location_id]) + users = [CouchUser.wrap_correctly(u) + for u in iter_docs(CouchUser.get_db(), user_ids_at_this_location)] + else: + users = get_all_users_by_location(domain, location_id) + + for user in users: if user.is_active and user.get_id not in user_ids: user_ids.add(user.get_id) yield user diff --git a/corehq/toggles/__init__.py b/corehq/toggles/__init__.py index 69e94007ac25..322ede6dce8e 100644 --- a/corehq/toggles/__init__.py +++ b/corehq/toggles/__init__.py @@ -2977,3 +2977,11 @@ def domain_has_privilege_from_toggle(privilege_slug, domain): tag=TAG_CUSTOM, namespaces=[NAMESPACE_DOMAIN], ) + +INCLUDE_ALL_LOCATIONS = StaticToggle( + slug='include_all_locations', + label='USH: When sending conditional alert that target locations expand them to users that are assigned to ' + 'the location no matter if it is their primary location or not.', + tag=TAG_CUSTOM, + namespaces=[NAMESPACE_DOMAIN], +)