Skip to content

Commit ce85461

Browse files
committed
Adding get_notifcation_by_job_and_job_row_number and using it to filter exceptions
Job Id and job row number are unique in the notifications table so they can be used to see if a notification has been added to the db already even if the notification has been given a different notification id. Handle error is the logic for all types of notification, but I only tested one save function .
1 parent c1110a0 commit ce85461

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

app/celery/tasks.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
dao_get_unknown_references,
4040
dao_update_notifications_by_reference,
4141
get_notification_by_id,
42+
get_notification_by_job_and_job_row_number,
4243
)
4344
from app.dao.report_requests_dao import dao_get_report_request_by_id, dao_update_report_request
4445
from app.dao.returned_letters_dao import _get_notification_ids_for_references, insert_returned_letters
@@ -513,12 +514,16 @@ def save_letter(
513514

514515

515516
def handle_exception(task, notification, notification_id, exc):
516-
if not get_notification_by_id(notification_id):
517+
job_id = notification.get("job", None)
518+
job_row_number = notification.get("row_number", None)
519+
if not get_notification_by_id(notification_id) and not get_notification_by_job_and_job_row_number(
520+
job_id, job_row_number
521+
):
517522
extra = {
518523
"notification_id": notification_id,
519524
"celery_task": task.name,
520-
"job_id": notification.get("job", None),
521-
"job_row_number": notification.get("row_number", None),
525+
"job_id": job_id,
526+
"job_row_number": job_row_number,
522527
}
523528
base_msg = (
524529
"task %(celery_task)s notification for job %(job_id)s row number %(job_row_number)s "

app/dao/notifications_dao.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ def get_notification_by_id(notification_id, service_id=None, _raise=False):
272272
return query.one() if _raise else query.first()
273273

274274

275+
def get_notification_by_job_and_job_row_number(job_id, job_row_number):
276+
filters = [Notification.job_id == job_id, Notification.job_row_number == job_row_number]
277+
query = Notification.query.filter(*filters)
278+
return query.first()
279+
280+
275281
def dao_get_notification_or_history_by_id(notification_id):
276282
if notification := Notification.query.get(notification_id):
277283
return notification

tests/app/celery/test_tasks.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,29 @@ def test_save_email_does_not_send_duplicate_and_does_not_put_in_retry_queue(
17161716
assert not retry.called
17171717

17181718

1719+
def test_save_email_does_not_send_duplicate_and_does_not_put_in_retry_queue_based_on_job_info(
1720+
sample_email_notification, mocker, mock_celery_task
1721+
):
1722+
json = _notification_json(
1723+
sample_email_notification.template,
1724+
sample_email_notification.to,
1725+
job_id=sample_email_notification.job_id,
1726+
row_number=sample_email_notification.job_row_number,
1727+
)
1728+
mock_task = mock_celery_task(provider_tasks.deliver_email)
1729+
retry = mocker.patch("app.celery.tasks.save_email.retry", side_effect=Exception())
1730+
1731+
save_email(
1732+
sample_email_notification.service_id,
1733+
uuid.uuid4(),
1734+
signing.encode(json),
1735+
)
1736+
1737+
assert Notification.query.count() == 1
1738+
assert not mock_task.called
1739+
assert not retry.called
1740+
1741+
17191742
def test_save_sms_does_not_send_duplicate_and_does_not_put_in_retry_queue(
17201743
sample_notification, mocker, mock_celery_task
17211744
):

tests/app/dao/notification_dao/test_notification_dao.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
dao_update_notification,
4040
dao_update_notifications_by_reference,
4141
get_notification_by_id,
42+
get_notification_by_job_and_job_row_number,
4243
get_notification_with_personalisation,
4344
get_notifications_for_job,
4445
get_notifications_for_service,
@@ -404,6 +405,20 @@ def test_get_notification_by_id_when_notification_does_not_exist(notify_db_sessi
404405
assert notification_from_db is None
405406

406407

408+
def test_get_notification_by_job_and_row_number_when_notification_exists(sample_notification):
409+
notification_from_db = get_notification_by_job_and_job_row_number(
410+
sample_notification.job_id, sample_notification.job_row_number
411+
)
412+
413+
assert sample_notification == notification_from_db
414+
415+
416+
def test_get_notification_by_job_and_row_number_when_job_id_notification_does_not_exist(notify_db_session, fake_uuid):
417+
notification_from_db = get_notification_by_job_and_job_row_number(fake_uuid, 1)
418+
419+
assert notification_from_db is None
420+
421+
407422
def test_get_notification_by_id_when_notification_exists_for_different_service(sample_notification):
408423
another_service = create_service(service_name="Another service")
409424

0 commit comments

Comments
 (0)