Skip to content

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

Update preorders with C&C features (#8683)

mstrumeck4c531bc4c71a
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.py
index e1732a31c4..5abcc39393 100644
--- a/saleor/core/management/commands/populatedb.py
+++ b/saleor/core/management/commands/populatedb.py
@@ -10,6 +10,7 @@ from ....account.utils import create_superuser
from ...utils.random_data import (
add_address_to_admin,
create_channels,
+ create_checkout_with_preorders,
create_gift_card,
create_menus,
create_orders,
@@ -111,6 +112,8 @@ class Command(BaseCommand):
self.stdout.write(msg)
for msg in create_menus():
self.stdout.write(msg)
+ for msg in create_checkout_with_preorders():
+ self.stdout.write(msg)
if options["createsuperuser"]:
credentials = {
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 23ce7919d7..4a6515c15b 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -1,3 +1,4 @@
+import datetime
import itertools
import json
import os
@@ -40,6 +41,9 @@ from ...attribute.models import (
)
from ...channel.models import Channel
from ...checkout import AddressType
+from ...checkout.fetch import fetch_checkout_info
+from ...checkout.models import Checkout
+from ...checkout.utils import add_variant_to_checkout
from ...core.permissions import (
AccountPermissions,
CheckoutPermissions,
@@ -1641,3 +1645,23 @@ def get_product_list_images_dir(placeholder_dir):
def get_image(image_dir, image_name):
img_path = os.path.join(image_dir, image_name)
return File(open(img_path, "rb"), name=image_name)
+
+
+def create_checkout_with_preorders():
+ channel = Channel.objects.get(currency_code="USD")
+ checkout = Checkout.objects.create(currency=channel.currency_code, channel=channel)
+ checkout.set_country(channel.default_country, commit=True)
+ checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
+ for product_variant in ProductVariant.objects.all()[:2]:
+ product_variant.is_preorder = True
+ product_variant.preorder_global_threshold = 10
+ product_variant.preorder_end_date = timezone.now() + datetime.timedelta(days=10)
+ product_variant.save(
+ update_fields=[
+ "is_preorder",
+ "preorder_global_threshold",
+ "preorder_end_date",
+ ]
+ )
+ add_variant_to_checkout(checkout_info, product_variant, 2)
+ yield f"Created checkout with two preorders. Checkout token: {checkout.token}"
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 914f00c961..8382220871 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -44,7 +44,7 @@ from ....product.models import ProductChannelListing, ProductVariant
from ....shipping import models as shipping_models
from ....shipping.models import ShippingMethodTranslation
from ....shipping.utils import convert_to_shipping_method_data
-from ....warehouse.models import PreorderReservation, Reservation, Stock
+from ....warehouse.models import PreorderReservation, Reservation, Stock, Warehouse
from ...tests.utils import assert_no_permission, get_graphql_content
from ..mutations import (
clean_delivery_method,
@@ -1768,6 +1768,53 @@ query getCheckout($token: UUID!) {
}
"""
+QUERY_GET_ALL_COLLECTION_POINTS_FROM_CHECKOUT = """
+query AvailableCollectionPoints($token: UUID!) {
+ checkout(token: $token) {
+ availableCollectionPoints {
+ name
+ }
+ }
+}
+"""
+
+
+def test_available_collection_points_for_preorders_variants_in_checkout(
+ api_client, staff_api_client, checkout_with_preorders_only
+):
+
+ expected_collection_points = list(
+ Warehouse.objects.for_country("US").values("name")
+ )
+ response = staff_api_client.post_graphql(
+ QUERY_GET_ALL_COLLECTION_POINTS_FROM_CHECKOUT,
+ variables={"token": checkout_with_preorders_only.token},
+ )
+ response_content = get_graphql_content(response)
+ assert (
+ expected_collection_points
+ == response_content["data"]["checkout"]["availableCollectionPoints"]
+ )
+
+
+def test_available_collection_points_for_preorders_and_regular_variants_in_checkout(
+ api_client,
+ staff_api_client,
+ checkout_with_preorders_and_regular_variant,
+ warehouses_for_cc,
+):
+
+ expected_collection_points = [{"name": warehouses_for_cc[1].name}]
+ response = staff_api_client.post_graphql(
+ QUERY_GET_ALL_COLLECTION_POINTS_FROM_CHECKOUT,
+ variables={"token": checkout_with_preorders_and_regular_variant.token},
+ )
+ response_content = get_graphql_content(response)
+ assert (
+ expected_collection_points
+ == response_content["data"]["checkout"]["availableCollectionPoints"]
+ )
+
def test_checkout_available_collection_points_with_lines_avail_in_1_local_and_1_all(
api_client, checkout_with_items_for_cc, stocks_for_cc
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index aa6fed2edf..a898c2c272 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -1,6 +1,7 @@
from decimal import Decimal
from unittest.mock import MagicMock, Mock, patch
+import graphene
import pytest
from prices import Money, TaxedMoney
@@ -14,11 +15,12 @@ from ...discount.models import (
VoucherType,
)
from ...discount.utils import validate_voucher_in_order
+from ...graphql.tests.utils import get_graphql_content
from ...payment import ChargeStatus
from ...payment.models import Payment
from ...plugins.manager import get_plugins_manager
from ...product.models import Collection
-from ...warehouse.models import Stock
+from ...warehouse.models import Stock, Warehouse
from ...warehouse.tests.utils import get_quantity_allocated_for_stock
from .. import FulfillmentStatus, OrderEvents, OrderStatus
from ..events import (
@@ -1250,3 +1252,59 @@ def test_email_sent_event_without_user_and_app_pk(
"email": order.get_customer_email(),
"email_type": expected_event_type,
}
+
+
+GET_ORDER_AVAILABLE_COLLECTION_POINTS = """
+ query getAvailableCollectionPointsForOrder(
+ $id: ID!
+ ){
+ order(id:$id){
+ availableCollectionPoints{
+ name
+ }
+ }
+ }
+"""
+
+
+def test_available_collection_points_for_preorders_variants_in_order(
+ api_client, staff_api_client, order_with_preorder_lines, permission_manage_orders
+):
+ expected_collection_points = list(
+ Warehouse.objects.for_country("US").values("name")
+ )
+ response = staff_api_client.post_graphql(
+ GET_ORDER_AVAILABLE_COLLECTION_POINTS,
+ variables={
+ "id": graphene.Node.to_global_id("Order", order_with_preorder_lines.id)
+ },
+ permissions=[permission_manage_orders],
+ )
+ response_content = get_graphql_content(response)
+ assert (
+ expected_collection_points
+ == response_content["data"]["order"]["availableCollectionPoints"]
+ )
+
+
+def test_available_collection_points_for_preorders_and_regular_variants_in_order(
+ api_client,
+ staff_api_client,
+ order_with_preorder_lines,
+ permission_manage_orders,
+ warehouse,
+):
+
+ expected_collection_points = [{"name": warehouse.name}]
+ response = staff_api_client.post_graphql(
+ GET_ORDER_AVAILABLE_COLLECTION_POINTS,
+ variables={
+ "id": graphene.Node.to_global_id("Order", order_with_preorder_lines.id)
+ },
+ permissions=[permission_manage_orders],
+ )
+ response_content = get_graphql_content(response)
+ assert (
+ expected_collection_points
+ == response_content["data"]["order"]["availableCollectionPoints"]
+ )
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index df1cd81a67..a4afceda50 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -37,7 +37,7 @@ from ..attribute.models import (
AttributeValueTranslation,
)
from ..attribute.utils import associate_attribute_values_to_instance
-from ..checkout.fetch import fetch_checkout_info
+from ..checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ..checkout.models import Checkout, CheckoutLine
from ..checkout.utils import add_variant_to_checkout
from ..core import JobStatus, TimePeriodType
@@ -463,6 +463,35 @@ def checkout_with_gift_card(checkout_with_item, gift_card):
return checkout_with_item
+def checkout_with_preorders_only(
+ checkout,
+ stocks_for_cc,
+ preorder_variant_with_end_date,
+ preorder_variant_channel_threshold,
+):
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 2)
+
+ checkout.save()
+ return checkout
+
+
+def checkout_with_preorders_and_regular_variant(
+ checkout, stocks_for_cc, preorder_variant_with_end_date, product_variant_list
+):
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
+ add_variant_to_checkout(checkout_info, product_variant_list[0], 2)
+
+ checkout.save()
+ return checkout
+
+
@pytest.fixture
def checkout_with_gift_card_items(
checkout, non_shippable_gift_card_product, shippable_gift_card_product
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index 9b3ef16e2d..d417e67eeb 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -56,6 +56,12 @@ class WarehouseQueryset(models.QuerySet):
a single warehouse.
"""
+ if all(
+ line.variant.is_preorder_active()
+ for line in lines_qs.select_related("variant").only("variant_id")
+ ):
+ return self.for_country(country)
+
lines_quantity = (
lines_qs.filter(variant_id=OuterRef("product_variant_id"))
.annotate(prod_sum=Sum("quantity"))