2017-11-28 10:35:16 +00:00
|
|
|
from flask import Blueprint, jsonify, current_app
|
2016-09-21 13:27:32 +01:00
|
|
|
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2016-09-21 13:27:32 +01:00
|
|
|
from app.delivery import send_to_providers
|
|
|
|
|
from app.models import EMAIL_TYPE
|
|
|
|
|
from app.celery import provider_tasks
|
2016-09-22 09:16:58 +01:00
|
|
|
from app.dao import notifications_dao
|
2017-11-28 10:35:16 +00:00
|
|
|
from app.errors import register_errors
|
2016-09-21 13:27:32 +01:00
|
|
|
|
2016-09-22 14:01:25 +01:00
|
|
|
delivery_blueprint = Blueprint('delivery', __name__)
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
|
2016-09-22 14:01:25 +01:00
|
|
|
register_errors(delivery_blueprint)
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
|
2016-09-22 14:01:25 +01:00
|
|
|
@delivery_blueprint.route('/deliver/notification/<uuid:notification_id>', methods=['POST'])
|
2016-09-22 09:16:58 +01:00
|
|
|
def send_notification_to_provider(notification_id):
|
|
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
|
|
|
|
if not notification:
|
2016-09-22 14:01:25 +01:00
|
|
|
return jsonify({"result": "error", "message": "No result found"}), 404
|
2016-09-22 09:16:58 +01:00
|
|
|
|
|
|
|
|
if notification.notification_type == EMAIL_TYPE:
|
2016-09-22 14:01:25 +01:00
|
|
|
send_response(
|
|
|
|
|
send_to_providers.send_email_to_provider,
|
|
|
|
|
provider_tasks.deliver_email,
|
2017-07-20 16:17:04 +01:00
|
|
|
notification,
|
|
|
|
|
QueueNames.SEND_EMAIL
|
|
|
|
|
)
|
2016-09-21 13:27:32 +01:00
|
|
|
else:
|
2016-09-22 14:01:25 +01:00
|
|
|
send_response(
|
|
|
|
|
send_to_providers.send_sms_to_provider,
|
|
|
|
|
provider_tasks.deliver_sms,
|
2017-07-20 16:17:04 +01:00
|
|
|
notification,
|
|
|
|
|
QueueNames.SEND_SMS
|
|
|
|
|
)
|
2016-09-21 13:27:32 +01:00
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-07-20 16:17:04 +01:00
|
|
|
def send_response(send_call, task_call, notification, queue):
|
2016-09-21 13:27:32 +01:00
|
|
|
try:
|
2016-09-22 09:16:58 +01:00
|
|
|
send_call(notification)
|
2016-09-21 13:27:32 +01:00
|
|
|
except Exception as e:
|
2016-09-22 14:01:25 +01:00
|
|
|
current_app.logger.exception(
|
|
|
|
|
"Failed to send notification, retrying in celery. ID {} type {}".format(
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.notification_type),
|
|
|
|
|
e)
|
2017-07-20 16:17:04 +01:00
|
|
|
task_call.apply_async((str(notification.id)), queue=queue)
|