From 6d84ec64e5c39970fc13702a0c814720bfa3072b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 24 Oct 2023 11:35:00 -0700 Subject: [PATCH] notify-api-522 --- app/celery/provider_tasks.py | 16 ++++++++++++---- app/clients/cloudwatch/aws_cloudwatch.py | 14 +++++++++++--- app/dao/notifications_dao.py | 11 +++++++++-- migrations/versions/0403_add_carrier.py | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 migrations/versions/0403_add_carrier.py diff --git a/app/celery/provider_tasks.py b/app/celery/provider_tasks.py index dae150ee6..822b1f119 100644 --- a/app/celery/provider_tasks.py +++ b/app/celery/provider_tasks.py @@ -48,16 +48,21 @@ def check_sms_delivery_receipt(self, message_id, notification_id, sent_at): if aws_cloudwatch_client.is_localstack(): status = "success" provider_response = "this is a fake successful localstack sms message" + carrier = "unknown" else: try: - status, provider_response = aws_cloudwatch_client.check_sms( + status, provider_response, carrier = aws_cloudwatch_client.check_sms( message_id, notification_id, sent_at ) except NotificationTechnicalFailureException as ntfe: provider_response = "Unable to find carrier response -- still looking" status = "pending" + carrier = "" update_notification_status_by_id( - notification_id, status, provider_response=provider_response + notification_id, + status, + carrier=carrier, + provider_response=provider_response, ) raise self.retry(exc=ntfe) @@ -69,14 +74,17 @@ def check_sms_delivery_receipt(self, message_id, notification_id, sent_at): if status == NOTIFICATION_DELIVERED: sanitize_successful_notification_by_id( - notification_id, provider_response=provider_response + notification_id, carrier=carrier, provider_response=provider_response ) current_app.logger.info( f"Sanitized notification {notification_id} that was successfully delivered" ) else: update_notification_status_by_id( - notification_id, status, provider_response=provider_response + notification_id, + status, + carrier=carrier, + provider_response=provider_response, ) current_app.logger.info( f"Updated notification {notification_id} with response '{provider_response}'" diff --git a/app/clients/cloudwatch/aws_cloudwatch.py b/app/clients/cloudwatch/aws_cloudwatch.py index 6f91ed817..eb8343810 100644 --- a/app/clients/cloudwatch/aws_cloudwatch.py +++ b/app/clients/cloudwatch/aws_cloudwatch.py @@ -103,7 +103,11 @@ class AwsCloudwatchClient(Client): event = all_log_events[0] message = json.loads(event["message"]) current_app.logger.info(f"MESSAGE {message}") - return "success", message["delivery"]["providerResponse"] + return ( + "success", + message["delivery"]["providerResponse"], + message["delivery"]["phoneCarrier"], + ) log_group_name = ( f"sns/{region}/{account_number[4]}/DirectPublishToPhoneNumber/Failure" @@ -115,12 +119,16 @@ class AwsCloudwatchClient(Client): event = all_failed_events[0] message = json.loads(event["message"]) current_app.logger.info(f"MESSAGE {message}") - return "failure", message["delivery"]["providerResponse"] + return ( + "failure", + message["delivery"]["providerResponse"], + message["delivery"]["phoneCarrier"], + ) if time_now > (created_at + timedelta(hours=3)): # see app/models.py Notification. This message corresponds to "permanent-failure", # but we are copy/pasting here to avoid circular imports. - return "failure", "Unable to find carrier response." + return "failure", "Unable to find carrier response.", "unknown" raise NotificationTechnicalFailureException( f"No event found for message_id {message_id} notification_id {notification_id}" ) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index aa8ebb880..fea8cd06b 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -90,20 +90,24 @@ def _decide_permanent_temporary_failure(current_status, status): return status -def _update_notification_status(notification, status, provider_response=None): +def _update_notification_status( + notification, status, provider_response=None, carrier=None +): status = _decide_permanent_temporary_failure( current_status=notification.status, status=status ) notification.status = status if provider_response: notification.provider_response = provider_response + if carrier: + notification.carrier = carrier dao_update_notification(notification) return notification @autocommit def update_notification_status_by_id( - notification_id, status, sent_by=None, provider_response=None + notification_id, status, sent_by=None, provider_response=None, carrier=None ): notification = ( Notification.query.with_for_update() @@ -137,12 +141,15 @@ def update_notification_status_by_id( return None if provider_response: notification.provider_response = provider_response + if carrier: + notification.carrier = carrier if not notification.sent_by and sent_by: notification.sent_by = sent_by return _update_notification_status( notification=notification, status=status, provider_response=notification.provider_response, + carrier=notification.carrier, ) diff --git a/migrations/versions/0403_add_carrier.py b/migrations/versions/0403_add_carrier.py new file mode 100644 index 000000000..b13e66c96 --- /dev/null +++ b/migrations/versions/0403_add_carrier.py @@ -0,0 +1,23 @@ +""" + +Revision ID: 0403_add_carrier +Revises: 0402_total_message_limit_default + +""" +from alembic import op +from flask import current_app +import sqlalchemy as sa + + +down_revision = "0402_total_message_limit_default" +revision = "0403_add_carrier" + + +def upgrade(): + op.execute("ALTER TABLE notifications ADD COLUMN carrier text") + op.execute("ALTER TABLE notification_history ADD COLUMN carrier text") + + +def downgrade(): + op.execute("ALTER TABLE notifications DROP COLUMN carrier text") + op.execute("ALTER TABLE notification_history DROP COLUMN carrier text")