Skip to content

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

Add can_finalize to order type in GraphQL API

fowczarek516ffad8819f
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 66ffce0be8..c5491722bc 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -9,6 +9,7 @@ from ..core.types.common import CountableDjangoObjectType
from ..core.types.money import Money, TaxedMoney
from ..payment.types import OrderAction, Payment, PaymentChargeStatusEnum
from ..shipping.types import ShippingMethod
+from .utils import can_finalize_order
OrderEventsEnum = graphene.Enum.from_enum(OrderEvents)
OrderEventsEmailsEnum = graphene.Enum.from_enum(OrderEventsEmails)
@@ -175,6 +176,8 @@ class Order(CountableDjangoObjectType):
TaxedMoney,
description='The sum of line prices not including shipping.')
status_display = graphene.String(description='User-friendly order status.')
+ can_finalize = graphene.Boolean(
+ description='Informs if an order draft can be complete.')
total_authorized = graphene.Field(
Money, description='Amount authorized for the order.')
total_captured = graphene.Field(
@@ -284,6 +287,10 @@ class Order(CountableDjangoObjectType):
def resolve_status_display(self, info):
return self.get_status_display()
+ @staticmethod
+ def resolve_can_finalize(self, info):
+ return can_finalize_order(self)
+
@staticmethod
def resolve_user_email(self, info):
if self.user_email:
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index e96541e48d..69dc52f008 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -1,12 +1,11 @@
from ..core.types.common import Error
-def check_for_draft_order_errors(order, errors=[]):
- # TODO: New Description
+def check_for_draft_order_errors(order, errors):
"""Return a list of errors associated with the order.
- Checks, if given order has a proper shipping address and method
- set up and return list of errors if not.
+ Checks, if given order has a proper customer data, shipping
+ address and method set up and return list of errors if not.
"""
if order.get_total_quantity() == 0:
errors.append(
@@ -31,3 +30,12 @@ def check_for_draft_order_errors(order, errors=[]):
field=None,
message='Both user and user_email fields are null'))
return errors
+
+
+def can_finalize_order(order):
+ """Return `True` if an order draft can be complete.
+ """
+
+ errors = []
+ errors = check_for_draft_order_errors(order, errors)
+ return not errors
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index b208fad5b4..13b1057c6a 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -10,7 +10,8 @@ from saleor.graphql.order.mutations.orders import (
clean_order_cancel, clean_order_capture, clean_order_mark_as_paid,
clean_refund_payment, clean_void_payment)
from saleor.graphql.order.types import OrderEventsEmailsEnum, OrderStatusFilter
-from saleor.graphql.order.utils import check_for_draft_order_errors
+from saleor.graphql.order.utils import (
+ can_finalize_order, check_for_draft_order_errors)
from saleor.graphql.payment.types import PaymentChargeStatusEnum
from saleor.order import OrderEvents, OrderEventsEmails, OrderStatus
from saleor.order.models import Order, OrderEvent
@@ -60,6 +61,7 @@ def test_order_query(
edges {
node {
number
+ canFinalize
status
statusDisplay
paymentStatus
@@ -111,6 +113,7 @@ def test_order_query(
content = get_graphql_content(response)
order_data = content['data']['orders']['edges'][0]['node']
assert order_data['number'] == str(order.pk)
+ assert order_data['canFinalize'] == True
assert order_data['status'] == order.status.upper()
assert order_data['statusDisplay'] == order.get_status_display()
assert order_data['paymentStatus'] == order.get_last_payment_status()
@@ -353,6 +356,14 @@ def test_draft_order_delete(
order.refresh_from_db()
+def test_can_finalize_order(order_with_lines):
+ assert can_finalize_order(order_with_lines) == True
+
+
+def test_can_finalize_order_no_order_lines(order):
+ assert can_finalize_order(order) == False
+
+
def test_check_for_draft_order_errors(order_with_lines):
errors = check_for_draft_order_errors(order_with_lines, [])
assert not errors