Update ses callback to interpret hard and soft bounces.

If the notification has a status == sending then update the status otherwise do not update the status.
In other words do not change the status more than once.
This commit is contained in:
Rebecca Law
2016-05-17 15:38:49 +01:00
parent 4762aef1aa
commit 281a93b32d
6 changed files with 212 additions and 62 deletions

View File

@@ -5,10 +5,16 @@ from app.clients import STATISTICS_DELIVERED, STATISTICS_FAILURE
from app.clients.email import (EmailClientException, EmailClient)
ses_response_map = {
'Bounce': {
"message": 'Bounced',
'Permanent': {
"message": 'Hard bounced',
"success": False,
"notification_status": 'failed',
"notification_status": 'permanent-failure',
"notification_statistics_status": STATISTICS_FAILURE
},
'Temporary': {
"message": 'Soft bounced',
"success": False,
"notification_status": 'temporary-failure',
"notification_statistics_status": STATISTICS_FAILURE
},
'Delivery': {

View File

@@ -1,4 +1,4 @@
from sqlalchemy import (desc, func, Integer)
from sqlalchemy import (desc, func, Integer, and_)
from sqlalchemy.sql.expression import cast
from datetime import (
@@ -188,11 +188,9 @@ def update_notification_status_by_id(notification_id, status, notification_stati
def update_notification_status_by_reference(reference, status, notification_statistics_status):
count = db.session.query(Notification).filter_by(
reference=reference
).update({
Notification.status: status
})
count = db.session.query(Notification).filter(Notification.reference == reference,
Notification.status == 'sending').update(
{Notification.status: status})
if count == 1:
notification = Notification.query.filter_by(

View File

@@ -58,6 +58,11 @@ def process_ses_response():
), 400
notification_type = ses_message['notificationType']
if notification_type == 'Bounce':
if ses_message['bounce']['bounceType'] == 'Permanent':
notification_type = ses_message['bounce']['bounceType'] # permanent or not
else:
notification_type = 'Temporary'
try:
aws_response_dict = get_aws_responses(notification_type)
except KeyError:
@@ -87,12 +92,14 @@ def process_ses_response():
notification_status,
notification_statistics_status
) == 0:
message = "SES callback failed: notification either not found or already updated " \
"from sending. Status {}".format(notification_status)
current_app.logger.info(
"SES callback failed: notification not found. Status {}".format(notification_status)
message
)
return jsonify(
result="error",
message="SES callback failed: notification not found. Status {}".format(notification_status)
message=message
), 404
if not aws_response_dict['success']: