Set sent_by if not set during sms provider callback

- refactored argument reference to provider_reference to make it clearer
This commit is contained in:
Ken Tsang
2018-03-21 18:11:10 +00:00
parent 5481d743ac
commit ffa093d8c7
3 changed files with 57 additions and 19 deletions

View File

@@ -13,6 +13,7 @@ from app.celery.service_callback_tasks import (
create_encrypted_callback_data,
)
from app.config import QueueNames
from app.dao.notifications_dao import dao_update_notification
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
sms_response_mapper = {
@@ -30,18 +31,18 @@ def validate_callback_data(data, fields, client_name):
return errors if len(errors) > 0 else None
def process_sms_client_response(status, reference, client_name):
def process_sms_client_response(status, provider_reference, client_name):
success = None
errors = None
# validate reference
if reference == 'send-sms-code':
if provider_reference == 'send-sms-code':
success = "{} callback succeeded: send-sms-code".format(client_name)
return success, errors
try:
uuid.UUID(reference, version=4)
uuid.UUID(provider_reference, version=4)
except ValueError:
errors = "{} callback with invalid reference {}".format(client_name, reference)
errors = "{} callback with invalid reference {}".format(client_name, provider_reference)
return success, errors
try:
@@ -53,27 +54,39 @@ def process_sms_client_response(status, reference, client_name):
try:
notification_status = response_parser(status)
current_app.logger.info('{} callback return status of {} for reference: {}'.format(
client_name, status, reference)
client_name, status, provider_reference)
)
except KeyError:
_process_for_status(notification_status='technical-failure', client_name=client_name, reference=reference)
_process_for_status(
notification_status='technical-failure',
client_name=client_name,
provider_reference=provider_reference
)
raise ClientException("{} callback failed: status {} not found.".format(client_name, status))
success = _process_for_status(notification_status=notification_status, client_name=client_name, reference=reference)
success = _process_for_status(
notification_status=notification_status,
client_name=client_name,
provider_reference=provider_reference
)
return success, errors
def _process_for_status(notification_status, client_name, reference):
def _process_for_status(notification_status, client_name, provider_reference):
# record stats
notification = notifications_dao.update_notification_status_by_id(reference, notification_status)
notification = notifications_dao.update_notification_status_by_id(provider_reference, notification_status)
if not notification:
current_app.logger.warning("{} callback failed: notification {} either not found or already updated "
"from sending. Status {}".format(client_name,
reference,
provider_reference,
notification_status))
return
statsd_client.incr('callback.{}.{}'.format(client_name.lower(), notification_status))
if not notification.sent_by:
set_notification_sent_by(notification, client_name)
if notification.sent_at:
statsd_client.timing_with_dates(
'callback.{}.elapsed-time'.format(client_name.lower()),
@@ -89,5 +102,10 @@ def _process_for_status(notification_status, client_name, reference):
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
queue=QueueNames.CALLBACKS)
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
success = "{} callback succeeded. reference {} updated".format(client_name, provider_reference)
return success
def set_notification_sent_by(notification, client_name):
notification.sent_by = client_name
dao_update_notification(notification)