- Refactor version 1 of post notificaitons to use the common persist_notificaiton and send_notification_to_queue methods.

- It would be nice to refactor the send_sms and send_email tasks to use these common functions as well, that way I can get rid of the new Notifications.from_v2_api_request method.
- Still not happy with the format of the errors. Would like to find a happy place, where the message is descript enough that we do not need external documentation to explain the error. Perhaps we still only need documentation to explain the trial mode concept.
This commit is contained in:
Rebecca Law
2016-10-28 17:10:00 +01:00
parent 6e4bad135a
commit 8cf2fc72a8
11 changed files with 109 additions and 145 deletions

View File

@@ -2,8 +2,9 @@ from flask import request, jsonify
from app import api_user
from app.dao import services_dao, templates_dao
from app.models import SMS_TYPE
from app.notifications.process_notifications import create_content_for_notification, persist_notification, \
send_notification_to_queue
from app.notifications.process_notifications import (create_content_for_notification,
persist_notification,
send_notification_to_queue)
from app.notifications.validators import (check_service_message_limit,
check_template_is_for_notification_type,
check_template_is_active,
@@ -20,21 +21,11 @@ def post_sms_notification():
form = validate(request.get_json(), post_sms_request)
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
# following checks will be in a common function for all versions of the endpoint.
# check service has not exceeded the sending limit
check_service_message_limit(api_user.key_type, service)
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service, SMS_TYPE)
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=form['template_id'],
service_id=service.id)
template, content = __validate_template(form, service)
check_template_is_for_notification_type(SMS_TYPE, template.template_type)
check_template_is_active(template)
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
check_sms_content_char_count(template_with_content.replaced_content_count)
# persist notification
notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=form['phone_number'],
@@ -44,7 +35,7 @@ def post_sms_notification():
api_key_id=api_user.id,
key_type=api_user.key_type)
send_notification_to_queue(notification, service.research_mode)
resp = create_post_sms_response_from_notification(notification, template_with_content.content)
resp = create_post_sms_response_from_notification(notification, content)
return jsonify(resp), 201
@@ -58,3 +49,13 @@ def post_email_notification():
# create content
# return post_email_response schema
pass
def __validate_template(form, service):
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
service_id=service.id)
check_template_is_for_notification_type(SMS_TYPE, template.template_type)
check_template_is_active(template)
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
check_sms_content_char_count(template_with_content.replaced_content_count)
return template, template_with_content.replaced_content_count