Exemple #1
0
        private async Task <decimal> GetExchangeRateAsync()
        {
            var          ukrainianNow        = TimeZones.GetTimeZoneNow(TimeZones.UkrainianTime);
            const string LocalCurrencyCode   = "UAH";
            const string ForeignCurrencyCode = "PLN";

            // todo: cache
            var exchangeRate = await _centralBankExchangeRateManager.GetCurrentRateAsync(LocalCurrencyCode, ForeignCurrencyCode, ukrainianNow);

            if (exchangeRate == null)
            {
                throw new InvalidOperationException($"CB exchange rate for {LocalCurrencyCode}-{ForeignCurrencyCode} is not presented.");
            }

            return(exchangeRate.Value);
        }
Exemple #2
0
        public async Task AutoUpdateRatesAsync(CancellationToken cancellationToken)
        {
            try
            {
                // NBU
                string nbuRatesStatusMessage;
                var    nbuRates = await _uaCentralBankApiProxy.GetRatesAsync();

                if (nbuRates.Count > 0)
                {
                    var lastAppliedNbuRatesDateString = await _persistentCache.GetValueAsync(LastAppliedNbuRatesDateKey, cancellationToken);

                    // at the moment NBU returns rates only for one date
                    var nbuRateDate       = nbuRates[0].Date;
                    var nbuRateDateString = nbuRateDate.ToString("yyyy-MM-dd");
                    if (lastAppliedNbuRatesDateString == nbuRateDateString)
                    {
                        nbuRatesStatusMessage = $"INFO: NBU rates are already updated to {nbuRateDateString}.";
                    }
                    else
                    {
                        // tomorrow 00:00
                        var uahRateUpdateStartTime = TimeZones.GetTimeZoneNow(TimeZones.UkrainianTime).Date.AddDays(1);
                        var uahRates = await _dbContext.CentralBankExchangeRates
                                       .Where(x => x.LocalCurrencyCode == "UAH")
                                       .Select(x => new
                        {
                            x.Id,
                            x.LocalCurrencyCode,
                            x.ForeignCurrencyCode,
                        })
                                       .ToArrayAsync(cancellationToken);

                        var updatedCurrencies = new List <string>();
                        foreach (var uahRate in uahRates)
                        {
                            var nbuRate = nbuRates
                                          .Where(x => x.LocalCurrencyCode == uahRate.LocalCurrencyCode &&
                                                 x.ForeignCurrencyCode == uahRate.ForeignCurrencyCode)
                                          .FirstOrDefault();
                            if (nbuRate != null)
                            {
                                _dbContext.CentralBankExchangeRateUpdates.Add(
                                    new CentralBankExchangeRateUpdate()
                                {
                                    CentralBankExchangeRateId = uahRate.Id,
                                    Rate      = nbuRate.Rate,
                                    StartTime = uahRateUpdateStartTime,
                                });
                                updatedCurrencies.Add(nbuRate.ForeignCurrencyCode);
                            }
                        }

                        await _dbContext.SaveChangesAsync(cancellationToken);

                        await _persistentCache.SetValueAsync(LastAppliedNbuRatesDateKey, nbuRateDateString, CancellationToken.None);

                        nbuRatesStatusMessage = $"SUCCESS: NBU rates for {string.Join(", ", updatedCurrencies)} were updated to {nbuRateDateString}.";
                    }
                }
                else
                {
                    nbuRatesStatusMessage = "WARNING: No NBU rates.";
                }

                await _notificationManager.SendWorkerMessageAsync(nbuRatesStatusMessage);
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UnhandledException, ex, $"{nameof(AutoUpdateRatesAsync)} failed.");

                await _notificationManager.SendWorkerMessageAsync($"ERROR: {nameof(AutoUpdateRatesAsync)} failed: {ex.Message}.");
            }
        }