Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invite expiration fix #1534

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading