Skip to content

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

Add Buyer field to gift card

fowczarek0656ab1f84e9
diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.py
index f4d42b73f3..4b8bfdaf14 100644
--- a/saleor/giftcard/models.py
+++ b/saleor/giftcard/models.py
@@ -8,13 +8,16 @@ from django_prices.models import MoneyField
class GiftCard(models.Model):
code = models.CharField(max_length=16, unique=True, db_index=True)
- creator = models.ForeignKey(
+ buyer = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name="gift_cards",
)
+ creator = models.ForeignKey(
+ settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL
+ )
created = models.DateTimeField(auto_now_add=True)
start_date = models.DateField(default=date.today)
expiration_date = models.DateField(null=True, blank=True)
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index 7b7448aa65..0bb7f817e8 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -1,8 +1,9 @@
from datetime import date
import graphene
-from django.core.exceptions import ValidationError
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from ...account.models import User
from ...giftcard import models
from ...giftcard.utils import generate_gift_card_code
from ..core.mutations import BaseMutation, ModelMutation
@@ -19,6 +20,9 @@ class GiftCardInput(graphene.InputObjectType):
description="End date of the gift card in ISO 8601 format."
)
balance = Decimal(description="Value of the gift card.")
+ buyer_email = graphene.String(
+ required=False, description="The customer's email of the gift card buyer."
+ )
class GiftCardCreate(ModelMutation):
@@ -42,6 +46,14 @@ class GiftCardCreate(ModelMutation):
if balance:
cleaned_input["current_balance"] = balance
cleaned_input["initial_balance"] = balance
+ buyer_email = data.get("buyer_email", None)
+ if buyer_email:
+ try:
+ cleaned_input["buyer"] = User.objects.get(email=buyer_email)
+ except ObjectDoesNotExist:
+ raise ValidationError(
+ {"email": "Customer with this email doesn't exist."}
+ )
return cleaned_input
@classmethod
@@ -79,9 +91,10 @@ class GiftCardDeactivate(BaseMutation):
permissions = ("giftcard.manage_gift_card",)
@classmethod
- def perform_mutation(cls, _root, info, id):
+ def perform_mutation(cls, _root, info, **data):
+ gift_card_id = data.get("id")
gift_card = cls.get_node_or_error(
- info, id, field="gift_card_id", only_type=GiftCard
+ info, gift_card_id, field="gift_card_id", only_type=GiftCard
)
gift_card.is_active = False
gift_card.last_used_on = date.today()
diff --git a/saleor/graphql/giftcard/resolvers.py b/saleor/graphql/giftcard/resolvers.py
index 39312f497b..d787e75346 100644
--- a/saleor/graphql/giftcard/resolvers.py
+++ b/saleor/graphql/giftcard/resolvers.py
@@ -7,6 +7,6 @@ def resolve_gift_card(info, gift_card_id):
"""Return gift card only for user assigned to it or proper staff user."""
user = info.context.user
gift_card = graphene.Node.get_node_from_global_id(info, gift_card_id, GiftCard)
- if user.has_perm("giftcard.manage_gift_card") or gift_card.creator == user:
+ if user.has_perm("giftcard.manage_gift_card") or gift_card.buyer == user:
return gift_card
return None
diff --git a/saleor/graphql/giftcard/types.py b/saleor/graphql/giftcard/types.py
index f7f4067013..a99e08f90d 100644
--- a/saleor/graphql/giftcard/types.py
+++ b/saleor/graphql/giftcard/types.py
@@ -16,6 +16,7 @@ class GiftCard(CountableDjangoObjectType):
)
only_fields = [
"code",
+ "buyer",
"creator",
"created",
"start_date",
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index 0fa9fd45db..dc946b5a00 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -6,11 +6,14 @@ from tests.api.utils import get_graphql_content
from .utils import assert_no_permission
-def test_query_own_gift_card(user_api_client, gift_card):
+def test_query_own_gift_card(user_api_client, staff_user, gift_card):
query = """
query giftCard($id: ID!) {
giftCard(id: $id){
code
+ buyer {
+ email
+ }
creator {
email
}
@@ -34,7 +37,8 @@ def test_query_own_gift_card(user_api_client, gift_card):
content = get_graphql_content(response)
data = content["data"]["giftCard"]
assert data["code"] == gift_card.code
- assert data["creator"]["email"] == gift_card.creator.email
+ assert data["buyer"]["email"] == gift_card.buyer.email
+ assert data["creator"]["email"] == staff_user.email
assert data["created"] == gift_card.created.isoformat()
assert data["startDate"] == gift_card.start_date.isoformat()
assert data["expirationDate"] == gift_card.expiration_date
@@ -51,7 +55,7 @@ def test_query_gift_card_with_premissions(
query giftCard($id: ID!) {
giftCard(id: $id){
code
- creator {
+ buyer {
email
}
}
@@ -64,7 +68,7 @@ def test_query_gift_card_with_premissions(
content = get_graphql_content(response)
data = content["data"]["giftCard"]
assert data["code"] == gift_card.code
- assert data["creator"]["email"] == gift_card.creator.email
+ assert data["buyer"]["email"] == gift_card.buyer.email
def test_query_gift_card_without_premissions(
@@ -132,17 +136,20 @@ def test_query_own_gift_cards(user_api_client, gift_card, gift_card_created_by_s
CREATE_GIFT_CARD_MUTATION = """
mutation giftCardCreate(
$code: String, $startDate: Date, $expirationDate: Date,
- $balance: Decimal) {
+ $balance: Decimal, $buyerEmail: String!) {
giftCardCreate(input: {
code: $code, startDate: $startDate,
expirationDate: $expirationDate,
- balance: $balance}) {
+ balance: $balance, buyerEmail: $buyerEmail }) {
errors {
field
message
}
giftCard {
code
+ buyer {
+ email
+ }
creator {
email
}
@@ -163,7 +170,7 @@ mutation giftCardCreate(
"""
-def test_create_gift_card(staff_api_client, permission_manage_gift_card):
+def test_create_gift_card(staff_api_client, customer_user, permission_manage_gift_card):
code = "mirumee"
start_date = date(day=1, month=1, year=2018)
expiration_date = date(day=1, month=1, year=2019)
@@ -173,6 +180,7 @@ def test_create_gift_card(staff_api_client, permission_manage_gift_card):
"startDate": start_date.isoformat(),
"expirationDate": expiration_date.isoformat(),
"balance": initial_balance,
+ "buyerEmail": customer_user.email,
}
response = staff_api_client.post_graphql(
CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
@@ -180,6 +188,7 @@ def test_create_gift_card(staff_api_client, permission_manage_gift_card):
content = get_graphql_content(response)
data = content["data"]["giftCardCreate"]["giftCard"]
assert data["code"] == code
+ assert data["buyer"]["email"] == customer_user.email
assert data["creator"]["email"] == staff_api_client.user.email
assert data["startDate"] == start_date.isoformat()
assert data["expirationDate"] == expiration_date.isoformat()
@@ -200,6 +209,7 @@ def test_create_gift_card_with_empty_code(
"startDate": start_date.isoformat(),
"expirationDate": expiration_date.isoformat(),
"balance": initial_balance,
+ "buyerEmail": staff_api_client.user.email,
}
response = staff_api_client.post_graphql(
CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
@@ -209,6 +219,52 @@ def test_create_gift_card_with_empty_code(
assert data["code"] != ""
+def test_create_gift_card_without_buyer(staff_api_client, permission_manage_gift_card):
+ code = "mirumee1"
+ start_date = date(day=1, month=1, year=2018)
+ expiration_date = date(day=1, month=1, year=2019)
+ initial_balance = 123
+ variables = {
+ "code": code,
+ "startDate": start_date.isoformat(),
+ "expirationDate": expiration_date.isoformat(),
+ "balance": initial_balance,
+ "buyerEmail": "",
+ }
+ response = staff_api_client.post_graphql(
+ CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardCreate"]["giftCard"]
+ assert data["code"] == code
+ assert not data["buyer"]
+
+
+def test_create_gift_card_with_incorrect_buyer_email(
+ staff_api_client, permission_manage_gift_card
+):
+ code = "mirumee1"
+ start_date = date(day=1, month=1, year=2018)
+ expiration_date = date(day=1, month=1, year=2019)
+ initial_balance = 123
+ variables = {
+ "code": code,
+ "startDate": start_date.isoformat(),
+ "expirationDate": expiration_date.isoformat(),
+ "balance": initial_balance,
+ "buyerEmail": "[email protected]",
+ }
+ response = staff_api_client.post_graphql(
+ CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
+ )
+ content = get_graphql_content(response)
+ assert content["data"]["giftCardCreate"]["errors"]
+ errors = content["data"]["giftCardCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "email"
+ assert errors[0]["message"] == "Customer with this email doesn't exist."
+
+
def test_create_gift_card_without_premissions(staff_api_client):
code = "mirumee"
start_date = date(day=1, month=1, year=2018)
@@ -219,6 +275,7 @@ def test_create_gift_card_without_premissions(staff_api_client):
"startDate": start_date.isoformat(),
"expirationDate": expiration_date.isoformat(),
"balance": initial_balance,
+ "buyerEmail": staff_api_client.user.email,
}
response = staff_api_client.post_graphql(CREATE_GIFT_CARD_MUTATION, variables)
assert_no_permission(response)
@@ -227,11 +284,11 @@ def test_create_gift_card_without_premissions(staff_api_client):
UPDATE_GIFT_CARD_MUTATION = """
mutation giftCardUpdate(
$id: ID!, $code: String, $startDate: Date, $expirationDate: Date,
- $balance: Decimal) {
+ $balance: Decimal, $buyerEmail: String!) {
giftCardUpdate(id: $id, input: {
code: $code, startDate: $startDate,
expirationDate: $expirationDate,
- balance: $balance}) {
+ balance: $balance, buyerEmail: $buyerEmail}) {
errors {
field
message
@@ -242,6 +299,9 @@ mutation giftCardUpdate(
currentBalance {
amount
}
+ buyer {
+ email
+ }
}
}
}
@@ -256,10 +316,12 @@ def test_update_gift_card(staff_api_client, gift_card, permission_manage_gift_ca
assert gift_card.code != new_code
assert gift_card.current_balance != balance
assert gift_card.last_used_on != date.today()
+ assert gift_card.buyer != staff_api_client.user
variables = {
"id": graphene.Node.to_global_id("GiftCard", gift_card.id),
"code": new_code,
"balance": balance,
+ "buyerEmail": staff_api_client.user.email,
}
response = staff_api_client.post_graphql(
@@ -270,6 +332,7 @@ def test_update_gift_card(staff_api_client, gift_card, permission_manage_gift_ca
assert data["code"] == new_code
assert data["currentBalance"]["amount"] == balance
assert data["lastUsedOn"] == date.today().isoformat()
+ assert data["buyer"]["email"] == staff_api_client.user.email
def test_update_gift_card_without_premissions(staff_api_client, gift_card):
@@ -280,6 +343,7 @@ def test_update_gift_card_without_premissions(staff_api_client, gift_card):
variables = {
"id": graphene.Node.to_global_id("GiftCard", gift_card.id),
"balance": balance,
+ "buyerEmail": staff_api_client.user.email,
}
response = staff_api_client.post_graphql(UPDATE_GIFT_CARD_MUTATION, variables)
diff --git a/tests/conftest.py b/tests/conftest.py
index d2c67285ed..2b7d0ff194 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -617,10 +617,10 @@ def order_line(order, variant, vatlayer):
@pytest.fixture
-def gift_card(customer_user):
+def gift_card(customer_user, staff_user):
return GiftCard.objects.create(
- code='mirumee', creator=customer_user, initial_balance=10,
- current_balance=10)
+ code='mirumee', buyer=customer_user, initial_balance=10,
+ current_balance=10, creator=staff_user)
@pytest.fixture