Skip to content

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

Replace MoneyScalar with PositiveDecimal scalar

IKarbowiaka0cde37d5c8c
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.py
index 6b9df69184..02e198d7a9 100644
--- a/saleor/graphql/core/scalars.py
+++ b/saleor/graphql/core/scalars.py
@@ -1,8 +1,6 @@
import decimal
import graphene
-from django.conf import settings
-from django_prices.utils.formatting import get_currency_fraction
from graphql.error import GraphQLError
from graphql.language import ast
from measurement.measures import Weight
@@ -38,28 +36,20 @@ class Decimal(graphene.Float):
return None
-class MoneyScalar(Decimal):
- """Money scalar implementation.
+class PositiveDecimal(Decimal):
+ """Positive Decimal scalar implementation.
- Should be used in every price input field.
- Contains validations that check if the money value is not lower than 0 and
- if the value does not contain too many decimal values.
+ Should be used in places where value must be positive.
"""
@staticmethod
def parse_value(value):
- value = super(MoneyScalar, MoneyScalar).parse_value(value)
+ value = super(PositiveDecimal, PositiveDecimal).parse_value(value)
if not value:
return value
if value < 0:
- raise GraphQLError("Money value cannot be lower than 0.")
-
- currency_fraction = get_currency_fraction(settings.DEFAULT_CURRENCY)
- value = value.normalize()
- if abs(value.as_tuple().exponent) > currency_fraction:
raise GraphQLError(
- "Ensure the provided money field value has no more than "
- f"{currency_fraction} decimal places."
+ f"Value cannot be lower than 0. Unsupported value: {value}"
)
return value
diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.py
index ddf006e079..4a6f45c5f4 100644
--- a/saleor/graphql/core/tests/test_core.py
+++ b/saleor/graphql/core/tests/test_core.py
@@ -10,11 +10,7 @@ from graphene import InputField
from ....product.models import Category, Product
from ...product import types as product_types
-from ...tests.utils import (
- _get_graphql_content_from_response,
- assert_too_many_decimal_places_in_amount,
- get_graphql_content,
-)
+from ...tests.utils import _get_graphql_content_from_response, get_graphql_content
from ...utils import get_database_id, requestor_is_superuser
from ...utils.filters import filter_range_field, reporting_period_to_date
from ..enums import ReportingPeriod
@@ -137,7 +133,9 @@ def test_mutation_money_scalar_input(
staff_api_client, variant, stock, permission_manage_products
):
query = """
- mutation moneyScalarInput($id: ID!, $cost: MoneyScalar, $price: MoneyScalar) {
+ mutation PositiveDecimalInput(
+ $id: ID!, $cost: PositiveDecimal, $price: PositiveDecimal
+ ) {
productVariantUpdate(id: $id, input: {costPrice: $cost, price: $price}) {
errors {
field
@@ -170,7 +168,7 @@ def test_mutation_money_scalar_input_without_arguments(
):
query = """
mutation ProductVariantUpdate(
- $id: ID!, $price: MoneyScalar, $costPrice: MoneyScalar
+ $id: ID!, $price: PositiveDecimal, $costPrice: PositiveDecimal
) {
productVariantUpdate(id: $id, input: {costPrice: $costPrice, price: $price}) {
errors {
@@ -198,72 +196,6 @@ def test_mutation_money_scalar_input_without_arguments(
assert data["errors"] == []
-def test_mutation_money_scalar_to_many_decimal_places(
- staff_api_client, variant, permission_manage_products
-):
- # given
- query = """
- mutation testMoneyScalar($id: ID!, $price: MoneyScalar, $costPrice: MoneyScalar) {
- productVariantUpdate(id: $id,
- input: {costPrice: $costPrice, price: $price}) {
- errors {
- field
- message
- }
- productVariant {
- id
- }
- }
- }
- """
- staff_api_client.user.user_permissions.add(permission_manage_products)
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.id),
- "price": 15,
- "costPrice": 12.1234,
- }
-
- # when
- response = staff_api_client.post_graphql(query, variables)
-
- # then
- assert_too_many_decimal_places_in_amount(response)
-
-
-def test_mutation_money_scalar_with_to_many_0_decimal_places(
- staff_api_client, variant, permission_manage_products
-):
- # given
- query = """
- mutation testMoneyScalar($id: ID!, $price: MoneyScalar, $costPrice: MoneyScalar) {
- productVariantUpdate(id: $id,
- input: {costPrice: $costPrice, price: $price}) {
- errors {
- field
- message
- }
- productVariant {
- id
- }
- }
- }
- """
- staff_api_client.user.user_permissions.add(permission_manage_products)
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.id),
- "price": 15,
- "costPrice": 12.1200,
- }
-
- # when
- response = staff_api_client.post_graphql(query, variables)
-
- # then
- content = get_graphql_content(response)
- data = content["data"]["productVariantUpdate"]
- assert not data["errors"]
-
-
def test_filter_input():
class CreatedEnum(graphene.Enum):
WEEK = "week"
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 90f1558733..fcc2f2d335 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -10,7 +10,7 @@ from ...product.tasks import (
update_products_minimal_variant_prices_of_discount_task,
)
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ..core.scalars import Decimal, MoneyScalar
+from ..core.scalars import PositiveDecimal
from ..core.types.common import DiscountError
from ..product.types import Category, Collection, Product
from .enums import DiscountValueTypeEnum, VoucherTypeEnum
@@ -95,7 +95,7 @@ class VoucherInput(graphene.InputObjectType):
discount_value_type = DiscountValueTypeEnum(
description="Choices: fixed or percentage."
)
- discount_value = Decimal(description="Value of the voucher.")
+ discount_value = PositiveDecimal(description="Value of the voucher.")
products = graphene.List(
graphene.ID, description="Products discounted by the voucher.", name="products"
)
@@ -109,7 +109,7 @@ class VoucherInput(graphene.InputObjectType):
description="Categories discounted by the voucher.",
name="categories",
)
- min_amount_spent = MoneyScalar(
+ min_amount_spent = PositiveDecimal(
description="Min purchase amount required to apply the voucher."
)
min_checkout_items_quantity = graphene.Int(
@@ -243,7 +243,7 @@ class VoucherRemoveCatalogues(VoucherBaseCatalogueMutation):
class SaleInput(graphene.InputObjectType):
name = graphene.String(description="Voucher name.")
type = DiscountValueTypeEnum(description="Fixed or percentage.")
- value = Decimal(description="Value of the voucher.")
+ value = PositiveDecimal(description="Value of the voucher.")
products = graphene.List(
graphene.ID, description="Products related to the discount.", name="products"
)
diff --git a/saleor/graphql/discount/tests/test_discount.py b/saleor/graphql/discount/tests/test_discount.py
index 81fd99e536..ef5c7c52c2 100644
--- a/saleor/graphql/discount/tests/test_discount.py
+++ b/saleor/graphql/discount/tests/test_discount.py
@@ -143,8 +143,8 @@ CREATE_VOUCHER_MUTATION = """
mutation voucherCreate(
$type: VoucherTypeEnum, $name: String, $code: String,
$discountValueType: DiscountValueTypeEnum, $usageLimit: Int,
- $discountValue: Decimal,
- $minAmountSpent: MoneyScalar, $minCheckoutItemsQuantity: Int,
+ $discountValue: PositiveDecimal,
+ $minAmountSpent: PositiveDecimal, $minCheckoutItemsQuantity: Int,
$startDate: DateTime, $endDate: DateTime, $applyOncePerOrder: Boolean,
$applyOncePerCustomer: Boolean) {
voucherCreate(input: {
@@ -533,7 +533,7 @@ def test_voucher_remove_no_catalogues(
def test_create_sale(staff_api_client, permission_manage_discounts):
query = """
mutation saleCreate(
- $type: DiscountValueTypeEnum, $name: String, $value: Decimal,
+ $type: DiscountValueTypeEnum, $name: String, $value: PositiveDecimal,
$startDate: DateTime, $endDate: DateTime) {
saleCreate(input: {
name: $name, type: $type, value: $value,
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index 7674750098..bf304fa74d 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -12,7 +12,7 @@ from ...giftcard import models
from ...giftcard.error_codes import GiftCardErrorCode
from ...giftcard.utils import activate_gift_card, deactivate_gift_card
from ..core.mutations import BaseMutation, ModelMutation
-from ..core.scalars import MoneyScalar
+from ..core.scalars import PositiveDecimal
from ..core.types.common import GiftCardError
from .types import GiftCard
@@ -24,7 +24,7 @@ class GiftCardUpdateInput(graphene.InputObjectType):
end_date = graphene.types.datetime.Date(
description="End date of the gift card in ISO 8601 format."
)
- balance = MoneyScalar(description="Value of the gift card.")
+ balance = PositiveDecimal(description="Value of the gift card.")
user_email = graphene.String(
required=False, description="The customer's email of the gift card buyer."
)
diff --git a/saleor/graphql/giftcard/tests/test_gift_card.py b/saleor/graphql/giftcard/tests/test_gift_card.py
index 5a4f5974bb..41d1ccd97b 100644
--- a/saleor/graphql/giftcard/tests/test_gift_card.py
+++ b/saleor/graphql/giftcard/tests/test_gift_card.py
@@ -234,7 +234,7 @@ def test_query_gift_card_by_app_without_premissions(
CREATE_GIFT_CARD_MUTATION = """
mutation giftCardCreate(
$code: String, $startDate: Date, $endDate: Date,
- $balance: MoneyScalar!, $userEmail: String) {
+ $balance: PositiveDecimal!, $userEmail: String) {
giftCardCreate(input: {
code: $code, startDate: $startDate,
endDate: $endDate,
@@ -476,7 +476,7 @@ def test_create_gift_card_without_premissions(staff_api_client):
UPDATE_GIFT_CARD_MUTATION = """
mutation giftCardUpdate(
$id: ID!, $startDate: Date, $endDate: Date,
- $balance: MoneyScalar, $userEmail: String!) {
+ $balance: PositiveDecimal, $userEmail: String!) {
giftCardUpdate(id: $id, input: {startDate: $startDate,
endDate: $endDate,
balance: $balance, userEmail: $userEmail}) {
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index f06eda8acb..531714c944 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -21,7 +21,7 @@ from ....warehouse.management import allocate_stock
from ...account.i18n import I18nMixin
from ...account.types import AddressInput
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ...core.scalars import MoneyScalar
+from ...core.scalars import PositiveDecimal
from ...core.types.common import OrderError
from ...product.types import ProductVariant
from ..types import Order, OrderLine
@@ -46,7 +46,7 @@ class DraftOrderInput(InputObjectType):
descripton="Customer associated with the draft order.", name="user"
)
user_email = graphene.String(description="Email address of the customer.")
- discount = MoneyScalar(description="Discount amount for the order.")
+ discount = PositiveDecimal(description="Discount amount for the order.")
shipping_address = AddressInput(description="Shipping address of the customer.")
shipping_method = graphene.ID(
description="ID of a selected shipping method.", name="shippingMethod"
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 5d03f931dd..a3f81d3bcf 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -19,7 +19,7 @@ from ....order.utils import get_valid_shipping_methods_for_order
from ....payment import CustomPaymentChoices, PaymentError, TransactionKind, gateway
from ...account.types import AddressInput
from ...core.mutations import BaseMutation
-from ...core.scalars import UUID, MoneyScalar
+from ...core.scalars import UUID, PositiveDecimal
from ...core.types.common import OrderError
from ...core.utils import validate_required_string_field
from ...meta.deprecated.mutations import ClearMetaBaseMutation, UpdateMetaBaseMutation
@@ -362,7 +362,9 @@ class OrderCapture(BaseMutation):
class Arguments:
id = graphene.ID(required=True, description="ID of the order to capture.")
- amount = MoneyScalar(required=True, description="Amount of money to capture.")
+ amount = PositiveDecimal(
+ required=True, description="Amount of money to capture."
+ )
class Meta:
description = "Capture an order."
@@ -429,7 +431,9 @@ class OrderRefund(BaseMutation):
class Arguments:
id = graphene.ID(required=True, description="ID of the order to refund.")
- amount = MoneyScalar(required=True, description="Amount of money to refund.")
+ amount = PositiveDecimal(
+ required=True, description="Amount of money to refund."
+ )
class Meta:
description = "Refund an order."
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 0bd110960c..079a2e7640 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -572,7 +572,7 @@ def test_draft_order_create(
variant_0 = variant
query = """
mutation draftCreate(
- $user: ID, $discount: MoneyScalar, $lines: [OrderLineCreateInput],
+ $user: ID, $discount: PositiveDecimal, $lines: [OrderLineCreateInput],
$shippingAddress: AddressInput, $shippingMethod: ID, $voucher: ID,
$customerNote: String) {
draftOrderCreate(
@@ -1666,7 +1666,7 @@ def test_order_capture(
):
order = payment_txn_preauth.order
query = """
- mutation captureOrder($id: ID!, $amount: MoneyScalar!) {
+ mutation captureOrder($id: ID!, $amount: PositiveDecimal!) {
orderCapture(id: $id, amount: $amount) {
order {
paymentStatus
@@ -1873,7 +1873,7 @@ def test_order_void_payment_error(
def test_order_refund(staff_api_client, permission_manage_orders, payment_txn_captured):
order = payment_txn_captured.order
query = """
- mutation refundOrder($id: ID!, $amount: MoneyScalar!) {
+ mutation refundOrder($id: ID!, $amount: PositiveDecimal!) {
orderRefund(id: $id, amount: $amount) {
order {
paymentStatus
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index d6f8d3ec9a..3b7a42aab6 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -15,7 +15,7 @@ from ..account.i18n import I18nMixin
from ..account.types import AddressInput
from ..checkout.types import Checkout
from ..core.mutations import BaseMutation
-from ..core.scalars import MoneyScalar
+from ..core.scalars import PositiveDecimal
from ..core.types import common as common_types
from ..core.utils import from_global_id_strict_type
from .types import Payment
@@ -34,7 +34,7 @@ class PaymentInput(graphene.InputObjectType):
"billing data in a secure manner."
),
)
- amount = MoneyScalar(
+ amount = PositiveDecimal(
required=False,
description=(
"Total amount of the transaction, including "
@@ -190,7 +190,7 @@ class PaymentCapture(BaseMutation):
class Arguments:
payment_id = graphene.ID(required=True, description="Payment ID.")
- amount = MoneyScalar(description="Transaction amount.")
+ amount = PositiveDecimal(description="Transaction amount.")
class Meta:
description = "Captures the authorized payment amount."
diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.py
index b39b5e66d2..6019a87a9b 100644
--- a/saleor/graphql/payment/tests/test_payment.py
+++ b/saleor/graphql/payment/tests/test_payment.py
@@ -398,7 +398,7 @@ def test_create_payment_for_checkout_with_active_payments(
CAPTURE_QUERY = """
- mutation PaymentCapture($paymentId: ID!, $amount: MoneyScalar!) {
+ mutation PaymentCapture($paymentId: ID!, $amount: PositiveDecimal!) {
paymentCapture(paymentId: $paymentId, amount: $amount) {
payment {
id,
@@ -547,7 +547,7 @@ def test_payment_capture_gateway_dummy_credit_card_error(
REFUND_QUERY = """
- mutation PaymentRefund($paymentId: ID!, $amount: MoneyScalar!) {
+ mutation PaymentRefund($paymentId: ID!, $amount: PositiveDecimal!) {
paymentRefund(paymentId: $paymentId, amount: $amount) {
payment {
id,
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 77029319bb..45e41e5d31 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -30,7 +30,7 @@ from ....product.utils.attributes import (
generate_name_for_variant,
)
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ...core.scalars import MoneyScalar, WeightScalar
+from ...core.scalars import PositiveDecimal, WeightScalar
from ...core.types import SeoInput, Upload
from ...core.types.common import ProductError
from ...core.utils import (
@@ -535,7 +535,7 @@ class ProductInput(graphene.InputObjectType):
"is only used if a product doesn't use variants."
)
)
- base_price = MoneyScalar(
+ base_price = PositiveDecimal(
description=(
"Default price for product variant. "
"Note: this field is only used if a product doesn't use variants."
@@ -1104,8 +1104,8 @@ class ProductVariantInput(graphene.InputObjectType):
required=False,
description="List of attributes specific to this variant.",
)
- cost_price = MoneyScalar(description="Cost price of the variant.")
- price = MoneyScalar(description="Price of the particular variant.")
+ cost_price = PositiveDecimal(description="Cost price of the variant.")
+ price = PositiveDecimal(description="Price of the particular variant.")
sku = graphene.String(description="Stock keeping unit.")
track_inventory = graphene.Boolean(
description=(
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 04f814685e..35c8ce90d7 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -33,7 +33,7 @@ from ....product.utils.attributes import associate_attribute_values_to_instance
from ....warehouse.models import Allocation, Stock, Warehouse
from ...core.enums import ReportingPeriod
from ...tests.utils import (
- assert_negative_price_amount,
+ assert_negative_positive_decimal_value,
assert_no_permission,
get_graphql_content,
get_multipart_request_body,
@@ -1566,7 +1566,7 @@ def test_create_product_with_negative_base_price(
response = staff_api_client.post_graphql(query, variables)
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
def test_create_product_with_unicode_in_slug_and_name(
@@ -1610,7 +1610,7 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """
$productTypeId: ID!,
$categoryId: ID!
$name: String!,
- $basePrice: MoneyScalar!,
+ $basePrice: PositiveDecimal!,
$sku: String,
$trackInventory: Boolean)
{
@@ -1905,7 +1905,7 @@ def test_update_product(
$visibleInListings: Boolean!,
$chargeTaxes: Boolean!,
$taxCode: String!,
- $basePrice: MoneyScalar!,
+ $basePrice: PositiveDecimal!,
$attributes: [AttributeValueInput!]) {
productUpdate(
id: $productId,
@@ -2472,7 +2472,7 @@ def test_update_product_with_negative_base_price(
query = """
mutation updateProduct(
$productId: ID!,
- $basePrice: MoneyScalar)
+ $basePrice: PositiveDecimal)
{
productUpdate(
id: $productId,
@@ -2499,7 +2499,7 @@ def test_update_product_with_negative_base_price(
response = staff_api_client.post_graphql(query, variables)
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
def test_update_product_without_category_and_true_is_published_value(
@@ -4361,7 +4361,7 @@ mutation createProduct(
$name: String!,
$sku: String,
$stocks: [StockInput!],
- $basePrice: MoneyScalar!
+ $basePrice: PositiveDecimal!
$trackInventory: Boolean)
{
productCreate(
@@ -4551,7 +4551,7 @@ mutation createProduct(
$category: ID!
$name: String!,
$sku: String,
- $basePrice: MoneyScalar!,
+ $basePrice: PositiveDecimal!,
$weight: WeightScalar)
{
productCreate(
@@ -4656,7 +4656,7 @@ def test_create_product_with_weight_input(
$category: ID!,
$name: String!,
$sku: String,
- $basePrice: MoneyScalar!)
+ $basePrice: PositiveDecimal!)
{{
productCreate(
input: {{
diff --git a/saleor/graphql/product/tests/test_product_minimal_variant_price.py b/saleor/graphql/product/tests/test_product_minimal_variant_price.py
index 0b44241bb3..916bb9d5c1 100644
--- a/saleor/graphql/product/tests/test_product_minimal_variant_price.py
+++ b/saleor/graphql/product/tests/test_product_minimal_variant_price.py
@@ -5,7 +5,7 @@ from freezegun import freeze_time
from graphql_relay import from_global_id, to_global_id
from ...discount.enums import DiscountValueTypeEnum
-from ...tests.utils import assert_negative_price_amount, get_graphql_content
+from ...tests.utils import assert_negative_positive_decimal_value, get_graphql_content
@patch(
@@ -22,7 +22,7 @@ def test_product_variant_create_updates_minimal_variant_price(
mutation ProductVariantCreate(
$productId: ID!,
$sku: String!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
$attributes: [AttributeValueInput]!,
) {
productVariantCreate(
@@ -83,7 +83,7 @@ def test_product_variant_update_updates_minimal_variant_price(
query = """
mutation ProductVariantUpdate(
$id: ID!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
) {
productVariantUpdate(
id: $id,
@@ -132,7 +132,7 @@ def test_product_variant_update_updates_invalid_variant_price(
query = """
mutation ProductVariantUpdate(
$id: ID!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
) {
productVariantUpdate(
id: $id,
@@ -159,7 +159,7 @@ def test_product_variant_update_updates_invalid_variant_price(
response = staff_api_client.post_graphql(query, variables)
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
@patch(
@@ -175,7 +175,7 @@ def test_product_variant_update_updates_invalid_cost_price(
query = """
mutation ProductVariantUpdate(
$id: ID!,
- $costPrice: MoneyScalar,
+ $costPrice: PositiveDecimal,
) {
productVariantUpdate(
id: $id,
@@ -202,7 +202,7 @@ def test_product_variant_update_updates_invalid_cost_price(
response = staff_api_client.post_graphql(query, variables)
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
@patch(
@@ -390,7 +390,7 @@ def test_sale_create_updates_products_minimal_variant_prices(
mutation SaleCreate(
$name: String,
$type: DiscountValueTypeEnum,
- $value: Decimal,
+ $value: PositiveDecimal,
$products: [ID]
) {
saleCreate(input: {
@@ -439,7 +439,7 @@ def test_sale_update_updates_products_minimal_variant_prices(
permission_manage_discounts,
):
query = """
- mutation SaleUpdate($id: ID!, $value: Decimal) {
+ mutation SaleUpdate($id: ID!, $value: PositiveDecimal) {
saleUpdate(id: $id, input: {value: $value}) {
sale {
id
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 835bf50819..d61685ed4f 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -12,7 +12,7 @@ from ....product.utils.attributes import associate_attribute_values_to_instance
from ....warehouse.error_codes import StockErrorCode
from ....warehouse.models import Stock, Warehouse
from ...core.enums import WeightUnitsEnum
-from ...tests.utils import assert_negative_price_amount, get_graphql_content
+from ...tests.utils import assert_negative_positive_decimal_value, get_graphql_content
def test_fetch_variant(
@@ -96,8 +96,8 @@ def test_create_variant(
$productId: ID!,
$sku: String!,
$stocks: [StockInput!],
- $price: MoneyScalar,
- $costPrice: MoneyScalar,
+ $price: PositiveDecimal,
+ $costPrice: PositiveDecimal,
$attributes: [AttributeValueInput]!,
$weight: WeightScalar,
$trackInventory: Boolean!) {
@@ -284,7 +284,7 @@ def test_create_product_variant_not_all_attributes(
query = """
mutation createVariant (
$productId: ID!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
$sku: String!,
$attributes: [AttributeValueInput]!) {
productVariantCreate(
@@ -340,7 +340,7 @@ def test_create_product_variant_duplicated_attributes(
query = """
mutation createVariant (
$productId: ID!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
$sku: String!,
$attributes: [AttributeValueInput]!
) {
@@ -393,8 +393,8 @@ def test_create_product_variant_update_with_new_attributes(
mutation VariantUpdate(
$id: ID!
$attributes: [AttributeValueInput]
- $costPrice: MoneyScalar
- $price: MoneyScalar
+ $costPrice: PositiveDecimal
+ $price: PositiveDecimal
$sku: String
$trackInventory: Boolean!
) {
@@ -466,8 +466,8 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
mutation updateVariant (
$id: ID!,
$sku: String!,
- $price: MoneyScalar,
- $costPrice: MoneyScalar,
+ $price: PositiveDecimal,
+ $costPrice: PositiveDecimal,
$trackInventory: Boolean!) {
productVariantUpdate(
id: $id,
@@ -525,7 +525,7 @@ def test_update_product_variant_with_negative_weight(
query = """
mutation updateVariant (
$id: ID!,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
$weight: WeightScalar
) {
productVariantUpdate(
@@ -570,8 +570,8 @@ def test_update_product_variant_unset_cost_price(
mutation updateVariant (
$id: ID!,
$sku: String!,
- $price: MoneyScalar,
- $costPrice: MoneyScalar) {
+ $price: PositiveDecimal,
+ $costPrice: PositiveDecimal) {
productVariantUpdate(
id: $id,
input: {
@@ -615,7 +615,7 @@ QUERY_UPDATE_VARIANT_ATTRIBUTES = """
mutation updateVariant (
$id: ID!,
$sku: String,
- $price: MoneyScalar,
+ $price: PositiveDecimal,
$attributes: [AttributeValueInput]!) {
productVariantUpdate(
id: $id,
@@ -1214,7 +1214,7 @@ def test_product_variant_bulk_create_by_attribute_id_with_invalid_price(
)
# then
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
def test_product_variant_bulk_create_empty_attribute(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 3f8f1968d6..e2953571b5 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1630,8 +1630,6 @@ type DeactivateAllUserTokens {
accountErrors: [AccountError!]!
}
-scalar Decimal
-
type DeleteMetadata {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.")
metadataErrors: [MetadataError!]!
@@ -1778,7 +1776,7 @@ input DraftOrderCreateInput {
billingAddress: AddressInput
user: ID
userEmail: String
- discount: MoneyScalar
+ discount: PositiveDecimal
shippingAddress: AddressInput
shippingMethod: ID
voucher: ID
@@ -1796,7 +1794,7 @@ input DraftOrderInput {
billingAddress: AddressInput
user: ID
userEmail: String
- discount: MoneyScalar
+ discount: PositiveDecimal
shippingAddress: AddressInput
shippingMethod: ID
voucher: ID
@@ -2064,7 +2062,7 @@ type GiftCardCreate {
input GiftCardCreateInput {
startDate: Date
endDate: Date
- balance: MoneyScalar
+ balance: PositiveDecimal
userEmail: String
code: String
}
@@ -2099,7 +2097,7 @@ type GiftCardUpdate {
input GiftCardUpdateInput {
startDate: Date
endDate: Date
- balance: MoneyScalar
+ balance: PositiveDecimal
userEmail: String
}
@@ -2557,8 +2555,6 @@ type MoneyRange {
stop: Money
}
-scalar MoneyScalar
-
input MoveProductInput {
productId: ID!
sortOrder: Int
@@ -2676,8 +2672,8 @@ type Mutation {
productVariantClearPrivateMetadata(id: ID!, input: MetaPath!): ProductVariantClearPrivateMeta @deprecated(reason: "Use the `deletePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
variantImageAssign(imageId: ID!, variantId: ID!): VariantImageAssign
variantImageUnassign(imageId: ID!, variantId: ID!): VariantImageUnassign
- paymentCapture(amount: MoneyScalar, paymentId: ID!): PaymentCapture
- paymentRefund(amount: MoneyScalar, paymentId: ID!): PaymentRefund
+ paymentCapture(amount: PositiveDecimal, paymentId: ID!): PaymentCapture
+ paymentRefund(amount: PositiveDecimal, paymentId: ID!): PaymentRefund
paymentVoid(paymentId: ID!): PaymentVoid
pageCreate(input: PageInput!): PageCreate
pageDelete(id: ID!): PageDelete
@@ -2696,7 +2692,7 @@ type Mutation {
draftOrderUpdate(id: ID!, input: DraftOrderInput!): DraftOrderUpdate
orderAddNote(order: ID!, input: OrderAddNoteInput!): OrderAddNote
orderCancel(id: ID!): OrderCancel
- orderCapture(amount: MoneyScalar!, id: ID!): OrderCapture
+ orderCapture(amount: PositiveDecimal!, id: ID!): OrderCapture
orderClearPrivateMeta(id: ID!, input: MetaPath!): OrderClearPrivateMeta @deprecated(reason: "Use the `deletePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
orderClearMeta(input: MetaPath!, token: UUID!): OrderClearMeta @deprecated(reason: "Use the `deleteMetadata` mutation instead. This field will be removed after 2020-07-31.")
orderFulfill(input: OrderFulfillInput!, order: ID): OrderFulfill
@@ -2707,7 +2703,7 @@ type Mutation {
orderFulfillmentUpdateMeta(id: ID!, input: MetaInput!): FulfillmentUpdateMeta @deprecated(reason: "Use the `updateMetadata` mutation instead. This field will be removed after 2020-07-31.")
orderFulfillmentUpdatePrivateMeta(id: ID!, input: MetaInput!): FulfillmentUpdatePrivateMeta @deprecated(reason: "Use the `updatePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
orderMarkAsPaid(id: ID!): OrderMarkAsPaid
- orderRefund(amount: MoneyScalar!, id: ID!): OrderRefund
+ orderRefund(amount: PositiveDecimal!, id: ID!): OrderRefund
orderUpdate(id: ID!, input: OrderUpdateInput!): OrderUpdate
orderUpdateMeta(input: MetaInput!, token: UUID!): OrderUpdateMeta @deprecated(reason: "Use the `updateMetadata` mutation instead. This field will be removed after 2020-07-31.")
orderUpdatePrivateMeta(id: ID!, input: MetaInput!): OrderUpdatePrivateMeta @deprecated(reason: "Use the `updatePrivateMetadata` mutation instead. This field will be removed after 2020-07-31.")
@@ -3450,7 +3446,7 @@ type PaymentGateway {
input PaymentInput {
gateway: String!
token: String
- amount: MoneyScalar
+ amount: PositiveDecimal
billingAddress: AddressInput
returnUrl: String
}
@@ -3619,6 +3615,8 @@ input PluginUpdateInput {
configuration: [ConfigurationItemInput]
}
+scalar PositiveDecimal
+
input PriceRangeInput {
gte: Float
lte: Float
@@ -3726,7 +3724,7 @@ input ProductCreateInput {
weight: WeightScalar
sku: String
trackInventory: Boolean
- basePrice: MoneyScalar
+ basePrice: PositiveDecimal
visibleInListings: Boolean
productType: ID!
stocks: [StockInput!]
@@ -3860,7 +3858,7 @@ input ProductInput {
weight: WeightScalar
sku: String
trackInventory: Boolean
- basePrice: MoneyScalar
+ basePrice: PositiveDecimal
visibleInListings: Boolean
}
@@ -4107,8 +4105,8 @@ type ProductVariantBulkCreate {
input ProductVariantBulkCreateInput {
attributes: [AttributeValueInput]!
- costPrice: MoneyScalar
- price: MoneyScalar
+ costPrice: PositiveDecimal
+ price: PositiveDecimal
sku: String!
trackInventory: Boolean
weight: WeightScalar
@@ -4152,8 +4150,8 @@ type ProductVariantCreate {
input ProductVariantCreateInput {
attributes: [AttributeValueInput]!
- costPrice: MoneyScalar
- price: MoneyScalar
+ costPrice: PositiveDecimal
+ price: PositiveDecimal
sku: String
trackInventory: Boolean
weight: WeightScalar
@@ -4169,8 +4167,8 @@ type ProductVariantDelete {
input ProductVariantInput {
attributes: [AttributeValueInput]
- costPrice: MoneyScalar
- price: MoneyScalar
+ costPrice: PositiveDecimal
+ price: PositiveDecimal
sku: String
trackInventory: Boolean
weight: WeightScalar
@@ -4397,7 +4395,7 @@ input SaleFilterInput {
input SaleInput {
name: String
type: DiscountValueTypeEnum
- value: Decimal
+ value: PositiveDecimal
products: [ID]
categories: [ID]
collections: [ID]
@@ -4643,9 +4641,9 @@ type ShippingPriceDelete {
input ShippingPriceInput {
name: String
- price: MoneyScalar
- minimumOrderPrice: MoneyScalar
- maximumOrderPrice: MoneyScalar
+ price: PositiveDecimal
+ minimumOrderPrice: PositiveDecimal
+ maximumOrderPrice: PositiveDecimal
minimumOrderWeight: WeightScalar
maximumOrderWeight: WeightScalar
type: ShippingMethodTypeEnum
@@ -5355,11 +5353,11 @@ input VoucherInput {
startDate: DateTime
endDate: DateTime
discountValueType: DiscountValueTypeEnum
- discountValue: Decimal
+ discountValue: PositiveDecimal
products: [ID]
collections: [ID]
categories: [ID]
- minAmountSpent: MoneyScalar
+ minAmountSpent: PositiveDecimal
minCheckoutItemsQuantity: Int
countries: [String]
applyOncePerOrder: Boolean
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 3e973c4dfd..aba39cf4d8 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -10,7 +10,7 @@ from ...shipping.utils import (
get_countries_without_shipping_zone,
)
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ..core.scalars import MoneyScalar, WeightScalar
+from ..core.scalars import PositiveDecimal, WeightScalar
from ..core.types.common import ShippingError
from ..core.utils import get_duplicates_ids
from .enums import ShippingMethodTypeEnum
@@ -19,11 +19,11 @@ from .types import ShippingMethod, ShippingZone
class ShippingPriceInput(graphene.InputObjectType):
name = graphene.String(description="Name of the shipping method.")
- price = MoneyScalar(description="Shipping price of the shipping method.")
- minimum_order_price = MoneyScalar(
+ price = PositiveDecimal(description="Shipping price of the shipping method.")
+ minimum_order_price = PositiveDecimal(
description="Minimum order price to use this shipping method."
)
- maximum_order_price = MoneyScalar(
+ maximum_order_price = PositiveDecimal(
description="Maximum order price to use this shipping method."
)
minimum_order_weight = WeightScalar(
diff --git a/saleor/graphql/shipping/tests/test_shipping_method.py b/saleor/graphql/shipping/tests/test_shipping_method.py
index 646747fefa..7724e3af83 100644
--- a/saleor/graphql/shipping/tests/test_shipping_method.py
+++ b/saleor/graphql/shipping/tests/test_shipping_method.py
@@ -6,7 +6,7 @@ from ....core.weight import WeightUnits
from ....shipping.error_codes import ShippingErrorCode
from ....shipping.utils import get_countries_without_shipping_zone
from ...core.enums import WeightUnitsEnum
-from ...tests.utils import assert_negative_price_amount, get_graphql_content
+from ...tests.utils import assert_negative_positive_decimal_value, get_graphql_content
from ..types import ShippingMethodTypeEnum
SHIPPING_ZONE_QUERY = """
@@ -523,9 +523,9 @@ def test_delete_shipping_zone(
PRICE_BASED_SHIPPING_QUERY = """
mutation createShippingPrice(
- $type: ShippingMethodTypeEnum, $name: String!, $price: MoneyScalar,
- $shippingZone: ID!, $minimumOrderPrice: MoneyScalar,
- $maximumOrderPrice: MoneyScalar) {
+ $type: ShippingMethodTypeEnum, $name: String!, $price: PositiveDecimal,
+ $shippingZone: ID!, $minimumOrderPrice: PositiveDecimal,
+ $maximumOrderPrice: PositiveDecimal) {
shippingPriceCreate(input: {
name: $name, price: $price, shippingZone: $shippingZone,
minimumOrderPrice: $minimumOrderPrice,
@@ -619,7 +619,7 @@ def test_create_shipping_method_with_invalid_price(
response = staff_api_client.post_graphql(query, variables)
- assert_negative_price_amount(response)
+ assert_negative_positive_decimal_value(response)
def test_create_price_shipping_method_errors(
@@ -644,7 +644,7 @@ def test_create_price_shipping_method_errors(
WEIGHT_BASED_SHIPPING_QUERY = """
mutation createShippingPrice(
- $type: ShippingMethodTypeEnum, $name: String!, $price: MoneyScalar,
+ $type: ShippingMethodTypeEnum, $name: String!, $price: PositiveDecimal,
$shippingZone: ID!, $maximumOrderWeight: WeightScalar,
$minimumOrderWeight: WeightScalar) {
shippingPriceCreate(
@@ -781,8 +781,8 @@ def test_update_shipping_method(
):
query = """
mutation updateShippingPrice(
- $id: ID!, $price: MoneyScalar, $shippingZone: ID!,
- $type: ShippingMethodTypeEnum!, $minimumOrderPrice: MoneyScalar) {
+ $id: ID!, $price: PositiveDecimal, $shippingZone: ID!,
+ $type: ShippingMethodTypeEnum!, $minimumOrderPrice: PositiveDecimal) {
shippingPriceUpdate(
id: $id, input: {
price: $price, shippingZone: $shippingZone,
diff --git a/saleor/graphql/tests/utils.py b/saleor/graphql/tests/utils.py
index 95b06ab8a5..429478be41 100644
--- a/saleor/graphql/tests/utils.py
+++ b/saleor/graphql/tests/utils.py
@@ -26,12 +26,12 @@ def assert_no_permission(response):
), content["errors"]
-def assert_negative_price_amount(response):
+def assert_negative_positive_decimal_value(response):
content = _get_graphql_content_from_response(response)
assert "errors" in content, content
- assert content["errors"][0]["message"] == (
- "Money value cannot be lower than 0."
- ), content["errors"]
+ assert "Value cannot be lower than 0." in content["errors"][0]["message"], content[
+ "errors"
+ ]
def assert_too_many_decimal_places_in_amount(response):
diff --git a/saleor/product/tests/test_product_minimal_variant_price.py b/saleor/product/tests/test_product_minimal_variant_price.py
index 4e40973caa..09a98f8d4e 100644
--- a/saleor/product/tests/test_product_minimal_variant_price.py
+++ b/saleor/product/tests/test_product_minimal_variant_price.py
@@ -81,7 +81,7 @@ def test_product_update_updates_minimal_variant_price(
query = """
mutation ProductUpdate(
$productId: ID!,
- $basePrice: MoneyScalar!,
+ $basePrice: PositiveDecimal!,
) {
productUpdate(
id: $productId