diff --git a/app/celery/letters_pdf_tasks.py b/app/celery/letters_pdf_tasks.py index f76492849..43ca345b2 100644 --- a/app/celery/letters_pdf_tasks.py +++ b/app/celery/letters_pdf_tasks.py @@ -128,15 +128,17 @@ def collate_letter_pdfs_for_day(): that have not yet been sent. If run after midnight, it will collect up letters created before 5:30pm the day before. """ - date = convert_utc_to_bst(datetime.utcnow()) - if date.time() < LETTER_PROCESSING_DEADLINE: - date = date - timedelta(days=1) + print_run_date = convert_utc_to_bst(datetime.utcnow()) + if print_run_date.time() < LETTER_PROCESSING_DEADLINE: + print_run_date = print_run_date - timedelta(days=1) - # Using the truncated date is ok because UTC to BST does not make a difference to the date, - # since it is triggered mid afternoon. - print_run_date = date.strftime("%Y-%m-%d") + # We can truncate the datetime to a date and add our own time (5:30pm) because UTC to BST does not + # make a difference to the date since it is triggered mid afternoon. + print_run_deadline = print_run_date.replace( + hour=17, minute=30, second=0, microsecond=0 + ) - letters_to_print = get_key_and_size_of_letters_to_be_sent_to_print(print_run_date) + letters_to_print = get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline) for i, letters in enumerate(group_letters(letters_to_print)): filenames = [letter['Key'] for letter in letters] @@ -144,7 +146,7 @@ def collate_letter_pdfs_for_day(): hash = urlsafe_b64encode(sha512(''.join(filenames).encode()).digest())[:20].decode() # eg NOTIFY.2018-12-31.001.Wjrui5nAvObjPd-3GEL-.ZIP dvla_filename = 'NOTIFY.{date}.{num:03}.{hash}.ZIP'.format( - date=print_run_date, + date=print_run_deadline.strftime("%Y-%m-%d"), num=i + 1, hash=hash ) @@ -167,8 +169,8 @@ def collate_letter_pdfs_for_day(): ) -def get_key_and_size_of_letters_to_be_sent_to_print(print_run_date): - letters_awaiting_sending = dao_get_letters_to_be_printed(print_run_date) +def get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline): + letters_awaiting_sending = dao_get_letters_to_be_printed(print_run_deadline) letter_pdfs = [] for letter in letters_awaiting_sending: diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 7bec34351..645bf9090 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -731,16 +731,12 @@ def notifications_not_yet_sent(should_be_sending_after_seconds, notification_typ return notifications -def dao_get_letters_to_be_printed(print_run_date): +def dao_get_letters_to_be_printed(print_run_deadline): """ - Given a date for a print run, Return all letters created before 5:30pm that day that have not yet been sent + Return all letters created before the print run deadline that have not yet been sent """ - last_processing_deadline = datetime.strptime(print_run_date, "%Y-%m-%d").replace( - hour=17, minute=30, second=0, microsecond=0 - ) - notifications = Notification.query.filter( - Notification.created_at < convert_bst_to_utc(last_processing_deadline), + Notification.created_at < convert_bst_to_utc(print_run_deadline), Notification.notification_type == LETTER_TYPE, Notification.status == NOTIFICATION_CREATED, Notification.key_type == KEY_TYPE_NORMAL diff --git a/tests/app/celery/test_letters_pdf_tasks.py b/tests/app/celery/test_letters_pdf_tasks.py index a66045e2b..911907009 100644 --- a/tests/app/celery/test_letters_pdf_tasks.py +++ b/tests/app/celery/test_letters_pdf_tasks.py @@ -296,7 +296,7 @@ def test_get_key_and_size_of_letters_to_be_sent_to_print(notify_api, mocker, sam {'ContentLength': 3}, ]) - results = get_key_and_size_of_letters_to_be_sent_to_print('2020-02-17') + results = get_key_and_size_of_letters_to_be_sent_to_print(datetime.now() - timedelta(minutes=30)) assert mock_s3.call_count == 3 mock_s3.assert_has_calls(