Skip to content

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

Order actions performance (#7175)

Tomasz Szymański83d7fa85bca3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5cac13890e..0e37065caa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -59,6 +59,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix incorrect payment data for klarna - #7150 by @IKarbowiak
- Drop deleted images from storage - #7129 by @IKarbowiak
- Fix core sorting on related fields - #7195 by @tomaszszymanski129
+- Performance upgrade on orders query with `actions` field - #7175 by @tomaszszymanski129
- Performance upgrade on orders query with `totalAuthorized` field - #7170 by @tomaszszymanski129
### Breaking
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 9a7df38e1f..a06241ecb8 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -23,7 +23,11 @@ from ...order.models import FulfillmentStatus
from ...order.utils import get_order_country, get_valid_shipping_methods_for_order
from ...payment import ChargeStatus
from ...payment.dataloaders import PaymentsByOrderIdLoader
-from ...payment.model_helpers import get_total_authorized, get_total_captured
+from ...payment.model_helpers import (
+ get_last_payment,
+ get_total_authorized,
+ get_total_captured,
+)
from ...product.product_images import get_product_image_thumbnail
from ..account.dataloaders import AddressByIdLoader, UserByUserIdLoader
from ..account.types import User
@@ -775,18 +779,23 @@ class Order(CountableDjangoObjectType):
@staticmethod
@traced_resolver
- def resolve_actions(root: models.Order, _info):
- actions = []
- payment = root.get_last_payment()
- if root.can_capture(payment):
- actions.append(OrderAction.CAPTURE)
- if root.can_mark_as_paid():
- actions.append(OrderAction.MARK_AS_PAID)
- if root.can_refund(payment):
- actions.append(OrderAction.REFUND)
- if root.can_void(payment):
- actions.append(OrderAction.VOID)
- return actions
+ def resolve_actions(root: models.Order, info):
+ def _resolve_actions(payments):
+ actions = []
+ payment = get_last_payment(payments)
+ if root.can_capture(payment):
+ actions.append(OrderAction.CAPTURE)
+ if root.can_mark_as_paid(payments):
+ actions.append(OrderAction.MARK_AS_PAID)
+ if root.can_refund(payment):
+ actions.append(OrderAction.REFUND)
+ if root.can_void(payment):
+ actions.append(OrderAction.VOID)
+ return actions
+
+ return (
+ PaymentsByOrderIdLoader(info.context).load(root.id).then(_resolve_actions)
+ )
@staticmethod
@traced_resolver
diff --git a/saleor/order/models.py b/saleor/order/models.py
index f761141bc7..330a9d2b62 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -338,8 +338,10 @@ class Order(ModelWithMetadata):
return False
return payment.can_refund()
- def can_mark_as_paid(self):
- return len(self.payments.all()) == 0
+ def can_mark_as_paid(self, payments=None):
+ if not payments:
+ payments = self.payments.all()
+ return len(payments) == 0
@property
def total_authorized(self):