diff --git a/app/celery/tasks.py b/app/celery/tasks.py index bece59f7c..52a5741f3 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -36,6 +36,7 @@ from app.dao.jobs_dao import ( dao_update_job_status) from app.dao.notifications_dao import ( get_notification_by_id, + update_notification_status_by_reference, dao_update_notifications_for_job_to_sent_to_dvla, dao_update_notifications_by_reference, dao_get_last_notification_added_for_job_id) @@ -46,6 +47,7 @@ from app.dao.templates_dao import dao_get_template_by_id from app.models import ( Job, Notification, + DVLA_STATUS_SENT, EMAIL_TYPE, JOB_STATUS_CANCELLED, JOB_STATUS_FINISHED, @@ -55,6 +57,8 @@ from app.models import ( JOB_STATUS_SENT_TO_DVLA, JOB_STATUS_ERROR, KEY_TYPE_NORMAL, LETTER_TYPE, + NOTIFICATION_DELIVERED, + NOTIFICATION_FAILED, NOTIFICATION_SENDING, NOTIFICATION_TECHNICAL_FAILURE, SMS_TYPE, @@ -443,8 +447,21 @@ def update_letter_notifications_statuses(self, filename): raise else: for update in notification_updates: - current_app.logger.info('DVLA update: {}'.format(str(update))) - # TODO: Update notifications with desired status + status = NOTIFICATION_DELIVERED if update.status == DVLA_STATUS_SENT else NOTIFICATION_FAILED + notification = update_notification_status_by_reference( + update.reference, + status + ) + + if not notification: + msg = "Update letter notification file {filename} failed: notification either not found " \ + "or already updated from delivered. Status {status} for notification reference {reference}".format( + filename=filename, status=status, reference=update.reference) + current_app.logger.error(msg) + else: + current_app.logger.info( + 'DVLA file: {filename}, notification updated to {status}: {reference}'.format( + filename=filename, status=status, reference=str(update.reference))) def process_updates_from_file(response_file): diff --git a/tests/app/celery/test_ftp_update_tasks.py b/tests/app/celery/test_ftp_update_tasks.py index dc96ac91d..6239d9285 100644 --- a/tests/app/celery/test_ftp_update_tasks.py +++ b/tests/app/celery/test_ftp_update_tasks.py @@ -7,10 +7,14 @@ from flask import current_app from app.models import ( Job, Notification, + NOTIFICATION_DELIVERED, + NOTIFICATION_FAILED, NOTIFICATION_SENDING, NOTIFICATION_CREATED, - NOTIFICATION_TECHNICAL_FAILURE + NOTIFICATION_TECHNICAL_FAILURE, + NOTIFICATION_STATUS_LETTER_RECEIVED ) +from app.dao.notifications_dao import dao_update_notifications_by_reference from app.celery.tasks import ( update_job_to_sent_to_dvla, update_dvla_job_to_error, @@ -91,6 +95,20 @@ def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mo assert updates[1].cost_threshold == 'Sorted' +def test_update_letter_notifications_statuses_persisted(notify_api, mocker, sample_letter_template): + sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING) + failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING) + + valid_file = '{}|Sent|1|Unsorted\n{}|Failed|2|Sorted'.format( + sent_letter.reference, failed_letter.reference) + mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) + + update_letter_notifications_statuses(filename='foo.txt') + + assert sent_letter.status == NOTIFICATION_DELIVERED + assert failed_letter.status == NOTIFICATION_FAILED + + def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references( client, sample_letter_template