Skip to content

Commit

Permalink
Do not allow deleting gifts from order.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedzior committed Jan 26, 2024
1 parent 0bdce0d commit f362bd1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions saleor/graphql/order/mutations/order_line_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ....core.taxes import zero_taxed_money
from ....core.tracing import traced_atomic_transaction
from ....order import events
from ....order.error_codes import OrderErrorCode
from ....order.fetch import OrderLineInfo
from ....order.search import update_order_search_vector
from ....order.utils import (
Expand All @@ -16,6 +17,7 @@
from ...core.doc_category import DOC_CATEGORY_ORDERS
from ...core.mutations import BaseMutation
from ...core.types import OrderError
from ...core.utils import raise_validation_error
from ...plugins.dataloaders import get_plugin_manager_promise
from ..types import Order, OrderLine
from .utils import EditableOrderValidationMixin, get_webhook_handler_by_order_status
Expand Down Expand Up @@ -48,8 +50,7 @@ def perform_mutation( # type: ignore[override]
only_type=OrderLine,
)
order = line.order
cls.check_channel_permissions(info, [order.channel_id])
cls.validate_order(line.order)
cls.validate(info, order, line)

db_id = line.id
warehouse_pk = (
Expand Down Expand Up @@ -99,3 +100,14 @@ def perform_mutation( # type: ignore[override]
func = get_webhook_handler_by_order_status(order.status, manager)
cls.call_event(func, order)
return OrderLineDelete(order=order, order_line=line)

@classmethod
def validate(cls, info, order, line):
cls.check_channel_permissions(info, [order.channel_id])
cls.validate_order(line.order)
if line.is_gift:
raise_validation_error(
message="Order line marked as gift can't be deleted.",
field="id",
code=OrderErrorCode.NON_REMOVABLE_GIFT_LINE.value,
)
30 changes: 30 additions & 0 deletions saleor/graphql/order/tests/mutations/test_order_line_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .....order import OrderStatus
from .....order import events as order_events
from .....order.error_codes import OrderErrorCode
from .....order.models import OrderEvent
from .....warehouse.models import Stock
from ....tests.utils import assert_no_permission, get_graphql_content
Expand All @@ -17,6 +18,7 @@
errors {
field
message
code
}
orderLine {
id
Expand Down Expand Up @@ -232,3 +234,31 @@ def test_draft_order_properly_recalculate_total_after_shipping_product_removed(
assert data["order"]["total"]["net"]["amount"] == float(
line.total_price_net_amount
) + float(order.shipping_price_net_amount)


def test_order_line_delete_non_removable_gift(
draft_order,
permission_group_manage_orders,
staff_api_client,
):
# given
query = ORDER_LINE_DELETE_MUTATION
permission_group_manage_orders.user_set.add(staff_api_client.user)
order = draft_order
line = order.lines.first()
line.is_gift = True
line.save(update_fields=["is_gift"])
line_id = graphene.Node.to_global_id("OrderLine", line.id)
variables = {"id": line_id}

# when
response = staff_api_client.post_graphql(query, variables)
content = get_graphql_content(response)

# then
data = content["data"]["orderLineDelete"]
assert not data["orderLine"]
errors = data["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "id"
assert errors[0]["code"] == OrderErrorCode.NON_REMOVABLE_GIFT_LINE.name
1 change: 1 addition & 0 deletions saleor/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12799,6 +12799,7 @@ enum OrderErrorCode @doc(category: "Orders") {
INVALID_VOUCHER
INVALID_VOUCHER_CODE
NON_EDITABLE_GIFT_LINE_QUANTITY

Check warning on line 12801 in saleor/graphql/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Enum value 'NON_EDITABLE_GIFT_LINE_QUANTITY' was added to enum 'OrderErrorCode'

Adding an enum value may break existing clients that were not programming defensively against an added case when querying an enum.
NON_REMOVABLE_GIFT_LINE

Check warning on line 12802 in saleor/graphql/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Enum value 'NON_REMOVABLE_GIFT_LINE' was added to enum 'OrderErrorCode'

Adding an enum value may break existing clients that were not programming defensively against an added case when querying an enum.
}

"""An enumeration."""
Expand Down
1 change: 1 addition & 0 deletions saleor/order/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class OrderErrorCode(Enum):
INVALID_VOUCHER = "invalid_voucher"
INVALID_VOUCHER_CODE = "invalid_voucher_code"
NON_EDITABLE_GIFT_LINE_QUANTITY = "non_editable_gift_line_quantity"
NON_REMOVABLE_GIFT_LINE = "non_removable_gift_line"


class OrderGrantRefundCreateErrorCode(Enum):
Expand Down

0 comments on commit f362bd1

Please sign in to comment.