Skip to content

Commit

Permalink
fix pinpoint delivery callback for shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed Jun 4, 2024
1 parent ee48085 commit 068cdae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
26 changes: 26 additions & 0 deletions app/aws/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ def pinpoint_delivered_callback(reference=None, timestamp=1467074434, destinatio
return _pinpoint_callback(body)


def pinpoint_shortcode_delivered_callback(reference=None, timestamp=1467074434, destination="+1XXX5550100"):
body = {
"eventType": "TEXT_SUCCESSFUL",
"eventVersion": "1.0",
"eventTimestamp": timestamp,
"isFinal": True,
"originationPhoneNumber": "+18078061258",
"destinationPhoneNumber": destination,
"isoCountryCode": "CA",
"mcc": "302",
"mnc": "610",
"carrierName": "Bell Cellular Inc. / Aliant Telecom",
"messageId": reference,
"messageRequestTimestamp": timestamp,
"messageEncoding": "GSM",
"messageType": "TRANSACTIONAL",
"messageStatus": "SUCCESSFUL",
"messageStatusDescription": "Message has been accepted by phone carrier",
"totalMessageParts": 1,
"totalMessagePrice": 0.00581,
"totalCarrierFee": 0.00767,
}

return _pinpoint_callback(body)


# Note that 1467074434 = 2016-06-28 00:40:34.558 UTC
def pinpoint_failed_callback(provider_response, reference=None, timestamp=1467074434, destination="+1XXX5550100"):
body = {
Expand Down
8 changes: 5 additions & 3 deletions app/celery/process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def process_pinpoint_results(self, response):
reference = receipt["messageId"]
status = receipt["messageStatus"]
provider_response = receipt["messageStatusDescription"]
isFinal = receipt["isFinal"]

notification_status = determine_pinpoint_status(status, provider_response)
notification_status = determine_pinpoint_status(status, provider_response, isFinal)

if notification_status == NOTIFICATION_SENT:
return # we don't want to update the status to sent if it's already sent
Expand Down Expand Up @@ -116,18 +117,19 @@ def process_pinpoint_results(self, response):
self.retry(queue=QueueNames.RETRY)


def determine_pinpoint_status(status: str, provider_response: str) -> Union[str, None]:
def determine_pinpoint_status(status: str, provider_response: str, isFinal: bool) -> Union[str, None]:
"""Determine the notification status based on the SMS status and provider response.
Args:
status (str): message status from AWS
provider_response (str): detailed status from the SMS provider
isFinal (bool): whether this is the last update for this send
Returns:
Union[str, None]: the notification status or None if the status is not handled
"""

if status == "DELIVERED":
if status == "DELIVERED" or status == "SUCCESSFUL" and isFinal:
return NOTIFICATION_DELIVERED
elif status == "SUCCESSFUL": # carrier has accepted the message but it hasn't gone to the phone yet
return NOTIFICATION_SENT
Expand Down
6 changes: 4 additions & 2 deletions tests/app/celery/test_process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.aws.mocks import (
pinpoint_delivered_callback,
pinpoint_failed_callback,
pinpoint_shortcode_delivered_callback,
pinpoint_successful_callback,
)
from app.celery.process_pinpoint_receipts_tasks import process_pinpoint_results
Expand All @@ -28,7 +29,8 @@
)


def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_db_session, mocker):
@pytest.mark.parametrize("callback", [pinpoint_delivered_callback, pinpoint_shortcode_delivered_callback])
def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_db_session, callback, mocker):
mock_logger = mocker.patch("app.celery.process_pinpoint_receipts_tasks.current_app.logger.info")
mock_callback_task = mocker.patch("app.notifications.callbacks._check_and_queue_callback_task")

Expand All @@ -43,7 +45,7 @@ def test_process_pinpoint_results_delivered(sample_template, notify_db, notify_d
)
assert get_notification_by_id(notification.id).status == NOTIFICATION_SENT

process_pinpoint_results(pinpoint_delivered_callback(reference="ref"))
process_pinpoint_results(callback(reference="ref"))

assert mock_callback_task.called_once_with(get_notification_by_id(notification.id))
assert get_notification_by_id(notification.id).status == NOTIFICATION_DELIVERED
Expand Down

0 comments on commit 068cdae

Please sign in to comment.