Skip to content

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

Add TAG_UPDATED event

IKarbowiak884327b653cc
diff --git a/saleor/giftcard/__init__.py b/saleor/giftcard/__init__.py
index c94e4548b0..9ac37d7361 100644
--- a/saleor/giftcard/__init__.py
+++ b/saleor/giftcard/__init__.py
@@ -8,6 +8,7 @@ class GiftCardEvents:
DEACTIVATED = "deactivated"
BALANCE_RESET = "balance_reset"
EXPIRY_DATE_UPDATED = "expiry_date_updated"
+ TAG_UPDATED = "tag_updated"
SENT_TO_CUSTOMER = "sent_to_customer"
RESENT = "resent"
NOTE_ADDED = "note_added"
@@ -21,6 +22,7 @@ class GiftCardEvents:
(DEACTIVATED, "The gift card was deactivated."),
(BALANCE_RESET, "The gift card balance was reset."),
(EXPIRY_DATE_UPDATED, "The gift card expiry date was updated."),
+ (TAG_UPDATED, "The gift card tag was updated."),
(SENT_TO_CUSTOMER, "The gift card was sent to the customer."),
(RESENT, "The gift card was resent to the customer."),
(NOTE_ADDED, "A note was added to the gift card."),
diff --git a/saleor/giftcard/events.py b/saleor/giftcard/events.py
index 9f7a46660d..dfff1dfe7b 100644
--- a/saleor/giftcard/events.py
+++ b/saleor/giftcard/events.py
@@ -100,6 +100,26 @@ def gift_card_expiry_date_updated_event(
)
+def gift_card_tag_updated_event(
+ gift_card: GiftCard,
+ old_gift_card: GiftCard,
+ user: UserType,
+ app: AppType,
+):
+ if not user_is_valid(user):
+ user = None
+ return GiftCardEvent.objects.create(
+ gift_card=gift_card,
+ user=user,
+ app=app,
+ type=GiftCardEvents.TAG_UPDATED,
+ parameters={
+ "tag": gift_card.tag,
+ "old_tag": old_gift_card.tag,
+ },
+ )
+
+
def gift_card_activated_event(
gift_card: GiftCard,
user: UserType,
diff --git a/saleor/giftcard/migrations/0010_auto_20210818_0633.py b/saleor/giftcard/migrations/0010_auto_20210818_0633.py
index 60172068a4..8629023d25 100644
--- a/saleor/giftcard/migrations/0010_auto_20210818_0633.py
+++ b/saleor/giftcard/migrations/0010_auto_20210818_0633.py
@@ -34,6 +34,7 @@ class Migration(migrations.Migration):
("deactivated", "The gift card was deactivated."),
("balance_reset", "The gift card balance was reset."),
("expiry_date_updated", "The gift card expiry date was updated."),
+ ("tag_updated", "The gift card tag was updated."),
("sent_to_customer", "The gift card was sent to the customer."),
("resent", "The gift card was resent to the customer."),
("note_added", "A note was added to the gift card."),
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index c986304328..61b32a088c 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -250,6 +250,10 @@ class GiftCardUpdate(GiftCardCreate):
events.gift_card_expiry_date_updated_event(
instance, old_instance, info.context.user, info.context.app
)
+ if "tag" in cleaned_input:
+ events.gift_card_tag_updated_event(
+ instance, old_instance, info.context.user, info.context.app
+ )
return cls.success_response(instance)
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py
index 94c3bd226a..ce52944213 100644
--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py
@@ -50,6 +50,8 @@ UPDATE_GIFT_CARD_MUTATION = """
app {
name
}
+ tag
+ oldTag
balance {
initialBalance {
amount
@@ -92,6 +94,7 @@ def test_update_gift_card(
# given
old_initial_balance = float(gift_card.initial_balance.amount)
old_current_balance = float(gift_card.current_balance.amount)
+ old_tag = gift_card.tag
initial_balance = 100.0
currency = gift_card.currency
@@ -136,7 +139,7 @@ def test_update_gift_card(
assert data["initialBalance"]["amount"] == initial_balance
assert data["currentBalance"]["amount"] == initial_balance
- assert len(data["events"]) == 2
+ assert len(data["events"]) == 3
events = [
{
"type": GiftCardEvents.BALANCE_RESET.upper(),
@@ -164,6 +167,8 @@ def test_update_gift_card(
},
"expiryDate": None,
"oldExpiryDate": None,
+ "tag": None,
+ "oldTag": None,
},
{
"type": GiftCardEvents.EXPIRY_DATE_UPDATED.upper(),
@@ -174,6 +179,20 @@ def test_update_gift_card(
"balance": None,
"expiryDate": date_value.isoformat(),
"oldExpiryDate": None,
+ "tag": None,
+ "oldTag": None,
+ },
+ {
+ "type": GiftCardEvents.TAG_UPDATED.upper(),
+ "user": {
+ "email": staff_api_client.user.email,
+ },
+ "app": None,
+ "balance": None,
+ "expiryDate": None,
+ "oldExpiryDate": None,
+ "tag": tag,
+ "oldTag": old_tag,
},
]
for event in data["events"]:
@@ -190,6 +209,7 @@ def test_update_gift_card_by_app(
# given
old_initial_balance = float(gift_card.initial_balance.amount)
old_current_balance = float(gift_card.current_balance.amount)
+ old_tag = gift_card.tag
initial_balance = 100.0
currency = gift_card.currency
@@ -234,7 +254,7 @@ def test_update_gift_card_by_app(
assert data["initialBalance"]["amount"] == initial_balance
assert data["currentBalance"]["amount"] == initial_balance
- assert len(data["events"]) == 2
+ assert len(data["events"]) == 3
events = [
{
"type": GiftCardEvents.BALANCE_RESET.upper(),
@@ -260,6 +280,8 @@ def test_update_gift_card_by_app(
},
"expiryDate": None,
"oldExpiryDate": None,
+ "tag": None,
+ "oldTag": None,
},
{
"type": GiftCardEvents.EXPIRY_DATE_UPDATED.upper(),
@@ -268,6 +290,18 @@ def test_update_gift_card_by_app(
"balance": None,
"expiryDate": date_value.isoformat(),
"oldExpiryDate": None,
+ "tag": None,
+ "oldTag": None,
+ },
+ {
+ "type": GiftCardEvents.TAG_UPDATED.upper(),
+ "user": None,
+ "app": {"name": app_api_client.app.name},
+ "balance": None,
+ "expiryDate": None,
+ "oldExpiryDate": None,
+ "tag": tag,
+ "oldTag": old_tag,
},
]
for event in data["events"]:
@@ -367,6 +401,8 @@ def test_update_gift_card_balance(
},
"expiryDate": None,
"oldExpiryDate": None,
+ "tag": None,
+ "oldTag": None,
}
assert expected_event == data["events"][0]
@@ -421,6 +457,8 @@ def test_update_gift_card_change_to_never_expire(
"app": None,
"balance": None,
"expiryDate": None,
+ "tag": None,
+ "oldTag": None,
"oldExpiryDate": old_expiry_date.isoformat(),
}
assert expected_event == data["events"][0]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 9b528352bc..fcd67413fd 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2373,6 +2373,7 @@ enum GiftCardEventsEnum {
DEACTIVATED
BALANCE_RESET
EXPIRY_DATE_UPDATED
+ TAG_UPDATED
SENT_TO_CUSTOMER
RESENT
NOTE_ADDED