Skip to content

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

Order total authorized performance (#7170)

Tomasz Szymańskiea0ffb255a8e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ed8dfa0b60..5cac13890e 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 `totalAuthorized` field - #7170 by @tomaszszymanski129
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 6d21b8efe6..9a7df38e1f 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -23,7 +23,7 @@ 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_captured
+from ...payment.model_helpers import 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
@@ -802,9 +802,15 @@ class Order(CountableDjangoObjectType):
return root.undiscounted_total
@staticmethod
- def resolve_total_authorized(root: models.Order, _info):
- # FIXME adjust to multiple payments in the future
- return root.total_authorized
+ def resolve_total_authorized(root: models.Order, info):
+ def _resolve_total_get_total_authorized(payments):
+ return get_total_authorized(payments, root.currency)
+
+ return (
+ PaymentsByOrderIdLoader(info.context)
+ .load(root.id)
+ .then(_resolve_total_get_total_authorized)
+ )
@staticmethod
def resolve_total_captured(root: models.Order, info):
diff --git a/saleor/order/models.py b/saleor/order/models.py
index f90d3bcf74..f761141bc7 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -19,14 +19,14 @@ from ..account.models import Address
from ..channel.models import Channel
from ..core.models import ModelWithMetadata
from ..core.permissions import OrderPermissions
-from ..core.taxes import zero_money, zero_taxed_money
+from ..core.taxes import zero_taxed_money
from ..core.utils.json_serializer import CustomJsonEncoder
from ..core.weight import WeightUnits, zero_weight
from ..discount import DiscountValueType
from ..discount.models import Voucher
from ..giftcard.models import GiftCard
from ..payment import ChargeStatus, TransactionKind
-from ..payment.model_helpers import get_total_captured
+from ..payment.model_helpers import get_total_authorized, get_total_captured
from ..shipping.models import ShippingMethod
from . import FulfillmentStatus, OrderEvents, OrderStatus
@@ -343,10 +343,7 @@ class Order(ModelWithMetadata):
@property
def total_authorized(self):
- payment = self.get_last_payment()
- if payment:
- return payment.get_authorized_amount()
- return zero_money(self.currency)
+ return get_total_authorized(self.payments.all(), self.currency)
@property
def total_captured(self):
diff --git a/saleor/payment/model_helpers.py b/saleor/payment/model_helpers.py
index 4e71a1595e..92aabf1e5a 100644
--- a/saleor/payment/model_helpers.py
+++ b/saleor/payment/model_helpers.py
@@ -15,12 +15,22 @@ def get_last_payment(payments: List[Payment]):
def get_total_captured(payments: List[Payment], fallback_currency: str):
# FIXME adjust to multiple payments in the future
if last_payment := get_last_payment(payments):
- if last_payment.charge_status in ( # type: ignore
- ChargeStatus.PARTIALLY_CHARGED,
- ChargeStatus.FULLY_CHARGED,
- ChargeStatus.PARTIALLY_REFUNDED,
- ):
- return Money(
- last_payment.captured_amount, last_payment.currency # type: ignore
+ if (
+ last_payment.charge_status
+ in (
+ ChargeStatus.PARTIALLY_CHARGED,
+ ChargeStatus.FULLY_CHARGED,
+ ChargeStatus.PARTIALLY_REFUNDED,
)
+ and last_payment.is_active
+ ):
+ return Money(last_payment.captured_amount, last_payment.currency)
+ return zero_money(fallback_currency)
+
+
+def get_total_authorized(payments: List[Payment], fallback_currency: str):
+ # FIXME adjust to multiple payments in the future
+ if last_payment := get_last_payment(payments):
+ if last_payment.is_active:
+ return last_payment.get_authorized_amount()
return zero_money(fallback_currency)