diff --git a/backoffice/modules/catalog/components/ProductGeneralInformation.tsx b/backoffice/modules/catalog/components/ProductGeneralInformation.tsx index c43959f303..bb2e4c1ba5 100644 --- a/backoffice/modules/catalog/components/ProductGeneralInformation.tsx +++ b/backoffice/modules/catalog/components/ProductGeneralInformation.tsx @@ -1,6 +1,6 @@ import { useRouter } from 'next/router'; import { useEffect, useState } from 'react'; -import { FieldErrorsImpl, UseFormRegister, UseFormSetValue } from 'react-hook-form'; +import { FieldErrorsImpl, UseFormRegister, UseFormSetValue, UseFormWatch } from 'react-hook-form'; import { toast } from 'react-toastify'; import slugify from 'slugify'; @@ -19,9 +19,10 @@ type Props = { register: UseFormRegister; errors: FieldErrorsImpl; setValue: UseFormSetValue; + watch: UseFormWatch; }; -const ProductGeneralInformation = ({ register, errors, setValue }: Props) => { +const ProductGeneralInformation = ({ register, errors, setValue, watch }: Props) => { //Get ID const router = useRouter(); const { id } = router.query; @@ -33,6 +34,8 @@ const ProductGeneralInformation = ({ register, errors, setValue }: Props) => { const [isLoading, setLoading] = useState(false); const [taxClasses, setTaxClasses] = useState([]); + const width = watch('width', 0); + useEffect(() => { getBrands().then((data) => { setBrands(data); @@ -176,7 +179,10 @@ const ProductGeneralInformation = ({ register, errors, setValue }: Props) => { type="number" registerOptions={{ required: { value: true, message: 'Product length is required' }, - validate: { positive: (v) => v > 0 || 'Length must be greater than 0' }, + validate: { + positive: (v) => v > 0 || 'Length must be greater than 0', + greaterThanWidth: (v) => (width && v > width) || 'Length must be greater than width', + }, }} /> diff --git a/backoffice/pages/catalog/products/[id]/edit.tsx b/backoffice/pages/catalog/products/[id]/edit.tsx index 16172c1b67..2d643e99e1 100644 --- a/backoffice/pages/catalog/products/[id]/edit.tsx +++ b/backoffice/pages/catalog/products/[id]/edit.tsx @@ -37,6 +37,7 @@ const EditProduct: NextPage = () => { setValue, handleSubmit, getValues, + watch, formState: { errors }, } = useForm(); @@ -91,7 +92,12 @@ const EditProduct: NextPage = () => {
setTabKey(e)}> - + diff --git a/backoffice/pages/catalog/products/create.tsx b/backoffice/pages/catalog/products/create.tsx index 841efb9eb3..5301cd28db 100644 --- a/backoffice/pages/catalog/products/create.tsx +++ b/backoffice/pages/catalog/products/create.tsx @@ -30,6 +30,7 @@ const ProductCreate: NextPage = () => { setValue, handleSubmit, getValues, + watch, formState: { errors }, } = useForm({ defaultValues: { @@ -82,7 +83,12 @@ const ProductCreate: NextPage = () => { setTabKey(e)}> - + diff --git a/product/src/main/java/com/yas/product/service/ProductService.java b/product/src/main/java/com/yas/product/service/ProductService.java index bed9433f3e..8ba6a11960 100644 --- a/product/src/main/java/com/yas/product/service/ProductService.java +++ b/product/src/main/java/com/yas/product/service/ProductService.java @@ -157,6 +157,8 @@ private void validateProductVm(ProductSaveVm< private void validateProductVm(ProductSaveVm productSaveVm, Product existingProduct) { + validateLengthMustGreaterThanWidth(productSaveVm); + validateExistingProductProperties(productSaveVm, existingProduct); validateProductVariationDuplicates(productSaveVm); @@ -175,6 +177,12 @@ private void validateProductVm(ProductSaveVm< } } + private void validateLengthMustGreaterThanWidth(ProductSaveVm productPostVm) { + if (productPostVm.length() < productPostVm.width()) { + throw new BadRequestException(Constants.ErrorCode.MAKE_SURE_LENGTH_GREATER_THAN_WIDTH); + } + } + private void validateExistingProductProperties(ProductProperties productProperties, Product existingProduct) { checkPropertyExists(productProperties.slug().toLowerCase(), existingProduct, productRepository::findBySlugAndIsPublishedTrue, Constants.ErrorCode.SLUG_ALREADY_EXISTED_OR_DUPLICATED); diff --git a/product/src/main/java/com/yas/product/utils/Constants.java b/product/src/main/java/com/yas/product/utils/Constants.java index febcff6b8e..9f8ead66ea 100644 --- a/product/src/main/java/com/yas/product/utils/Constants.java +++ b/product/src/main/java/com/yas/product/utils/Constants.java @@ -34,5 +34,6 @@ public final class ErrorCode { public static final String PRODUCT_OPTION_VALUE_IS_NOT_FOUND = "PRODUCT_OPTION_VALUE_IS_NOT_FOUND"; public static final String PRODUCT_COMBINATION_PROCESSING_FAILED = "PRODUCT_COMBINATION_PROCESSING_FAILED"; public static final String NO_MATCHING_PRODUCT_OPTIONS = "NO_MATCHING_PRODUCT_OPTIONS"; + public static final String MAKE_SURE_LENGTH_GREATER_THAN_WIDTH = "MAKE_SURE_LENGTH_GREATER_THAN_WIDTH"; } } diff --git a/product/src/main/java/com/yas/product/viewmodel/product/ProductSaveVm.java b/product/src/main/java/com/yas/product/viewmodel/product/ProductSaveVm.java index ac7c6c0567..411106c325 100644 --- a/product/src/main/java/com/yas/product/viewmodel/product/ProductSaveVm.java +++ b/product/src/main/java/com/yas/product/viewmodel/product/ProductSaveVm.java @@ -8,6 +8,10 @@ public interface ProductSaveVm extends Product Boolean isPublished(); + Double length(); + + Double width(); + @Override default Long id() { return null; diff --git a/product/src/main/resources/messages/messages.properties b/product/src/main/resources/messages/messages.properties index 3a9d20b290..650eed630f 100644 --- a/product/src/main/resources/messages/messages.properties +++ b/product/src/main/resources/messages/messages.properties @@ -18,4 +18,5 @@ SKU_ALREADY_EXISTED_OR_DUPLICATED=Sku {} is already existed or is duplicated GTIN_ALREADY_EXISTED_OR_DUPLICATED=Gtin {} is already existed or is duplicated PRODUCT_OPTION_VALUE_IS_NOT_FOUND=Product option value {} is not found PRODUCT_COMBINATION_PROCESSING_FAILED=An error occurred while processing product combinations -NO_MATCHING_PRODUCT_OPTIONS=The provided product options could not be found \ No newline at end of file +NO_MATCHING_PRODUCT_OPTIONS=The provided product options could not be found +MAKE_SURE_LENGTH_GREATER_THAN_WIDTH=Please make sure length greater than width \ No newline at end of file