Skip to content

Commit

Permalink
Add comments for user.add_balance, remove unnecessary is_paying from …
Browse files Browse the repository at this point in the history
…update_fields there
  • Loading branch information
nikochiko authored and devxpy committed Oct 16, 2023
1 parent 9adaa93 commit 635d880
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app_users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ def first_name(self):
@db_middleware
@transaction.atomic
def add_balance(self, amount: int, invoice_id: str) -> "AppUserTransaction":
"""
Used to add/deduct credits when they are bought or consumed.
When credits are bought with stripe -- invoice_id is the stripe
invoice ID.
When credits are deducted due to a run -- invoice_id is of the
form "gooey_in_{uuid}"
"""

# if an invoice entry exists
try:
# avoid updating twice for same invoice
Expand All @@ -121,7 +130,7 @@ def add_balance(self, amount: int, invoice_id: str) -> "AppUserTransaction":
# Also we're not using .update() here because it won't give back the updated end balance
user: AppUser = AppUser.objects.select_for_update().get(pk=self.pk)
user.balance += amount
user.save(update_fields=["balance", "is_paying"])
user.save(update_fields=["balance"])

return AppUserTransaction.objects.create(
user=self,
Expand Down

0 comments on commit 635d880

Please sign in to comment.