diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index b70d1fad7..1e85d6cab 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -391,7 +391,7 @@ def get_notifications_for_service(service_id, query = Notification.query.filter(*filters) query = _filter_query(query, filter_dict) if personalisation: - query.options( + query = query.options( joinedload('actual_template') ) return query.order_by(desc(Notification.created_at)).paginate( diff --git a/app/models.py b/app/models.py index 3d9e12cfa..ae52827c4 100644 --- a/app/models.py +++ b/app/models.py @@ -448,9 +448,6 @@ class Notification(db.Model): reference = db.Column(db.String, nullable=True, index=True) _personalisation = db.Column(db.String, nullable=True) - # __table_args__ = ( - # ForeignKeyConstraint(['template_id', 'template_version'], ['template_history.id', 'template_history.version']), - # ) actual_template = db.relationship('TemplateHistory', primaryjoin=and_( foreign(template_id) == remote(TemplateHistory.id), foreign(template_version) == remote(TemplateHistory.version) diff --git a/app/schemas.py b/app/schemas.py index 9c2454e34..6de8c5ed4 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -282,12 +282,21 @@ class NotificationWithTemplateSchema(BaseSchema): strict = True exclude = ('_personalisation',) - template = fields.Nested(TemplateSchema, only=["id", "name", "template_type", "content", "subject"], dump_only=True) + template = fields.Nested( + TemplateSchema, + only=['id', 'version', 'name', 'template_type', 'content', 'subject'], + dump_only=True + ) job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True) personalisation = fields.Dict(required=False) class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema): + template = None + actual_template = fields.Nested(TemplateHistorySchema, + only=['id', 'name', 'template_type', 'content', 'subject', 'version'], + dump_only=True) + @pre_dump def handle_personalisation_property(self, in_data): self.personalisation = in_data.personalisation @@ -295,6 +304,7 @@ class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema): @post_dump def handle_template_merge(self, in_data): + in_data['template'] = in_data.pop('actual_template') from notifications_utils.template import Template template = Template( in_data['template'], diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index c5774c6a9..df9277724 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -8,6 +8,7 @@ from freezegun import freeze_time from app.dao.notifications_dao import dao_update_notification from app.dao.api_key_dao import save_model_api_key +from app.dao.templates_dao import dao_update_template from app.models import ApiKey, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST from tests import create_authorization_header from tests.app.conftest import sample_notification as create_sample_notification @@ -29,7 +30,9 @@ def test_get_sms_notification_by_id(notify_api, sample_notification): assert notification['template'] == { 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, - 'template_type': sample_notification.template.template_type} + 'template_type': sample_notification.template.template_type, + 'version': 1 + } assert notification['job'] == { 'id': str(sample_notification.job.id), 'original_file_name': sample_notification.job.original_file_name @@ -62,7 +65,9 @@ def test_get_email_notification_by_id(notify_api, notify_db, notify_db_session, assert notification['template'] == { 'id': str(email_notification.template.id), 'name': email_notification.template.name, - 'template_type': email_notification.template.template_type} + 'template_type': email_notification.template.template_type, + 'version': 1 + } assert notification['job'] == { 'id': str(email_notification.job.id), 'original_file_name': email_notification.job.original_file_name @@ -166,7 +171,9 @@ def test_get_all_notifications(notify_api, sample_notification): assert notifications['notifications'][0]['template'] == { 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, - 'template_type': sample_notification.template.template_type} + 'template_type': sample_notification.template.template_type, + 'version': 1 + } assert notifications['notifications'][0]['job'] == { 'id': str(sample_notification.job.id), 'original_file_name': sample_notification.job.original_file_name @@ -626,8 +633,9 @@ def test_get_notification_selects_correct_template_for_personalisation(notify_ap notify_db_session, service=sample_template.service, template=sample_template) - + original_content = sample_template.content sample_template.content = '((name))' + dao_update_template(sample_template) notify_db.session.commit() create_sample_notification(notify_db, @@ -641,14 +649,18 @@ def test_get_notification_selects_correct_template_for_personalisation(notify_ap response = client.get(path='/notifications', headers=[auth_header]) - assert response.status_code == 200 + assert response.status_code == 200 - resp = json.loads(response.get_data(as_text=True)) - assert len(resp['notifications']) == 2 - assert resp['notifications'][0]['template_version'] == 1 - assert resp['notifications'][0]['body'] == 'This is a template' - assert resp['notifications'][1]['template_version'] == 2 - assert resp['notifications'][1]['body'] == 'foo' + resp = json.loads(response.get_data(as_text=True)) + notis = sorted(resp['notifications'], key=lambda x: x['template_version']) + assert len(notis) == 2 + assert notis[0]['template_version'] == 1 + assert notis[0]['body'] == original_content + assert notis[1]['template_version'] == 2 + assert notis[1]['body'] == 'foo' + + assert notis[0]['template_version'] == notis[0]['template']['version'] + assert notis[1]['template_version'] == notis[1]['template']['version'] def _create_auth_header_from_key(api_key):