From f4972037912af5af98fe90f6fa6607180aac12d4 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Mon, 13 Jan 2025 10:07:33 -0700 Subject: [PATCH 1/5] Ensure created_at stamp is correct --- app/dao/notifications_dao.py | 25 ++++++++++++++++++++++++- app/service_invite/rest.py | 8 ++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 139f7ae8a..691b29065 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -1,5 +1,6 @@ import json -from datetime import timedelta +import os +from datetime import datetime, timedelta from time import time from flask import current_app @@ -29,6 +30,7 @@ from app.utils import ( escape_special_characters, get_midnight_in_utc, + hilite, midnight_n_days_ago, utc_now, ) @@ -95,6 +97,27 @@ def dao_create_notification(notification): # notify-api-1454 insert only if it doesn't exist if not dao_notification_exists(notification.id): db.session.add(notification) + # There have been issues with invites expiring. + # Ensure the created at value is set and debug. + if notification.notification_type == "email": + orig_time = notification.created_at + + now_time = utc_now() + print(hilite(f"original time: {orig_time} - {type(orig_time)} \n now time: {now_time} - {type(now_time)}")) + diff_time = now_time - datetime.strptime(orig_time, "%Y-%m-%D-%H-%M-%S") + current_app.logger.error( + f"dao_create_notification orig created at: {orig_time} and now created at: {now_time}" + ) + if diff_time.total_seconds() > 300: + current_app.logger.error( + "Something is wrong with notification.created_at in email!" + ) + if os.getenv("NOTIFY_ENVIRONMENT") not in ["test"]: + notification.created_at = now_time + dao_update_notification(notification) + current_app.logger.error( + f"Email notification created_at reset to {notification.created_at}" + ) def country_records_delivery(phone_prefix): diff --git a/app/service_invite/rest.py b/app/service_invite/rest.py index e375b93a5..e1f26236f 100644 --- a/app/service_invite/rest.py +++ b/app/service_invite/rest.py @@ -25,7 +25,7 @@ send_notification_to_queue, ) from app.schemas import invited_user_schema -from app.utils import utc_now +from app.utils import hilite, utc_now from notifications_utils.url_safe_token import check_token, generate_token service_invite = Blueprint("service_invite", __name__) @@ -67,7 +67,7 @@ def _create_service_invite(invited_user, nonce, state): "service_name": invited_user.service.name, "url": url, } - + created_at = utc_now() saved_notification = persist_notification( template_id=template.id, template_version=template.version, @@ -78,6 +78,10 @@ def _create_service_invite(invited_user, nonce, state): api_key_id=None, key_type=KeyType.NORMAL, reply_to_text=invited_user.from_user.email_address, + created_at=created_at, + ) + print( + hilite(f"saved notification created at time: {saved_notification.created_at}") ) saved_notification.personalisation = personalisation redis_store.set( From 3fd8009e3336e5522e35492e9ff0c34ed8ea8911 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Wed, 15 Jan 2025 12:49:47 -0700 Subject: [PATCH 2/5] Add error handling for possible string/datetime created at stamps --- app/dao/notifications_dao.py | 15 +++++++++++++-- tests/app/celery/test_reporting_tasks.py | 1 - 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 4ad50c111..b5690e535 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -103,8 +103,19 @@ def dao_create_notification(notification): orig_time = notification.created_at now_time = utc_now() - print(hilite(f"original time: {orig_time} - {type(orig_time)} \n now time: {now_time} - {type(now_time)}")) - diff_time = now_time - datetime.strptime(orig_time, "%Y-%m-%D-%H-%M-%S") + print( + hilite( + f"original time: {orig_time} - {type(orig_time)} \n now time: {now_time} - {type(now_time)}" + ) + ) + try: + diff_time = now_time - orig_time + except TypeError: + try: + orig_time = datetime.strptime(orig_time, "%Y-%m-%dT%H:%M:%S.%fZ") + except ValueError: + orig_time = datetime.strptime(orig_time, "%Y-%m-%d") + diff_time = now_time - orig_time current_app.logger.error( f"dao_create_notification orig created at: {orig_time} and now created at: {now_time}" ) diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index 124038d48..952c65e09 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -103,7 +103,6 @@ def test_create_nightly_notification_status_triggers_relevant_tasks( mock_celery = mocker.patch( "app.celery.reporting_tasks.create_nightly_notification_status_for_service_and_day" ).apply_async - for notification_type in NotificationType: template = create_template(sample_service, template_type=notification_type) create_notification(template=template, created_at=notification_date) From f1118d6a198b5279011679e6e383206b77ba7867 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Thu, 16 Jan 2025 08:59:27 -0700 Subject: [PATCH 3/5] Remove print statement --- app/dao/notifications_dao.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index b5690e535..ba04f24ba 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -101,13 +101,7 @@ def dao_create_notification(notification): # Ensure the created at value is set and debug. if notification.notification_type == "email": orig_time = notification.created_at - now_time = utc_now() - print( - hilite( - f"original time: {orig_time} - {type(orig_time)} \n now time: {now_time} - {type(now_time)}" - ) - ) try: diff_time = now_time - orig_time except TypeError: From 7a7daf8323724a07e922f6b775e50defe5774b97 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Thu, 16 Jan 2025 09:02:07 -0700 Subject: [PATCH 4/5] Remove another print statement --- app/service_invite/rest.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/service_invite/rest.py b/app/service_invite/rest.py index e1f26236f..d59af35ca 100644 --- a/app/service_invite/rest.py +++ b/app/service_invite/rest.py @@ -80,9 +80,6 @@ def _create_service_invite(invited_user, nonce, state): reply_to_text=invited_user.from_user.email_address, created_at=created_at, ) - print( - hilite(f"saved notification created at time: {saved_notification.created_at}") - ) saved_notification.personalisation = personalisation redis_store.set( f"email-personalisation-{saved_notification.id}", From d7c97d64280b07e31d1d1d121b2fd1f75f7ff4fa Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Thu, 16 Jan 2025 09:05:49 -0700 Subject: [PATCH 5/5] Remove hilite imports --- app/dao/notifications_dao.py | 1 - app/service_invite/rest.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index e1aed5037..fed5d1be8 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -30,7 +30,6 @@ from app.utils import ( escape_special_characters, get_midnight_in_utc, - hilite, midnight_n_days_ago, utc_now, ) diff --git a/app/service_invite/rest.py b/app/service_invite/rest.py index d59af35ca..f53556b95 100644 --- a/app/service_invite/rest.py +++ b/app/service_invite/rest.py @@ -25,7 +25,7 @@ send_notification_to_queue, ) from app.schemas import invited_user_schema -from app.utils import hilite, utc_now +from app.utils import utc_now from notifications_utils.url_safe_token import check_token, generate_token service_invite = Blueprint("service_invite", __name__)