Send complaints on to service callback APIs using an async task

This commit is contained in:
Pea Tyczynska
2018-07-18 17:03:16 +01:00
parent 946e0b2700
commit 812f4d20dd
9 changed files with 199 additions and 59 deletions

View File

@@ -12,12 +12,16 @@ from app.dao import (
)
from app.dao.complaint_dao import save_complaint
from app.dao.notifications_dao import dao_get_notification_history_by_reference
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
from app.dao.service_callback_api_dao import (
get_service_delivery_status_callback_api_for_service, get_service_complaint_callback_api_for_service
)
from app.models import Complaint
from app.notifications.process_client_response import validate_callback_data
from app.celery.service_callback_tasks import (
send_delivery_status_to_service,
create_encrypted_callback_data,
send_complaint_to_service,
create_delivery_status_callback_data,
create_complaint_callback_data
)
from app.config import QueueNames
@@ -38,7 +42,8 @@ def process_ses_response(ses_request):
if notification_type == 'Bounce':
notification_type = determine_notification_bounce_type(notification_type, ses_message)
elif notification_type == 'Complaint':
handle_complaint(ses_message)
complaint, notification, recipient = handle_complaint(ses_message)
_check_and_queue_complaint_callback_task(complaint, notification, recipient)
return
try:
@@ -105,7 +110,7 @@ def determine_notification_bounce_type(notification_type, ses_message):
def handle_complaint(ses_message):
remove_emails_from_complaint(ses_message)
recipient_email = remove_emails_from_complaint(ses_message)[0]
current_app.logger.info("Complaint from SES: \n{}".format(ses_message))
try:
reference = ses_message['mail']['messageId']
@@ -123,6 +128,7 @@ def handle_complaint(ses_message):
complaint_date=ses_complaint.get('timestamp', None) if ses_complaint else None
)
save_complaint(complaint)
return complaint, notification, recipient_email
def remove_mail_headers(dict_to_edit):
@@ -141,13 +147,21 @@ def remove_emails_from_bounce(bounce_dict):
def remove_emails_from_complaint(complaint_dict):
remove_mail_headers(complaint_dict)
complaint_dict['complaint'].pop('complainedRecipients')
complaint_dict['mail'].pop('destination')
return complaint_dict['mail'].pop('destination')
def _check_and_queue_callback_task(notification):
# queue callback task only if the service_callback_api exists
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
if service_callback_api:
encrypted_notification = create_encrypted_callback_data(notification, service_callback_api)
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
notification_data = create_delivery_status_callback_data(notification, service_callback_api)
send_delivery_status_to_service.apply_async([str(notification.id), notification_data],
queue=QueueNames.CALLBACKS)
def _check_and_queue_complaint_callback_task(complaint, notification, recipient):
# queue callback task only if the service_callback_api exists
service_callback_api = get_service_complaint_callback_api_for_service(service_id=notification.service_id)
if service_callback_api:
complaint_data = create_complaint_callback_data(complaint, notification, service_callback_api, recipient)
send_complaint_to_service.apply_async([complaint_data], queue=QueueNames.CALLBACKS)

View File

@@ -10,7 +10,7 @@ from app.clients.sms.firetext import get_firetext_responses
from app.clients.sms.mmg import get_mmg_responses
from app.celery.service_callback_tasks import (
send_delivery_status_to_service,
create_encrypted_callback_data,
create_delivery_status_callback_data,
)
from app.config import QueueNames
from app.dao.notifications_dao import dao_update_notification
@@ -98,7 +98,7 @@ def _process_for_status(notification_status, client_name, provider_reference):
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
if service_callback_api:
encrypted_notification = create_encrypted_callback_data(notification, 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)