Exemple #1
0
        public async Task <(bool Success, string Error, decimal?Price)> FetchMetalPriceAsync(Guid metalId, Guid?karatId, DateTime?date, string transactionAction)
        {
            var     success = false;
            string  error   = "";
            decimal?price   = 0;

            try
            {
                if (date.HasValue)
                {
                    var priceInfo = await _priceRepository.FetchPriceByMetalIdKaratIdAsync(metalId, karatId, date);

                    if (priceInfo != null)
                    {
                        if (transactionAction == Constants.TransactionAction.Purchase)
                        {
                            price = priceInfo.BuyPrice;
                        }
                        else if (transactionAction == Constants.TransactionAction.Sell)
                        {
                            price = priceInfo.SellPrice;
                        }
                        else if (transactionAction == Constants.TransactionAction.Loan)
                        {
                            price = priceInfo.LoanPrice;
                        }

                        // calculate price per gram before sending it
                        if (priceInfo.PerOunce && price.HasValue)
                        {
                            price = decimal.Round(price.Value / Constants.OzToGm._1Oz, 2);
                        }

                        success = true;
                    }
                    else
                    {
                        error = "Unable to find a price for the selected Metal, Karat and Date";
                    }
                }
            }
            catch (Exception ex)
            {
                error = "Unexpected error occurred while processing your request";
                _logger.LogError("AdminService.FetchMetalPriceAsync - Exception:{@Ex}", args: new object[] { ex });
            }

            return(Success : success, Error : error, Price : price);
        }
        public async Task <bool> CopyPricesToTodayAsync()
        {
            var success = true;

            try
            {
                var utcNow            = DateTime.UtcNow;
                var previousDate      = utcNow.AddDays(-1).Date;
                var previousDayPrices = await _priceRepository.ListBasePricesByDateAsync(previousDate);

                if (previousDayPrices.Any())
                {
                    foreach (var price in previousDayPrices)
                    {
                        // avoid copying, if the price already exist
                        var possibleExistingPrice = await _priceRepository.FetchPriceByMetalIdKaratIdAsync(price.MetalId, price.KaratId, utcNow.Date);

                        if (possibleExistingPrice == null)
                        {
                            var newPrice = new Price
                            {
                                Date             = utcNow.Date,
                                MetalId          = price.MetalId,
                                KaratId          = price.KaratId,
                                BuyPrice         = price.BuyPrice,
                                SellPrice        = price.SellPrice,
                                LoanPrice        = price.LoanPrice,
                                LoanPricePercent = price.LoanPricePercent,
                                PerOunce         = price.PerOunce,
                                CreatedUserId    = null,
                                CreatedUtc       = DateTime.UtcNow
                            };

                            await _priceRepository.SavePriceAsync(newPrice);
                        }
                    }

                    success = true;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("ScheduledTaskService.CopyPricesToTodayAsync - exception:{@Ex}", args: new object[] { ex });
            }

            return(success);
        }