import logging from celery import shared_task logger = logging.getLogger(__name__) @shared_task(queue="default") def notify_challenge_expiring(challenge_id: str): """ Send an FCM notification to all participants of a challenge that is expiring soon. Scheduled by celery-beat. """ from apps.challenges.models import Challenge, ChallengeParticipant from apps.utils.fcm import send_notification try: challenge = Challenge.objects.get(pk=challenge_id, status=Challenge.Status.ACTIVE) except Challenge.DoesNotExist: return participants = ChallengeParticipant.objects.filter( challenge=challenge, submitted_splat__isnull=True, # only those who haven't submitted yet ).select_related("user") for participant in participants: send_notification( participant.user.fcm_token, title="Challenge expiring soon!", body=f'"{challenge.title}" is closing soon. Don\'t miss your chance!', data={"challenge_id": str(challenge.id), "type": "challenge_expiring"}, ) logger.info("Sent expiry notifications for challenge %s to %d participants", challenge_id, participants.count())