Skip to content

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

Move activate and deactivate gift card logic to utils

fowczarekdd20057b9e24
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index a87de62b77..4b245dbaaf 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -18,7 +18,21 @@ def add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str):
def remove_gift_card_code_from_checkout(checkout: Checkout, gift_card_code: str):
- """Remove voucher data from checkout by code"""
+ """Remove gift card data from checkout by code"""
gift_card = checkout.gift_cards.filter(code=gift_card_code).first()
if gift_card:
checkout.gift_cards.remove(gift_card)
+
+
+def deactivate_gift_card(gift_card: GiftCard):
+ """Set gift card status as inactive"""
+ if gift_card.is_active:
+ gift_card.is_active = False
+ gift_card.save(update_fields=["is_active"])
+
+
+def activate_gift_card(gift_card: GiftCard):
+ """Set gift card status as active"""
+ if not gift_card.is_active:
+ gift_card.is_active = True
+ gift_card.save(update_fields=["is_active"])
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index e2315d210d..42d579dcce 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -6,6 +6,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError
from ...account.models import User
from ...core.utils.promo_code import generate_promo_code, is_available_promo_code
from ...giftcard import models
+from ...giftcard.utils import activate_gift_card, deactivate_gift_card
from ..core.mutations import BaseMutation, ModelMutation
from ..core.scalars import Decimal
from .types import GiftCard
@@ -93,9 +94,7 @@ class GiftCardDeactivate(BaseMutation):
gift_card = cls.get_node_or_error(
info, gift_card_id, field="gift_card_id", only_type=GiftCard
)
- if gift_card.is_active:
- gift_card.is_active = False
- gift_card.save(update_fields=["is_active"])
+ deactivate_gift_card(gift_card)
return GiftCardDeactivate(gift_card=gift_card)
@@ -115,7 +114,5 @@ class GiftCardActivate(BaseMutation):
gift_card = cls.get_node_or_error(
info, gift_card_id, field="gift_card_id", only_type=GiftCard
)
- if not gift_card.is_active:
- gift_card.is_active = True
- gift_card.save(update_fields=["is_active"])
- return GiftCardDeactivate(gift_card=gift_card)
+ activate_gift_card(gift_card)
+ return GiftCardActivate(gift_card=gift_card)