2016-09-21 13:27:32 +01:00
|
|
|
from flask import Blueprint, jsonify
|
|
|
|
|
|
|
|
|
|
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
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
delivery = Blueprint('delivery', __name__)
|
|
|
|
|
|
2016-09-22 09:16:58 +01:00
|
|
|
from app.errors import register_errors
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
register_errors(delivery)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@delivery.route('/deliver/notification/<uuid:notification_id>', methods=['POST'])
|
2016-09-22 09:16:58 +01:00
|
|
|
def send_notification_to_provider(notification_id):
|
2016-09-21 13:27:32 +01:00
|
|
|
|
2016-09-22 09:16:58 +01:00
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
|
|
|
|
if not notification:
|
|
|
|
|
return jsonify(notification={"result": "error", "message": "No result found"}), 404
|
|
|
|
|
|
|
|
|
|
if notification.notification_type == EMAIL_TYPE:
|
2016-09-21 13:27:32 +01:00
|
|
|
send_response(send_to_providers.send_email_response, provider_tasks.deliver_email, notification_id, 'send-email')
|
|
|
|
|
else:
|
|
|
|
|
send_response(send_to_providers.send_sms_response, provider_tasks.deliver_sms, notification_id, 'send-sms')
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2016-09-22 09:16:58 +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 09:16:58 +01:00
|
|
|
task_call.apply_async((str(notification.id)), queue=queue)
|