Coverage for apps/accounting/signals.py: 70%
10 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-26 13:33 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-26 13:33 -0600
1from django.db.models import Sum, signals
2from django.dispatch import receiver
4from apps.campaigns.models import CampaignClaim
5from apps.rewards.models import RewardClaim
7from .models import Accounting
10@receiver(signals.pre_save, sender=Accounting)
11def update_balance_on_account_save(sender, instance, **kwargs):
12 campaign_balance = (
13 CampaignClaim.objects.filter(fan=instance.fan, campaign__organization=instance.organization, is_valid=True)
14 .aggregate(campaign_balance=Sum("campaign__value"))
15 .get("campaign_balance")
16 or 0
17 )
18 reward_balance = (
19 RewardClaim.objects.filter(fan=instance.fan, reward__organization=instance.organization)
20 .aggregate(reward_balance=Sum("reward__points_required"))
21 .get("reward_balance")
22 or 0
23 )
24 instance.balance = campaign_balance - reward_balance