44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from celery import shared_task
|
|
from django.utils import timezone
|
|
|
|
from apps.profiles.models import SearchProfile
|
|
|
|
from .models import DigestOutbox, ReminderOutbox
|
|
from .services import (
|
|
create_daily_outbox,
|
|
create_due_reminders,
|
|
send_digest,
|
|
send_reminder,
|
|
)
|
|
|
|
|
|
@shared_task(name="apps.notifications.tasks.build_due_digests")
|
|
def build_due_digests() -> dict[str, int]:
|
|
created = sent = 0
|
|
for profile in SearchProfile.objects.filter(is_active=True).select_related("user"):
|
|
outbox = create_daily_outbox(profile)
|
|
if outbox is None:
|
|
continue
|
|
created += 1
|
|
if outbox.status == DigestOutbox.Status.PENDING:
|
|
send_digest(outbox)
|
|
sent += 1
|
|
return {"processed": created, "sent": sent}
|
|
|
|
|
|
@shared_task(name="apps.notifications.tasks.build_due_reminders")
|
|
def build_due_reminders() -> dict[str, int]:
|
|
now = timezone.now()
|
|
created = sent = 0
|
|
for profile in SearchProfile.objects.filter(is_active=True).select_related("user"):
|
|
outboxes = create_due_reminders(profile, now=now)
|
|
created += len(outboxes)
|
|
for outbox in ReminderOutbox.objects.filter(
|
|
status=ReminderOutbox.Status.PENDING, scheduled_for__lte=now
|
|
).select_related("profile"):
|
|
send_reminder(outbox)
|
|
sent += 1
|
|
return {"created": created, "sent": sent}
|