2017-07-19 13:50:29 +01:00
|
|
|
|
from app.config import QueueNames
|
2017-06-13 17:33:04 +01:00
|
|
|
|
from app.notifications.validators import (
|
|
|
|
|
|
check_service_over_daily_message_limit,
|
|
|
|
|
|
validate_and_format_recipient,
|
2017-06-15 13:40:38 +01:00
|
|
|
|
validate_template,
|
2017-10-11 16:23:31 +01:00
|
|
|
|
check_service_email_reply_to_id)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
create_content_for_notification,
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
2017-10-11 16:23:31 +01:00
|
|
|
|
persist_email_reply_to_id_for_notification)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
from app.models import (
|
|
|
|
|
|
KEY_TYPE_NORMAL,
|
|
|
|
|
|
PRIORITY,
|
|
|
|
|
|
SMS_TYPE,
|
2017-10-11 16:23:31 +01:00
|
|
|
|
EMAIL_TYPE)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
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
|
2017-06-16 16:30:03 +01:00
|
|
|
|
from app.dao.users_dao import get_user_by_id
|
|
|
|
|
|
from app.v2.errors import BadRequestError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_created_by(service, created_by_id):
|
|
|
|
|
|
user = get_user_by_id(created_by_id)
|
|
|
|
|
|
if service not in user.services:
|
|
|
|
|
|
message = 'Can’t create notification - {} is not part of the "{}" service'.format(
|
|
|
|
|
|
user.name,
|
|
|
|
|
|
service.name
|
|
|
|
|
|
)
|
|
|
|
|
|
raise BadRequestError(message=message)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
2017-06-15 13:40:38 +01:00
|
|
|
|
validate_template(template.id, personalisation, service, template.template_type)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
validate_created_by(service, post_data['created_by'])
|
|
|
|
|
|
|
2017-06-13 17:33:04 +01:00
|
|
|
|
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,
|
2017-06-16 16:30:03 +01:00
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
|
created_by_id=post_data['created_by']
|
2017-06-13 17:33:04 +01:00
|
|
|
|
)
|
2017-10-11 16:23:31 +01:00
|
|
|
|
sender_id = post_data.get('sender_id', None)
|
|
|
|
|
|
if sender_id and template.template_type == EMAIL_TYPE:
|
|
|
|
|
|
check_service_email_reply_to_id(service_id, sender_id)
|
|
|
|
|
|
persist_email_reply_to_id_for_notification(notification.id, sender_id)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
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)}
|