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

fix/opted out phone numbers in pinpoint #2207

Merged
merged 6 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion app/clients/sms/aws_pinpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

for match in phonenumbers.PhoneNumberMatcher(to, "US"):
matched = True
opted_out = False
to = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
destinationNumber = to

Expand All @@ -44,6 +45,12 @@
MessageType=messageType,
ConfigurationSetName=self.current_app.config["AWS_PINPOINT_CONFIGURATION_SET_NAME"],
)
except self._client.exceptions.ConflictException as e:
if e.response.get("Reason") == "DESTINATION_PHONE_NUMBER_OPTED_OUT":
opted_out = True
else:
raise e

except Exception as e:
self.statsd_client.incr("clients.pinpoint.error")
raise Exception(e)
Expand All @@ -52,7 +59,7 @@
self.current_app.logger.info("AWS Pinpoint request finished in {}".format(elapsed_time))
self.statsd_client.timing("clients.pinpoint.request-time", elapsed_time)
self.statsd_client.incr("clients.pinpoint.success")
return response["MessageId"]
return "opted_out" if opted_out else response["MessageId"]
Fixed Show fixed Hide fixed

if not matched:
self.statsd_client.incr("clients.pinpoint.error")
Expand Down
14 changes: 13 additions & 1 deletion app/delivery/send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
EMAIL_TYPE,
KEY_TYPE_TEST,
NOTIFICATION_CONTAINS_PII,
NOTIFICATION_PERMANENT_FAILURE,
NOTIFICATION_SENDING,
NOTIFICATION_SENT,
NOTIFICATION_TECHNICAL_FAILURE,
Expand Down Expand Up @@ -118,7 +119,10 @@ def send_sms_to_provider(notification):
else:
notification.reference = reference
notification.billable_units = template.fragment_count
update_notification_to_sending(notification, provider)
if reference == "opted_out":
update_notification_to_opted_out(notification, provider)
else:
update_notification_to_sending(notification, provider)

# Record StatsD stats to compute SLOs
statsd_client.timing_with_dates("sms.total-time", notification.sent_at, notification.created_at)
Expand Down Expand Up @@ -340,6 +344,14 @@ def update_notification_to_sending(notification, provider):
dao_update_notification(notification)


def update_notification_to_opted_out(notification, provider):
notification.sent_at = datetime.utcnow()
notification.sent_by = provider.get_name()
notification.status = NOTIFICATION_PERMANENT_FAILURE
notification.provider_response = "Phone number is opted out"
dao_update_notification(notification)


def provider_to_use(
notification_type: str,
notification_id: UUID,
Expand Down
Loading