From 46ec1aafa88cb800f3f69cef1e72f59a15caeb61 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Mon, 23 Dec 2024 11:17:51 -0500 Subject: [PATCH] Getting the IntegrityErrors to be OK if the data already exists in the DB. Signed-off-by: Cliff Hill --- app/celery/tasks.py | 46 ++++++++++++---------- app/dao/notifications_dao.py | 10 +++-- app/notifications/process_notifications.py | 5 +++ 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index c21f4e65c..72d23531f 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -23,6 +23,7 @@ from app.enums import JobStatus, KeyType, NotificationType from app.errors import TotalRequestsError from app.notifications.process_notifications import ( get_notification, + notification_exists, persist_notification, ) from app.notifications.validators import check_service_over_total_message_limit @@ -218,22 +219,28 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i job = dao_get_job_by_id(job_id) created_by_id = job.created_by_id - saved_notification = persist_notification( - template_id=notification["template"], - template_version=notification["template_version"], - recipient=notification["to"], - service=service, - personalisation=notification.get("personalisation"), - notification_type=NotificationType.SMS, - api_key_id=None, - key_type=KeyType.NORMAL, - created_at=utc_now(), - created_by_id=created_by_id, - job_id=notification.get("job", None), - job_row_number=notification.get("row_number", None), - notification_id=notification_id, - reply_to_text=reply_to_text, - ) + try: + saved_notification = persist_notification( + template_id=notification["template"], + template_version=notification["template_version"], + recipient=notification["to"], + service=service, + personalisation=notification.get("personalisation"), + notification_type=NotificationType.SMS, + api_key_id=None, + key_type=KeyType.NORMAL, + created_at=utc_now(), + created_by_id=created_by_id, + job_id=notification.get("job", None), + job_row_number=notification.get("row_number", None), + notification_id=notification_id, + reply_to_text=reply_to_text, + ) + except IntegrityError as e: + if notification_exists(notification_id): + saved_notification = get_notification(notification_id) + else: + raise # Kick off sns process in provider_tasks.py sn = saved_notification @@ -247,11 +254,8 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i ) current_app.logger.debug( - "SMS {} created at {} for job {}".format( - saved_notification.id, - saved_notification.created_at, - notification.get("job", None), - ) + f"SMS {saved_notification.id} created at {saved_notification.created_at} " + f"for job {notification.get('job', None)}" ) except SQLAlchemyError as e: diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 6ce97b801..b9c3118fa 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -65,6 +65,12 @@ def dao_get_last_date_template_was_used(template_id, service_id): return last_date +def dao_notification_exists(notification_id) -> bool: + stmt = select(Notification).where(Notification.id == notification_id) + result = db.session.execute(stmt).scalar() + return result is not None + + @autocommit def dao_create_notification(notification): if not notification.id: @@ -86,9 +92,7 @@ def dao_create_notification(notification): notification.normalised_to = "1" # notify-api-1454 insert only if it doesn't exist - stmt = select(Notification).where(Notification.id == notification.id) - result = db.session.execute(stmt).scalar() - if result is None: + if not dao_notification_exists(notification.id): db.session.add(notification) diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 8568b8894..5f1c6676d 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -8,6 +8,7 @@ from app.config import QueueNames from app.dao.notifications_dao import ( dao_create_notification, dao_delete_notifications_by_id, + dao_notification_exists, get_notification_by_id, ) from app.enums import KeyType, NotificationStatus, NotificationType @@ -153,6 +154,10 @@ def persist_notification( return notification +def notification_exists(notification_id): + return dao_notification_exists(notification_id) + + def send_notification_to_queue_detached( key_type, notification_type, notification_id, queue=None ):