Skip to content

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

Extend Invoice object types with Order reference (#11505)

Przemysław Łada73f5eca2351f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e38bfdebf6..487fcebf03 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,9 @@ All notable, unreleased changes to this project will be documented in this file.
- `Order`
- `User`
- `Warehouse`
+- Extend invoice object types with `Order` references - #11505 by @przlada
+ - Add `Invoice.order` field
+ - Add `InvoiceRequested.order`, `InvoiceDeleted.order` and `InvoiceSent.order` fields
### Other changes
- Fix fetching the `checkout.availableCollectionPoints` - #11489 by @IKarbowiak
diff --git a/saleor/graphql/invoice/tests/test_invoice_create.py b/saleor/graphql/invoice/tests/test_invoice_create.py
index 2abb0223c9..78ac82e1bd 100644
--- a/saleor/graphql/invoice/tests/test_invoice_create.py
+++ b/saleor/graphql/invoice/tests/test_invoice_create.py
@@ -22,6 +22,9 @@ INVOICE_CREATE_MUTATION = """
status
number
url
+ order {
+ id
+ }
}
errors {
field
@@ -35,8 +38,9 @@ INVOICE_CREATE_MUTATION = """
def test_create_invoice(staff_api_client, permission_manage_orders, order):
number = "01/12/2020/TEST"
url = "http://www.example.com"
+ order_id = graphene.Node.to_global_id("Order", order.pk)
variables = {
- "orderId": graphene.Node.to_global_id("Order", order.pk),
+ "orderId": order_id,
"number": number,
"url": url,
}
@@ -45,6 +49,7 @@ def test_create_invoice(staff_api_client, permission_manage_orders, order):
)
content = get_graphql_content(response)
invoice = Invoice.objects.get(order=order, status=JobStatus.SUCCESS)
+ assert order_id == content["data"]["invoiceCreate"]["invoice"]["order"]["id"]
assert invoice.url == content["data"]["invoiceCreate"]["invoice"]["url"]
assert invoice.number == content["data"]["invoiceCreate"]["invoice"]["number"]
assert (
diff --git a/saleor/graphql/invoice/types.py b/saleor/graphql/invoice/types.py
index c79783cbd6..65e6afa4a9 100644
--- a/saleor/graphql/invoice/types.py
+++ b/saleor/graphql/invoice/types.py
@@ -1,8 +1,10 @@
import graphene
from ...invoice import models
+from ..core.descriptions import ADDED_IN_310
from ..core.types import Job, ModelObjectType
from ..meta.types import ObjectWithMetadata
+from ..order.dataloaders import OrderByIdLoader
class Invoice(ModelObjectType):
@@ -12,8 +14,16 @@ class Invoice(ModelObjectType):
updated_at = graphene.DateTime(required=True)
message = graphene.String()
url = graphene.String(description="URL to download an invoice.")
+ order = graphene.Field(
+ "saleor.graphql.order.types.Order",
+ description="Order related to the invoice." + ADDED_IN_310,
+ )
class Meta:
description = "Represents an Invoice."
interfaces = [ObjectWithMetadata, Job, graphene.relay.Node]
model = models.Invoice
+
+ @staticmethod
+ def resolve_order(root: models.Invoice, info):
+ return OrderByIdLoader(info.context).load(root.order_id)
diff --git a/saleor/graphql/query_cost_map.py b/saleor/graphql/query_cost_map.py
index 5cefe04f66..6fe0ae13cc 100644
--- a/saleor/graphql/query_cost_map.py
+++ b/saleor/graphql/query_cost_map.py
@@ -190,6 +190,9 @@ COST_MAP = {
"permissions": {"complexity": 1},
"users": {"complexity": 1},
},
+ "Invoice": {
+ "order": {"complexity": 1},
+ },
"Menu": {
"items": {"complexity": 1},
},
@@ -211,7 +214,7 @@ COST_MAP = {
"events": {"complexity": 1},
"fulfillments": {"complexity": 1},
"giftCards": {"complexity": 1},
- "invoices": {"complexity": 1},
+ "invoices": {"complexity": 10},
"lines": {"complexity": 1},
"payments": {"complexity": 1},
"shippingAddress": {"complexity": 1},
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 6939fdef52..3582f7b31d 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -9887,6 +9887,13 @@ type Invoice implements ObjectWithMetadata & Job & Node {
"""URL to download an invoice."""
url: String
+
+ """
+ Order related to the invoice.
+
+ Added in Saleor 3.10.
+ """
+ order: Order
}
interface Job {
@@ -24906,6 +24913,13 @@ type InvoiceRequested implements Event {
"""The invoice the event relates to."""
invoice: Invoice
+
+ """
+ Order related to the invoice.
+
+ Added in Saleor 3.10.
+ """
+ order: Order!
}
"""
@@ -24930,6 +24944,13 @@ type InvoiceDeleted implements Event {
"""The invoice the event relates to."""
invoice: Invoice
+
+ """
+ Order related to the invoice.
+
+ Added in Saleor 3.10.
+ """
+ order: Order
}
"""
@@ -24954,6 +24975,13 @@ type InvoiceSent implements Event {
"""The invoice the event relates to."""
invoice: Invoice
+
+ """
+ Order related to the invoice.
+
+ Added in Saleor 3.10.
+ """
+ order: Order
}
"""
diff --git a/saleor/graphql/tests/queries/fragments.py b/saleor/graphql/tests/queries/fragments.py
index 0cff870b85..620b6f9fc8 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -107,6 +107,18 @@ fragment InvoiceDetails on Invoice {
id
status
number
+ order {
+ id
+ }
+}
+"""
+
+INVOICE_ORDER_DETAILS = """
+fragment InvoiceOrderDetails on Order {
+ id
+ number
+ userEmail
+ isPaid
}
"""
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index da6865d8d0..e31d597b10 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -31,10 +31,12 @@ from ..core.descriptions import (
ADDED_IN_36,
ADDED_IN_37,
ADDED_IN_38,
+ ADDED_IN_310,
PREVIEW_FEATURE,
)
from ..core.scalars import PositiveDecimal
from ..core.types import NonNullList
+from ..order.dataloaders import OrderByIdLoader
from ..payment.enums import TransactionActionEnum
from ..payment.types import TransactionItem
from ..plugins.dataloaders import plugin_manager_promise_callback
@@ -790,14 +792,29 @@ class InvoiceBase(AbstractType):
"saleor.graphql.invoice.types.Invoice",
description="The invoice the event relates to.",
)
+ order = graphene.Field(
+ "saleor.graphql.order.types.Order",
+ description="Order related to the invoice." + ADDED_IN_310,
+ )
@staticmethod
def resolve_invoice(root, _info):
_, invoice = root
return invoice
+ @staticmethod
+ def resolve_order(root, _info):
+ _, invoice = root
+ return OrderByIdLoader(_info.context).load(invoice.order_id)
+
class InvoiceRequested(ObjectType, InvoiceBase):
+ order = graphene.Field(
+ "saleor.graphql.order.types.Order",
+ required=True,
+ description="Order related to the invoice." + ADDED_IN_310,
+ )
+
class Meta:
interfaces = (Event,)
description = (
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
index 04d6c56e31..98c7edebe1 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -233,13 +233,24 @@ def generate_permission_group_payload(group):
def generate_invoice_payload(invoice):
- return {
+ payload = {
"invoice": {
"id": graphene.Node.to_global_id("Invoice", invoice.pk),
"status": invoice.status.upper(),
"number": invoice.number,
+ "order": None,
}
}
+ if invoice.order_id:
+ order_id = graphene.Node.to_global_id("Order", invoice.order_id)
+ payload["invoice"]["order"] = {"id": order_id}
+ payload["order"] = {
+ "id": order_id,
+ "number": str(invoice.order.number),
+ "userEmail": invoice.order.user_email,
+ "isPaid": invoice.order.is_fully_paid(),
+ }
+ return payload
def generate_category_payload(category):
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 0e797a4e30..9ba670343b 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -944,6 +944,7 @@ SALE_TOGGLE = (
INVOICE_REQUESTED = (
fragments.INVOICE_DETAILS
+ + fragments.INVOICE_ORDER_DETAILS
+ """
subscription{
event{
@@ -951,6 +952,9 @@ INVOICE_REQUESTED = (
invoice{
...InvoiceDetails
}
+ order {
+ ...InvoiceOrderDetails
+ }
}
}
}
@@ -959,6 +963,7 @@ INVOICE_REQUESTED = (
INVOICE_DELETED = (
fragments.INVOICE_DETAILS
+ + fragments.INVOICE_ORDER_DETAILS
+ """
subscription{
event{
@@ -966,6 +971,9 @@ INVOICE_DELETED = (
invoice{
...InvoiceDetails
}
+ order {
+ ...InvoiceOrderDetails
+ }
}
}
}
@@ -974,6 +982,7 @@ INVOICE_DELETED = (
INVOICE_SENT = (
fragments.INVOICE_DETAILS
+ + fragments.INVOICE_ORDER_DETAILS
+ """
subscription{
event{
@@ -981,6 +990,9 @@ INVOICE_SENT = (
invoice{
...InvoiceDetails
}
+ order {
+ ...InvoiceOrderDetails
+ }
}
}
}