60 lines
2.6 KiB
Python
60 lines
2.6 KiB
Python
"""Automatic event registration notifications."""
|
|
|
|
import logging
|
|
|
|
from celery import shared_task
|
|
|
|
from apps.events.models import Registration
|
|
from apps.events.checkout import expire_stale_registration_checkouts
|
|
from apps.notifications.services import notify_user
|
|
from apps.users.tasks import send_critical_sms
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@shared_task(name="apps.events.expire_stale_registration_checkouts")
|
|
def expire_stale_registration_checkouts_task():
|
|
"""Release seats held by new checkout attempts that reached their deadline."""
|
|
return expire_stale_registration_checkouts()
|
|
|
|
|
|
@shared_task(bind=True, max_retries=3)
|
|
def send_registration_confirmation_notification(self, registration_pk: str):
|
|
try:
|
|
registration = Registration.objects.select_related("event", "user").get(pk=registration_pk)
|
|
notify_user(registration.user_id, {
|
|
"type": "event_registration",
|
|
"title": f"ثبتنام شما برای «{registration.event.title}» تأیید شد",
|
|
"message": "ثبتنام شما در این رویداد تأیید شده است.",
|
|
"level": "success",
|
|
"action_url": f"/events/{registration.event.slug}",
|
|
"entity_type": "event",
|
|
"entity_id": registration.event_id,
|
|
"meta": {"ticket_id": str(registration.ticket_id)},
|
|
})
|
|
return {"sent": True}
|
|
except Exception as exc:
|
|
logger.error("Failed to notify registration confirmation: %s", exc)
|
|
raise self.retry(exc=exc, countdown=60)
|
|
|
|
|
|
@shared_task(bind=True, max_retries=3)
|
|
def send_registration_cancellation_notification(self, registration_pk: str):
|
|
try:
|
|
registration = Registration.objects.select_related("event", "user").get(pk=registration_pk)
|
|
notify_user(registration.user_id, {
|
|
"type": "event_cancellation",
|
|
"title": f"ثبتنام شما برای «{registration.event.title}» لغو شد",
|
|
"message": "ثبتنام شما در این رویداد لغو شد.",
|
|
"level": "warning",
|
|
"action_url": f"/events/{registration.event.slug}",
|
|
"entity_type": "event",
|
|
"entity_id": registration.event_id,
|
|
})
|
|
if registration.user.mobile and registration.user.is_mobile_verified:
|
|
send_critical_sms.delay(registration.user.mobile, "event_cancellation", registration.event.title)
|
|
return {"sent": True}
|
|
except Exception as exc:
|
|
logger.error("Failed to notify registration cancellation: %s", exc)
|
|
raise self.retry(exc=exc, countdown=60)
|