mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-09 23:02:13 -05:00
notify-api-522
This commit is contained in:
@@ -48,16 +48,21 @@ def check_sms_delivery_receipt(self, message_id, notification_id, sent_at):
|
|||||||
if aws_cloudwatch_client.is_localstack():
|
if aws_cloudwatch_client.is_localstack():
|
||||||
status = "success"
|
status = "success"
|
||||||
provider_response = "this is a fake successful localstack sms message"
|
provider_response = "this is a fake successful localstack sms message"
|
||||||
|
carrier = "unknown"
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
status, provider_response = aws_cloudwatch_client.check_sms(
|
status, provider_response, carrier = aws_cloudwatch_client.check_sms(
|
||||||
message_id, notification_id, sent_at
|
message_id, notification_id, sent_at
|
||||||
)
|
)
|
||||||
except NotificationTechnicalFailureException as ntfe:
|
except NotificationTechnicalFailureException as ntfe:
|
||||||
provider_response = "Unable to find carrier response -- still looking"
|
provider_response = "Unable to find carrier response -- still looking"
|
||||||
status = "pending"
|
status = "pending"
|
||||||
|
carrier = ""
|
||||||
update_notification_status_by_id(
|
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)
|
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:
|
if status == NOTIFICATION_DELIVERED:
|
||||||
sanitize_successful_notification_by_id(
|
sanitize_successful_notification_by_id(
|
||||||
notification_id, provider_response=provider_response
|
notification_id, carrier=carrier, provider_response=provider_response
|
||||||
)
|
)
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f"Sanitized notification {notification_id} that was successfully delivered"
|
f"Sanitized notification {notification_id} that was successfully delivered"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
update_notification_status_by_id(
|
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(
|
current_app.logger.info(
|
||||||
f"Updated notification {notification_id} with response '{provider_response}'"
|
f"Updated notification {notification_id} with response '{provider_response}'"
|
||||||
|
|||||||
@@ -103,7 +103,11 @@ class AwsCloudwatchClient(Client):
|
|||||||
event = all_log_events[0]
|
event = all_log_events[0]
|
||||||
message = json.loads(event["message"])
|
message = json.loads(event["message"])
|
||||||
current_app.logger.info(f"MESSAGE {message}")
|
current_app.logger.info(f"MESSAGE {message}")
|
||||||
return "success", message["delivery"]["providerResponse"]
|
return (
|
||||||
|
"success",
|
||||||
|
message["delivery"]["providerResponse"],
|
||||||
|
message["delivery"]["phoneCarrier"],
|
||||||
|
)
|
||||||
|
|
||||||
log_group_name = (
|
log_group_name = (
|
||||||
f"sns/{region}/{account_number[4]}/DirectPublishToPhoneNumber/Failure"
|
f"sns/{region}/{account_number[4]}/DirectPublishToPhoneNumber/Failure"
|
||||||
@@ -115,12 +119,16 @@ class AwsCloudwatchClient(Client):
|
|||||||
event = all_failed_events[0]
|
event = all_failed_events[0]
|
||||||
message = json.loads(event["message"])
|
message = json.loads(event["message"])
|
||||||
current_app.logger.info(f"MESSAGE {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)):
|
if time_now > (created_at + timedelta(hours=3)):
|
||||||
# see app/models.py Notification. This message corresponds to "permanent-failure",
|
# see app/models.py Notification. This message corresponds to "permanent-failure",
|
||||||
# but we are copy/pasting here to avoid circular imports.
|
# 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(
|
raise NotificationTechnicalFailureException(
|
||||||
f"No event found for message_id {message_id} notification_id {notification_id}"
|
f"No event found for message_id {message_id} notification_id {notification_id}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -90,20 +90,24 @@ def _decide_permanent_temporary_failure(current_status, status):
|
|||||||
return 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(
|
status = _decide_permanent_temporary_failure(
|
||||||
current_status=notification.status, status=status
|
current_status=notification.status, status=status
|
||||||
)
|
)
|
||||||
notification.status = status
|
notification.status = status
|
||||||
if provider_response:
|
if provider_response:
|
||||||
notification.provider_response = provider_response
|
notification.provider_response = provider_response
|
||||||
|
if carrier:
|
||||||
|
notification.carrier = carrier
|
||||||
dao_update_notification(notification)
|
dao_update_notification(notification)
|
||||||
return notification
|
return notification
|
||||||
|
|
||||||
|
|
||||||
@autocommit
|
@autocommit
|
||||||
def update_notification_status_by_id(
|
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 = (
|
||||||
Notification.query.with_for_update()
|
Notification.query.with_for_update()
|
||||||
@@ -137,12 +141,15 @@ def update_notification_status_by_id(
|
|||||||
return None
|
return None
|
||||||
if provider_response:
|
if provider_response:
|
||||||
notification.provider_response = provider_response
|
notification.provider_response = provider_response
|
||||||
|
if carrier:
|
||||||
|
notification.carrier = carrier
|
||||||
if not notification.sent_by and sent_by:
|
if not notification.sent_by and sent_by:
|
||||||
notification.sent_by = sent_by
|
notification.sent_by = sent_by
|
||||||
return _update_notification_status(
|
return _update_notification_status(
|
||||||
notification=notification,
|
notification=notification,
|
||||||
status=status,
|
status=status,
|
||||||
provider_response=notification.provider_response,
|
provider_response=notification.provider_response,
|
||||||
|
carrier=notification.carrier,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
23
migrations/versions/0403_add_carrier.py
Normal file
23
migrations/versions/0403_add_carrier.py
Normal 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")
|
||||||
Reference in New Issue
Block a user