Skip to content

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

Use denormalized quantity allocated (#16982)

Filip Owczarekab4ce9e1155b
diff --git a/saleor/graphql/warehouse/tests/queries/test_stock.py b/saleor/graphql/warehouse/tests/queries/test_stock.py
index dbb0db7da3..4b947cd584 100644
--- a/saleor/graphql/warehouse/tests/queries/test_stock.py
+++ b/saleor/graphql/warehouse/tests/queries/test_stock.py
@@ -64,6 +64,26 @@ def test_query_stock(staff_api_client, stock, permission_manage_products):
assert content_stock["quantityReserved"] == 0
+def test_query_stock_use_denormalized_quantity_allocated(
+ staff_api_client, stock, permission_manage_products
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ stock_id = graphene.Node.to_global_id("Stock", stock.pk)
+ expected_quantity_allocated = 10
+ stock.quantity_allocated = expected_quantity_allocated
+ stock.save(update_fields=["quantity_allocated"])
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_STOCK, variables={"id": stock_id})
+
+ # then
+ content = get_graphql_content(response)
+ content_stock = content["data"]["stock"]
+ assert content_stock["quantityAllocated"] == expected_quantity_allocated
+ assert stock.allocations.count() == 0
+
+
def test_query_stock_with_reservations(
site_settings_with_reservations,
staff_api_client,
diff --git a/saleor/graphql/warehouse/types.py b/saleor/graphql/warehouse/types.py
index 316ed8f156..9c3884bee3 100644
--- a/saleor/graphql/warehouse/types.py
+++ b/saleor/graphql/warehouse/types.py
@@ -234,11 +234,7 @@ class Stock(ModelObjectType[models.Stock]):
@staticmethod
def resolve_quantity_allocated(root, info: ResolveInfo):
- return root.allocations.using(
- get_database_connection_name(info.context)
- ).aggregate(quantity_allocated=Coalesce(Sum("quantity_allocated"), 0))[
- "quantity_allocated"
- ]
+ return root.quantity_allocated
@staticmethod
@load_site_callback
diff --git a/saleor/order/tests/fixtures/order.py b/saleor/order/tests/fixtures/order.py
index dfb8cd0c70..00df0e736a 100644
--- a/saleor/order/tests/fixtures/order.py
+++ b/saleor/order/tests/fixtures/order.py
@@ -292,13 +292,16 @@ def order_with_lines(
cost_price_amount=Decimal(1),
currency=channel_USD.currency_code,
)
+ quantity = 3
stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
+ warehouse=warehouse,
+ product_variant=variant,
+ quantity=5,
+ quantity_allocated=quantity,
)
base_price = variant.get_price(channel_listing)
currency = base_price.currency
gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
- quantity = 3
unit_price = TaxedMoney(net=base_price, gross=gross)
line = order.lines.create(
product_name=str(variant.product),
@@ -345,8 +348,12 @@ def order_with_lines(
cost_price_amount=Decimal(2),
currency=channel_USD.currency_code,
)
+ quantity = 2
stock = Stock.objects.create(
- product_variant=variant, warehouse=warehouse, quantity=2
+ product_variant=variant,
+ warehouse=warehouse,
+ quantity=2,
+ quantity_allocated=quantity,
)
stock.refresh_from_db()
@@ -354,7 +361,6 @@ def order_with_lines(
currency = base_price.currency
gross = Money(amount=base_price.amount * Decimal(1.23), currency=currency)
unit_price = TaxedMoney(net=base_price, gross=gross)
- quantity = 2
line = order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),