Skip to content

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

Gift card add note mutation

Tomasz Szymanskic5f60bd46f72
diff --git a/saleor/giftcard/__init__.py b/saleor/giftcard/__init__.py
index 3762de68c4..3460def1f1 100644
--- a/saleor/giftcard/__init__.py
+++ b/saleor/giftcard/__init__.py
@@ -24,6 +24,7 @@ class GiftCardEvents:
EXPIRY_SETTINGS_UPDATED = "expiry_settings_updated"
SENT_TO_CUSTOMER = "sent_to_customer"
RESENT = "resent"
+ NOTE_ADDED = "note_added"
CHOICES = [
(ISSUED, "The gift card was created be staff user or app."),
@@ -35,4 +36,5 @@ class GiftCardEvents:
(EXPIRY_SETTINGS_UPDATED, "The gift card expiry settings 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 cbcf71fcc1..98e1f54b78 100644
--- a/saleor/giftcard/events.py
+++ b/saleor/giftcard/events.py
@@ -173,3 +173,15 @@ def gift_cards_deactivated(
for gift_card_id in gift_card_ids
]
return GiftCardEvent.objects.bulk_create(events)
+
+
+def gift_card_note_added(
+ gift_card: GiftCard, user: UserType, app: AppType, message: str
+) -> GiftCardEvent:
+ return GiftCardEvent.objects.create(
+ gift_card=gift_card,
+ user=user,
+ app=app,
+ type=GiftCardEvents.NOTE_ADDED,
+ parameters={"message": message},
+ )
diff --git a/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py b/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py
new file mode 100644
index 0000000000..8250357131
--- /dev/null
+++ b/saleor/giftcard/migrations/0008_alter_giftcardevent_type.py
@@ -0,0 +1,35 @@
+# Generated by Django 3.2.5 on 2021-08-07 16:33
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("giftcard", "0007_auto_20210719_1311"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="giftcardevent",
+ name="type",
+ field=models.CharField(
+ choices=[
+ ("issued", "The gift card was created be staff user or app."),
+ ("bought", "The gift card was bought by customer."),
+ ("updated", "The gift card was updated."),
+ ("activated", "The gift card was activated."),
+ ("deactivated", "The gift card was deactivated."),
+ ("balance_reset", "The gift card balance was reset."),
+ (
+ "expiry_settings_updated",
+ "The gift card expiry settings 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."),
+ ],
+ max_length=255,
+ ),
+ ),
+ ]
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index ddfd4b13bd..8a0c963a2d 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -15,9 +15,10 @@ from ..core.descriptions import ADDED_IN_31, DEPRECATED_IN_3X_INPUT
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import PositiveDecimal
from ..core.types.common import GiftCardError, PriceInput, TimePeriodInputType
+from ..core.utils import validate_required_string_field
from ..core.validators import validate_price_precision
from .enums import GiftCardExpiryTypeEnum
-from .types import GiftCard
+from .types import GiftCard, GiftCardEvent
class GiftCardExpirySettingsInput(graphene.InputObjectType):
@@ -423,3 +424,56 @@ class GiftCardResend(BaseMutation):
email=target_email,
)
return GiftCardResend(gift_card=gift_card)
+
+
+class GiftCardAddNoteInput(graphene.InputObjectType):
+ message = graphene.String(
+ description="Note message.", name="message", required=True
+ )
+
+
+class GiftCardAddNote(BaseMutation):
+ gift_card = graphene.Field(GiftCard, description="Gift card with the note added.")
+ event = graphene.Field(GiftCardEvent, description="Gift card note created.")
+
+ class Arguments:
+ id = graphene.ID(
+ required=True, description="ID of the gift card to add a note for."
+ )
+ input = GiftCardAddNoteInput(
+ required=True,
+ description="Fields required to create a note for the gift card.",
+ )
+
+ class Meta:
+ description = "Adds note to the gift card."
+ permissions = (GiftcardPermissions.MANAGE_GIFT_CARD,)
+ error_type_class = GiftCardError
+ error_type_field = "gift_card_errors"
+
+ @classmethod
+ def clean_input(cls, _info, _instance, data):
+ try:
+ cleaned_input = validate_required_string_field(data["input"], "message")
+ except ValidationError:
+ raise ValidationError(
+ {
+ "message": ValidationError(
+ "Message can't be empty.",
+ code=GiftCardErrorCode.REQUIRED,
+ )
+ }
+ )
+ return cleaned_input
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ gift_card = cls.get_node_or_error(info, data.get("id"), only_type=GiftCard)
+ cleaned_input = cls.clean_input(info, gift_card, data)
+ event = events.gift_card_note_added(
+ gift_card=gift_card,
+ user=info.context.user,
+ app=info.context.app,
+ message=cleaned_input["message"],
+ )
+ return GiftCardAddNote(gift_card=gift_card, event=event)
diff --git a/saleor/graphql/giftcard/schema.py b/saleor/graphql/giftcard/schema.py
index 78af8f80c1..0c2786cd3c 100644
--- a/saleor/graphql/giftcard/schema.py
+++ b/saleor/graphql/giftcard/schema.py
@@ -13,6 +13,7 @@ from .bulk_mutations import (
from .filters import GiftCardFilterInput
from .mutations import (
GiftCardActivate,
+ GiftCardAddNote,
GiftCardCreate,
GiftCardDeactivate,
GiftCardDelete,
@@ -56,6 +57,7 @@ class GiftCardMutations(graphene.ObjectType):
gift_card_deactivate = GiftCardDeactivate.Field()
gift_card_update = GiftCardUpdate.Field()
gift_card_resend = GiftCardResend.Field()
+ gift_card_add_note = GiftCardAddNote.Field()
gift_card_bulk_delete = GiftCardBulkDelete.Field()
gift_card_bulk_activate = GiftCardBulkActivate.Field()
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_add_note.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_add_note.py
new file mode 100644
index 0000000000..30c2de7de8
--- /dev/null
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_add_note.py
@@ -0,0 +1,75 @@
+import graphene
+import pytest
+
+from .....giftcard import GiftCardEvents
+from .....giftcard.error_codes import GiftCardErrorCode
+from ....tests.utils import get_graphql_content
+
+GIFT_CARD_ADD_NOTE_MUTATION = """
+ mutation addNote($id: ID!, $message: String!) {
+ giftCardAddNote(id: $id, input: {message: $message}) {
+ errors {
+ field
+ message
+ code
+ }
+ giftCard {
+ id
+ }
+ event {
+ user {
+ email
+ }
+ message
+ }
+ }
+ }
+"""
+
+
+def test_gift_card_add_note_as_staff_user(
+ staff_api_client, permission_manage_gift_card, gift_card, staff_user
+):
+ assert not gift_card.events.all()
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.id)
+ message = "nuclear note"
+ variables = {"id": gift_card_id, "message": message}
+ response = staff_api_client.post_graphql(
+ GIFT_CARD_ADD_NOTE_MUTATION,
+ variables,
+ permissions=[permission_manage_gift_card],
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardAddNote"]
+
+ assert data["giftCard"]["id"] == gift_card_id
+ assert data["event"]["user"]["email"] == staff_user.email
+ assert data["event"]["message"] == message
+
+ event = gift_card.events.get()
+ assert event.type == GiftCardEvents.NOTE_ADDED
+ assert event.user == staff_user
+ assert event.parameters == {"message": message}
+
+
+ "message",
+ (
+ "",
+ " ",
+ ),
+)
+def test_gift_card_add_note_fail_on_empty_message(
+ message, staff_api_client, permission_manage_gift_card, gift_card
+):
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.id)
+ variables = {"id": gift_card_id, "message": message}
+ response = staff_api_client.post_graphql(
+ GIFT_CARD_ADD_NOTE_MUTATION,
+ variables,
+ permissions=[permission_manage_gift_card],
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardAddNote"]
+ assert data["errors"][0]["field"] == "message"
+ assert data["errors"][0]["code"] == GiftCardErrorCode.REQUIRED.name
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index af4eab395d..6d2bba3e21 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2259,6 +2259,17 @@ type GiftCardActivate {
errors: [GiftCardError!]!
}
+type GiftCardAddNote {
+ giftCard: GiftCard
+ event: GiftCardEvent
+ giftCardErrors: [GiftCardError!]! @deprecated(reason: "Use errors field instead. This field will be removed in Saleor 4.0.")
+ errors: [GiftCardError!]!
+}
+
+input GiftCardAddNoteInput {
+ message: String!
+}
+
type GiftCardBulkActivate {
count: Int!
errors: [GiftCardError!]!
@@ -2371,6 +2382,7 @@ enum GiftCardEventsEnum {
EXPIRY_SETTINGS_UPDATED
SENT_TO_CUSTOMER
RESENT
+ NOTE_ADDED
}
input GiftCardExpirySettingsInput {
@@ -3819,6 +3831,7 @@ type Mutation {
giftCardDeactivate(id: ID!): GiftCardDeactivate
giftCardUpdate(id: ID!, input: GiftCardUpdateInput!): GiftCardUpdate
giftCardResend(input: GiftCardResendInput!): GiftCardResend
+ giftCardAddNote(id: ID!, input: GiftCardAddNoteInput!): GiftCardAddNote
giftCardBulkDelete(ids: [ID]!): GiftCardBulkDelete
giftCardBulkActivate(ids: [ID]!): GiftCardBulkActivate
giftCardBulkDeactivate(ids: [ID]!): GiftCardBulkDeactivate