public async Task <OperationResult <CouponCodeDto> > UpdateCouponCodeAsync(UpdateCouponCodeInput input) { var validationResult = await _couponCodeValidator.ValidateUpdateCouponCodeAsync(input); if (validationResult.IsSuccess) { var entity = await _couponCodeRepository.GetCouponAsync(input.Code.ToUpper()); entity.Categories = input.Categories; entity.Customers = input.Customers; entity.DateExpire = input.DateExpire; entity.DateStart = input.DateStart; entity.MaxAmount = input.MaxAmount; entity.MinAmount = input.MinAmount; entity.Products = input.Products; entity.Customers = input.Customers; entity.Value = input.Value; entity.SubCategories = input.SubCategories; entity.Type = input.Type; await _couponCodeRepository.UpdateAsync(entity); return(OperationResult <CouponCodeDto> .Success(entity.ConvertToDto())); } return(OperationResult <CouponCodeDto> .Fail(validationResult)); }
public async Task <IActionResult> Update(UpdateCouponCodeInput input) { var result = await _service.UpdateCouponCodeAsync(input); return(new OperationActionResult(result)); }
public async Task <ValidationResult> ValidateUpdateCouponCodeAsync(UpdateCouponCodeInput input) { ValidationResult validationResult = new(); if (string.IsNullOrWhiteSpace(input.Code)) { validationResult.Messages.Add(new(nameof(RemoveCouponCodeInput.Code), "Debe indicar un código.")); } else { CouponCode couponCode = await _couponCodeRepository.GetCouponAsync(input.Code.ToUpper()); if (couponCode is null) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Code), $"El código {input.Code} no existe.")); } } if (!Enum.IsDefined(input.Type)) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Type), "El tipo de código no existe.")); } if (input.Value <= 0) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Value), "El valor del código debe ser mayor a cero.")); } if (input.Type == CouponCodeOffType.Percentage && input.Value > 100) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Value), "Un código de tipo porcentaje no puede tener un valor mayor de 100.")); } if (input.MinAmount < 0) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.MinAmount), "El monto mínimo del código debe ser mayor o igual a cero.")); } if (input.MaxAmount.HasValue && input.MaxAmount.Value <= 0) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.MaxAmount), "El monto máximo del código debe ser mayor a cero.")); } if (input.MaxAmount.HasValue && input.MaxAmount.Value < input.MinAmount) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.MaxAmount), "El monto máximo del código no puede ser menor al monto mínimo.")); } if (input.DateExpire.HasValue && input.DateExpire.Value.ToUniversalTime().Date < input.DateStart.ToUniversalTime().Date) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.DateExpire), "La fecha de expiración no puedo ser anterior a la fecha de inicio.")); } if (input.Products?.Any() ?? false) { var products = _productRepository.GetAvailableProducts(input.Products); if (input.Products.Distinct().WhereIsNotEmply().Count() > products.Count()) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Products), "Algunos productos no existen o no estan activos.")); } } if (input.Customers?.Any() ?? false) { foreach (var customerEmail in input.Customers.Distinct().WhereIsNotEmply()) { var customer = await _customerRepository.GetCustomer(customerEmail); if (customer is null) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Customers), $"El cliente {customerEmail} no existe.")); } } } if (input.Categories?.Any() ?? false) { foreach (var categoryName in input.Categories.Distinct().WhereIsNotEmply()) { var category = await _productCategoryRepository.GetByNameAsync(categoryName); if (category is null) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.Categories), $"La categoria {categoryName} no existe.")); } } } if (input.SubCategories?.Any() ?? false) { var groups = input.SubCategories.GroupBy(x => x.Category); foreach (var group in groups) { var category = await _productCategoryRepository.GetByNameAsync(group.Key); if (category is null) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.SubCategories), $"La categoria {group.Key} no existe.")); } else { foreach (var item in group) { if (!category.SubCategories.Contains(item.SubCategory)) { validationResult.Messages.Add(new(nameof(UpdateCouponCodeInput.SubCategories), $"La subcategoria {item.SubCategory} no existe en la categoria {category.Name}.")); } } } } } return(validationResult); }