Skip to content

Commit

Permalink
Add FF to include more users in conditional alerts
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
MartinRiese committed Dec 3, 2024
1 parent ddd252f commit 18e07b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions corehq/messaging/scheduling/scheduling_partitioned/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions corehq/toggles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)

0 comments on commit 18e07b9

Please sign in to comment.