Skip to content

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

Extend order payload by fulfillment lines (#7347)

Maciej Korycinski241e2a3c98ce
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 55d0b202c2..0caf46e148 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -152,6 +152,14 @@ def generate_order_payload(order: "Order"):
shipping_method_fields = ("name", "type", "currency", "price_amount")
lines = order.lines.all()
+
+ fulfillments_data = serializer.serialize(
+ order.fulfillments.all(),
+ fields=fulfillment_fields,
+ extra_dict_data={
+ "lines": lambda f: json.loads(generate_fulfillment_lines_payload(f))
+ },
+ )
order_data = serializer.serialize(
[order],
fields=ORDER_FIELDS,
@@ -161,12 +169,12 @@ def generate_order_payload(order: "Order"):
"payments": (lambda o: o.payments.all(), payment_fields),
"shipping_address": (lambda o: o.shipping_address, ADDRESS_FIELDS),
"billing_address": (lambda o: o.billing_address, ADDRESS_FIELDS),
- "fulfillments": (lambda o: o.fulfillments.all(), fulfillment_fields),
"discounts": (lambda o: o.discounts.all(), discount_fields),
},
extra_dict_data={
"original": graphene.Node.to_global_id("Order", order.original_id),
"lines": json.loads(generate_order_lines_payload(lines)),
+ "fulfillments": json.loads(fulfillments_data),
},
)
return order_data
@@ -378,8 +386,19 @@ def generate_fulfillment_lines_payload(fulfillment: Fulfillment):
"product_type": (
lambda fl: fl.order_line.variant.product.product_type.name
),
+ "unit_price_net": lambda fl: fl.order_line.unit_price_net_amount,
"unit_price_gross": lambda fl: fl.order_line.unit_price_gross_amount,
- "currency": (lambda fl: fl.order_line.currency),
+ "undiscounted_unit_price_net": (
+ lambda fl: fl.order_line.undiscounted_unit_price.net.amount
+ ),
+ "undiscounted_unit_price_gross": (
+ lambda fl: fl.order_line.undiscounted_unit_price.gross.amount
+ ),
+ "total_price_net_amount": lambda fl: fl.order_line.total_price_net_amount,
+ "total_price_gross_amount": (
+ lambda fl: fl.order_line.total_price_gross_amount
+ ),
+ "currency": lambda fl: fl.order_line.currency,
},
)
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index 742284968b..7c44e4aa7d 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -1,26 +1,31 @@
import json
from decimal import Decimal
from itertools import chain
+from unittest import mock
from unittest.mock import ANY
import graphene
from ...discount import DiscountValueType, OrderDiscountType
-from ...order import OrderOrigin
+from ...order import OrderLineData, OrderOrigin
+from ...order.actions import fulfill_order_lines
from ...order.models import Order
from ...product.models import ProductVariant
from ..payloads import (
ORDER_FIELDS,
PRODUCT_VARIANT_FIELDS,
+ generate_fulfillment_lines_payload,
generate_invoice_payload,
generate_order_payload,
generate_product_variant_payload,
)
[email protected]("saleor.webhook.payloads.generate_fulfillment_lines_payload")
def test_generate_order_payload(
- order_with_lines, fulfilled_order, payment_txn_captured
+ mocked_fulfillment_lines, order_with_lines, fulfilled_order, payment_txn_captured
):
+ mocked_fulfillment_lines.return_value = "{}"
new_order = Order.objects.create(
channel=order_with_lines.channel,
billing_address=order_with_lines.billing_address,
@@ -44,6 +49,9 @@ def test_generate_order_payload(
name="Voucher",
)
+ assert fulfilled_order.fulfillments.count() == 1
+ fulfillment = fulfilled_order.fulfillments.first()
+
order_id = graphene.Node.to_global_id("Order", order_with_lines.id)
payload = json.loads(generate_order_payload(order_with_lines))[0]
@@ -61,6 +69,39 @@ def test_generate_order_payload(
assert payload.get("discounts")
assert payload.get("original") == graphene.Node.to_global_id("Order", new_order.pk)
+ mocked_fulfillment_lines.assert_called_with(fulfillment)
+
+
+def test_generate_fulfillment_lines_payload(order_with_lines):
+ fulfillment = order_with_lines.fulfillments.create(tracking_number="123")
+ line = order_with_lines.lines.first()
+ stock = line.allocations.get().stock
+ warehouse_pk = stock.warehouse.pk
+ fulfillment_line = fulfillment.lines.create(
+ order_line=line, quantity=line.quantity, stock=stock
+ )
+ fulfill_order_lines(
+ [
+ OrderLineData(line=line, quantity=line.quantity, warehouse_pk=warehouse_pk),
+ ]
+ )
+ payload = json.loads(generate_fulfillment_lines_payload(fulfillment))[0]
+ assert payload == {
+ "currency": "USD",
+ "id": graphene.Node.to_global_id("FulfillmentLine", fulfillment_line.id),
+ "product_type": "Default Type",
+ "quantity": fulfillment_line.quantity,
+ "total_price_gross_amount": str(line.total_price.gross.amount),
+ "total_price_net_amount": str(line.total_price.net.amount),
+ "type": "FulfillmentLine",
+ "undiscounted_unit_price_gross": str(line.undiscounted_unit_price.gross.amount),
+ "undiscounted_unit_price_net": str(line.undiscounted_unit_price.net.amount),
+ "unit_price_gross": str(line.unit_price.gross.amount),
+ "unit_price_net": str(line.unit_price.net.amount),
+ "weight": 0.0,
+ "weight_unit": "gram",
+ }
+
def test_order_lines_have_all_required_fields(order, order_line_with_one_allocation):
order.lines.add(order_line_with_one_allocation)