diff --git a/apps/promotions/models.py b/apps/promotions/models.py index 269eda1..6f9a661 100644 --- a/apps/promotions/models.py +++ b/apps/promotions/models.py @@ -3,7 +3,7 @@ import uuid from django.conf import settings from django.contrib.postgres.indexes import GinIndex from django.db import models, transaction -from django.db.models import F +from django.db.models import F, Q from django.utils import timezone from django_minio_backend import MinioBackend, iso_date_prefix @@ -136,10 +136,16 @@ class Plan(BaseModel): return self.title def get_configured_promotion_amount(self, user_uuid=None): + access_filter = Q(access_type=RecipientTypeChoices.PUBLIC) + if user_uuid: + access_filter |= Q(access_type=RecipientTypeChoices.RESTRICTED, allowed_users__user__uuid=user_uuid) + + recipients = self.recipients.filter( + recipient_uuid_field='->event:user' #TODO: instead of hard code use better soloution + ).filter(access_filter).distinct() + amount = 0 - for recipient in self.recipients.filter(recipient_uuid_field='->event:user'): #TODO: instead of hard code use better soloution - if not recipient.is_user_allowed(user_uuid): - continue + for recipient in recipients: base_amount, promotion_amount = recipient.get_promotion_amount() amount += promotion_amount return amount @@ -393,7 +399,7 @@ class PromotionQuerySet(models.QuerySet): def get_event_status_for_user(user_uuid, event_label): processed = Promotion.objects.is_processed_for_user(user_uuid, event_label) - promotion_amount = None + promotion_amount = 0 if not processed: plan = Plan.objects.filter(event_list__icontains=event_label).first() if plan: