Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix last used on when use gift card

fowczarek6c71a4bfb017
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 7eb06a53c6..55a3c1b57b 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -3,6 +3,7 @@ from functools import wraps
from django.conf import settings
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
+from django.utils import timezone
from prices import Money, TaxedMoney
from ..account.utils import store_user_address
@@ -300,11 +301,11 @@ def add_gift_card_to_order(order, gift_card, total_price_left):
if total_price_left < gift_card.current_balance:
gift_card.current_balance = gift_card.current_balance - total_price_left
total_price_left = ZERO_MONEY
- gift_card.save(update_fields=["current_balance"])
else:
total_price_left = total_price_left - gift_card.current_balance
gift_card.current_balance = 0
- gift_card.save(update_fields=["current_balance"])
+ gift_card.last_used_on = timezone.now()
+ gift_card.save(update_fields=["current_balance", "last_used_on"])
return total_price_left
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 5b5952eeff..fa3f43082d 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -881,6 +881,8 @@ def test_checkout_complete(
address,
shipping_method,
):
+ assert not gift_card.last_used_on
+
checkout = checkout_with_gift_card
checkout.shipping_address = address
checkout.shipping_method = shipping_method
@@ -924,8 +926,10 @@ def test_checkout_complete(
order_payment = order.payments.first()
assert order_payment == payment
assert payment.transactions.count() == 1
+
gift_card.refresh_from_db()
assert gift_card.current_balance == ZERO_MONEY
+ assert gift_card.last_used_on
# assert that the checkout instance has been deleted after checkout
with pytest.raises(Checkout.DoesNotExist):