Skip to content

Commit

Permalink
Fix discount usage verification
Browse files Browse the repository at this point in the history
  • Loading branch information
KwikKill committed Dec 27, 2024
1 parent d1e2092 commit 7783d4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion insalan/payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def validate_transaction(self):
# For each discount, mark it as used
for discount in self.discounts.all():
discount.use()

self.save()
logger.info("Transaction %s succeeded", self.id)
self.run_success_hooks()
Expand Down Expand Up @@ -394,6 +394,10 @@ class Meta:
)
count = models.IntegerField(default=1, editable=True, verbose_name=_("Quantité"))

# Discount

class DiscountAlreadyUsedError(Exception):
"""Error raised when trying to use an already used discount"""

class Discount(models.Model):
"""
Expand Down Expand Up @@ -434,6 +438,8 @@ class Meta:

def use(self):
"""Use the discount"""
if self.used:
raise DiscountAlreadyUsedError("Discount already used")
self.used = True
self.used_date = timezone.make_aware(datetime.now())
self.save()
2 changes: 1 addition & 1 deletion insalan/payment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def create(self, request):

# If the user has a discount for some products, apply them
for product in transaction_obj.products.all():
discounts = Discount.objects.filter(user=payer, product=product)
discounts = Discount.objects.filter(user=payer, product=product, used=False)
if discounts.exists():
discount = discounts.first()
amount = transaction_obj.amount - discount.discount
Expand Down

0 comments on commit 7783d4b

Please sign in to comment.