add send_notification file for handling sending one off messages

currently, they're made by creating a one-line job, but we want to
reduce task/csv file noise so we're moving them to persist in the
same vein as API usage. However, we can't just call through to that
since there are some differences:

* no api keys
* tighter control over API format
* no scheduling
* no client references

etc.

So, re-using as much of the v2 validation stuff as possible, I've
created this file that just does basic validation, and then calls
through to persist_notification and schedules a task. Woo.
This commit is contained in:
Leo Hemsted
2017-06-13 17:33:04 +01:00
parent 9f307fd1c5
commit 10b851b456
5 changed files with 105 additions and 33 deletions

View File

@@ -61,6 +61,7 @@ from app.service import statistics
from app.service.service_inbound_api_schema import service_inbound_api, update_service_inbound_api_schema
from app.service.utils import get_whitelist_objects
from app.service.sender import send_notification_to_service_users
from app.service.send_notification import send_one_off_notification
from app.schemas import (
service_schema,
api_key_schema,
@@ -591,3 +592,9 @@ def handle_sql_errror(e):
return jsonify(result='error', message="No result found"), 404
else:
raise e
@service_blueprint.route('/<uuid:service_id>/send-notification', methods=['POST'])
def post_notification(service_id):
resp = send_one_off_notification(service_id, request.get_json())
return jsonify(resp), 201

View File

@@ -0,0 +1,61 @@
from app.config import QueueNames
from app.notifications.validators import (
check_service_over_daily_message_limit,
check_sms_content_char_count,
validate_and_format_recipient,
)
from app.notifications.process_notifications import (
create_content_for_notification,
persist_notification,
send_notification_to_queue,
)
from app.models import (
KEY_TYPE_NORMAL,
PRIORITY,
SMS_TYPE,
)
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
def send_one_off_notification(service_id, post_data):
service = dao_fetch_service_by_id(service_id)
template = dao_get_template_by_id_and_service_id(
template_id=post_data['template_id'],
service_id=service_id
)
personalisation = post_data.get('personalisation', None)
if template.template_type == SMS_TYPE:
template_with_content = create_content_for_notification(template, personalisation)
check_sms_content_char_count(template_with_content.content_count)
check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service)
validate_and_format_recipient(
send_to=post_data['to'],
key_type=KEY_TYPE_NORMAL,
service=service,
notification_type=template.template_type
)
notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=post_data['to'],
service=service,
personalisation=personalisation,
notification_type=template.template_type,
api_key_id=None,
key_type=KEY_TYPE_NORMAL
)
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
send_notification_to_queue(
notification=notification,
research_mode=service.research_mode,
queue=queue_name
)
return {'id': str(notification.id)}