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

Use SNS for international phone numbers #2195

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions app/delivery/send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ def provider_to_use(
) -> Any:
"""
Get the provider to use for sending the notification.
SMS that are being sent with a dedicated number or to a US number should not use Pinpoint.
SMS that are being sent with a dedicated number or internationally should not use Pinpoint.

Args:
notification_type (str): SMS or EMAIL.
notification_id (UUID): id of notification. Just used for logging.
to (str, optional): recipient. Defaults to None.
international (bool, optional): Recipient is international. Defaults to False.
international (bool, optional): Flags whether or not the message is outside of Canada and the US. Defaults to False.
sender (str, optional): reply_to_text to use. Defaults to None.
template_id (str, optional): template_id to use. Defaults to None.

Expand All @@ -369,16 +369,25 @@ def provider_to_use(

has_dedicated_number = sender is not None and sender.startswith("+1")
sending_to_us_number = False
sending_internationally = False
if to is not None:
match = next(iter(phonenumbers.PhoneNumberMatcher(to, "US")), None)
if match and phonenumbers.region_code_for_number(match.number) == "US":
if (
match and phonenumbers.region_code_for_number(match.number) == "US"
): # The US is a special case that needs to send from a US toll free number
sending_to_us_number = True
elif (
match and phonenumbers.region_code_for_number(match.number) != "CA"
): # Currently Pinpoint is having issues sending to non-Canadian numbers.
sending_internationally = True

using_sc_pool_template = template_id is not None and str(template_id) in current_app.config["AWS_PINPOINT_SC_TEMPLATE_IDS"]

do_not_use_pinpoint = (
has_dedicated_number
or international # Defaulting back to SNS: it's not entirely clear what this flag is for. Not always set for international recipients.
or sending_to_us_number
or sending_internationally
or not current_app.config["AWS_PINPOINT_SC_POOL_ID"]
or ((not current_app.config["AWS_PINPOINT_DEFAULT_POOL_ID"]) and not using_sc_pool_template)
)
Expand Down
11 changes: 11 additions & 0 deletions tests/app/delivery/test_send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ def test_should_use_sns_for_sms_if_sending_to_the_US(self, restore_provider_deta
provider = send_to_providers.provider_to_use("sms", "1234", "+17065551234")
assert provider.name == "sns"

def test_should_use_sns_for_sms_if_sending_internationally(self, restore_provider_details, notify_api):
with set_config_values(
notify_api,
{
"AWS_PINPOINT_SC_POOL_ID": "sc_pool_id",
"AWS_PINPOINT_DEFAULT_POOL_ID": "default_pool_id",
},
):
provider = send_to_providers.provider_to_use("sms", "1234", "+4408456021111") # British Telecom test line
assert provider.name == "sns"

@pytest.mark.parametrize("sc_pool_id, default_pool_id", [("", "default_pool_id"), ("sc_pool_id", "")])
def test_should_use_sns_if_pinpoint_not_configured(self, restore_provider_details, notify_api, sc_pool_id, default_pool_id):
with set_config_values(
Expand Down
Loading