Skip to content

Commit

Permalink
Prepare for moving orderSettings to Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
kadewu committed Jan 11, 2023
1 parent e56dd1b commit e5ef72c
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 7 deletions.
23 changes: 23 additions & 0 deletions saleor/channel/migrations/0006_order_settings_fields.py
@@ -0,0 +1,23 @@
# Generated by Django 3.2.16 on 2022-12-08 09:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("channel", "0005_channel_allocation_strategy"),
]

operations = [
migrations.AddField(
model_name="channel",
name="automatically_confirm_all_new_orders",
field=models.BooleanField(default=True, null=True),
),
migrations.AddField(
model_name="channel",
name="automatically_fulfill_non_shippable_gift_card",
field=models.BooleanField(default=True, null=True),
),
]
29 changes: 29 additions & 0 deletions saleor/channel/migrations/0007_order_settings_per_channel.py
@@ -0,0 +1,29 @@
from django.db import migrations


def set_order_settings(apps, schema_editor):
SiteSettings = apps.get_model("site", "SiteSettings")
Channel = apps.get_model("channel", "Channel")

site_settings = SiteSettings.objects.first()

Channel.objects.update(
automatically_confirm_all_new_orders=(
site_settings.automatically_confirm_all_new_orders
),
automatically_fulfill_non_shippable_gift_card=(
site_settings.automatically_fulfill_non_shippable_gift_card
),
)


class Migration(migrations.Migration):

dependencies = [
("channel", "0006_order_settings_fields"),
("site", "0032_gift_card_settings"),
]

operations = [
migrations.RunPython(set_order_settings, migrations.RunPython.noop),
]
5 changes: 5 additions & 0 deletions saleor/channel/models.py
Expand Up @@ -17,6 +17,11 @@ class Channel(models.Model):
choices=AllocationStrategy.CHOICES,
default=AllocationStrategy.PRIORITIZE_SORTING_ORDER,
)
automatically_confirm_all_new_orders = models.BooleanField(default=True, null=True)
automatically_fulfill_non_shippable_gift_card = models.BooleanField(
default=True,
null=True,
)

class Meta:
ordering = ("slug",)
Expand Down
8 changes: 5 additions & 3 deletions saleor/graphql/shop/mutations.py
Expand Up @@ -2,6 +2,7 @@
from django.core.exceptions import ValidationError

from ...account import models as account_models
from ...channel import models as channel_models
from ...core.error_codes import ShopErrorCode
from ...core.permissions import GiftcardPermissions, OrderPermissions, SitePermissions
from ...core.utils.url import validate_storefront_url
Expand Down Expand Up @@ -399,15 +400,16 @@ def perform_mutation(cls, _root, info, **data):
]
site = get_site_promise(info.context).get()
instance = site.settings
update_fields = []
update_fields = {}
for field in FIELDS:
value = data["input"].get(field)
if value is not None:
setattr(instance, field, value)
update_fields.append(field)
update_fields[field] = value

if update_fields:
instance.save(update_fields=update_fields)
instance.save(update_fields=update_fields.keys())
channel_models.Channel.objects.update(**update_fields)
return OrderSettingsUpdate(order_settings=instance)


Expand Down
52 changes: 48 additions & 4 deletions saleor/graphql/shop/tests/test_shop.py
Expand Up @@ -1339,71 +1339,115 @@ def test_staff_notification_update_mutation_with_empty_email(


def test_order_settings_update_by_staff(
staff_api_client, permission_manage_orders, site_settings
staff_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN
):
# given
assert site_settings.automatically_confirm_all_new_orders is True
staff_api_client.user.user_permissions.add(permission_manage_orders)

# when
response = staff_api_client.post_graphql(
ORDER_SETTINGS_UPDATE_MUTATION,
{"confirmOrders": False, "fulfillGiftCards": False},
)
content = get_graphql_content(response)

# then
response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"]
assert response_settings["automaticallyConfirmAllNewOrders"] is False
assert response_settings["automaticallyFulfillNonShippableGiftCard"] is False
site_settings.refresh_from_db()
assert site_settings.automatically_confirm_all_new_orders is False
assert site_settings.automatically_fulfill_non_shippable_gift_card is False
channel_PLN.refresh_from_db()
channel_USD.refresh_from_db()
assert channel_PLN.automatically_confirm_all_new_orders is False
assert channel_PLN.automatically_fulfill_non_shippable_gift_card is False
assert channel_USD.automatically_confirm_all_new_orders is False
assert channel_USD.automatically_fulfill_non_shippable_gift_card is False


def test_order_settings_update_by_staff_nothing_changed(
staff_api_client, permission_manage_orders, site_settings
staff_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN
):
# given
assert site_settings.automatically_confirm_all_new_orders is True
staff_api_client.user.user_permissions.add(permission_manage_orders)

# when
response = staff_api_client.post_graphql(
ORDER_SETTINGS_UPDATE_MUTATION,
{},
)
content = get_graphql_content(response)

# then
response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"]
assert response_settings["automaticallyConfirmAllNewOrders"] is True
assert response_settings["automaticallyFulfillNonShippableGiftCard"] is True
site_settings.refresh_from_db()
assert site_settings.automatically_confirm_all_new_orders is True
assert site_settings.automatically_fulfill_non_shippable_gift_card is True
channel_PLN.refresh_from_db()
channel_USD.refresh_from_db()
assert channel_PLN.automatically_confirm_all_new_orders is True
assert channel_PLN.automatically_fulfill_non_shippable_gift_card is True
assert channel_USD.automatically_confirm_all_new_orders is True
assert channel_USD.automatically_fulfill_non_shippable_gift_card is True


def test_order_settings_update_by_app(
app_api_client, permission_manage_orders, site_settings
app_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN
):
# given
assert site_settings.automatically_confirm_all_new_orders is True
app_api_client.app.permissions.set([permission_manage_orders])

# when
response = app_api_client.post_graphql(
ORDER_SETTINGS_UPDATE_MUTATION,
{"confirmOrders": False, "fulfillGiftCards": False},
)
content = get_graphql_content(response)

# then
response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"]
assert response_settings["automaticallyConfirmAllNewOrders"] is False
assert response_settings["automaticallyFulfillNonShippableGiftCard"] is False
site_settings.refresh_from_db()
assert site_settings.automatically_confirm_all_new_orders is False
assert site_settings.automatically_fulfill_non_shippable_gift_card is False
channel_PLN.refresh_from_db()
channel_USD.refresh_from_db()
assert channel_PLN.automatically_confirm_all_new_orders is False
assert channel_PLN.automatically_fulfill_non_shippable_gift_card is False
assert channel_USD.automatically_confirm_all_new_orders is False
assert channel_USD.automatically_fulfill_non_shippable_gift_card is False


def test_order_settings_update_by_user_without_permissions(
user_api_client, permission_manage_orders, site_settings
user_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN
):
# given
assert site_settings.automatically_confirm_all_new_orders is True

# when
response = user_api_client.post_graphql(
ORDER_SETTINGS_UPDATE_MUTATION,
{"confirmOrders": False, "fulfillGiftCards": False},
)

# then
assert_no_permission(response)
site_settings.refresh_from_db()
assert site_settings.automatically_confirm_all_new_orders is True
assert site_settings.automatically_fulfill_non_shippable_gift_card is True
channel_PLN.refresh_from_db()
channel_USD.refresh_from_db()
assert channel_PLN.automatically_confirm_all_new_orders is True
assert channel_PLN.automatically_fulfill_non_shippable_gift_card is True
assert channel_USD.automatically_confirm_all_new_orders is True
assert channel_USD.automatically_fulfill_non_shippable_gift_card is True


ORDER_SETTINGS_QUERY = """
Expand Down

0 comments on commit e5ef72c

Please sign in to comment.