Skip to content

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

Unify channel ID params (#7378)

Iga Karbowiakbd154e57bf17
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21c94b90f9..3b36a8f80a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -102,6 +102,12 @@ All notable, unreleased changes to this project will be documented in this file.
- Trigger `checkout_updated` hook for checkout meta mutations - #7392 by @maarcingebala
- Optimize `inputType` resolver on `AttributeValue` type - 7396 by @tomaszszymanski129
- Allow filtering pages by ids - #7393 by @IKarbowiak
+- Fix invoice generation - #7376 by tomaszszymanski129
+- Unify channel ID params #7378
+ - targetChannel from ChannelDeleteInput changed to channelId
+ - `channel` from `DraftOrderCreateInput` changed to channelId
+ - `channel` from `DraftOrderInput` changed to channelId
+ - `channel` from `pluginUpdate` changed to channelId
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/channel/error_codes.py b/saleor/channel/error_codes.py
index 7594c45ec9..704b9d84e3 100644
--- a/saleor/channel/error_codes.py
+++ b/saleor/channel/error_codes.py
@@ -8,7 +8,6 @@ class ChannelErrorCode(Enum):
NOT_FOUND = "not_found"
REQUIRED = "required"
UNIQUE = "unique"
- CHANNEL_TARGET_ID_MUST_BE_DIFFERENT = "channel_target_id_must_be_different"
CHANNELS_CURRENCY_MUST_BE_THE_SAME = "channels_currency_must_be_the_same"
CHANNEL_WITH_ORDERS = "channel_with_orders"
DUPLICATED_INPUT_ITEM = "duplicated_input_item"
diff --git a/saleor/graphql/channel/mutations.py b/saleor/graphql/channel/mutations.py
index ced6ce23f7..b75b2f3157 100644
--- a/saleor/graphql/channel/mutations.py
+++ b/saleor/graphql/channel/mutations.py
@@ -139,7 +139,7 @@ class ChannelUpdate(ModelMutation):
class ChannelDeleteInput(graphene.InputObjectType):
- target_channel = graphene.ID(
+ channel_id = graphene.ID(
required=True,
description="ID of channel to migrate orders from origin channel.",
)
@@ -166,10 +166,9 @@ class ChannelDelete(ModelDeleteMutation):
if origin_channel.id == target_channel.id:
raise ValidationError(
{
- "target_channel": ValidationError(
- "channelID and targetChannelID cannot be the same. "
- "Use different target channel ID.",
- code=ChannelErrorCode.CHANNEL_TARGET_ID_MUST_BE_DIFFERENT,
+ "channel_id": ValidationError(
+ "Cannot migrate data to the channel that is being removed.",
+ code=ChannelErrorCode.INVALID,
)
}
)
@@ -178,7 +177,7 @@ class ChannelDelete(ModelDeleteMutation):
if origin_channel_currency != target_channel_currency:
raise ValidationError(
{
- "target_channel": ValidationError(
+ "channel_id": ValidationError(
f"Cannot migrate from {origin_channel_currency} "
f"to {target_channel_currency}. "
"Migration are allowed between the same currency",
@@ -225,7 +224,7 @@ class ChannelDelete(ModelDeleteMutation):
@classmethod
def perform_mutation(cls, _root, info, **data):
origin_channel = cls.get_node_or_error(info, data["id"], only_type=Channel)
- target_channel_global_id = data.get("input", {}).get("target_channel")
+ target_channel_global_id = data.get("input", {}).get("channel_id")
if target_channel_global_id:
target_channel = cls.get_node_or_error(
info, target_channel_global_id, only_type=Channel
diff --git a/saleor/graphql/channel/tests/test_channel_delete_mutations.py b/saleor/graphql/channel/tests/test_channel_delete_mutations.py
index f73fd69cc5..da96efad20 100644
--- a/saleor/graphql/channel/tests/test_channel_delete_mutations.py
+++ b/saleor/graphql/channel/tests/test_channel_delete_mutations.py
@@ -41,7 +41,7 @@ def test_channel_delete_mutation_as_staff_user(
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
channel_target_id = graphene.Node.to_global_id("Channel", other_channel_USD.id)
- variables = {"id": channel_id, "input": {"targetChannel": channel_target_id}}
+ variables = {"id": channel_id, "input": {"channelId": channel_target_id}}
assert Checkout.objects.first() is not None
# when
response = staff_api_client.post_graphql(
@@ -62,7 +62,7 @@ def test_channel_delete_mutation_with_the_same_channel_and_target_channel_id(
):
# given
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
- variables = {"id": channel_id, "input": {"targetChannel": channel_id}}
+ variables = {"id": channel_id, "input": {"channelId": channel_id}}
# when
response = staff_api_client.post_graphql(
@@ -73,8 +73,8 @@ def test_channel_delete_mutation_with_the_same_channel_and_target_channel_id(
content = get_graphql_content(response)
error = content["data"]["channelDelete"]["errors"][0]
- assert error["field"] == "targetChannel"
- assert error["code"] == ChannelErrorCode.CHANNEL_TARGET_ID_MUST_BE_DIFFERENT.name
+ assert error["field"] == "channelId"
+ assert error["code"] == ChannelErrorCode.INVALID.name
def test_channel_delete_mutation_without_migration_channel_with_orders(
@@ -139,7 +139,7 @@ def test_channel_delete_mutation_with_different_currency(
# given
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
target_channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
- variables = {"id": channel_id, "input": {"targetChannel": target_channel_id}}
+ variables = {"id": channel_id, "input": {"channelId": target_channel_id}}
# when
response = staff_api_client.post_graphql(
@@ -150,7 +150,7 @@ def test_channel_delete_mutation_with_different_currency(
content = get_graphql_content(response)
error = content["data"]["channelDelete"]["errors"][0]
- assert error["field"] == "targetChannel"
+ assert error["field"] == "channelId"
assert error["code"] == ChannelErrorCode.CHANNELS_CURRENCY_MUST_BE_THE_SAME.name
@@ -168,7 +168,7 @@ def test_channel_delete_mutation_as_app(
order.save()
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
channel_target_id = graphene.Node.to_global_id("Channel", other_channel_USD.id)
- variables = {"id": channel_id, "input": {"targetChannel": channel_target_id}}
+ variables = {"id": channel_id, "input": {"channelId": channel_target_id}}
assert Checkout.objects.first() is not None
# when
@@ -193,7 +193,7 @@ def test_channel_delete_mutation_as_customer(
# given
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
channel_target_id = graphene.Node.to_global_id("Channel", other_channel_USD.id)
- variables = {"id": channel_id, "input": {"targetChannel": channel_target_id}}
+ variables = {"id": channel_id, "input": {"channelId": channel_target_id}}
# when
response = user_api_client.post_graphql(
@@ -213,7 +213,7 @@ def test_channel_delete_mutation_as_anonymous(
# given
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
channel_target_id = graphene.Node.to_global_id("Channel", other_channel_USD.id)
- variables = {"id": channel_id, "input": {"targetChannel": channel_target_id}}
+ variables = {"id": channel_id, "input": {"channelId": channel_target_id}}
# when
response = api_client.post_graphql(
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 7935bdf59e..156cc30ecf 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -64,9 +64,7 @@ class DraftOrderInput(InputObjectType):
customer_note = graphene.String(
description="A note from a customer. Visible by customers in the order summary."
)
- channel = graphene.ID(
- description="ID of the channel associated with the order.", name="channel"
- )
+ channel_id = graphene.ID(description="ID of the channel associated with the order.")
redirect_url = graphene.String(
required=False,
description=(
@@ -99,16 +97,60 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
error_type_field = "order_errors"
@classmethod
- def clean_channel_id(cls, instance, channel_id):
- if channel_id and hasattr(instance, "channel"):
- raise ValidationError(
- {
- "channel": ValidationError(
- "Can't update existing order channel id.",
- code=OrderErrorCode.NOT_EDITABLE.value,
- )
- }
- )
+ def clean_input(cls, info, instance, data):
+ shipping_address = data.pop("shipping_address", None)
+ billing_address = data.pop("billing_address", None)
+ redirect_url = data.pop("redirect_url", None)
+ channel_id = data.pop("channel_id", None)
+
+ cleaned_input = super().clean_input(info, instance, data)
+
+ channel = cls.clean_channel_id(info, instance, cleaned_input, channel_id)
+
+ voucher = cleaned_input.get("voucher", None)
+ if voucher:
+ cls.clean_voucher(voucher, channel)
+
+ if channel:
+ cleaned_input["currency"] = channel.currency_code
+
+ lines = data.pop("lines", None)
+ cls.clean_lines(cleaned_input, lines, channel)
+
+ cleaned_input["status"] = OrderStatus.DRAFT
+ cleaned_input["origin"] = OrderOrigin.DRAFT
+ display_gross_prices = info.context.site.settings.display_gross_prices
+ cleaned_input["display_gross_prices"] = display_gross_prices
+
+ cls.clean_addresses(
+ info, instance, cleaned_input, shipping_address, billing_address
+ )
+
+ if redirect_url:
+ cls.clean_redirect_url(redirect_url)
+ cleaned_input["redirect_url"] = redirect_url
+
+ return cleaned_input
+
+ @classmethod
+ def clean_channel_id(cls, info, instance, cleaned_input, channel_id):
+ if channel_id:
+ if hasattr(instance, "channel"):
+ raise ValidationError(
+ {
+ "channel_id": ValidationError(
+ "Can't update existing order channel id.",
+ code=OrderErrorCode.NOT_EDITABLE.value,
+ )
+ }
+ )
+ else:
+ channel = cls.get_node_or_error(info, channel_id, only_type=Channel)
+ cleaned_input["channel"] = channel
+ return channel
+
+ else:
+ return instance.channel if hasattr(instance, "channel") else None
@classmethod
def clean_voucher(cls, voucher, channel):
@@ -123,35 +165,7 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
)
@classmethod
- def clean_redirect_url(cls, redirect_url):
- try:
- validate_storefront_url(redirect_url)
- except ValidationError as error:
- error.code = OrderErrorCode.INVALID.value
- raise ValidationError({"redirect_url": error})
-
- @classmethod
- def clean_input(cls, info, instance, data):
- shipping_address = data.pop("shipping_address", None)
- redirect_url = data.pop("redirect_url", None)
- billing_address = data.pop("billing_address", None)
- cleaned_input = super().clean_input(info, instance, data)
- lines = data.pop("lines", None)
- channel_id = data.get("channel", None)
- if "channel" in cleaned_input and channel_id is None:
- del cleaned_input["channel"]
- cls.clean_channel_id(instance, channel_id)
- voucher = cleaned_input.get("voucher", None)
- if voucher:
- channel = cleaned_input.get("channel") or instance.channel
- cls.clean_voucher(voucher, channel)
-
- channel = instance.channel if hasattr(instance, "channel") else None
- if not channel and channel_id:
- channel = cls.get_node_or_error(info, channel_id, only_type=Channel)
- if channel:
- cleaned_input["currency"] = channel.currency_code
-
+ def clean_lines(cls, cleaned_input, lines, channel):
if lines:
variant_ids = [line.get("variant_id") for line in lines]
variants = cls.get_nodes_or_error(variant_ids, "variants", ProductVariant)
@@ -170,11 +184,10 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
cleaned_input["variants"] = variants
cleaned_input["quantities"] = quantities
- cleaned_input["status"] = OrderStatus.DRAFT
- cleaned_input["origin"] = OrderOrigin.DRAFT
- display_gross_prices = info.context.site.settings.display_gross_prices
- cleaned_input["display_gross_prices"] = display_gross_prices
-
+ @classmethod
+ def clean_addresses(
+ cls, info, instance, cleaned_input, shipping_address, billing_address
+ ):
if shipping_address:
shipping_address = cls.validate_address(
shipping_address,
@@ -197,11 +210,14 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
billing_address, "billing", user=instance
)
cleaned_input["billing_address"] = billing_address
- if redirect_url:
- cls.clean_redirect_url(redirect_url)
- cleaned_input["redirect_url"] = redirect_url
- return cleaned_input
+ @classmethod
+ def clean_redirect_url(cls, redirect_url):
+ try:
+ validate_storefront_url(redirect_url)
+ except ValidationError as error:
+ error.code = OrderErrorCode.INVALID.value
+ raise ValidationError({"redirect_url": error})
@staticmethod
def _save_addresses(info, instance: models.Order, cleaned_input):
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 9e5292a27b..7b0a928dfd 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -1266,7 +1266,7 @@ DRAFT_ORDER_CREATE_MUTATION = """
lines: $lines, shippingAddress: $shippingAddress,
billingAddress: $billingAddress,
shippingMethod: $shippingMethod, voucher: $voucher,
- channel: $channel,
+ channelId: $channel,
redirectUrl: $redirectUrl,
customerNote: $customerNote}) {
errors {
@@ -2111,7 +2111,7 @@ DRAFT_UPDATE_QUERY = """
input: {
voucher: $voucher,
customerNote: $customerNote
- channel: $channel
+ channelId: $channel
}) {
errors {
field
@@ -2150,7 +2150,7 @@ def test_draft_order_update_existing_channel_id(
error = content["data"]["draftOrderUpdate"]["errors"][0]
assert error["code"] == OrderErrorCode.NOT_EDITABLE.name
- assert error["field"] == "channel"
+ assert error["field"] == "channelId"
def test_draft_order_update_voucher_not_available(
diff --git a/saleor/graphql/plugins/mutations.py b/saleor/graphql/plugins/mutations.py
index 524fcda012..14775b546e 100644
--- a/saleor/graphql/plugins/mutations.py
+++ b/saleor/graphql/plugins/mutations.py
@@ -33,7 +33,7 @@ class PluginUpdate(BaseMutation):
class Arguments:
id = graphene.ID(required=True, description="ID of plugin to update.")
- channel = graphene.ID(
+ channel_id = graphene.ID(
required=False,
description="ID of a channel for which the data should be modified.",
)
@@ -51,7 +51,7 @@ class PluginUpdate(BaseMutation):
@classmethod
def clean_input(cls, info, data):
plugin_id = data.get("id")
- channel_id = data.get("channel")
+ channel_id = data.get("channel_id")
channel = None
if channel_id:
channel = cls.get_node_or_error(info, channel_id, only_type=Channel)
diff --git a/saleor/graphql/plugins/tests/test_mutation_plugin_update.py b/saleor/graphql/plugins/tests/test_mutation_plugin_update.py
index c80a8e2787..789975ef04 100644
--- a/saleor/graphql/plugins/tests/test_mutation_plugin_update.py
+++ b/saleor/graphql/plugins/tests/test_mutation_plugin_update.py
@@ -19,7 +19,7 @@ PLUGIN_UPDATE_MUTATION = """
) {
pluginUpdate(
id: $id
- channel: $channel
+ channelId: $channel
input: { active: $active, configuration: $configuration }
) {
plugin {
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index c9632e22ab..b88a65f26c 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -848,7 +848,7 @@ type ChannelDelete {
}
input ChannelDeleteInput {
- targetChannel: ID!
+ channelId: ID!
}
type ChannelError {
@@ -865,7 +865,6 @@ enum ChannelErrorCode {
NOT_FOUND
REQUIRED
UNIQUE
- CHANNEL_TARGET_ID_MUST_BE_DIFFERENT
CHANNELS_CURRENCY_MUST_BE_THE_SAME
CHANNEL_WITH_ORDERS
DUPLICATED_INPUT_ITEM
@@ -1842,7 +1841,7 @@ input DraftOrderCreateInput {
shippingMethod: ID
voucher: ID
customerNote: String
- channel: ID
+ channelId: ID
redirectUrl: String
lines: [OrderLineCreateInput]
}
@@ -1862,7 +1861,7 @@ input DraftOrderInput {
shippingMethod: ID
voucher: ID
customerNote: String
- channel: ID
+ channelId: ID
redirectUrl: String
}
@@ -2823,7 +2822,7 @@ type Mutation {
giftCardCreate(input: GiftCardCreateInput!): GiftCardCreate
giftCardDeactivate(id: ID!): GiftCardDeactivate
giftCardUpdate(id: ID!, input: GiftCardUpdateInput!): GiftCardUpdate
- pluginUpdate(channel: ID, id: ID!, input: PluginUpdateInput!): PluginUpdate
+ pluginUpdate(channelId: ID, id: ID!, input: PluginUpdateInput!): PluginUpdate
saleCreate(input: SaleInput!): SaleCreate
saleDelete(id: ID!): SaleDelete
saleBulkDelete(ids: [ID]!): SaleBulkDelete