39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
from celery import shared_task
|
|
from django.utils import timezone
|
|
from django.db import transaction
|
|
|
|
|
|
@shared_task
|
|
def process_pending_referral_bonuses():
|
|
"""Ежедневная обработка отложенных реферальных бонусов."""
|
|
from .models import PendingReferralBonus, UserActivityDay, UserReferralProfile
|
|
|
|
now = timezone.now()
|
|
paid_count = 0
|
|
|
|
for pending in PendingReferralBonus.objects.filter(
|
|
status=PendingReferralBonus.STATUS_PENDING
|
|
).select_related('referrer', 'referred_user'):
|
|
referred_at = pending.referred_at
|
|
active_days = UserActivityDay.objects.filter(
|
|
user=pending.referred_user,
|
|
date__gte=referred_at.date(),
|
|
).count()
|
|
days_since = (now - referred_at).days
|
|
if (days_since >= 30 and active_days >= 20) or active_days >= 21:
|
|
try:
|
|
with transaction.atomic():
|
|
profile = pending.referrer.referral_profile
|
|
profile.add_points(
|
|
pending.points,
|
|
reason=pending.reason or f'Реферал {pending.referred_user.email} выполнил условия'
|
|
)
|
|
pending.status = PendingReferralBonus.STATUS_PAID
|
|
pending.paid_at = now
|
|
pending.save(update_fields=['status', 'paid_at'])
|
|
paid_count += 1
|
|
except Exception:
|
|
pass
|
|
|
|
return f'Начислено бонусов: {paid_count}'
|