Example #1
0
        public Task <decimal> GetTrolleyCalculations(CalculateTrolleyTotalCommand.RequestTrolley request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting trolley total calculations");
            _logger.LogDebug("Starting trolley total calculations for {@Trolley}", request);

            var productPrices = new Dictionary <string, decimal>();

            foreach (var productQuantity in request.Quantities)
            {
                decimal markedPrice = GerMarkedPrice(request, productQuantity);

                var specialsPrice = GetSpecialsPrice(request, productQuantity, markedPrice);

                decimal approvedProductPrice = ChooseLowerPrice(markedPrice, specialsPrice);

                productPrices.Add(productQuantity.Name, approvedProductPrice);
            }

            var trolleyTotal = productPrices.Sum(x => x.Value);

            _logger.LogDebug("Calculated Price Per Product: {@ProductPrices}", productPrices);

            _logger.LogInformation("Calculated Price For Trolley {TrolleyTotal}", trolleyTotal);

            return(Task.FromResult(trolleyTotal));
        }
        public async Task <decimal> GetTrolleyCalculations(CalculateTrolleyTotalCommand.RequestTrolley request, CancellationToken cancellationToken)
        {
            var apiClient   = _apiClient;
            var appSettings = _appSettings;


            using var modelContent = new StringContent(JsonConvert.SerializeObject(request), System.Text.Encoding.UTF8, "application/json");

            var response = await apiClient.PostAsync(
                $"{appSettings.Value.Url}{UrlsConfig.TrolleyOperations.CalculateTrolleyTotal(appSettings.Value.Token)}",
                modelContent,
                cancellationToken);

            var data = await response.Content.ReadAsStringAsync(cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                decimal.TryParse(data, out var total);
                return(total);
            }

            throw new TrolleyCalculationRemoteException(response.StatusCode, data);
        }
Example #3
0
        private static decimal GetSpecialsPrice(CalculateTrolleyTotalCommand.RequestTrolley request,
                                                CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity productQuantity, decimal priceWithoutSpecials)
        {
            var productPrice = priceWithoutSpecials;

            try
            {
                var sps = FindApplicableSpecial(request, productQuantity);

                if (SpecialsIncorrectlySetup(sps))
                {
                    return(productPrice);
                }

                var specialSetQuantity = GetSpecialsQuantity(productQuantity, sps);

                if (SpecialQuantityInvalid(specialSetQuantity))
                {
                    return(productPrice);
                }

                var productsOverFlowingSpecial = productQuantity.Quantity % specialSetQuantity;

                var productSpecialPrice = (productQuantity.Quantity / specialSetQuantity) * sps.Total;

                var productOverFlowPrice = productsOverFlowingSpecial *
                                           request.Products.OrderBy(x => x.Price).FirstOrDefault().Price;

                productPrice = productOverFlowPrice + productSpecialPrice;

                return(productPrice);
            }
            catch
            {
                return(priceWithoutSpecials);
            }
        }
Example #4
0
 private static decimal GerMarkedPrice(CalculateTrolleyTotalCommand.RequestTrolley request, CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity productQuantity)
 {
     return(productQuantity.Quantity * request.Products.OrderBy(x => x.Price).FirstOrDefault().Price);
 }
Example #5
0
 private static CalculateTrolleyTotalCommand.RequestTrolley.TrolleySpecial FindApplicableSpecial(CalculateTrolleyTotalCommand.RequestTrolley request, CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity productQuantity)
 {
     try
     {
         return(request.Specials.Where(x => x.Quantities.Any(q => q.Name == productQuantity.Name)).OrderByDescending(x => x.Total).FirstOrDefault());
     }
     catch (Exception e)
     {
         return(null);
     }
 }