public async Task <ValidationResult> ValidateAddBasketProductAsync(AddBasketProductInput input)
        {
            ValidationResult validationResult = new();

            if (string.IsNullOrWhiteSpace(input.CustomerEmail))
            {
                validationResult.Messages.Add(new(nameof(AddBasketProductInput.CustomerEmail), $"Debe ingresar un cliente."));
            }
            else
            {
                Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail);

                if (customer is null)
                {
                    validationResult.Messages.Add(new(nameof(AddBasketProductInput.CustomerEmail), $"El usuario '{input.CustomerEmail}' no existe."));
                }
            }

            if (input.Product is null)
            {
                validationResult.Messages.Add(new(nameof(AddBasketProductInput.Product), $"Debe seleccionar un producto."));
            }
            else
            {
                Product product = await _productRepository.GetAsync(input.Product.ProductId);

                if (product is not null)
                {
                    if (!product.IsActive)
                    {
                        validationResult.Messages.Add(new(nameof(AddBasketProductInput.Product), $"El producto '{product.Name}' no esta activo."));
                    }

                    if (input.Product.Quantity <= 0)
                    {
                        validationResult.Messages.Add(new(nameof(AddBasketProductInput.Product.Quantity), $"La cantidad debe ser mayor a cero."));
                    }

                    IEnumerable <ProductVariant> variants = await _productVariantRepository.GetByProduct(product);

                    foreach (ProductVariantPair variantInput in input.Product.Variants)
                    {
                        if (!variants.Any(x => x.Name == variantInput.Name && x.Values.Contains(variantInput.Value)))
                        {
                            validationResult.Messages.Add(new(nameof(AddBasketProductInput.Product.Variants), $"La variante '{variantInput.Name}' con valor '{variantInput.Value}' del producto '{product.Name}' no existe."));
                        }
                    }
                }
                else
                {
                    validationResult.Messages.Add(new(nameof(AddBasketProductInput.Product.ProductId), "El producto no existe."));
                }
            }

            return(validationResult);
        }
Example #2
0
        public async Task <ValidationResult> CreateSaleAsync(CreateSaleInput input)
        {
            ValidationResult validationResult = new();

            if (!Enum.IsDefined(input.PaymentProvider))
            {
                validationResult.Messages.Add(new(nameof(CreateSaleInput.PaymentProvider), "El proveedor de pago no es valido."));
            }

            if (string.IsNullOrWhiteSpace(input.CustomerEmail))
            {
                validationResult.Messages.Add(new(nameof(CreateSaleInput.CustomerEmail), "Debe indicar un cliente."));
            }
            else
            {
                Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail);

                if (customer is null)
                {
                    validationResult.Messages.Add(new(nameof(CreateSaleInput.CustomerEmail), "No existe el cliente."));
                }
                else
                {
                    AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail);

                    Address address = addressCustomer.Addresses?.SingleOrDefault(x => x.Id == input.AddressId);

                    if (address is null)
                    {
                        validationResult.Messages.Add(new(nameof(CreateSaleInput.AddressId), "La dirección no existe."));
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(input.CouponCode))
            {
                CouponCode couponCode = await _couponCodeRepository.GetCouponAsync(input.CouponCode);

                if (couponCode is null)
                {
                    validationResult.Messages.Add(new(nameof(CreateSaleInput.CouponCode), "El código de cupón no existe."));
                }
            }

            if (input.Products is null)
            {
                validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), "Debe seleccionar productos."));
            }
            else
            {
                foreach (ProductDefinitive productDefinitive in input.Products)
                {
                    Product product = await _productRepository.GetAsync(productDefinitive.ProductId);

                    if (product is null)
                    {
                        validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto con Id {productDefinitive.ProductId} no existe."));
                    }
                    else if (!product.IsActive)
                    {
                        validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no esta activo."));
                    }
                    else if (product.Stock <= 0)
                    {
                        validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene stock."));
                    }
                    else
                    {
                        if (productDefinitive.Variants is not null)
                        {
                            IEnumerable <ProductVariant> variants = await _productVariantRepository.GetByProduct(product);

                            if (variants is not null)
                            {
                                foreach (ProductVariantPair variantPair in productDefinitive.Variants)
                                {
                                    ProductVariant productVariant = variants.SingleOrDefault(x => x.Name == variantPair.Name);

                                    if (productVariant is null)
                                    {
                                        validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene variaciones de {variantPair.Name}."));
                                    }
                                    else if (!productVariant.Values.Contains(variantPair.Value))
                                    {
                                        validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene una variación de {variantPair.Name} con el valor {variantPair.Value}."));
                                    }
                                }
                            }
                        }
                    }
                }
            }



            return(validationResult);
        }