Update notification status by message reference

- SES sends a reference to allow us to identify the notification
- use this to update status

If source of email is one of our internal emails (invites or validations) - don't try and update a notification.
This commit is contained in:
Martyn Inglis
2016-03-11 10:19:40 +00:00
parent 901d04605f
commit 62a7b8bcd0
5 changed files with 92 additions and 35 deletions

View File

@@ -80,20 +80,9 @@ def update_notification_status_by_id(notification_id, status):
return count
def update_notification_status_by_to(to, status):
count = db.session.query(Notification).filter_by(
to=to
).update({
Notification.status: status,
Notification.updated_at: datetime.utcnow()
})
db.session.commit()
return count
def update_notification_status_by_reference(reference, status):
count = db.session.query(Notification).filter_by(
refernce=reference
reference=reference
).update({
Notification.status: status,
Notification.updated_at: datetime.utcnow()

View File

@@ -67,9 +67,17 @@ def process_ses_response():
), 400
try:
recipients = ses_request['Message']['mail']['destination']
source = ses_request['Message']['mail']['source']
if is_not_a_notification(ses_request['Message']['mail']['source']):
current_app.logger.info(
"SES callback for notify success:. source {} status {}".format(source, status['notify_status'])
)
return jsonify(
result="success", message="SES callback succeeded"
), 200
if notifications_dao.update_notification_status_by_to(recipients[0], status['notify_status']) == 0:
reference = ses_request['Message']['mail']['messageId']
if notifications_dao.update_notification_status_by_reference(reference, status['notify_status']) == 0:
current_app.logger.info(
"SES callback failed: notification not found. Status {}".format(status['notify_status'])
)
@@ -83,10 +91,10 @@ def process_ses_response():
except KeyError:
current_app.logger.error(
"SES callback failed: destination missing"
"SES callback failed: messageId missing"
)
return jsonify(
result="error", message="SES callback failed: destination missing"
result="error", message="SES callback failed: messageId missing"
), 400
except ValueError as ex:
@@ -98,6 +106,18 @@ def process_ses_response():
), 400
def is_not_a_notification(source):
invite_email = "{}@{}".format(
current_app.config['INVITATION_EMAIL_FROM'],
current_app.config['NOTIFY_EMAIL_DOMAIN']
)
if current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'] == source:
return True
if invite_email == source:
return True
return False
@notifications.route('/notifications/sms/firetext', methods=['POST'])
def process_firetext_response():
if 'status' not in request.form: