Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add error logger in case of having order with netgative amount (#18240)
Maciej Korycinskifef7f9f24e21
diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 10314391df..211b356a4b 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -186,6 +186,103 @@ def _trigger_order_sync_webhooks()+def _get_extra_for_order_logger(order: "Order") -> dict:+ return {+ "order_id": order.id,+ "currency": order.currency,+ "status": order.status,+ "origin": order.origin,+ "checkout_id": order.checkout_token,+ "undiscounted_base_shipping_price_amount": order.undiscounted_base_shipping_price_amount,+ "base_shipping_price_amount": order.base_shipping_price_amount,+ "shipping_price_net_amount": order.shipping_price_net_amount,+ "shipping_price_gross_amount": order.shipping_price_gross_amount,+ "undiscounted_total_net_amount": order.undiscounted_total_net_amount,+ "total_net_amount": order.total_net_amount,+ "undiscounted_total_gross_amount": order.undiscounted_total_gross_amount,+ "total_gross_amount": order.total_gross_amount,+ "subtotal_net_amount": order.subtotal_net_amount,+ "subtotal_gross_amount": order.subtotal_gross_amount,+ "has_voucher_code": bool(order.voucher_code),+ "tax_exemption": order.tax_exemption,+ "tax_error": order.tax_error,+ }+++def _get_extra_for_order_line_logger(line: "OrderLine") -> dict:+ return {+ "line_id": line.id,+ "variant_id": line.variant_id,+ "quantity": line.quantity,+ "is_gift_card": line.is_gift_card,+ "is_price_overridden": line.is_price_overridden,+ "unit_price_net_amount": line.unit_price_net_amount,+ "unit_price_gross_amount": line.unit_price_gross_amount,+ "total_price_net_amount": line.total_price_net_amount,+ "total_price_gross_amount": line.total_price_gross_amount,+ "has_voucher_code": line.voucher_code,+ "unit_discount_amount": line.unit_discount_amount,+ "unit_discount_type": line.unit_discount_type,+ "unit_discount_reason": line.unit_discount_reason,+ }+++def _order_has_negative_prices(order: "Order", lines: list["OrderLineInfo"]) -> bool:+ if not order:+ logger.error("Received None as order to check for negative prices")+ return False++ if list(+ filter(+ lambda x: x < 0,+ [+ order.shipping_price_net_amount,+ order.shipping_price_gross_amount,+ order.total_net_amount,+ order.total_gross_amount,+ order.subtotal_net_amount,+ order.subtotal_gross_amount,+ ],+ )+ ):+ return True+ if lines is None:+ logger.error(+ "Received None as order lines to check for negative prices",+ extra=_get_extra_for_order_logger(order),+ )+ return False+ for line_info in lines:+ line = line_info.line+ if not line:+ logger.error(+ "Received None as order line to check for negative prices",+ extra=_get_extra_for_order_logger(order),+ )+ continue+ if list(+ filter(+ lambda x: x < 0,+ [+ line.unit_price_net_amount,+ line.unit_price_gross_amount,+ line.total_price_net_amount,+ line.total_price_gross_amount,+ ],+ )+ ):+ return True+ return False+++def _log_order_with_negative_price(order: "Order", lines: list["OrderLineInfo"]):+ extra = _get_extra_for_order_logger(order)+ extra["lines"] = [+ _get_extra_for_order_line_logger(line_info.line) for line_info in lines+ ]+ logger.error("Order with negative prices detected", extra=extra)++def call_order_events(manager: "PluginsManager",event_names: list[str],@@ -342,6 +439,9 @@ def order_created(if channel.automatically_confirm_all_new_orders:order_confirmed(order, user, app, manager, webhook_event_map=webhook_event_map)+ if _order_has_negative_prices(order, order_info.lines_data):+ _log_order_with_negative_price(order, order_info.lines_data)+def order_confirmed(order: "Order",diff --git a/saleor/order/tests/test_actions.py b/saleor/order/tests/test_actions.pyindex fb8e79f105..73f817020a 100644--- a/saleor/order/tests/test_actions.py+++ b/saleor/order/tests/test_actions.py@@ -5,8 +5,14 @@ import pytestfrom ...graphql.core.utils import to_global_id_or_nonefrom ...webhook.utils import get_webhooks_for_multiple_eventsfrom .. import OrderStatus-from ..actions import WEBHOOK_EVENTS_FOR_ORDER_CREATED, order_confirmed, order_created-from ..fetch import OrderInfo+from ..actions import (+ WEBHOOK_EVENTS_FOR_ORDER_CREATED,+ _get_extra_for_order_line_logger,+ _get_extra_for_order_logger,+ order_confirmed,+ order_created,+)+from ..fetch import OrderInfo, OrderLineInfoparametrize_order_statuses = [(status,) for status, _ in OrderStatus.CHOICES]@@ -86,3 +92,89 @@ def test_order_created_with_tax_error(order, customer_user, plugins_manager, cap# thenassert "Created non-draft order with tax_error for order" in caplog.textassert caplog.records[0].order_id == to_global_id_or_none(order)+++ "order_field_name",+ [+ "shipping_price_net_amount",+ "shipping_price_gross_amount",+ "total_net_amount",+ "total_gross_amount",+ "subtotal_net_amount",+ "subtotal_gross_amount",+ ],+)+@patch("saleor.order.actions.order_confirmed")+def test_order_created_negative_order_price(+ mock_order_confirmed,+ order_field_name,+ order,+ customer_user,+ plugins_manager,+ caplog,+):+ # given+ setattr(order, order_field_name, -10)+ order.save(update_fields=[order_field_name])+ order_info = OrderInfo(+ order=order,+ customer_email=order.get_customer_email(),+ channel=order.channel,+ payment=None,+ lines_data=[],+ )++ # when+ order_created(order_info, user=customer_user, app=None, manager=plugins_manager)++ # then+ order_extra_data = _get_extra_for_order_logger(order)+ assert order_extra_data.items() <= caplog.records[0].__dict__.items()++ assert "Order with negative prices detected" in caplog.text+++ "line_field_name",+ [+ "unit_price_net_amount",+ "unit_price_gross_amount",+ "total_price_net_amount",+ "total_price_gross_amount",+ ],+)+@patch("saleor.order.actions.order_confirmed")+def test_order_created_negative_order_line_price(+ mock_order_confirmed,+ line_field_name,+ order_with_lines,+ customer_user,+ plugins_manager,+ caplog,+):+ # given+ order_line = order_with_lines.lines.first()+ setattr(order_line, line_field_name, -10)+ order_line.save(update_fields=[line_field_name])+ order_info = OrderInfo(+ order=order_with_lines,+ customer_email=order_with_lines.get_customer_email(),+ channel=order_with_lines.channel,+ payment=None,+ lines_data=[+ OrderLineInfo(+ line=order_line,+ quantity=order_line.quantity,+ )+ ],+ )++ # when+ order_created(order_info, user=customer_user, app=None, manager=plugins_manager)++ # then+ line_extra_data = _get_extra_for_order_line_logger(order_line)+ assert line_extra_data == caplog.records[0].lines[0]++ assert "Order with negative prices detected" in caplog.text