2017-03-16 18:15:49 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from flask import (
|
|
|
|
|
current_app,
|
|
|
|
|
json
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from app import statsd_client
|
|
|
|
|
from app.clients.email.aws_ses import get_aws_responses
|
|
|
|
|
from app.dao import (
|
|
|
|
|
notifications_dao
|
|
|
|
|
)
|
2017-12-04 14:48:23 +00:00
|
|
|
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
|
2017-03-16 18:15:49 +00:00
|
|
|
from app.notifications.process_client_response import validate_callback_data
|
2018-03-16 14:00:49 +00:00
|
|
|
from app.celery.service_callback_tasks import (
|
|
|
|
|
send_delivery_status_to_service,
|
|
|
|
|
create_encrypted_callback_data,
|
|
|
|
|
)
|
2017-12-01 21:13:01 +00:00
|
|
|
from app.config import QueueNames
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
|
2017-08-03 18:05:42 +01:00
|
|
|
def process_ses_response(ses_request):
|
2017-03-16 18:15:49 +00:00
|
|
|
client_name = 'SES'
|
|
|
|
|
try:
|
|
|
|
|
errors = validate_callback_data(data=ses_request, fields=['Message'], client_name=client_name)
|
|
|
|
|
if errors:
|
2017-10-24 15:39:35 +01:00
|
|
|
return errors
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
ses_message = json.loads(ses_request['Message'])
|
|
|
|
|
errors = validate_callback_data(data=ses_message, fields=['notificationType'], client_name=client_name)
|
|
|
|
|
if errors:
|
2017-10-24 15:39:35 +01:00
|
|
|
return errors
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
notification_type = ses_message['notificationType']
|
|
|
|
|
if notification_type == 'Bounce':
|
2017-11-17 13:41:45 +00:00
|
|
|
current_app.logger.info('SES bounce dict: {}'.format(remove_emails_from_bounce(ses_message['bounce'])))
|
2017-03-16 18:15:49 +00:00
|
|
|
if ses_message['bounce']['bounceType'] == 'Permanent':
|
|
|
|
|
notification_type = ses_message['bounce']['bounceType'] # permanent or not
|
|
|
|
|
else:
|
|
|
|
|
notification_type = 'Temporary'
|
2018-05-30 16:16:36 +01:00
|
|
|
if notification_type == 'Complaint':
|
|
|
|
|
current_app.logger.info("Complaint from SES: \n{}".format(ses_message))
|
|
|
|
|
return
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
try:
|
|
|
|
|
aws_response_dict = get_aws_responses(notification_type)
|
|
|
|
|
except KeyError:
|
|
|
|
|
error = "{} callback failed: status {} not found".format(client_name, notification_type)
|
2017-10-24 15:39:35 +01:00
|
|
|
return error
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
notification_status = aws_response_dict['notification_status']
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
reference = ses_message['mail']['messageId']
|
|
|
|
|
notification = notifications_dao.update_notification_status_by_reference(
|
|
|
|
|
reference,
|
|
|
|
|
notification_status
|
|
|
|
|
)
|
|
|
|
|
if not notification:
|
2017-11-03 10:15:09 +00:00
|
|
|
warning = "SES callback failed: notification either not found or already updated " \
|
2017-11-03 12:09:20 +00:00
|
|
|
"from sending. Status {} for notification reference {}".format(notification_status, reference)
|
2017-11-03 10:15:09 +00:00
|
|
|
current_app.logger.warning(warning)
|
|
|
|
|
return
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
if not aws_response_dict['success']:
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"SES delivery failed: notification id {} and reference {} has error found. Status {}".format(
|
|
|
|
|
notification.id,
|
|
|
|
|
reference,
|
|
|
|
|
aws_response_dict['message']
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
current_app.logger.info('{} callback return status of {} for notification: {}'.format(
|
|
|
|
|
client_name,
|
|
|
|
|
notification_status,
|
|
|
|
|
notification.id))
|
|
|
|
|
statsd_client.incr('callback.ses.{}'.format(notification_status))
|
|
|
|
|
if notification.sent_at:
|
|
|
|
|
statsd_client.timing_with_dates(
|
|
|
|
|
'callback.ses.elapsed-time'.format(client_name.lower()),
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
notification.sent_at
|
|
|
|
|
)
|
2017-05-09 18:17:55 +01:00
|
|
|
|
2018-03-16 14:00:49 +00:00
|
|
|
_check_and_queue_callback_task(notification)
|
2017-10-24 15:39:35 +01:00
|
|
|
return
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
except KeyError:
|
2017-08-04 12:16:25 +01:00
|
|
|
error = "SES callback failed: messageId missing"
|
2017-10-24 15:39:35 +01:00
|
|
|
return error
|
2017-03-16 18:15:49 +00:00
|
|
|
|
2017-08-04 12:16:25 +01:00
|
|
|
except ValueError:
|
2017-03-16 18:15:49 +00:00
|
|
|
error = "{} callback failed: invalid json".format(client_name)
|
2017-10-24 15:39:35 +01:00
|
|
|
return error
|
2017-11-17 13:41:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_emails_from_bounce(bounce_dict):
|
|
|
|
|
for recip in bounce_dict['bouncedRecipients']:
|
|
|
|
|
recip.pop('emailAddress')
|
2017-12-04 14:48:23 +00:00
|
|
|
|
|
|
|
|
|
2018-03-16 14:00:49 +00:00
|
|
|
def _check_and_queue_callback_task(notification):
|
2017-12-04 14:48:23 +00:00
|
|
|
# queue callback task only if the service_callback_api exists
|
2018-03-16 14:00:49 +00:00
|
|
|
service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id)
|
2017-12-04 14:48:23 +00:00
|
|
|
if service_callback_api:
|
2018-03-16 14:00:49 +00:00
|
|
|
encrypted_notification = create_encrypted_callback_data(notification, service_callback_api)
|
|
|
|
|
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
|
|
|
|
queue=QueueNames.CALLBACKS)
|