Skip to content

Commit

Permalink
Merge pull request #1534 from GSA/invite-expiration-fix
Browse files Browse the repository at this point in the history
Invite expiration fix
  • Loading branch information
ccostino authored Jan 16, 2025
2 parents 0a7ccc5 + d7c97d6 commit e9206c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion app/dao/notifications_dao.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion app/service_invite/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion tests/app/celery/test_reporting_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e9206c3

Please sign in to comment.