diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index d8d5567f6..56b3734a5 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -15,8 +15,8 @@ from app.dao.notifications_dao import ( delete_notifications_created_more_than_a_week_ago, dao_timeout_notifications, is_delivery_slow_for_provider, - dao_get_scheduled_notifications -) + dao_get_scheduled_notifications, + set_scheduled_notification_to_processed) from app.dao.statistics_dao import dao_timeout_job_statistics from app.dao.provider_details_dao import ( get_current_provider, @@ -56,6 +56,7 @@ def send_scheduled_notifications(): scheduled_notifications = dao_get_scheduled_notifications() for notification in scheduled_notifications: send_notification_to_queue(notification, notification.service.research_mode) + set_scheduled_notification_to_processed(notification.id) current_app.logger.info( "Sent {} scheudled notifications to the provider queue".format(len(scheduled_notifications))) except SQLAlchemyError as e: diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 2ff6f3a17..75acf5152 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -477,6 +477,14 @@ def dao_get_scheduled_notifications(): ScheduledNotification ).filter( ScheduledNotification.scheduled_for < datetime.utcnow(), - Notification.status == NOTIFICATION_CREATED).all() + ScheduledNotification.pending).all() return notifications + + +def set_scheduled_notification_to_processed(notification_id): + ScheduledNotification.query.filter( + ScheduledNotification.notification_id == notification_id + ).update( + {'pending': False} + ) diff --git a/app/models.py b/app/models.py index cdbd7ba69..915a2fff2 100644 --- a/app/models.py +++ b/app/models.py @@ -958,6 +958,7 @@ class ScheduledNotification(db.Model): notification_id = db.Column(UUID(as_uuid=True), db.ForeignKey('notifications.id'), index=True, nullable=False) notification = db.relationship('Notification', uselist=False) scheduled_for = db.Column(db.DateTime, index=False, nullable=False) + pending = db.Column(db.Boolean, nullable=False, default=True) class InvitedUser(db.Model): diff --git a/migrations/versions/0085_scheduled_notifications.py b/migrations/versions/0085_scheduled_notifications.py index a415d69ee..cd0c932a9 100644 --- a/migrations/versions/0085_scheduled_notifications.py +++ b/migrations/versions/0085_scheduled_notifications.py @@ -18,6 +18,7 @@ def upgrade(): sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('notification_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('scheduled_for', sa.DateTime(), nullable=False), + sa.Column('pending', sa.Boolean, nullable=False, default=True), sa.ForeignKeyConstraint(['notification_id'], ['notifications.id'], ), sa.PrimaryKeyConstraint('id') ) diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 465deb101..97c184cdf 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -20,6 +20,7 @@ from app.celery.scheduled_tasks import ( ) from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient from app.dao.jobs_dao import dao_get_job_by_id +from app.dao.notifications_dao import dao_get_scheduled_notifications from app.dao.provider_details_dao import ( dao_update_provider_details, get_current_provider @@ -425,9 +426,14 @@ def test_should_send_all_scheduled_notifications_to_deliver_queue(notify_db, sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, template=sample_template, scheduled_for="2017-05-01 14") + scheduled_notifications = dao_get_scheduled_notifications() + assert len(scheduled_notifications) == 1 + send_scheduled_notifications() mocked.apply_async.assert_called_once_with([str(message_to_deliver.id)], queue='send-sms') + scheduled_notifications = dao_get_scheduled_notifications() + assert not scheduled_notifications def test_timeout_job_statistics_called_with_notification_timeout(notify_api, mocker): diff --git a/tests/app/conftest.py b/tests/app/conftest.py index b1734e10f..032a134fc 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -495,6 +495,8 @@ def sample_notification( notification_id=notification.id, scheduled_for=datetime.strptime(scheduled_for, "%Y-%m-%d %H")) + if status != 'created': + scheduled_notification.pending = False db.session.add(scheduled_notification) db.session.commit() diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 34e27c15b..c467cf845 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_get_scheduled_notifications) + dao_created_scheduled_notification, dao_get_scheduled_notifications, set_scheduled_notification_to_processed) from app.dao.services_dao import dao_update_service from tests.app.db import create_notification @@ -1723,3 +1723,18 @@ def test_dao_get_scheduled_notifications(notify_db, notify_db_session, sample_te scheduled_notifications = dao_get_scheduled_notifications() assert len(scheduled_notifications) == 1 assert scheduled_notifications[0].id == notification_1.id + assert scheduled_notifications[0].scheduled_notification.pending + + +def test_set_scheduled_notification_to_processed(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', + status='created') + scheduled_notifications = dao_get_scheduled_notifications() + assert len(scheduled_notifications) == 1 + assert scheduled_notifications[0].id == notification_1.id + assert scheduled_notifications[0].scheduled_notification.pending + + set_scheduled_notification_to_processed(notification_1.id) + scheduled_notifications = dao_get_scheduled_notifications() + assert not scheduled_notifications