mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-05 23:38:25 -04:00
Merge pull request #1433 from alphagov/add-reply-to-notifications
Populate reply-to-text field in notifications
This commit is contained in:
@@ -215,7 +215,8 @@ def save_sms(self,
|
||||
created_at=datetime.utcnow(),
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
notification_id=notification_id
|
||||
notification_id=notification_id,
|
||||
reply_to_text=service.get_default_sms_sender()
|
||||
)
|
||||
|
||||
provider_tasks.deliver_sms.apply_async(
|
||||
@@ -262,7 +263,8 @@ def save_email(self,
|
||||
created_at=datetime.utcnow(),
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
notification_id=notification_id
|
||||
notification_id=notification_id,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
provider_tasks.deliver_email.apply_async(
|
||||
@@ -303,7 +305,8 @@ def save_letter(
|
||||
job_id=notification['job'],
|
||||
job_row_number=notification['row_number'],
|
||||
notification_id=notification_id,
|
||||
reference=create_random_identifier()
|
||||
reference=create_random_identifier(),
|
||||
reply_to_text=service.get_default_letter_contact()
|
||||
)
|
||||
|
||||
current_app.logger.info("Letter {} created at {}".format(saved_notification.id, saved_notification.created_at))
|
||||
|
||||
@@ -42,7 +42,8 @@ def create_invited_user(service_id):
|
||||
},
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
||||
|
||||
@@ -3,7 +3,7 @@ from app.models import LETTER_TYPE
|
||||
from app.notifications.process_notifications import persist_notification
|
||||
|
||||
|
||||
def create_letter_notification(letter_data, template, api_key, status):
|
||||
def create_letter_notification(letter_data, template, api_key, status, reply_to_text=None):
|
||||
notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
@@ -18,6 +18,7 @@ def create_letter_notification(letter_data, template, api_key, status):
|
||||
job_row_number=None,
|
||||
reference=create_random_identifier(),
|
||||
client_reference=letter_data.get('reference'),
|
||||
status=status
|
||||
status=status,
|
||||
reply_to_text=reply_to_text
|
||||
)
|
||||
return notification
|
||||
|
||||
@@ -54,7 +54,8 @@ def persist_notification(
|
||||
notification_id=None,
|
||||
simulated=False,
|
||||
created_by_id=None,
|
||||
status=NOTIFICATION_CREATED
|
||||
status=NOTIFICATION_CREATED,
|
||||
reply_to_text=None
|
||||
):
|
||||
notification_created_at = created_at or datetime.utcnow()
|
||||
if not notification_id:
|
||||
@@ -76,7 +77,8 @@ def persist_notification(
|
||||
client_reference=client_reference,
|
||||
reference=reference,
|
||||
created_by_id=created_by_id,
|
||||
status=status
|
||||
status=status,
|
||||
reply_to_text=reply_to_text,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
|
||||
@@ -119,7 +119,7 @@ def send_notification(notification_type):
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
_service_can_send_internationally(authenticated_service, notification_form['to'])
|
||||
|
||||
reply_to = get_reply_to_text(notification_type)
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||
notification_model = persist_notification(template_id=template.id,
|
||||
@@ -130,7 +130,9 @@ def send_notification(notification_type):
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type,
|
||||
simulated=simulated)
|
||||
simulated=simulated,
|
||||
reply_to_text=reply_to
|
||||
)
|
||||
if not simulated:
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification_model,
|
||||
@@ -148,6 +150,15 @@ def send_notification(notification_type):
|
||||
), 201
|
||||
|
||||
|
||||
def get_reply_to_text(notification_type):
|
||||
if notification_type == EMAIL_TYPE:
|
||||
return authenticated_service.get_default_reply_to_email_address()
|
||||
if notification_type == SMS_TYPE:
|
||||
return authenticated_service.get_default_sms_sender()
|
||||
if notification_type == LETTER_TYPE:
|
||||
return authenticated_service.get_default_letter_contact()
|
||||
|
||||
|
||||
def get_notification_return_data(notification_id, notification, template):
|
||||
output = {
|
||||
'body': str(template),
|
||||
|
||||
@@ -138,11 +138,8 @@ def validate_template(template_id, personalisation, service, notification_type):
|
||||
|
||||
def check_service_email_reply_to_id(service_id, reply_to_id, notification_type):
|
||||
if reply_to_id:
|
||||
if notification_type != EMAIL_TYPE:
|
||||
message = 'email_reply_to_id is not a valid option for {} notification'.format(notification_type)
|
||||
raise BadRequestError(message=message)
|
||||
try:
|
||||
dao_get_reply_to_by_id(service_id, reply_to_id)
|
||||
return dao_get_reply_to_by_id(service_id, reply_to_id).email_address
|
||||
except NoResultFound:
|
||||
message = 'email_reply_to_id {} does not exist in database for service id {}'\
|
||||
.format(reply_to_id, service_id)
|
||||
@@ -151,9 +148,6 @@ def check_service_email_reply_to_id(service_id, reply_to_id, notification_type):
|
||||
|
||||
def check_service_sms_sender_id(service_id, sms_sender_id, notification_type):
|
||||
if sms_sender_id:
|
||||
if notification_type != SMS_TYPE:
|
||||
message = 'sms_sender_id is not a valid option for {} notification'.format(notification_type)
|
||||
raise BadRequestError(message=message)
|
||||
try:
|
||||
return dao_get_service_sms_senders_by_id(service_id, sms_sender_id).sms_sender
|
||||
except NoResultFound:
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from app.config import QueueNames
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.notifications.validators import (
|
||||
check_service_over_daily_message_limit,
|
||||
validate_and_format_recipient,
|
||||
validate_template)
|
||||
validate_template
|
||||
)
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
@@ -13,7 +16,9 @@ from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
EMAIL_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
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
@@ -52,6 +57,8 @@ 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)
|
||||
notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
@@ -61,9 +68,9 @@ def send_one_off_notification(service_id, post_data):
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_by_id=post_data['created_by']
|
||||
created_by_id=post_data['created_by'],
|
||||
reply_to_text=reply_to
|
||||
)
|
||||
sender_id = post_data.get('sender_id', None)
|
||||
if sender_id:
|
||||
if template.template_type == EMAIL_TYPE:
|
||||
persist_email_reply_to_id_for_notification(notification.id, sender_id)
|
||||
@@ -78,3 +85,23 @@ def send_one_off_notification(service_id, post_data):
|
||||
)
|
||||
|
||||
return {'id': str(notification.id)}
|
||||
|
||||
|
||||
def get_reply_to_text(notification_type, sender_id, service):
|
||||
reply_to = None
|
||||
if notification_type == EMAIL_TYPE:
|
||||
if sender_id:
|
||||
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:
|
||||
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()
|
||||
|
||||
return reply_to
|
||||
|
||||
@@ -23,7 +23,8 @@ def send_notification_to_service_users(service_id, template_id, personalisation=
|
||||
personalisation=personalisation,
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=notify_service.get_default_reply_to_email_address()
|
||||
)
|
||||
send_notification_to_queue(notification, False, queue=QueueNames.NOTIFY)
|
||||
|
||||
|
||||
@@ -214,7 +214,11 @@ def create_2fa_code(template_id, user_to_send_to, secret_code, recipient, person
|
||||
|
||||
# save the code in the VerifyCode table
|
||||
create_user_code(user_to_send_to, secret_code, template.template_type)
|
||||
|
||||
reply_to = None
|
||||
if template.template_type == SMS_TYPE:
|
||||
reply_to = template.service.get_default_sms_sender()
|
||||
elif template.template_type == EMAIL_TYPE:
|
||||
reply_to = template.service.get_default_reply_to_email_address()
|
||||
saved_notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
@@ -223,7 +227,8 @@ def create_2fa_code(template_id, user_to_send_to, secret_code, recipient, person
|
||||
personalisation=personalisation,
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=reply_to
|
||||
)
|
||||
# Assume that we never want to observe the Notify service's research mode
|
||||
# setting for this notification - we still need to be able to log into the
|
||||
@@ -253,7 +258,8 @@ def send_user_confirm_new_email(user_id):
|
||||
},
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
||||
@@ -279,7 +285,8 @@ def send_new_user_email_verification(user_id):
|
||||
},
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
||||
@@ -305,7 +312,8 @@ def send_already_registered_email(user_id):
|
||||
},
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
||||
@@ -367,7 +375,8 @@ def send_user_reset_password():
|
||||
},
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=service.get_default_reply_to_email_address()
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
||||
|
||||
@@ -127,7 +127,8 @@ post_sms_request = {
|
||||
"scheduled_for": {"type": ["string", "null"], "format": "datetime"},
|
||||
"sms_sender_id": uuid
|
||||
},
|
||||
"required": ["phone_number", "template_id"]
|
||||
"required": ["phone_number", "template_id"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
|
||||
sms_content = {
|
||||
@@ -172,7 +173,8 @@ post_email_request = {
|
||||
"scheduled_for": {"type": ["string", "null"], "format": "datetime"},
|
||||
"email_reply_to_id": uuid
|
||||
},
|
||||
"required": ["email_address", "template_id"]
|
||||
"required": ["email_address", "template_id"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
|
||||
email_content = {
|
||||
@@ -215,7 +217,8 @@ post_letter_request = {
|
||||
"template_id": uuid,
|
||||
"personalisation": letter_personalisation
|
||||
},
|
||||
"required": ["template_id", "personalisation"]
|
||||
"required": ["template_id", "personalisation"],
|
||||
"additionalProperties": False
|
||||
}
|
||||
|
||||
letter_content = {
|
||||
|
||||
@@ -63,15 +63,12 @@ def post_notification(notification_type):
|
||||
check_service_has_permission(notification_type, authenticated_service.permissions)
|
||||
|
||||
scheduled_for = form.get("scheduled_for", None)
|
||||
service_email_reply_to_id = form.get("email_reply_to_id", None)
|
||||
service_sms_sender_id = form.get("sms_sender_id", None)
|
||||
|
||||
check_service_can_schedule_notification(authenticated_service.permissions, scheduled_for)
|
||||
|
||||
check_rate_limiting(authenticated_service, api_user)
|
||||
|
||||
check_service_email_reply_to_id(str(authenticated_service.id), service_email_reply_to_id, notification_type)
|
||||
sms_sender = check_service_sms_sender_id(str(authenticated_service.id), service_sms_sender_id, notification_type)
|
||||
reply_to = get_reply_to_text(notification_type, form)
|
||||
|
||||
template, template_with_content = validate_template(
|
||||
form['template_id'],
|
||||
@@ -92,13 +89,14 @@ def post_notification(notification_type):
|
||||
notification_type=notification_type,
|
||||
api_key=api_user,
|
||||
template=template,
|
||||
service=authenticated_service
|
||||
service=authenticated_service,
|
||||
reply_to_text=reply_to
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
create_resp_partial = functools.partial(
|
||||
create_post_sms_response_from_notification,
|
||||
from_number=sms_sender or authenticated_service.get_default_sms_sender()
|
||||
from_number=reply_to
|
||||
)
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
create_resp_partial = functools.partial(
|
||||
@@ -121,7 +119,7 @@ def post_notification(notification_type):
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service):
|
||||
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None):
|
||||
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
|
||||
|
||||
send_to = validate_and_format_recipient(send_to=form_send_to,
|
||||
@@ -142,7 +140,8 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
|
||||
api_key_id=api_key.id,
|
||||
key_type=api_key.key_type,
|
||||
client_reference=form.get('reference', None),
|
||||
simulated=simulated
|
||||
simulated=simulated,
|
||||
reply_to_text=reply_to_text
|
||||
)
|
||||
|
||||
persist_sender_to_notification_mapping(form, notification)
|
||||
@@ -184,8 +183,12 @@ 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
|
||||
|
||||
notification = create_letter_notification(letter_data, template, api_key, status=status)
|
||||
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)
|
||||
|
||||
if not should_send:
|
||||
update_letter_notifications_to_sent_to_dvla.apply_async(
|
||||
@@ -194,3 +197,23 @@ def process_letter_notification(*, letter_data, api_key, template):
|
||||
)
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
def get_reply_to_text(notification_type, form):
|
||||
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()
|
||||
|
||||
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()
|
||||
|
||||
elif notification_type == LETTER_TYPE:
|
||||
reply_to = authenticated_service.get_default_letter_contact()
|
||||
|
||||
return reply_to
|
||||
|
||||
Reference in New Issue
Block a user