diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 1a97418d3..91f8cc774 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -27,7 +27,7 @@ from app.models import ( NOTIFICATION_PERMANENT_FAILURE, KEY_TYPE_NORMAL, KEY_TYPE_TEST, LETTER_TYPE, - NOTIFICATION_SENT) + NOTIFICATION_SENT, ScheduledNotification) from app.dao.dao_utils import transactional from app.statsd_decorators import statsd @@ -469,3 +469,9 @@ def dao_get_notifications_by_to_field(service_id, search_term): def dao_created_scheduled_notification(scheduled_notification): db.session.add(scheduled_notification) db.session.commit() + + +@statsd(namespace="dao") +def dao_get_scheduled_notifications(): + scheduled_notifications = ScheduledNotification.query.filter(ScheduledNotification.scheduled_for < datetime.utcnow()).all() + return scheduled_notifications diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 0f1f3721c..904dc89e1 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -42,7 +42,7 @@ from app.dao.notifications_dao import ( is_delivery_slow_for_provider, dao_update_notifications_sent_to_dvla, dao_get_notifications_by_to_field, - dao_created_scheduled_notification) + dao_created_scheduled_notification, dao_get_scheduled_notifications) from app.dao.services_dao import dao_update_service from tests.app.db import create_notification @@ -1704,3 +1704,13 @@ def test_dao_created_scheduled_notification(sample_notification): assert len(saved_notification) == 1 assert saved_notification[0].notification_id == sample_notification.id assert saved_notification[0].scheduled_for == datetime(2017, 1, 5, 14) + + +def test_dao_get_scheduled_notifications(notify_db, notify_db_session, sample_template): + notification_1 = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, + template=sample_template, scheduled_for='2017-05-05 14:00:00') + sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, + template=sample_template) + scheduled_notifications = dao_get_scheduled_notifications() + assert len(scheduled_notifications) == 1 + assert scheduled_notifications[0].notification_id == notification_1.id