diff --git a/app/letters/utils.py b/app/letters/utils.py index 786edbc57..8363d05a3 100644 --- a/app/letters/utils.py +++ b/app/letters/utils.py @@ -58,7 +58,10 @@ def get_bucket_name_and_prefix_for_notification(notification): bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME'] else: bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] - folder = get_folder_name(notification.created_at, False) + if notification.sent_at: + folder = "{}/".format(notification.sent_at.date()) + else: + folder = get_folder_name(notification.updated_at, False) upload_file_name = PRECOMPILED_BUCKET_PREFIX.format( folder=folder, diff --git a/tests/app/letters/test_letter_utils.py b/tests/app/letters/test_letter_utils.py index 7b989a582..75b9da976 100644 --- a/tests/app/letters/test_letter_utils.py +++ b/tests/app/letters/test_letter_utils.py @@ -27,6 +27,7 @@ def _sample_precompiled_letter_notification(sample_letter_notification): sample_letter_notification.reference = 'foo' with freeze_time(FROZEN_DATE_TIME): sample_letter_notification.created_at = datetime.utcnow() + sample_letter_notification.updated_at = datetime.utcnow() return sample_letter_notification @@ -42,6 +43,7 @@ def _sample_precompiled_letter_notification_using_test_key(sample_precompiled_le ]) def test_get_bucket_name_and_prefix_for_notification_valid_notification(sample_notification, created_at, folder): sample_notification.created_at = created_at + sample_notification.updated_at = created_at bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(sample_notification) @@ -52,6 +54,19 @@ def test_get_bucket_name_and_prefix_for_notification_valid_notification(sample_n ).upper() +def test_get_bucket_name_and_prefix_for_notification_get_from_sent_at_date(sample_notification): + sample_notification.created_at = datetime(2019, 8, 1, 17, 35) + sample_notification.sent_at = datetime(2019, 8, 2, 17, 45) + + bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(sample_notification) + + assert bucket == current_app.config['LETTERS_PDF_BUCKET_NAME'] + assert bucket_prefix == '{folder}/NOTIFY.{reference}'.format( + folder='2019-08-02', + reference=sample_notification.reference + ).upper() + + @freeze_time(FROZEN_DATE_TIME) def test_get_bucket_name_and_prefix_for_notification_precompiled_letter_using_test_key( sample_precompiled_letter_notification_using_test_key