refactor(src/notifications): batch‑fetch reminder logs to avoid per‑user DB queries

Reduce DB round‑trips when checking already‑sent deadline reminders, improving performance and simplifying query logic.
This commit is contained in:
sankalp committed 2026-06-30 10:35:21 +05:45
1 parent 3e9c06f722
commit 4b2fa52060
2 files changed
+12 -16

No files matched your search

+11 -8
View File
@@ -188,11 +188,20 @@ def process_deadline_reminders(ticket=None):
if t.created_by and t.created_by not in assignees:
assignees.append(t.created_by)
# Batch-fetch existing reminder logs for this ticket's assignees
existing_logs = set()
if assignees:
logs_qs = DeadlineReminderLog.objects.filter(
ticket=t,
user__in=assignees,
days_before__in=REMINDER_DAYS,
).values_list("user_id", "days_before")
existing_logs = {(uid, db) for uid, db in logs_qs}
for user in assignees:
if not user.email:
continue
# Respect user email preference
if (
hasattr(user, "receive_deadline_emails")
and not user.receive_deadline_emails
@@ -203,13 +212,7 @@ def process_deadline_reminders(ticket=None):
for days_before in REMINDER_DAYS:
if days_until_due == days_before:
already_sent = DeadlineReminderLog.objects.filter(
ticket=t,
user=user,
days_before=days_before,
).exists()
if already_sent:
if (user.id, days_before) in existing_logs:
continue
send_deadline_reminder_email(user, t, days_before)
+1 -8
View File
@@ -1,5 +1,3 @@
import json
from django.utils import timezone
from rest_framework import generics, permissions, status
from rest_framework.decorators import api_view, permission_classes
@@ -71,12 +69,7 @@ def subscribe_push_view(request):
@api_view(["POST"])
@permission_classes([permissions.IsAuthenticated])
def unsubscribe_push_view(request):
try:
data = json.loads(request.body)
except json.JSONDecodeError:
return Response({"error": "Invalid JSON"}, status=status.HTTP_400_BAD_REQUEST)
endpoint = data.get("endpoint")
endpoint = request.data.get("endpoint")
if not endpoint:
return Response(
{"error": "endpoint required"}, status=status.HTTP_400_BAD_REQUEST