From f42df8af730a0b6a696f800bda82ee014a439d77 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Mon, 11 Dec 2017 16:20:31 +0000 Subject: [PATCH] Ignore services that have letters_as_pdf permission - We don't want to process the letters as a build file for api calls when the service is generating letters as pdf. --- app/dao/notifications_dao.py | 18 ++++++++++++++- .../test_letter_api_notification_dao.py | 22 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index d9886fcfd..1ecef5f7c 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -26,6 +26,8 @@ from app.models import ( Notification, NotificationHistory, ScheduledNotification, + Service, + ServicePermission, Template, TemplateHistory, KEY_TYPE_NORMAL, @@ -541,14 +543,28 @@ def dao_set_created_live_letter_api_notifications_to_pending(): this is used in the run_scheduled_jobs task, so we put a FOR UPDATE lock on the job table for the duration of the transaction so that if the task is run more than once concurrently, one task will block the other select from completing until it commits. + + Note - do not process services that have letters_as_pdf permission as they + will get processed when the letters PDF zip task is created """ + + # Ignore services that have letters_as_pdf permission + services_without_letters_as_pdf = [ + s.id for s in Service.query.filter( + ~Service.permissions.any( + ServicePermission.permission == 'letters_as_pdf' + ) + ).all() + ] + notifications = db.session.query( Notification ).filter( Notification.notification_type == LETTER_TYPE, Notification.status == NOTIFICATION_CREATED, Notification.key_type == KEY_TYPE_NORMAL, - Notification.api_key != None # noqa + Notification.api_key != None, # noqa + Notification.service_id.in_(services_without_letters_as_pdf) ).with_for_update( ).all() diff --git a/tests/app/dao/notification_dao/test_letter_api_notification_dao.py b/tests/app/dao/notification_dao/test_letter_api_notification_dao.py index 4a624f3eb..96f79ceff 100644 --- a/tests/app/dao/notification_dao/test_letter_api_notification_dao.py +++ b/tests/app/dao/notification_dao/test_letter_api_notification_dao.py @@ -1,13 +1,15 @@ from app.models import ( + Notification, NOTIFICATION_CREATED, NOTIFICATION_PENDING, NOTIFICATION_SENDING, KEY_TYPE_TEST, - KEY_TYPE_NORMAL + KEY_TYPE_NORMAL, + LETTER_TYPE ) from app.dao.notifications_dao import dao_set_created_live_letter_api_notifications_to_pending -from tests.app.db import create_notification +from tests.app.db import create_notification, create_service, create_template def test_should_only_get_letter_notifications( @@ -23,6 +25,22 @@ def test_should_only_get_letter_notifications( assert ret == [sample_letter_notification] +def test_should_ignore_letters_as_pdf( + sample_letter_notification, +): + service = create_service(service_permissions=[LETTER_TYPE, 'letters_as_pdf']) + template = create_template(service, template_type=LETTER_TYPE) + create_notification(template) + + all_noti = Notification.query.all() + assert len(all_noti) == 2 + + ret = dao_set_created_live_letter_api_notifications_to_pending() + + assert sample_letter_notification.status == NOTIFICATION_PENDING + assert ret == [sample_letter_notification] + + def test_should_only_get_created_letters(sample_letter_template): created_noti = create_notification(sample_letter_template, status=NOTIFICATION_CREATED) create_notification(sample_letter_template, status=NOTIFICATION_PENDING)