Skip to content

Commit

Permalink
Use frozen order data to return shipping method (saleor#8688)
Browse files Browse the repository at this point in the history
* Use frozen shipping method data

* Add test

* Reformat the code

* Return frozen data only for external shipping method

* Use Money instead of TaxedMoney

* Reuse site object from context

* Reformat the code

* Check existence of channel_listing
  • Loading branch information
bogdal authored Dec 2, 2021
1 parent d908e83 commit 1f6a975
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
35 changes: 35 additions & 0 deletions saleor/graphql/order/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from ....account.models import CustomerEvent
from ....checkout import AddressType
from ....checkout.utils import PRIVATE_META_APP_SHIPPING_ID
from ....core.anonymize import obfuscate_email
from ....core.notify_events import NotifyEventType
from ....core.prices import quantize_price
Expand Down Expand Up @@ -355,6 +356,12 @@ def test_order_line_with_allocations(
}
shippingMethod{
id
name
price {
amount
currency
}
}
deliveryMethod {
__typename
Expand Down Expand Up @@ -475,6 +482,34 @@ def test_order_query_shipping_method_channel_listing_does_not_exist(
)


def test_order_query_external_shipping_method(
staff_api_client,
permission_manage_orders,
order_with_lines,
):
external_shipping_method_id = graphene.Node.to_global_id("app", "1:external123")

# given
order = order_with_lines
order.shipping_method = None
order.private_metadata = {PRIVATE_META_APP_SHIPPING_ID: external_shipping_method_id}
order.save()

staff_api_client.user.user_permissions.add(permission_manage_orders)

# when
response = staff_api_client.post_graphql(ORDERS_QUERY)
content = get_graphql_content(response)

# then
order_data = content["data"]["orders"]["edges"][0]["node"]
assert order_data["shippingMethod"]["id"] == external_shipping_method_id
assert order_data["shippingMethod"]["name"] == order.shipping_method_name
assert order_data["shippingMethod"]["price"]["amount"] == float(
order.shipping_price_gross.amount
)


def test_order_discounts_query(
staff_api_client,
permission_manage_orders,
Expand Down
18 changes: 9 additions & 9 deletions saleor/graphql/order/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from ...product import ProductMediaTypes
from ...product.models import ALL_PRODUCTS_PERMISSIONS
from ...product.product_images import get_product_image_thumbnail
from ...shipping.interface import ShippingMethodData
from ..account.dataloaders import AddressByIdLoader, UserByUserIdLoader
from ..account.types import User
from ..account.utils import requestor_has_access
Expand Down Expand Up @@ -1056,15 +1057,14 @@ def resolve_shipping_method(root: models.Order, info):
external_app_shipping_id = get_external_shipping_id(root)

if external_app_shipping_id:
shipping_method = info.context.plugins.get_shipping_method(
checkout=root,
channel_slug=root.channel.slug,
shipping_method_id=external_app_shipping_id,
keep_gross = info.context.site.settings.include_taxes_in_prices
price = root.shipping_price_gross if keep_gross else root.shipping_price_net
method = ShippingMethodData(
id=external_app_shipping_id,
name=root.shipping_method_name,
price=price,
)
if shipping_method:
return ChannelContext(
node=shipping_method, channel_slug=root.channel.slug
)
return ChannelContext(node=method, channel_slug=root.channel.slug)

if not root.shipping_method_id:
return None
Expand All @@ -1084,7 +1084,7 @@ def wrap_shipping_method_with_channel_context(data):

@classmethod
def resolve_delivery_method(cls, root: models.Order, info):
if root.shipping_method_id:
if root.shipping_method_id or get_external_shipping_id(root):
return cls.resolve_shipping_method(root, info)
if root.collection_point_id:
collection_point = WarehouseByIdLoader(info.context).load(
Expand Down
6 changes: 5 additions & 1 deletion saleor/graphql/shipping/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ def resolve_price(
info.context
)
.load((root.node.id, root.channel_slug))
.then(lambda channel_listing: channel_listing.price)
.then(
lambda channel_listing: channel_listing.price
if channel_listing
else None
)
)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion saleor/shipping/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class ShippingMethodData:
"""Dataclass for storing information about a shipping method."""

id: str
name: str
price: Optional[Money]
name: Optional[str] = None
description: Optional[str] = None
type: Optional[str] = None
maximum_order_price: Optional[Money] = None
Expand Down

0 comments on commit 1f6a975

Please sign in to comment.