Skip to content

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

Allow deleting and updating order lines without allocation

Ania Warzecha524a91deece3
diff --git a/saleor/graphql/order/mutations/order_line_delete.py b/saleor/graphql/order/mutations/order_line_delete.py
index ba045a9197..f8e8804f98 100644
--- a/saleor/graphql/order/mutations/order_line_delete.py
+++ b/saleor/graphql/order/mutations/order_line_delete.py
@@ -53,9 +53,10 @@ class OrderLineDelete(EditableOrderValidationMixin, BaseMutation):
cls.validate(info, order, line)
db_id = line.id
+ line_allocation = line.allocations.first()
warehouse_pk = (
- line.allocations.first().stock.warehouse.pk
- if order.is_unconfirmed()
+ line_allocation.stock.warehouse.pk
+ if line_allocation and order.is_unconfirmed()
else None
)
with traced_atomic_transaction():
diff --git a/saleor/graphql/order/mutations/order_line_update.py b/saleor/graphql/order/mutations/order_line_update.py
index 572b561485..162c1add81 100644
--- a/saleor/graphql/order/mutations/order_line_update.py
+++ b/saleor/graphql/order/mutations/order_line_update.py
@@ -72,9 +72,11 @@ class OrderLineUpdate(
@classmethod
def save(cls, info: ResolveInfo, instance, cleaned_input):
manager = get_plugin_manager_promise(info.context).get()
+
+ line_allocation = instance.allocations.first()
warehouse_pk = (
- instance.allocations.first().stock.warehouse.pk
- if instance.order.is_unconfirmed()
+ line_allocation.stock.warehouse.pk
+ if line_allocation and instance.order.is_unconfirmed()
else None
)
app = get_app_promise(info.context).get()
diff --git a/saleor/graphql/order/tests/mutations/test_order_line_delete.py b/saleor/graphql/order/tests/mutations/test_order_line_delete.py
index d48d142ff7..395fefbb75 100644
--- a/saleor/graphql/order/tests/mutations/test_order_line_delete.py
+++ b/saleor/graphql/order/tests/mutations/test_order_line_delete.py
@@ -123,6 +123,40 @@ def test_order_line_remove(
)
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
+def test_order_line_remove_no_line_allocations(
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
+ order_with_lines,
+ permission_group_manage_orders,
+ staff_api_client,
+):
+ query = ORDER_LINE_DELETE_MUTATION
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ order.save(update_fields=["status"])
+ line = order.lines.first()
+ line.allocations.all().delete()
+ line_id = graphene.Node.to_global_id("OrderLine", line.id)
+ variables = {"id": line_id}
+
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderLineDelete"]
+ assert OrderEvent.objects.count() == 1
+ assert OrderEvent.objects.last().type == order_events.OrderEvents.REMOVED_PRODUCTS
+ assert data["orderLine"]["id"] == line_id
+ assert line not in order.lines.all()
+ assert_proper_webhook_called_once(
+ order,
+ order.status,
+ draft_order_updated_webhook_mock,
+ order_updated_webhook_mock,
+ )
+
+
def test_order_line_remove_by_usr_no_channel_access(
order_with_lines,
permission_group_all_perms_channel_USD_only,
diff --git a/saleor/graphql/order/tests/mutations/test_order_line_update.py b/saleor/graphql/order/tests/mutations/test_order_line_update.py
index 19ad0526f9..9cc72ae25d 100644
--- a/saleor/graphql/order/tests/mutations/test_order_line_update.py
+++ b/saleor/graphql/order/tests/mutations/test_order_line_update.py
@@ -245,6 +245,69 @@ def test_order_line_update(
assert data["errors"][0]["field"] == "quantity"
+@patch("saleor.plugins.manager.PluginsManager.draft_order_updated")
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
+def test_order_line_update_no_allocation(
+ order_updated_webhook_mock,
+ draft_order_updated_webhook_mock,
+ order_with_lines,
+ permission_group_manage_orders,
+ staff_api_client,
+ staff_user,
+):
+ query = ORDER_LINE_UPDATE_MUTATION
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ order.save(update_fields=["status"])
+ line = order.lines.first()
+ line.allocations.all().delete()
+ new_quantity = 1
+ removed_quantity = 2
+ line_id = graphene.Node.to_global_id("OrderLine", line.id)
+ variables = {"lineId": line_id, "quantity": new_quantity}
+
+ # Ensure the line has the expected quantity
+ assert line.quantity == 3
+
+ # No event should exist yet
+ assert not OrderEvent.objects.exists()
+
+ # mutation should fail without proper permissions
+ response = staff_api_client.post_graphql(query, variables)
+ assert_no_permission(response)
+ order_updated_webhook_mock.assert_not_called()
+ draft_order_updated_webhook_mock.assert_not_called()
+
+ # assign permissions
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderLineUpdate"]
+ assert data["orderLine"]["quantity"] == new_quantity
+ assert_proper_webhook_called_once(
+ order,
+ order.status,
+ draft_order_updated_webhook_mock,
+ order_updated_webhook_mock,
+ )
+ removed_items_event = OrderEvent.objects.last() # type: OrderEvent
+ assert removed_items_event.type == order_events.OrderEvents.REMOVED_PRODUCTS
+ assert removed_items_event.user == staff_user
+ assert removed_items_event.parameters == {
+ "lines": [
+ {"quantity": removed_quantity, "line_pk": str(line.pk), "item": str(line)}
+ ]
+ }
+
+ # mutation should fail when quantity is lower than 1
+ variables = {"lineId": line_id, "quantity": 0}
+ response = staff_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderLineUpdate"]
+ assert data["errors"]
+ assert data["errors"][0]["field"] == "quantity"
+
+
def test_order_line_update_by_user_no_channel_access(
order_with_lines,
permission_group_all_perms_channel_USD_only,