Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Order total authorized performance (#7170)
Tomasz Szymańskiea0ffb255a8e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 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-wysockidiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 6d21b8efe6..9a7df38e1f 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -23,7 +23,7 @@ from ...order.models import FulfillmentStatusfrom ...order.utils import get_order_country, get_valid_shipping_methods_for_orderfrom ...payment import ChargeStatusfrom ...payment.dataloaders import PaymentsByOrderIdLoader-from ...payment.model_helpers import get_total_captured+from ...payment.model_helpers import get_total_authorized, get_total_capturedfrom ...product.product_images import get_product_image_thumbnailfrom ..account.dataloaders import AddressByIdLoader, UserByUserIdLoaderfrom ..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)+ )@staticmethoddef resolve_total_captured(root: models.Order, info):diff --git a/saleor/order/models.py b/saleor/order/models.pyindex f90d3bcf74..f761141bc7 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -19,14 +19,14 @@ from ..account.models import Addressfrom ..channel.models import Channelfrom ..core.models import ModelWithMetadatafrom ..core.permissions import OrderPermissions-from ..core.taxes import zero_money, zero_taxed_money+from ..core.taxes import zero_taxed_moneyfrom ..core.utils.json_serializer import CustomJsonEncoderfrom ..core.weight import WeightUnits, zero_weightfrom ..discount import DiscountValueTypefrom ..discount.models import Voucherfrom ..giftcard.models import GiftCardfrom ..payment import ChargeStatus, TransactionKind-from ..payment.model_helpers import get_total_captured+from ..payment.model_helpers import get_total_authorized, get_total_capturedfrom ..shipping.models import ShippingMethodfrom . import FulfillmentStatus, OrderEvents, OrderStatus@@ -343,10 +343,7 @@ class Order(ModelWithMetadata):@propertydef 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)@propertydef total_captured(self):diff --git a/saleor/payment/model_helpers.py b/saleor/payment/model_helpers.pyindex 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 futureif 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)