mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-02 17:48:36 -04:00
Add get_count_of_letters_to_process to notifications_dao
- will get the letter notifications from day before >= letter processing deadline (17:30) - letters_as_pdf permission is required in the service
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta, time
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@@ -32,6 +32,7 @@ class QueueNames(object):
|
|||||||
PROCESS_FTP = 'process-ftp-tasks'
|
PROCESS_FTP = 'process-ftp-tasks'
|
||||||
CREATE_LETTERS_PDF = 'create-letters-pdf-tasks'
|
CREATE_LETTERS_PDF = 'create-letters-pdf-tasks'
|
||||||
CALLBACKS = 'service-callbacks'
|
CALLBACKS = 'service-callbacks'
|
||||||
|
LETTERS = 'letter-tasks'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def all_queues():
|
def all_queues():
|
||||||
@@ -48,6 +49,7 @@ class QueueNames(object):
|
|||||||
QueueNames.NOTIFY,
|
QueueNames.NOTIFY,
|
||||||
QueueNames.CREATE_LETTERS_PDF,
|
QueueNames.CREATE_LETTERS_PDF,
|
||||||
QueueNames.CALLBACKS,
|
QueueNames.CALLBACKS,
|
||||||
|
QueueNames.LETTERS,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -238,6 +240,11 @@ class Config(object):
|
|||||||
'schedule': crontab(hour=17, minute=30),
|
'schedule': crontab(hour=17, minute=30),
|
||||||
'options': {'queue': QueueNames.PERIODIC}
|
'options': {'queue': QueueNames.PERIODIC}
|
||||||
},
|
},
|
||||||
|
'run-letter-pdfs': {
|
||||||
|
'task': 'run-letter-pdfs',
|
||||||
|
'schedule': crontab(hour=17, minute=50),
|
||||||
|
'options': {'queue': QueueNames.PERIODIC}
|
||||||
|
},
|
||||||
'run-letter-api-notifications': {
|
'run-letter-api-notifications': {
|
||||||
'task': 'run-letter-api-notifications',
|
'task': 'run-letter-api-notifications',
|
||||||
'schedule': crontab(hour=17, minute=40),
|
'schedule': crontab(hour=17, minute=40),
|
||||||
@@ -313,6 +320,8 @@ class Config(object):
|
|||||||
TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013')
|
TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013')
|
||||||
TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key')
|
TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key')
|
||||||
|
|
||||||
|
LETTER_PROCESSING_DEADLINE = time(17, 30)
|
||||||
|
|
||||||
|
|
||||||
######################
|
######################
|
||||||
# Config overrides ###
|
# Config overrides ###
|
||||||
|
|||||||
@@ -584,3 +584,34 @@ def dao_get_last_notification_added_for_job_id(job_id):
|
|||||||
).first()
|
).first()
|
||||||
|
|
||||||
return last_notification_added
|
return last_notification_added
|
||||||
|
|
||||||
|
|
||||||
|
def dao_get_count_of_letters_to_process_for_date(date_to_process=date.today()):
|
||||||
|
"""
|
||||||
|
Returns a count of letter notifications for services with letters_as_pdf permission set
|
||||||
|
to be processed today if no argument passed in otherwise will return the count for
|
||||||
|
the date passed in.
|
||||||
|
Records processed today = yesterday 17:30 to today 17:29:59
|
||||||
|
|
||||||
|
Note - services without letters_as_pdf permission will be ignored
|
||||||
|
"""
|
||||||
|
day_before = date_to_process - timedelta(days=1)
|
||||||
|
letter_deadline_time = current_app.config.get('LETTER_PROCESSING_DEADLINE')
|
||||||
|
|
||||||
|
start_datetime = datetime.combine(day_before, letter_deadline_time)
|
||||||
|
end_datetime = start_datetime + timedelta(days=1)
|
||||||
|
|
||||||
|
count_of_letters_to_process_for_date = Notification.query.join(
|
||||||
|
Service
|
||||||
|
).filter(
|
||||||
|
Notification.created_at >= start_datetime,
|
||||||
|
Notification.created_at < end_datetime,
|
||||||
|
Notification.notification_type == LETTER_TYPE,
|
||||||
|
Notification.status == NOTIFICATION_CREATED,
|
||||||
|
Notification.key_type != KEY_TYPE_TEST,
|
||||||
|
Service.permissions.any(
|
||||||
|
ServicePermission.permission == 'letters_as_pdf'
|
||||||
|
)
|
||||||
|
).count()
|
||||||
|
|
||||||
|
return count_of_letters_to_process_for_date
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from app.dao.notifications_dao import (
|
|||||||
dao_create_notification,
|
dao_create_notification,
|
||||||
dao_created_scheduled_notification,
|
dao_created_scheduled_notification,
|
||||||
dao_delete_notifications_and_history_by_id,
|
dao_delete_notifications_and_history_by_id,
|
||||||
|
dao_get_count_of_letters_to_process_for_date,
|
||||||
dao_get_last_notification_added_for_job_id,
|
dao_get_last_notification_added_for_job_id,
|
||||||
dao_get_last_template_usage,
|
dao_get_last_template_usage,
|
||||||
dao_get_notifications_by_to_field,
|
dao_get_notifications_by_to_field,
|
||||||
@@ -33,6 +34,7 @@ from app.dao.notifications_dao import (
|
|||||||
dao_get_notifications_by_references
|
dao_get_notifications_by_references
|
||||||
)
|
)
|
||||||
from app.dao.services_dao import dao_update_service
|
from app.dao.services_dao import dao_update_service
|
||||||
|
from app.dao.service_permissions_dao import dao_add_service_permission
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Job,
|
Job,
|
||||||
Notification,
|
Notification,
|
||||||
@@ -2001,3 +2003,90 @@ def test_dao_get_notifications_by_reference(sample_template):
|
|||||||
assert len(notifications) == 2
|
assert len(notifications) == 2
|
||||||
assert notifications[0].id in [notification_1.id, notification_2.id]
|
assert notifications[0].id in [notification_1.id, notification_2.id]
|
||||||
assert notifications[1].id in [notification_1.id, notification_2.id]
|
assert notifications[1].id in [notification_1.id, notification_2.id]
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2017-12-18 17:50")
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_today(sample_letter_template):
|
||||||
|
dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf')
|
||||||
|
|
||||||
|
# expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-17 17:30:00')
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:29:59')
|
||||||
|
|
||||||
|
# not expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-17 17:29:59')
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:30:00')
|
||||||
|
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date()
|
||||||
|
|
||||||
|
assert count_for_date == 2
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2017-12-18 17:50")
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_date_in_past(sample_letter_template):
|
||||||
|
dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf')
|
||||||
|
|
||||||
|
# expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-15 17:29:59')
|
||||||
|
|
||||||
|
# not expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-15 17:30:00')
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:29:00')
|
||||||
|
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date(date(2017, 12, 15))
|
||||||
|
|
||||||
|
assert count_for_date == 1
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2017-12-18 17:50")
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_date_in_future_does_not_raise_error(sample_letter_template):
|
||||||
|
dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf')
|
||||||
|
|
||||||
|
# not expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:30:00')
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-19 17:29:59')
|
||||||
|
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date(date(2017, 12, 20))
|
||||||
|
|
||||||
|
assert count_for_date == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_today_without_notis_does_not_raise_error(sample_letter_template):
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date()
|
||||||
|
|
||||||
|
assert count_for_date == 0
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2017-12-18 17:50")
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_date_ignores_service_not_letters_as_pdf(
|
||||||
|
sample_letter_template, sample_template):
|
||||||
|
# not expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:29:00')
|
||||||
|
|
||||||
|
dao_add_service_permission(sample_template.service.id, 'letters_as_pdf')
|
||||||
|
sample_template.template_type = 'letter'
|
||||||
|
|
||||||
|
# expected
|
||||||
|
create_notification(template=sample_template, created_at='2017-12-18 17:29:00')
|
||||||
|
create_notification(template=sample_template, created_at='2017-12-18 17:29:10')
|
||||||
|
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date()
|
||||||
|
|
||||||
|
assert 'letters_as_pdf' not in [p.permission for p in sample_letter_template.service.permissions]
|
||||||
|
assert count_for_date == 2
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time("2017-12-18 17:50")
|
||||||
|
def test_dao_get_count_of_letters_to_process_for_date_ignores_test_keys(sample_letter_template):
|
||||||
|
dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf')
|
||||||
|
|
||||||
|
# not expected
|
||||||
|
create_notification(template=sample_letter_template, key_type=KEY_TYPE_TEST, created_at='2017-12-18 17:29:00')
|
||||||
|
|
||||||
|
# expected
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:29:10')
|
||||||
|
create_notification(template=sample_letter_template, created_at='2017-12-18 17:29:20')
|
||||||
|
|
||||||
|
count_for_date = dao_get_count_of_letters_to_process_for_date()
|
||||||
|
|
||||||
|
assert count_for_date == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user