Skip to content

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

Gift card bulk deactivate mutation

Tomasz Szymanskib992c609fcc9
diff --git a/saleor/giftcard/events.py b/saleor/giftcard/events.py
index da30503354..cbcf71fcc1 100644
--- a/saleor/giftcard/events.py
+++ b/saleor/giftcard/events.py
@@ -154,3 +154,22 @@ def gift_cards_activated(
for gift_card_id in gift_card_ids
]
return GiftCardEvent.objects.bulk_create(events)
+
+
+def gift_cards_deactivated(
+ gift_card_ids: Iterable[int],
+ user: UserType,
+ app: AppType,
+):
+ if not user_is_valid(user):
+ user = None
+ events = [
+ GiftCardEvent(
+ gift_card_id=gift_card_id,
+ user=user,
+ app=app,
+ type=GiftCardEvents.DEACTIVATED,
+ )
+ for gift_card_id in gift_card_ids
+ ]
+ return GiftCardEvent.objects.bulk_create(events)
diff --git a/saleor/graphql/giftcard/bulk_mutations.py b/saleor/graphql/giftcard/bulk_mutations.py
index 81fabacea4..1db4a0a7a5 100644
--- a/saleor/graphql/giftcard/bulk_mutations.py
+++ b/saleor/graphql/giftcard/bulk_mutations.py
@@ -41,3 +41,28 @@ class GiftCardBulkActivate(BaseBulkMutation):
events.gift_cards_activated(
gift_card_ids, user=info.context.user, app=info.context.app
)
+
+
+class GiftCardBulkDeactivate(BaseBulkMutation):
+ class Arguments:
+ ids = graphene.List(
+ graphene.ID,
+ required=True,
+ description="List of gift card IDs to deactivate.",
+ )
+
+ class Meta:
+ description = "Deactivate gift cards."
+ model = models.GiftCard
+ permissions = (GiftcardPermissions.MANAGE_GIFT_CARD,)
+ error_type_class = GiftCardError
+
+ @classmethod
+ @traced_atomic_transaction()
+ def bulk_action(cls, info, queryset):
+ queryset = queryset.filter(is_active=True)
+ gift_card_ids = [gift_card.id for gift_card in queryset]
+ queryset.update(is_active=True)
+ events.gift_cards_deactivated(
+ gift_card_ids, user=info.context.user, app=info.context.app
+ )
diff --git a/saleor/graphql/giftcard/schema.py b/saleor/graphql/giftcard/schema.py
index f7db8aa131..78af8f80c1 100644
--- a/saleor/graphql/giftcard/schema.py
+++ b/saleor/graphql/giftcard/schema.py
@@ -5,7 +5,11 @@ from ..core.descriptions import ADDED_IN_31
from ..core.fields import FilterInputConnectionField
from ..core.utils import from_global_id_or_error
from ..decorators import permission_required
-from .bulk_mutations import GiftCardBulkActivate, GiftCardBulkDelete
+from .bulk_mutations import (
+ GiftCardBulkActivate,
+ GiftCardBulkDeactivate,
+ GiftCardBulkDelete,
+)
from .filters import GiftCardFilterInput
from .mutations import (
GiftCardActivate,
@@ -55,3 +59,4 @@ class GiftCardMutations(graphene.ObjectType):
gift_card_bulk_delete = GiftCardBulkDelete.Field()
gift_card_bulk_activate = GiftCardBulkActivate.Field()
+ gift_card_bulk_deactivate = GiftCardBulkDeactivate.Field()
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_bulk_deactivate.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_bulk_deactivate.py
new file mode 100644
index 0000000000..8feb030bb0
--- /dev/null
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_bulk_deactivate.py
@@ -0,0 +1,134 @@
+import graphene
+
+from .....giftcard import GiftCardEvents
+from .....giftcard.models import GiftCard, GiftCardEvent
+from ....tests.utils import assert_no_permission, get_graphql_content
+
+MUTATION_GIFT_CARD_BULK_DEACTIVATE = """
+ mutation GiftCardBulkDeactivate($ids: [ID]!) {
+ giftCardBulkDeactivate(ids: $ids) {
+ count
+ errors {
+ code
+ field
+ }
+ }
+ }
+"""
+
+
+def test_gift_card_bulk_deactivate_by_staff(
+ staff_api_client,
+ gift_card,
+ gift_card_expiry_period,
+ gift_card_expiry_date,
+ permission_manage_gift_card,
+):
+ # given
+ gift_cards = [gift_card, gift_card_expiry_period, gift_card_expiry_date]
+
+ ids = [graphene.Node.to_global_id("GiftCard", card.pk) for card in gift_cards]
+ variables = {"ids": ids}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_GIFT_CARD_BULK_DEACTIVATE,
+ variables,
+ permissions=(permission_manage_gift_card,),
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardBulkDeactivate"]
+
+ assert data["count"] == len(ids)
+ events = GiftCardEvent.objects.all()
+ assert events.count() == 3
+ assert {event.gift_card_id for event in events} == {
+ gift_card.id,
+ gift_card_expiry_period.id,
+ gift_card_expiry_date.id,
+ }
+ assert {event.type for event in events} == {GiftCardEvents.DEACTIVATED}
+
+
+def test_gift_card_bulk_deactivate_by_app(
+ app_api_client,
+ gift_card,
+ gift_card_expiry_period,
+ gift_card_expiry_date,
+ permission_manage_gift_card,
+):
+ # given
+ gift_cards = [gift_card, gift_card_expiry_period, gift_card_expiry_date]
+
+ ids = [graphene.Node.to_global_id("GiftCard", card.pk) for card in gift_cards]
+ variables = {"ids": ids}
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_GIFT_CARD_BULK_DEACTIVATE,
+ variables,
+ permissions=(permission_manage_gift_card,),
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardBulkDeactivate"]
+
+ assert data["count"] == len(ids)
+ events = GiftCardEvent.objects.all()
+ assert events.count() == len(ids)
+ assert {event.gift_card_id for event in events} == {card.id for card in gift_cards}
+ assert {event.type for event in events} == {GiftCardEvents.DEACTIVATED}
+
+
+def test_gift_card_bulk_deactivate_all_cards_already_inactive(
+ staff_api_client,
+ gift_card,
+ gift_card_expiry_period,
+ gift_card_expiry_date,
+ permission_manage_gift_card,
+):
+ # given
+ gift_cards = [gift_card, gift_card_expiry_period, gift_card_expiry_date]
+ for card in gift_cards:
+ card.is_active = False
+ GiftCard.objects.bulk_update(gift_cards, ["is_active"])
+
+ ids = [graphene.Node.to_global_id("GiftCard", card.pk) for card in gift_cards]
+ variables = {"ids": ids}
+
+ # when
+ response = staff_api_client.post_graphql(
+ MUTATION_GIFT_CARD_BULK_DEACTIVATE,
+ variables,
+ permissions=(permission_manage_gift_card,),
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardBulkDeactivate"]
+
+ assert data["count"] == len(ids)
+ events = GiftCardEvent.objects.all()
+ assert events.count() == 0
+
+
+def test_gift_card_bulk_deactivate_by_customer(
+ api_client, gift_card, gift_card_expiry_period
+):
+ # given
+ gift_cards = [gift_card, gift_card_expiry_period]
+
+ ids = [graphene.Node.to_global_id("GiftCard", card.pk) for card in gift_cards]
+ variables = {"ids": ids}
+
+ # when
+ response = api_client.post_graphql(
+ MUTATION_GIFT_CARD_BULK_DEACTIVATE,
+ variables,
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 866a6bdda9..bed1c05686 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2264,6 +2264,11 @@ type GiftCardBulkActivate {
errors: [GiftCardError!]!
}
+type GiftCardBulkDeactivate {
+ count: Int!
+ errors: [GiftCardError!]!
+}
+
type GiftCardBulkDelete {
count: Int!
errors: [GiftCardError!]!
@@ -3817,6 +3822,7 @@ type Mutation {
giftCardResend(input: GiftCardResendInput!): GiftCardResend
giftCardBulkDelete(ids: [ID]!): GiftCardBulkDelete
giftCardBulkActivate(ids: [ID]!): GiftCardBulkActivate
+ giftCardBulkDeactivate(ids: [ID]!): GiftCardBulkDeactivate
pluginUpdate(channelId: ID, id: ID!, input: PluginUpdateInput!): PluginUpdate
saleCreate(input: SaleInput!): SaleCreate
saleDelete(id: ID!): SaleDelete