notify-api-522

This commit is contained in:
Kenneth Kehl
2023-10-24 11:35:00 -07:00
parent 8779160f89
commit 6d84ec64e5
4 changed files with 55 additions and 9 deletions

View File

@@ -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}'"

View File

@@ -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}"
)

View File

@@ -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,
)

View File

@@ -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")