Skip to content

Commit

Permalink
Remove corresponding draft order lines when variants in bulk are remo…
Browse files Browse the repository at this point in the history
…ving
  • Loading branch information
IKarbowiak committed Sep 11, 2020
1 parent df537d0 commit bbf0f52
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
18 changes: 18 additions & 0 deletions saleor/graphql/product/bulk_mutations/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db import transaction

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 update_product_minimal_variant_price_task
Expand Down Expand Up @@ -342,6 +343,23 @@ class Meta:
error_type_class = ProductError
error_type_field = "product_errors"

@classmethod
def perform_mutation(cls, _root, info, ids, **data):
instances = cls.get_nodes_or_error(ids, "id", ProductVariant)
# get draft order lines for variants
order_line_pks = list(
order_models.OrderLine.objects.filter(
variant__in=instances, order__status=OrderStatus.DRAFT
).values_list("pk", flat=True)
)

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

# delete order lines for deleted variants
order_models.OrderLine.objects.filter(pk__in=order_line_pks).delete()

return response


class ProductVariantStocksCreate(BaseMutation):
product_variant = graphene.Field(
Expand Down
96 changes: 89 additions & 7 deletions saleor/graphql/product/tests/test_bulk_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import graphene
import pytest
from prices import Money, TaxedMoney

from ....order import OrderStatus
from ....order.models import OrderLine
from ....product.models import (
Attribute,
AttributeValue,
Expand Down Expand Up @@ -296,16 +299,19 @@ def test_delete_product_types(
).exists()


PRODUCT_VARIANT_BULK_DELETE_MUTATION = """
mutation productVariantBulkDelete($ids: [ID]!) {
productVariantBulkDelete(ids: $ids) {
count
}
}
"""


def test_delete_product_variants(
staff_api_client, product_variant_list, permission_manage_products
):
query = """
mutation productVariantBulkDelete($ids: [ID]!) {
productVariantBulkDelete(ids: $ids) {
count
}
}
"""
query = PRODUCT_VARIANT_BULK_DELETE_MUTATION

variables = {
"ids": [
Expand All @@ -322,3 +328,79 @@ def test_delete_product_variants(
assert not ProductVariant.objects.filter(
id__in=[variant.id for variant in product_variant_list]
).exists()


def test_delete_product_variants_in_draft_orders(
staff_api_client,
product_variant_list,
permission_manage_products,
order_line,
order_list,
):
# given
query = PRODUCT_VARIANT_BULK_DELETE_MUTATION
variants = product_variant_list

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

second_variant_in_draft = variants[1]
net = second_variant_in_draft.get_price()
gross = Money(amount=net.amount, currency=net.currency)
second_draft_order = OrderLine.objects.create(
variant=second_variant_in_draft,
order=draft_order,
product_name=str(second_variant_in_draft.product),
variant_name=str(second_variant_in_draft),
product_sku=second_variant_in_draft.sku,
is_shipping_required=second_variant_in_draft.is_shipping_required(),
unit_price=TaxedMoney(net=net, gross=gross),
quantity=3,
)

variant = variants[0]
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

variant_count = ProductVariant.objects.count()

variables = {
"ids": [
graphene.Node.to_global_id("ProductVariant", variant.id)
for variant in ProductVariant.objects.all()
]
}

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

# then
content = get_graphql_content(response)

assert content["data"]["productVariantBulkDelete"]["count"] == variant_count
assert not ProductVariant.objects.filter(
id__in=[variant.id for variant in product_variant_list]
).exists()

with pytest.raises(order_line._meta.model.DoesNotExist):
order_line.refresh_from_db()

with pytest.raises(second_draft_order._meta.model.DoesNotExist):
second_draft_order.refresh_from_db()

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

0 comments on commit bbf0f52

Please sign in to comment.