Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add missing fields to fulfillment (#14577)
Maciej Korycinski07993bbaa820
diff --git a/saleor/graphql/order/tests/queries/test_fulfillment.py b/saleor/graphql/order/tests/queries/test_fulfillment.pyindex c55743be63..13ee82c4d6 100644--- a/saleor/graphql/order/tests/queries/test_fulfillment.py+++ b/saleor/graphql/order/tests/queries/test_fulfillment.py@@ -1,3 +1,5 @@+from decimal import Decimal+import graphenefrom .....order import FulfillmentStatus@@ -14,6 +16,12 @@ QUERY_FULFILLMENT = """warehouse {id}+ shippingRefundedAmount {+ amount+ }+ totalRefundedAmount {+ amount+ }lines {orderLine {id@@ -111,3 +119,38 @@ def test_staff_can_query_order_fulfill_data_without_permission(variables = {"id": order_id}response = staff_api_client.post_graphql(QUERY_ORDER_FULFILL_DATA, variables)assert_no_permission(response)+++def test_fulfillment_with_refund_details(+ staff_api_client,+ fulfilled_order,+ warehouse,+ permission_manage_orders,+):+ # given+ order = fulfilled_order+ order_id = graphene.Node.to_global_id("Order", order.pk)+ fulfillment = order.fulfillments.first()+ shipping_refund_amount = Decimal("10")+ total_refund_amount = Decimal("15")+ fulfillment.shipping_refund_amount = shipping_refund_amount+ fulfillment.total_refund_amount = total_refund_amount+ fulfillment.save()++ staff_user = staff_api_client.user+ staff_user.user_permissions.add(permission_manage_orders)+ variables = {"id": order_id}++ # when+ response = staff_api_client.post_graphql(QUERY_FULFILLMENT, variables)++ # then+ content = get_graphql_content(response)+ data = content["data"]["order"]["fulfillments"]+ assert len(data) == 1+ fulfillment_data = data[0]++ assert (+ fulfillment_data["shippingRefundedAmount"]["amount"] == shipping_refund_amount+ )+ assert fulfillment_data["totalRefundedAmount"]["amount"] == total_refund_amountdiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 15a2c266a3..3e7d25e68f 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -632,6 +632,17 @@ class Fulfillment(ModelObjectType[models.Fulfillment]):required=False,description="Warehouse from fulfillment was fulfilled.",)+ shipping_refunded_amount = graphene.Field(+ Money,+ description="Amount of refunded shipping price." + ADDED_IN_314,+ required=False,+ )+ total_refunded_amount = graphene.Field(+ Money,+ description="Total refunded amount assigned to this fulfillment."+ + ADDED_IN_314,+ required=False,+ )class Meta:description = "Represents order fulfillment."@@ -674,6 +685,34 @@ class Fulfillment(ModelObjectType[models.Fulfillment]):.then(_resolve_stock))+ @staticmethod+ def resolve_shipping_refunded_amount(root: models.Fulfillment, info):+ if root.shipping_refund_amount is None:+ return None++ def _resolve_shipping_refund(order):+ return prices.Money(root.shipping_refund_amount, currency=order.currency)++ return (+ OrderByIdLoader(info.context)+ .load(root.order_id)+ .then(_resolve_shipping_refund)+ )++ @staticmethod+ def resolve_total_refunded_amount(root: models.Fulfillment, info):+ if root.total_refund_amount is None:+ return None++ def _resolve_total_refund_amount(order):+ return prices.Money(root.total_refund_amount, currency=order.currency)++ return (+ OrderByIdLoader(info.context)+ .load(root.order_id)+ .then(_resolve_total_refund_amount)+ )+class OrderLine(ModelObjectType[models.OrderLine]):id = graphene.GlobalID(required=True)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 927be05ffe..00f542eaa1 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -11803,6 +11803,20 @@ type Fulfillment implements Node & ObjectWithMetadata @doc(category: "Orders") {"""Warehouse from fulfillment was fulfilled."""warehouse: Warehouse++ """+ Amount of refunded shipping price.++ Added in Saleor 3.14.+ """+ shippingRefundedAmount: Money++ """+ Total refunded amount assigned to this fulfillment.++ Added in Saleor 3.14.+ """+ totalRefundedAmount: Money}"""An enumeration."""diff --git a/saleor/graphql/tests/queries/fragments.py b/saleor/graphql/tests/queries/fragments.pyindex e26bf4c22f..0049a77ebe 100644--- a/saleor/graphql/tests/queries/fragments.py+++ b/saleor/graphql/tests/queries/fragments.py@@ -190,6 +190,12 @@ fragment FulfillmentDetails on Fulfillment {fulfillmentOrdertrackingNumberstatus+ shippingRefundedAmount{+ amount+ }+ totalRefundedAmount{+ amount+ }lines {idquantitydiff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.pyindex 29cfc1dd2c..c18ff1f846 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py@@ -117,6 +117,8 @@ def generate_fulfillment_payload(fulfillment, add_notify_customer_field=False):payload = {"fulfillment": {"id": fulfillment_id,+ "shippingRefundedAmount": None,+ "totalRefundedAmount": None,"fulfillmentOrder": fulfillment.fulfillment_order,"trackingNumber": fulfillment.tracking_number,"status": fulfillment.status.upper(),diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.pyindex 5d3df84223..ad6c8cdcb1 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py@@ -1,4 +1,5 @@import json+from decimal import Decimalfrom unittest.mock import MagicMock, patchimport graphene@@ -1867,6 +1868,42 @@ def test_fulfillment_created(fulfillment, subscription_fulfillment_created_webhoassert deliveries[0].webhook == webhooks[0]+def test_fulfillment_with_refund_amounts(+ fulfillment, subscription_fulfillment_created_webhook+):+ # given+ shipping_refund = Decimal("10")+ total_refund = Decimal("15")+ fulfillment.shipping_refund_amount = shipping_refund+ fulfillment.total_refund_amount = total_refund+ fulfillment.save()++ webhooks = [subscription_fulfillment_created_webhook]+ event_type = WebhookEventAsyncType.FULFILLMENT_CREATED+ expected_payload = generate_fulfillment_payload(+ fulfillment, add_notify_customer_field=True+ )+ expected_payload["fulfillment"]["shippingRefundedAmount"] = {+ "amount": shipping_refund+ }+ expected_payload["fulfillment"]["totalRefundedAmount"] = {"amount": total_refund}++ # when+ deliveries = create_deliveries_for_subscriptions(+ event_type,+ {+ "fulfillment": fulfillment,+ "notify_customer": True,+ },+ webhooks,+ )++ # then+ assert json.loads(deliveries[0].payload.payload) == expected_payload+ assert len(deliveries) == len(webhooks)+ assert deliveries[0].webhook == webhooks[0]++def test_fulfillment_canceled(fulfillment, subscription_fulfillment_canceled_webhook):# givenwebhooks = [subscription_fulfillment_canceled_webhook]@@ -1877,7 +1914,7 @@ def test_fulfillment_canceled(fulfillment, subscription_fulfillment_canceled_webdeliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)# then- assert deliveries[0].payload.payload == json.dumps(expected_payload)+ assert json.loads(deliveries[0].payload.payload) == expected_payloadassert len(deliveries) == len(webhooks)assert deliveries[0].webhook == webhooks[0]@@ -1918,7 +1955,7 @@ def test_fulfillment_metadata_updated(deliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)# then- assert deliveries[0].payload.payload == json.dumps(expected_payload)+ assert json.loads(deliveries[0].payload.payload) == expected_payloadassert len(deliveries) == len(webhooks)assert deliveries[0].webhook == webhooks[0]@@ -1935,7 +1972,7 @@ def test_fulfillment_tracking_number_updated(deliveries = create_deliveries_for_subscriptions(event_type, fulfillment, webhooks)# then- assert deliveries[0].payload.payload == json.dumps(expected_payload)+ assert json.loads(deliveries[0].payload.payload) == expected_payloadassert len(deliveries) == len(webhooks)assert deliveries[0].webhook == webhooks[0]