Skip to content

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

Draft orders shouldn't create allocation for product withou tracking

fowczrek7bc5ff38cbca
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index f58763945a..31b038597f 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -264,17 +264,18 @@ class DraftOrderComplete(BaseMutation):
order.save()
for line in order:
- try:
- allocate_stock(line, country, line.quantity)
- except InsufficientStock as exc:
- raise ValidationError(
- {
- "lines": ValidationError(
- f"Insufficient product stock: {exc.item}",
- code=OrderErrorCode.INSUFFICIENT_STOCK,
- )
- }
- )
+ if line.variant.track_inventory:
+ try:
+ allocate_stock(line, country, line.quantity)
+ except InsufficientStock as exc:
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ f"Insufficient product stock: {exc.item}",
+ code=OrderErrorCode.INSUFFICIENT_STOCK,
+ )
+ }
+ )
order_created(order, user=info.context.user, from_draft=True)
return DraftOrderComplete(order=order)
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index ec01d24ecd..562664906d 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -800,19 +800,25 @@ def test_validate_draft_order_out_of_stock_variant(draft_order):
assert e.value.error_dict["lines"][0].message == msg
+DRAFT_ORDER_COMPLETE_MUTATION = """
+ mutation draftComplete($id: ID!) {
+ draftOrderComplete(id: $id) {
+ orderErrors {
+ field
+ code
+ }
+ order {
+ status
+ }
+ }
+ }
+"""
+
+
def test_draft_order_complete(
staff_api_client, permission_manage_orders, staff_user, draft_order,
):
order = draft_order
- query = """
- mutation draftComplete($id: ID!) {
- draftOrderComplete(id: $id) {
- order {
- status
- }
- }
- }
- """
# Ensure no events were created
assert not OrderEvent.objects.exists()
@@ -823,7 +829,7 @@ def test_draft_order_complete(
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders]
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
data = content["data"]["draftOrderComplete"]["order"]
@@ -840,23 +846,45 @@ def test_draft_order_complete(
assert draft_placed_event.parameters == {}
+def test_draft_order_complete_product_without_inventory_tracking(
+ staff_api_client,
+ permission_manage_orders,
+ staff_user,
+ draft_order_without_inventory_tracking,
+):
+ order = draft_order_without_inventory_tracking
+
+ # Ensure no events were created
+ assert not OrderEvent.objects.exists()
+
+ # Ensure no allocation were created
+ assert not Allocation.objects.filter(order_line__order=order).exists()
+
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ variables = {"id": order_id}
+ response = staff_api_client.post_graphql(
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderComplete"]["order"]
+
+ assert not content["data"]["draftOrderComplete"]["orderErrors"]
+
+ order.refresh_from_db()
+ assert data["status"] == order.status.upper()
+ draft_placed_event = OrderEvent.objects.get()
+
+ assert not Allocation.objects.filter(order_line__order=order).exists()
+
+ assert draft_placed_event.user == staff_user
+ assert draft_placed_event.type == order_events.OrderEvents.PLACED_FROM_DRAFT
+ assert draft_placed_event.parameters == {}
+
+
def test_draft_order_complete_out_of_stock_variant(
staff_api_client, permission_manage_orders, staff_user, draft_order
):
order = draft_order
- query = """
- mutation draftComplete($id: ID!) {
- draftOrderComplete(id: $id) {
- orderErrors {
- field
- code
- }
- order {
- status
- }
- }
- }
- """
# Ensure no events were created
assert not OrderEvent.objects.exists()
@@ -869,7 +897,7 @@ def test_draft_order_complete_out_of_stock_variant(
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders]
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
error = content["data"]["draftOrderComplete"]["orderErrors"][0]
@@ -887,19 +915,10 @@ def test_draft_order_complete_existing_user_email_updates_user_field(
order.user_email = customer_user.email
order.user = None
order.save()
- query = """
- mutation draftComplete($id: ID!) {
- draftOrderComplete(id: $id) {
- order {
- status
- }
- }
- }
- """
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders]
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
assert "errors" not in content
@@ -914,19 +933,10 @@ def test_draft_order_complete_anonymous_user_email_sets_user_field_null(
order.user_email = "[email protected]"
order.user = None
order.save()
- query = """
- mutation draftComplete($id: ID!) {
- draftOrderComplete(id: $id) {
- order {
- status
- }
- }
- }
- """
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders]
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
assert "errors" not in content
@@ -941,23 +951,10 @@ def test_draft_order_complete_anonymous_user_no_email(
order.user_email = ""
order.user = None
order.save()
- query = """
- mutation draftComplete($id: ID!) {
- draftOrderComplete(id: $id) {
- order {
- status
- }
- errors {
- field
- message
- }
- }
- }
- """
order_id = graphene.Node.to_global_id("Order", order.id)
variables = {"id": order_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_orders]
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
data = content["data"]["draftOrderComplete"]["order"]
diff --git a/tests/conftest.py b/tests/conftest.py
index 62547c5861..21486cdba4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -775,6 +775,25 @@ def product_with_default_variant(product_type_without_variant, category, warehou
return product
+def variant_without_inventory_tracking(
+ product_type_without_variant, category, warehouse
+):
+ product = Product.objects.create(
+ name="Test product without inventory tracking",
+ slug="test-product-without-tracking",
+ price=Money(10, "USD"),
+ product_type=product_type_without_variant,
+ category=category,
+ is_published=True,
+ )
+ variant = ProductVariant.objects.create(
+ product=product, sku="tracking123", track_inventory=False
+ )
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)
+ return variant
+
+
@pytest.fixture
def variant(product) -> ProductVariant:
product_variant = ProductVariant.objects.create(
@@ -1198,6 +1217,30 @@ def order_with_lines(order, product_type, category, shipping_zone, warehouse):
return order
+def order_with_line_without_inventory_tracking(
+ order, variant_without_inventory_tracking
+):
+ variant = variant_without_inventory_tracking
+ net = variant.get_price()
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ quantity=3,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+
+ recalculate_order(order)
+
+ order.refresh_from_db()
+ return order
+
+
@pytest.fixture
def order_events(order):
for event_type, _ in OrderEvents.CHOICES:
@@ -1257,6 +1300,13 @@ def draft_order(order_with_lines):
return order_with_lines
+def draft_order_without_inventory_tracking(order_with_line_without_inventory_tracking):
+ order_with_line_without_inventory_tracking.status = OrderStatus.DRAFT
+ order_with_line_without_inventory_tracking.save(update_fields=["status"])
+ return order_with_line_without_inventory_tracking
+
+
@pytest.fixture
def payment_txn_preauth(order_with_lines, payment_dummy):
order = order_with_lines