2016-05-24 13:08:38 +01:00
|
|
|
|
from datetime import datetime
|
2016-08-09 13:07:48 +01:00
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
from flask import (
|
|
|
|
|
|
Blueprint,
|
|
|
|
|
|
jsonify,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
request,
|
2016-03-01 13:30:10 +00:00
|
|
|
|
current_app,
|
2016-03-10 17:29:17 +00:00
|
|
|
|
json
|
2016-02-16 11:22:44 +00:00
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
|
from app import api_user, statsd_client
|
2016-04-06 16:34:45 +01:00
|
|
|
|
from app.clients.email.aws_ses import get_aws_responses
|
2016-02-22 17:17:29 +00:00
|
|
|
|
from app.dao import (
|
|
|
|
|
|
templates_dao,
|
|
|
|
|
|
services_dao,
|
2016-02-25 11:35:32 +00:00
|
|
|
|
notifications_dao
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2017-01-17 13:16:26 +00:00
|
|
|
|
from app.models import KEY_TYPE_TEAM, PRIORITY
|
2016-07-26 14:33:14 +01:00
|
|
|
|
from app.models import SMS_TYPE
|
2016-04-06 14:31:33 +01:00
|
|
|
|
from app.notifications.process_client_response import (
|
|
|
|
|
|
validate_callback_data,
|
|
|
|
|
|
process_sms_client_response
|
|
|
|
|
|
)
|
2017-01-17 12:08:24 +00:00
|
|
|
|
from app.notifications.process_notifications import (persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
simulated_recipient)
|
|
|
|
|
|
from app.notifications.validators import (check_service_message_limit,
|
|
|
|
|
|
check_template_is_for_notification_type,
|
|
|
|
|
|
check_template_is_active)
|
2016-02-03 13:16:19 +00:00
|
|
|
|
from app.schemas import (
|
2016-02-16 11:22:44 +00:00
|
|
|
|
email_notification_schema,
|
|
|
|
|
|
sms_template_notification_schema,
|
2016-07-26 14:33:14 +01:00
|
|
|
|
notification_with_personalisation_schema,
|
2016-05-19 16:42:21 +01:00
|
|
|
|
notifications_filter_schema,
|
2016-05-20 15:16:53 +01:00
|
|
|
|
notifications_statistics_schema,
|
2016-10-27 11:46:37 +01:00
|
|
|
|
day_schema
|
2016-02-16 11:22:44 +00:00
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
from app.service.utils import service_allowed_to_send_to
|
2016-12-09 15:56:25 +00:00
|
|
|
|
from app.utils import pagination_links, get_template_instance
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
notifications = Blueprint('notifications', __name__)
|
|
|
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
|
from app.errors import (
|
|
|
|
|
|
register_errors,
|
|
|
|
|
|
InvalidRequest
|
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
2016-11-07 11:55:59 +00:00
|
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
|
register_errors(notifications)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
2016-03-10 17:29:17 +00:00
|
|
|
|
@notifications.route('/notifications/email/ses', methods=['POST'])
|
|
|
|
|
|
def process_ses_response():
|
2016-04-06 16:34:45 +01:00
|
|
|
|
client_name = 'SES'
|
2016-03-10 17:29:17 +00:00
|
|
|
|
try:
|
|
|
|
|
|
ses_request = json.loads(request.data)
|
2016-04-06 16:34:45 +01:00
|
|
|
|
errors = validate_callback_data(data=ses_request, fields=['Message'], client_name=client_name)
|
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
2016-03-14 14:49:02 +00:00
|
|
|
|
ses_message = json.loads(ses_request['Message'])
|
2016-04-06 16:34:45 +01:00
|
|
|
|
errors = validate_callback_data(data=ses_message, fields=['notificationType'], client_name=client_name)
|
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
|
notification_type = ses_message['notificationType']
|
2016-05-17 15:38:49 +01:00
|
|
|
|
if notification_type == 'Bounce':
|
|
|
|
|
|
if ses_message['bounce']['bounceType'] == 'Permanent':
|
|
|
|
|
|
notification_type = ses_message['bounce']['bounceType'] # permanent or not
|
|
|
|
|
|
else:
|
|
|
|
|
|
notification_type = 'Temporary'
|
2016-03-21 13:24:37 +00:00
|
|
|
|
try:
|
2016-04-06 16:34:45 +01:00
|
|
|
|
aws_response_dict = get_aws_responses(notification_type)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
except KeyError:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = "{} callback failed: status {} not found".format(client_name, notification_type)
|
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
|
notification_status = aws_response_dict['notification_status']
|
2016-03-21 13:24:37 +00:00
|
|
|
|
|
2016-03-10 17:29:17 +00:00
|
|
|
|
try:
|
2016-03-14 14:49:02 +00:00
|
|
|
|
reference = ses_message['mail']['messageId']
|
2016-09-13 13:04:44 +01:00
|
|
|
|
notification = notifications_dao.update_notification_status_by_reference(
|
|
|
|
|
|
reference,
|
|
|
|
|
|
notification_status
|
|
|
|
|
|
)
|
|
|
|
|
|
if not notification:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = "SES callback failed: notification either not found or already updated " \
|
|
|
|
|
|
"from sending. Status {}".format(notification_status)
|
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
|
2016-04-06 16:34:45 +01:00
|
|
|
|
if not aws_response_dict['success']:
|
2016-03-21 13:24:37 +00:00
|
|
|
|
current_app.logger.info(
|
2016-12-15 10:34:29 +00:00
|
|
|
|
"SES delivery failed: notification id {} and reference {} has error found. Status {}".format(
|
|
|
|
|
|
notification.id,
|
2016-03-21 13:24:37 +00:00
|
|
|
|
reference,
|
2016-04-06 16:34:45 +01:00
|
|
|
|
aws_response_dict['message']
|
2016-03-21 13:24:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2016-12-15 10:34:29 +00:00
|
|
|
|
else:
|
|
|
|
|
|
current_app.logger.info('{} callback return status of {} for notification: {}'.format(
|
|
|
|
|
|
client_name,
|
|
|
|
|
|
notification_status,
|
|
|
|
|
|
notification.id))
|
2016-08-25 11:55:38 +01:00
|
|
|
|
statsd_client.incr('callback.ses.{}'.format(notification_status))
|
2016-09-13 13:57:06 +01:00
|
|
|
|
if notification.sent_at:
|
|
|
|
|
|
statsd_client.timing_with_dates(
|
|
|
|
|
|
'callback.ses.elapsed-time'.format(client_name.lower()),
|
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
|
notification.sent_at
|
|
|
|
|
|
)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
return jsonify(
|
|
|
|
|
|
result="success", message="SES callback succeeded"
|
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
|
except KeyError:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
message = "SES callback failed: messageId missing"
|
|
|
|
|
|
raise InvalidRequest(message, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
2016-03-11 09:06:22 +00:00
|
|
|
|
except ValueError as ex:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = "{} callback failed: invalid json".format(client_name)
|
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-04-04 15:02:21 +01:00
|
|
|
|
@notifications.route('/notifications/sms/mmg', methods=['POST'])
|
|
|
|
|
|
def process_mmg_response():
|
2016-04-06 14:31:33 +01:00
|
|
|
|
client_name = 'MMG'
|
|
|
|
|
|
data = json.loads(request.data)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
errors = validate_callback_data(data=data,
|
|
|
|
|
|
fields=['status', 'CID'],
|
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
|
if errors:
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
|
2016-04-20 09:45:13 +01:00
|
|
|
|
success, errors = process_sms_client_response(status=str(data.get('status')),
|
2016-04-06 14:31:33 +01:00
|
|
|
|
reference=data.get('CID'),
|
2016-04-21 11:37:38 +01:00
|
|
|
|
client_name=client_name)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-04 18:08:37 +01:00
|
|
|
|
else:
|
2016-04-06 14:31:33 +01:00
|
|
|
|
return jsonify(result='success', message=success), 200
|
2016-04-04 18:08:37 +01:00
|
|
|
|
|
2016-03-10 15:40:41 +00:00
|
|
|
|
|
2016-04-04 18:08:37 +01:00
|
|
|
|
@notifications.route('/notifications/sms/firetext', methods=['POST'])
|
|
|
|
|
|
def process_firetext_response():
|
2016-04-06 14:31:33 +01:00
|
|
|
|
client_name = 'Firetext'
|
2016-06-14 15:07:23 +01:00
|
|
|
|
errors = validate_callback_data(data=request.form,
|
|
|
|
|
|
fields=['status', 'reference'],
|
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
|
if errors:
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
|
2016-06-09 10:25:27 +01:00
|
|
|
|
response_code = request.form.get('code')
|
|
|
|
|
|
status = request.form.get('status')
|
|
|
|
|
|
current_app.logger.info('Firetext status: {}, extended error code: {}'.format(status, response_code))
|
2016-06-02 15:53:04 +01:00
|
|
|
|
|
2016-06-09 10:25:27 +01:00
|
|
|
|
success, errors = process_sms_client_response(status=status,
|
2016-04-06 14:31:33 +01:00
|
|
|
|
reference=request.form.get('reference'),
|
|
|
|
|
|
client_name=client_name)
|
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-04-06 14:31:33 +01:00
|
|
|
|
else:
|
|
|
|
|
|
return jsonify(result='success', message=success), 200
|
2016-03-10 15:40:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
|
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
2016-08-09 13:07:48 +01:00
|
|
|
|
def get_notification_by_id(notification_id):
|
|
|
|
|
|
notification = notifications_dao.get_notification_with_personalisation(str(api_user.service_id),
|
|
|
|
|
|
notification_id,
|
2016-10-10 17:29:38 +01:00
|
|
|
|
key_type=None)
|
2016-07-26 14:33:14 +01:00
|
|
|
|
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
|
@notifications.route('/notifications', methods=['GET'])
|
|
|
|
|
|
def get_all_notifications():
|
2016-06-14 15:07:23 +01:00
|
|
|
|
data = notifications_filter_schema.load(request.args).data
|
2016-09-15 16:01:26 +01:00
|
|
|
|
include_jobs = data.get('include_jobs', False)
|
2016-11-22 17:30:03 +00:00
|
|
|
|
page = data.get('page', 1)
|
|
|
|
|
|
page_size = data.get('page_size', current_app.config.get('PAGE_SIZE'))
|
2016-04-28 16:10:35 +01:00
|
|
|
|
limit_days = data.get('limit_days')
|
2016-03-01 13:30:10 +00:00
|
|
|
|
|
2016-03-21 12:37:34 +00:00
|
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
2016-06-29 16:33:02 +01:00
|
|
|
|
str(api_user.service_id),
|
2016-08-09 13:07:48 +01:00
|
|
|
|
personalisation=True,
|
2016-03-21 12:37:34 +00:00
|
|
|
|
filter_dict=data,
|
2016-04-19 10:52:52 +01:00
|
|
|
|
page=page,
|
2016-04-28 16:10:35 +01:00
|
|
|
|
page_size=page_size,
|
2016-06-30 18:43:15 +01:00
|
|
|
|
limit_days=limit_days,
|
2016-09-15 16:01:26 +01:00
|
|
|
|
key_type=api_user.key_type,
|
|
|
|
|
|
include_jobs=include_jobs)
|
2016-03-01 13:30:10 +00:00
|
|
|
|
return jsonify(
|
2016-07-26 14:33:14 +01:00
|
|
|
|
notifications=notification_with_personalisation_schema.dump(pagination.items, many=True).data,
|
2016-04-19 10:52:52 +01:00
|
|
|
|
page_size=page_size,
|
|
|
|
|
|
total=pagination.total,
|
2016-03-01 13:30:10 +00:00
|
|
|
|
links=pagination_links(
|
2016-03-21 12:37:34 +00:00
|
|
|
|
pagination,
|
2016-03-01 13:30:10 +00:00
|
|
|
|
'.get_all_notifications',
|
2016-03-16 16:47:18 +00:00
|
|
|
|
**request.args.to_dict()
|
2016-03-01 13:30:10 +00:00
|
|
|
|
)
|
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@notifications.route('/notifications/statistics')
|
|
|
|
|
|
def get_notification_statistics_for_day():
|
|
|
|
|
|
data = day_schema.load(request.args).data
|
|
|
|
|
|
statistics = notifications_dao.dao_get_potential_notification_statistics_for_day(
|
|
|
|
|
|
day=data['day']
|
|
|
|
|
|
)
|
|
|
|
|
|
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
|
|
|
|
|
return jsonify(data=data), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
|
@notifications.route('/notifications/<string:notification_type>', methods=['POST'])
|
2016-02-25 11:35:32 +00:00
|
|
|
|
def send_notification(notification_type):
|
2016-11-10 11:50:49 +00:00
|
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
|
if notification_type not in ['sms', 'email']:
|
|
|
|
|
|
assert False
|
2016-02-24 09:55:05 +00:00
|
|
|
|
|
2017-01-17 13:16:26 +00:00
|
|
|
|
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
2016-03-09 11:06:37 +00:00
|
|
|
|
|
2016-02-29 11:03:48 +00:00
|
|
|
|
notification, errors = (
|
2016-07-21 12:20:50 +01:00
|
|
|
|
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
2016-02-29 11:03:48 +00:00
|
|
|
|
).load(request.get_json())
|
2016-09-23 15:46:48 +01:00
|
|
|
|
if errors:
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
|
check_service_message_limit(api_user.key_type, service)
|
2016-09-06 14:21:36 +01:00
|
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification['template'],
|
2017-01-17 13:16:26 +00:00
|
|
|
|
service_id=service.id)
|
2016-09-23 15:46:48 +01:00
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
|
check_template_is_for_notification_type(notification_type, template.template_type)
|
|
|
|
|
|
check_template_is_active(template)
|
2016-05-24 13:08:38 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template_object = create_template_object_for_notification(template, notification.get('personalisation', {}))
|
2016-06-14 15:07:23 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
_service_allowed_to_send_to(notification, service)
|
2016-04-04 15:02:25 +01:00
|
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
|
|
|
|
|
simulated = simulated_recipient(notification['to'], notification_type)
|
|
|
|
|
|
saved_notification = persist_notification(template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=notification['to'],
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
personalisation=notification.get('personalisation', None),
|
|
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
|
api_key_id=api_user.id,
|
|
|
|
|
|
key_type=api_user.key_type,
|
|
|
|
|
|
simulated=simulated)
|
|
|
|
|
|
if not simulated:
|
2017-01-17 13:16:26 +00:00
|
|
|
|
queue_name = 'notify' if template.process_type == PRIORITY else None
|
|
|
|
|
|
send_notification_to_queue(notification=saved_notification,
|
|
|
|
|
|
research_mode=service.research_mode,
|
|
|
|
|
|
queue=queue_name)
|
2017-01-17 12:08:24 +00:00
|
|
|
|
else:
|
|
|
|
|
|
current_app.logger.info("POST simulated notification for id: {}".format(saved_notification.id))
|
2016-10-28 17:10:00 +01:00
|
|
|
|
notification.update({"template_version": template.version})
|
2017-01-17 12:08:24 +00:00
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
return jsonify(
|
|
|
|
|
|
data=get_notification_return_data(
|
2017-01-17 12:08:24 +00:00
|
|
|
|
saved_notification.id,
|
2016-06-15 12:11:48 +01:00
|
|
|
|
notification,
|
|
|
|
|
|
template_object)
|
|
|
|
|
|
), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_notification_return_data(notification_id, notification, template):
|
|
|
|
|
|
output = {
|
2016-12-09 15:56:25 +00:00
|
|
|
|
'body': str(template),
|
2016-06-15 12:11:48 +01:00
|
|
|
|
'template_version': notification['template_version'],
|
|
|
|
|
|
'notification': {'id': notification_id}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if template.template_type == 'email':
|
2016-12-09 15:56:25 +00:00
|
|
|
|
output.update({'subject': template.subject})
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
|
|
return output
|
2016-05-19 16:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
def _service_allowed_to_send_to(notification, service):
|
|
|
|
|
|
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
|
|
|
|
|
|
if api_user.key_type == KEY_TYPE_TEAM:
|
|
|
|
|
|
message = 'Can’t send to this recipient using a team-only API key'
|
|
|
|
|
|
else:
|
|
|
|
|
|
message = (
|
|
|
|
|
|
'Can’t send to this recipient when service is in trial mode '
|
|
|
|
|
|
'– see https://www.notifications.service.gov.uk/trial-mode'
|
|
|
|
|
|
)
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
{'to': [message]},
|
|
|
|
|
|
status_code=400
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_template_object_for_notification(template, personalisation):
|
2016-12-09 15:56:25 +00:00
|
|
|
|
template_object = get_template_instance(template.__dict__, personalisation)
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
if template_object.missing_data:
|
|
|
|
|
|
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
|
|
|
|
|
errors = {'template': [message]}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
if template_object.additional_data:
|
|
|
|
|
|
message = 'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
|
|
|
|
|
|
errors = {'template': [message]}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
template_object.template_type == SMS_TYPE and
|
2016-12-02 10:26:03 +00:00
|
|
|
|
template_object.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
2016-10-03 10:57:10 +01:00
|
|
|
|
):
|
|
|
|
|
|
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(char_count)
|
|
|
|
|
|
errors = {'content': [message]}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
return template_object
|