Update notificaiton API endpoints to use template's reply_to

Sets the reply_to_text on notification from the template value if
it's set.
This commit is contained in:
Alexey Bezhan
2017-12-15 17:13:55 +00:00
parent 3b0790f950
commit a98e5247b8
4 changed files with 67 additions and 25 deletions

View File

@@ -15,7 +15,6 @@ from app.models import (
PRIORITY,
SMS_TYPE,
EMAIL_TYPE,
LETTER_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
@@ -56,7 +55,12 @@ def send_one_off_notification(service_id, post_data):
validate_created_by(service, post_data['created_by'])
sender_id = post_data.get('sender_id', None)
reply_to = get_reply_to_text(notification_type=template.template_type, sender_id=sender_id, service=service)
reply_to = get_reply_to_text(
notification_type=template.template_type,
sender_id=sender_id,
service=service,
template=template
)
notification = persist_notification(
template_id=template.id,
template_version=template.version,
@@ -80,21 +84,14 @@ def send_one_off_notification(service_id, post_data):
return {'id': str(notification.id)}
def get_reply_to_text(notification_type, sender_id, service):
def get_reply_to_text(notification_type, sender_id, service, template):
reply_to = None
if notification_type == EMAIL_TYPE:
if sender_id:
if sender_id:
if notification_type == EMAIL_TYPE:
reply_to = dao_get_reply_to_by_id(service.id, sender_id).email_address
else:
service.get_default_reply_to_email_address()
elif notification_type == SMS_TYPE:
if sender_id:
elif notification_type == SMS_TYPE:
reply_to = dao_get_service_sms_senders_by_id(service.id, sender_id).sms_sender
else:
reply_to = service.get_default_sms_sender()
elif notification_type == LETTER_TYPE:
reply_to = service.get_default_letter_contact()
else:
reply_to = template.get_reply_to_text()
return reply_to

View File

@@ -70,8 +70,6 @@ def post_notification(notification_type):
check_rate_limiting(authenticated_service, api_user)
reply_to = get_reply_to_text(notification_type, form)
template, template_with_content = validate_template(
form['template_id'],
form.get('personalisation', {}),
@@ -79,11 +77,14 @@ def post_notification(notification_type):
notification_type,
)
reply_to = get_reply_to_text(notification_type, form, template)
if notification_type == LETTER_TYPE:
notification = process_letter_notification(
letter_data=form,
api_key=api_user,
template=template,
reply_to_text=reply_to
)
else:
notification = process_sms_or_email_notification(
@@ -164,7 +165,7 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
return notification
def process_letter_notification(*, letter_data, api_key, template):
def process_letter_notification(*, letter_data, api_key, template, reply_to_text):
if api_key.key_type == KEY_TYPE_TEAM:
raise BadRequestError(message='Cannot send letters with a team api key', status_code=403)
@@ -175,12 +176,11 @@ def process_letter_notification(*, letter_data, api_key, template):
# if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
status = NOTIFICATION_CREATED if should_send else NOTIFICATION_SENDING
letter_contact_block = api_key.service.get_default_letter_contact()
notification = create_letter_notification(letter_data=letter_data,
template=template,
api_key=api_key,
status=status,
reply_to_text=letter_contact_block)
reply_to_text=reply_to_text)
if not should_send:
update_letter_notifications_to_sent_to_dvla.apply_async(
@@ -198,21 +198,21 @@ def process_letter_notification(*, letter_data, api_key, template):
@statsd(namespace="performance-testing")
def get_reply_to_text(notification_type, form):
def get_reply_to_text(notification_type, form, template):
reply_to = None
if notification_type == EMAIL_TYPE:
service_email_reply_to_id = form.get("email_reply_to_id", None)
reply_to = check_service_email_reply_to_id(
str(authenticated_service.id), service_email_reply_to_id, notification_type
) or authenticated_service.get_default_reply_to_email_address()
) or template.get_reply_to_text()
elif notification_type == SMS_TYPE:
service_sms_sender_id = form.get("sms_sender_id", None)
reply_to = check_service_sms_sender_id(
str(authenticated_service.id), service_sms_sender_id, notification_type
) or authenticated_service.get_default_sms_sender()
) or template.get_reply_to_text()
elif notification_type == LETTER_TYPE:
reply_to = authenticated_service.get_default_letter_contact()
reply_to = template.get_reply_to_text()
return reply_to