Skip to content

Commit

Permalink
Remove corresponding draft order lines when variant is removing
Browse files Browse the repository at this point in the history
  • Loading branch information
IKarbowiak committed Sep 11, 2020
1 parent 8252623 commit 03c0339
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
20 changes: 20 additions & 0 deletions saleor/graphql/product/mutations/products.py
Expand Up @@ -12,6 +12,7 @@

from ....core.exceptions import PermissionDenied
from ....core.permissions import ProductPermissions
from ....order import OrderStatus, models as order_models
from ....product import models
from ....product.error_codes import ProductErrorCode
from ....product.tasks import (
Expand Down Expand Up @@ -1372,6 +1373,25 @@ def success_response(cls, instance):
update_product_minimal_variant_price_task.delay(instance.product_id)
return super().success_response(instance)

@classmethod
def perform_mutation(cls, _root, info, **data):
node_id = data.get("id")
instance = cls.get_node_or_error(info, node_id, only_type=ProductVariant)

# get draft order lines for variant
line_pks = list(
order_models.OrderLine.objects.filter(
variant=instance, order__status=OrderStatus.DRAFT
).values_list("pk", flat=True)
)

response = super().perform_mutation(_root, info, **data)

# delete order lines for deleted variant
order_models.OrderLine.objects.filter(pk__in=line_pks).delete()

return response


class ProductVariantUpdateMeta(UpdateMetaBaseMutation):
class Meta:
Expand Down
67 changes: 57 additions & 10 deletions saleor/graphql/product/tests/test_variant.py
Expand Up @@ -4,8 +4,11 @@
import graphene
import pytest
from measurement.measures import Weight
from prices import Money, TaxedMoney

from ....core.weight import WeightUnits
from ....order import OrderStatus
from ....order.models import OrderLine
from ....product.error_codes import ProductErrorCode
from ....product.models import Product, ProductVariant
from ....product.utils.attributes import associate_attribute_values_to_instance
Expand Down Expand Up @@ -999,17 +1002,20 @@ def test_update_product_variant_with_price_does_not_raise_price_validation_error
)


def test_delete_variant(staff_api_client, product, permission_manage_products):
query = """
mutation variantDelete($id: ID!) {
productVariantDelete(id: $id) {
productVariant {
sku
id
}
}
DELETE_VARIANT_MUTATION = """
mutation variantDelete($id: ID!) {
productVariantDelete(id: $id) {
productVariant {
sku
id
}
"""
}
}
"""


def test_delete_variant(staff_api_client, product, permission_manage_products):
query = DELETE_VARIANT_MUTATION
variant = product.variants.first()
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variables = {"id": variant_id}
Expand All @@ -1023,6 +1029,47 @@ def test_delete_variant(staff_api_client, product, permission_manage_products):
variant.refresh_from_db()


def test_delete_variant_in_draft_order(
staff_api_client, order_line, permission_manage_products, order_list
):
query = DELETE_VARIANT_MUTATION

draft_order = order_line.order
draft_order.status = OrderStatus.DRAFT
draft_order.save(update_fields=["status"])

variant = order_line.variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variables = {"id": variant_id}

net = variant.get_price()
gross = Money(amount=net.amount, currency=net.currency)
order_not_draft = order_list[-1]
order_line_not_in_draft = OrderLine.objects.create(
variant=variant,
order=order_not_draft,
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
quantity=3,
)
order_line_not_in_draft_pk = order_line_not_in_draft.pk

response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
)

content = get_graphql_content(response)
data = content["data"]["productVariantDelete"]
assert data["productVariant"]["sku"] == variant.sku
with pytest.raises(order_line._meta.model.DoesNotExist):
order_line.refresh_from_db()

assert OrderLine.objects.filter(pk=order_line_not_in_draft_pk).exists()


def _fetch_all_variants(client, permissions=None):
query = """
query fetchAllVariants {
Expand Down

0 comments on commit 03c0339

Please sign in to comment.