diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 7460f3b90..7b5717b66 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -203,6 +203,7 @@ def save_sms(self, key_type=KEY_TYPE_NORMAL): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) + template = dao_get_template_by_id(notification['template'], version=notification['template_version']) if not service_allowed_to_send_to(notification['to'], service, key_type): current_app.logger.info( @@ -224,7 +225,7 @@ def save_sms(self, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, - reply_to_text=service.get_default_sms_sender() + reply_to_text=template.get_reply_to_text() ) provider_tasks.deliver_sms.apply_async( @@ -252,7 +253,9 @@ def save_email(self, api_key_id=None, key_type=KEY_TYPE_NORMAL): notification = encryption.decrypt(encrypted_notification) + service = dao_fetch_service_by_id(service_id) + template = dao_get_template_by_id(notification['template'], version=notification['template_version']) if not service_allowed_to_send_to(notification['to'], service, key_type): current_app.logger.info("Email {} failed as restricted service".format(notification_id)) @@ -272,7 +275,7 @@ def save_email(self, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, - reply_to_text=service.get_default_reply_to_email_address() + reply_to_text=template.get_reply_to_text() ) provider_tasks.deliver_email.apply_async( @@ -299,6 +302,8 @@ def save_letter( recipient = notification['personalisation']['addressline1'] service = dao_fetch_service_by_id(service_id) + template = dao_get_template_by_id(notification['template'], version=notification['template_version']) + try: saved_notification = persist_notification( template_id=notification['template'], @@ -314,7 +319,7 @@ def save_letter( job_row_number=notification['row_number'], notification_id=notification_id, reference=create_random_identifier(), - reply_to_text=service.get_default_letter_contact() + reply_to_text=template.get_reply_to_text() ) if service.has_permission('letters_as_pdf'): diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index a5837b3a2..882fc5b9f 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -1040,6 +1040,47 @@ def test_save_letter_saves_letter_to_database(mocker, notify_db_session): assert notification_db.reply_to_text == "Address contact" +def test_save_letter_uses_template_reply_to_text(mocker, notify_db_session): + service = create_service() + create_letter_contact(service=service, contact_block="Address contact", is_default=True) + template_contact = create_letter_contact( + service=service, + contact_block="Template address contact", + is_default=False + ) + template = create_template( + service=service, + template_type=LETTER_TYPE, + reply_to=template_contact.id + ) + + job = create_job(template=template) + + mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life") + + personalisation = { + 'addressline1': 'Foo', + 'addressline2': 'Bar', + 'postcode': 'Flob', + } + notification_json = _notification_json( + template=job.template, + to='Foo', + personalisation=personalisation, + job_id=job.id, + row_number=1 + ) + + save_letter( + job.service_id, + uuid.uuid4(), + encryption.encrypt(notification_json), + ) + + notification_db = Notification.query.one() + assert notification_db.reply_to_text == "Template address contact" + + def test_save_letter_calls_update_noti_to_sent_task_with_letters_as_pdf_permission_in_research_mode( mocker, notify_db_session, sample_letter_job): sample_letter_job.service.research_mode = True