diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index fece5b3d2..fed5d1be8 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 @@ -95,6 +96,32 @@ 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() + 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}" + ) + 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..f53556b95 100644 --- a/app/service_invite/rest.py +++ b/app/service_invite/rest.py @@ -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,7 @@ 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, ) saved_notification.personalisation = personalisation redis_store.set( 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)