diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 22da11966..356b74ed2 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -494,7 +494,7 @@ def dao_get_notifications_by_to_field(service_id, search_term, statuses=None): if statuses: filters.append(Notification.status.in_(statuses)) - results = db.session.query(Notification).filter(*filters).all() + results = db.session.query(Notification).filter(*filters).order_by(desc(Notification.created_at)).all() return results diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index dc0f3fc34..086a24c57 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -1903,3 +1903,24 @@ def test_dao_get_notifications_by_to_field_returns_all_if_no_status_filter(sampl assert len(notifications) == 2 assert notification1.id in notification_ids assert notification2.id in notification_ids + + +@freeze_time('2016-01-01 11:10:00') +def test_dao_get_notifications_by_to_field_orders_by_created_at_desc(sample_template): + notification = partial( + create_notification, + template=sample_template, + to_field='+447700900855', + normalised_to='447700900855' + ) + + notification_a_minute_ago = notification(created_at=datetime.utcnow() - timedelta(minutes=1)) + notification = notification(created_at=datetime.utcnow()) + + notifications = dao_get_notifications_by_to_field( + sample_template.service_id, '+447700900855' + ) + + assert len(notifications) == 2 + assert notifications[0].id == notification.id + assert notifications[1].id == notification_a_minute_ago.id