Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix creation of the event. (#18959)
Marcin Wcisłofd463c8aae38
diff --git a/saleor/graphql/order/mutations/order_line_discount_remove.py b/saleor/graphql/order/mutations/order_line_discount_remove.pyindex ea75b67f27..a309d13dc0 100644--- a/saleor/graphql/order/mutations/order_line_discount_remove.py+++ b/saleor/graphql/order/mutations/order_line_discount_remove.py@@ -1,3 +1,5 @@+import copy+import graphenefrom ....core.tracing import traced_atomic_transaction@@ -45,6 +47,8 @@ class OrderLineDiscountRemove(OrderDiscountCommon):order = order_line.ordercls.check_channel_permissions(info, [order.channel_id])cls.validate(info, order)++ order_line_before_deletion = copy.deepcopy(order_line)with traced_atomic_transaction():remove_discount_from_order_line(order_line, order)app = get_app_promise(info.context).get()@@ -52,7 +56,7 @@ class OrderLineDiscountRemove(OrderDiscountCommon):order=order,user=info.context.user,app=app,- line=order_line,+ line=order_line_before_deletion,)invalidate_order_prices(order, save=True)diff --git a/saleor/graphql/order/tests/mutations/test_order_discount.py b/saleor/graphql/order/tests/mutations/test_order_discount.pyindex 3fcc142af9..a515284fe2 100644--- a/saleor/graphql/order/tests/mutations/test_order_discount.py+++ b/saleor/graphql/order/tests/mutations/test_order_discount.py@@ -16,6 +16,7 @@ from .....order import OrderEvents, OrderStatusfrom .....order.calculations import fetch_order_prices_if_expiredfrom .....order.error_codes import OrderErrorCodefrom .....order.interface import OrderTaxedPricesData+from .....order.models import OrderEventfrom .....order.utils import invalidate_order_pricesfrom ....discount.enums import DiscountValueTypeEnumfrom ....tests.utils import assert_no_permission, get_graphql_content@@ -2006,6 +2007,151 @@ def test_delete_discount_from_order_line(assert not line.discounts.exists()+ORDER_QUERY_WITH_EVENT_LINE_DISCOUNTS = """+query OrderDetails($id: ID!) {+ order(id: $id) {+ events {+ type+ lines {+ discount {+ valueType+ value+ amount {+ amount+ }+ }+ }+ }+ }+}+"""+++@patch("saleor.plugins.manager.PluginsManager.calculate_order_line_unit")+@patch("saleor.plugins.manager.PluginsManager.calculate_order_line_total")+def test_query_order_after_line_discount_removed_returns_discount_event_data(+ mocked_calculate_order_line_total,+ mocked_calculate_order_line_unit,+ draft_order,+ staff_api_client,+ permission_group_manage_orders,+):+ """We used to have a bug here where when we removed order line discount, the order event data was incorrectly captured.++ This This test case removes order line discount and validates that the order event data is correctly captured now.+ """+ # given+ order = draft_order+ line = order.lines.first()++ line_undiscounted_price = TaxedMoney(+ line.undiscounted_base_unit_price, line.undiscounted_base_unit_price+ )+ line_undiscounted_total_price = line_undiscounted_price * line.quantity++ mocked_calculate_order_line_unit.return_value = OrderTaxedPricesData(+ undiscounted_price=line_undiscounted_price,+ price_with_discounts=line_undiscounted_price,+ )+ mocked_calculate_order_line_total.return_value = OrderTaxedPricesData(+ undiscounted_price=line_undiscounted_total_price,+ price_with_discounts=line_undiscounted_total_price,+ )++ line.unit_discount_amount = Decimal("2.5")+ line.unit_discount_type = DiscountValueType.FIXED+ line.unit_discount_value = Decimal("2.5")+ line.save()++ discount = line.discounts.create(+ type=DiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=Decimal("2.5"),+ currency=order.currency,+ )++ variables = {+ "orderLineId": graphene.Node.to_global_id("OrderLine", line.pk),+ }+ permission_group_manage_orders.user_set.add(staff_api_client.user)++ # when+ staff_api_client.post_graphql(ORDER_LINE_DISCOUNT_REMOVE, variables)++ # then - querying order events should not crash+ order_id = graphene.Node.to_global_id("Order", order.pk)+ response = staff_api_client.post_graphql(+ ORDER_QUERY_WITH_EVENT_LINE_DISCOUNTS, {"id": order_id}+ )+ content = get_graphql_content(response)++ order_data = content["data"]["order"]+ assert len(order_data["events"]) == 1++ event = order_data["events"][0]+ assert event["type"] == OrderEvents.ORDER_LINE_DISCOUNT_REMOVED.upper()++ event_lines = event["lines"]+ assert len(event_lines) == 1++ discount_data = event_lines[0]["discount"]+ assert discount_data["valueType"] == discount.value_type.upper()+ assert discount_data["value"] == discount.value+++def test_query_order_event_with_null_discount_value_type(+ draft_order,+ staff_api_client,+ permission_group_manage_orders,+):+ """We used to have a bug here where when we removed order line discount, the order event data was incorrectly captured.++ This in turn broke the resolver and made the code crash. This test replicates the+ broken event and checks whether resolver asking for `value_type` crashes when the+ value type is None.+ """++ # given+ order = draft_order+ line = order.lines.first()++ OrderEvent.objects.create(+ order=order,+ type=OrderEvents.ORDER_LINE_DISCOUNT_REMOVED,+ parameters={+ "lines": [+ {+ "quantity": line.quantity,+ "line_pk": str(line.pk),+ "item": str(line),+ "discount": {+ "value": Decimal(0),+ "amount_value": Decimal(0),+ "currency": order.currency,+ "value_type": None,+ "reason": None,+ },+ }+ ]+ },+ )++ permission_group_manage_orders.user_set.add(staff_api_client.user)+ order_id = graphene.Node.to_global_id("Order", order.pk)++ # when+ response = staff_api_client.post_graphql(+ ORDER_QUERY_WITH_EVENT_LINE_DISCOUNTS, {"id": order_id}+ )++ # then+ content = get_graphql_content(response)+ order_data = content["data"]["order"]+ assert len(order_data["events"]) == 1+ event = order_data["events"][0]+ assert event["type"] == OrderEvents.ORDER_LINE_DISCOUNT_REMOVED.upper()++@patch("saleor.plugins.manager.PluginsManager.calculate_order_line_unit")@patch("saleor.plugins.manager.PluginsManager.calculate_order_line_total")def test_delete_discount_from_order_line_by_user_no_channel_access(diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 68ff789849..cc76887440 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -185,6 +185,13 @@ logger = logging.getLogger(__name__)def get_order_discount_event(discount_obj: dict):+ # Value type is required in OrderDiscount class.+ # Such event, without `value_type` filled, could have been created when the code was buggy.+ # Such events cannot be repaired, this logic only guards code from crashing.+ value_type = discount_obj.get("value_type")+ if not value_type:+ return None+currency = discount_obj["currency"]amount = prices.Money(Decimal(discount_obj["amount_value"]), currency)@@ -197,7 +204,7 @@ def get_order_discount_event(discount_obj: dict):return OrderEventDiscountObject(value=discount_obj.get("value"),amount=amount,- value_type=discount_obj.get("value_type"),+ value_type=value_type,reason=discount_obj.get("reason"),old_value_type=discount_obj.get("old_value_type"),old_value=discount_obj.get("old_value"),