diff --git a/app/celery/tasks.py b/app/celery/tasks.py index d03d08e43..616d9ee77 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -43,6 +43,7 @@ from app.dao.notifications_dao import ( dao_get_notification_history_by_reference, ) from app.dao.provider_details_dao import get_provider_details_by_notification_type +from app.dao.returned_letters_dao import insert_or_update_returned_letters from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id @@ -619,6 +620,8 @@ def process_returned_letters_list(notification_references): {"status": NOTIFICATION_RETURNED_LETTER} ) + insert_or_update_returned_letters(notification_references) + current_app.logger.info( "Updated {} letter notifications ({} history notifications, from {} references) to returned-letter".format( updated, updated_history, len(notification_references) diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 515ef5f8c..698e61e74 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -44,6 +44,7 @@ from app.models import ( JOB_STATUS_IN_PROGRESS, LETTER_TYPE, SMS_TYPE, + ReturnedLetter ) from tests.app import load_example_csv @@ -1626,3 +1627,15 @@ def test_process_returned_letters_list_updates_history_if_notification_is_alread assert [n.status for n in notifications] == ['returned-letter', 'returned-letter'] assert all(n.updated_at for n in notifications) + + +def test_process_returned_letters_populates_returned_letters_table( + sample_letter_template +): + create_notification_history(sample_letter_template, reference='ref1') + create_notification_history(sample_letter_template, reference='ref2') + + process_returned_letters_list(['ref1', 'ref2', 'unknown-ref']) + + returned_letters = ReturnedLetter.query.all() + assert len(returned_letters) == 2