Skip to content

Commit

Permalink
Fixed checks on sending
Browse files Browse the repository at this point in the history
  • Loading branch information
clr-li committed Feb 15, 2024
1 parent a84933b commit 618609e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions celeryapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,31 @@ def save(done=False):

def low_balance_email(sr: SavedRun):
user = AppUser.objects.get(uid=sr.uid)
last_positive_transaction = (
AppUserTransaction.objects.filter(user=user, amount__gt=0)
.order_by("-created_at")
.first()
)
if last_positive_transaction:
last_positive_transaction = last_positive_transaction.created_at
else:
last_positive_transaction = timezone.now() - datetime.timedelta(days=8)
if (
user.is_paying
and user.balance < 500
and (
user.low_balance_email_sent_at == None
or user.low_balance_email_sent_at
< timezone.now() - datetime.timedelta(days=7)
or last_positive_transaction > user.low_balance_email_sent_at
)
):
total_credits_consumed = (
-1
* AppUserTransaction.objects.filter(
user=user,
created_at__gte=timezone.now() - datetime.timedelta(days=7),
amount__lt=0,
).aggregate(Sum("amount"))["amount__sum"]
)
send_low_balance_email(
Expand Down

0 comments on commit 618609e

Please sign in to comment.