2016-04-06 14:31:33 +01:00
|
|
|
import uuid
|
2016-09-13 13:04:44 +01:00
|
|
|
|
|
|
|
|
from datetime import datetime
|
2016-04-06 14:31:33 +01:00
|
|
|
from flask import current_app
|
2019-06-28 12:19:21 +01:00
|
|
|
from notifications_utils.template import SMSMessageTemplate
|
2016-05-13 17:15:39 +01:00
|
|
|
|
|
|
|
|
from app import statsd_client
|
2018-02-07 17:49:15 +00:00
|
|
|
from app.clients import ClientException
|
2016-04-06 14:31:33 +01:00
|
|
|
from app.dao import notifications_dao
|
|
|
|
|
from app.clients.sms.firetext import get_firetext_responses
|
|
|
|
|
from app.clients.sms.mmg import get_mmg_responses
|
2018-03-16 14:00:49 +00:00
|
|
|
from app.celery.service_callback_tasks import (
|
|
|
|
|
send_delivery_status_to_service,
|
2018-07-18 17:03:16 +01:00
|
|
|
create_delivery_status_callback_data,
|
2018-03-16 14:00:49 +00:00
|
|
|
)
|
2017-12-01 20:30:53 +00:00
|
|
|
from app.config import QueueNames
|
2018-03-21 18:11:10 +00:00
|
|
|
from app.dao.notifications_dao import dao_update_notification
|
2018-07-18 10:14:32 +01:00
|
|
|
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
2019-06-28 12:19:21 +01:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2018-10-24 11:24:53 +01:00
|
|
|
from app.models import NOTIFICATION_PENDING
|
2017-12-04 14:48:23 +00:00
|
|
|
|
2016-08-25 11:55:38 +01:00
|
|
|
sms_response_mapper = {
|
|
|
|
|
'MMG': get_mmg_responses,
|
|
|
|
|
'Firetext': get_firetext_responses
|
|
|
|
|
}
|
2016-04-06 14:31:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_callback_data(data, fields, client_name):
|
|
|
|
|
errors = []
|
|
|
|
|
for f in fields:
|
2016-04-20 09:45:13 +01:00
|
|
|
if not str(data.get(f, '')):
|
2016-04-06 14:31:33 +01:00
|
|
|
error = "{} callback failed: {} missing".format(client_name, f)
|
|
|
|
|
errors.append(error)
|
|
|
|
|
return errors if len(errors) > 0 else None
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 18:11:10 +00:00
|
|
|
def process_sms_client_response(status, provider_reference, client_name):
|
2016-04-06 14:31:33 +01:00
|
|
|
success = None
|
|
|
|
|
errors = None
|
|
|
|
|
# validate reference
|
2018-03-21 18:11:10 +00:00
|
|
|
if provider_reference == 'send-sms-code':
|
2016-04-06 14:31:33 +01:00
|
|
|
success = "{} callback succeeded: send-sms-code".format(client_name)
|
|
|
|
|
return success, errors
|
|
|
|
|
|
|
|
|
|
try:
|
2018-03-21 18:11:10 +00:00
|
|
|
uuid.UUID(provider_reference, version=4)
|
2016-04-06 14:31:33 +01:00
|
|
|
except ValueError:
|
2018-03-21 18:11:10 +00:00
|
|
|
errors = "{} callback with invalid reference {}".format(client_name, provider_reference)
|
2018-02-12 12:06:43 +00:00
|
|
|
return success, errors
|
2016-04-06 14:31:33 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response_parser = sms_response_mapper[client_name]
|
|
|
|
|
except KeyError:
|
|
|
|
|
return success, 'unknown sms client: {}'.format(client_name)
|
|
|
|
|
|
|
|
|
|
# validate status
|
|
|
|
|
try:
|
2018-02-07 17:49:15 +00:00
|
|
|
notification_status = response_parser(status)
|
2017-05-09 22:03:57 +01:00
|
|
|
current_app.logger.info('{} callback return status of {} for reference: {}'.format(
|
2018-03-21 18:11:10 +00:00
|
|
|
client_name, status, provider_reference)
|
2017-05-09 22:03:57 +01:00
|
|
|
)
|
2016-04-06 14:31:33 +01:00
|
|
|
except KeyError:
|
2018-03-21 18:11:10 +00:00
|
|
|
_process_for_status(
|
|
|
|
|
notification_status='technical-failure',
|
|
|
|
|
client_name=client_name,
|
|
|
|
|
provider_reference=provider_reference
|
|
|
|
|
)
|
2018-02-07 17:49:15 +00:00
|
|
|
raise ClientException("{} callback failed: status {} not found.".format(client_name, status))
|
2016-04-06 14:31:33 +01:00
|
|
|
|
2018-03-21 18:11:10 +00:00
|
|
|
success = _process_for_status(
|
|
|
|
|
notification_status=notification_status,
|
|
|
|
|
client_name=client_name,
|
|
|
|
|
provider_reference=provider_reference
|
|
|
|
|
)
|
2018-02-07 17:49:15 +00:00
|
|
|
return success, errors
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 18:11:10 +00:00
|
|
|
def _process_for_status(notification_status, client_name, provider_reference):
|
2016-04-06 14:31:33 +01:00
|
|
|
# record stats
|
2018-10-24 11:24:53 +01:00
|
|
|
notification = notifications_dao.update_notification_status_by_id(
|
|
|
|
|
notification_id=provider_reference,
|
|
|
|
|
status=notification_status,
|
|
|
|
|
sent_by=client_name.lower()
|
|
|
|
|
)
|
2016-09-13 13:04:44 +01:00
|
|
|
if not notification:
|
2018-02-07 17:49:15 +00:00
|
|
|
return
|
2016-04-06 14:31:33 +01:00
|
|
|
|
2016-08-25 11:55:38 +01:00
|
|
|
statsd_client.incr('callback.{}.{}'.format(client_name.lower(), notification_status))
|
2018-03-21 18:11:10 +00:00
|
|
|
|
2016-09-13 13:57:06 +01:00
|
|
|
if notification.sent_at:
|
|
|
|
|
statsd_client.timing_with_dates(
|
|
|
|
|
'callback.{}.elapsed-time'.format(client_name.lower()),
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
notification.sent_at
|
|
|
|
|
)
|
2017-05-09 22:03:57 +01:00
|
|
|
|
2019-06-28 12:19:21 +01:00
|
|
|
if notification.billable_units == 0:
|
|
|
|
|
service = notification.service
|
|
|
|
|
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
|
|
|
|
|
|
|
|
|
template = SMSMessageTemplate(
|
|
|
|
|
template_model.__dict__,
|
|
|
|
|
values=notification.personalisation,
|
|
|
|
|
prefix=service.name,
|
|
|
|
|
show_prefix=service.prefix_sms,
|
|
|
|
|
)
|
|
|
|
|
notification.billable_units = template.fragment_count
|
|
|
|
|
notifications_dao.dao_update_notification(notification)
|
|
|
|
|
|
2018-10-24 11:24:53 +01:00
|
|
|
if notification_status != NOTIFICATION_PENDING:
|
|
|
|
|
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
|
|
|
|
|
# queue callback task only if the service_callback_api exists
|
|
|
|
|
if service_callback_api:
|
|
|
|
|
encrypted_notification = create_delivery_status_callback_data(notification, service_callback_api)
|
|
|
|
|
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
|
|
|
|
queue=QueueNames.CALLBACKS)
|
2017-12-01 20:30:53 +00:00
|
|
|
|
2018-03-21 18:11:10 +00:00
|
|
|
success = "{} callback succeeded. reference {} updated".format(client_name, provider_reference)
|
2018-02-07 17:49:15 +00:00
|
|
|
return success
|
2018-03-21 18:11:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_notification_sent_by(notification, client_name):
|
|
|
|
|
notification.sent_by = client_name
|
|
|
|
|
dao_update_notification(notification)
|