From e92725f38bfab74b33bdc4a3d196039bdba84ea2 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 18 Nov 2024 01:01:18 +0800 Subject: [PATCH] add the Central Bank of the Republic of Uzbekistan exchange rates data source --- conf/ezbookkeeping.ini | 1 + pkg/api/exchange_rates_test.go | 20 +++ .../central_bank_of_uzbekistan_datasource.go | 163 ++++++++++++++++++ ...tral_bank_of_uzbekistan_datasource_test.go | 145 ++++++++++++++++ .../exchange_rates_datasource_container.go | 3 + pkg/settings/setting.go | 2 + 6 files changed, 334 insertions(+) create mode 100644 pkg/exchangerates/central_bank_of_uzbekistan_datasource.go create mode 100644 pkg/exchangerates/central_bank_of_uzbekistan_datasource_test.go diff --git a/conf/ezbookkeeping.ini b/conf/ezbookkeeping.ini index 01159ad6..fd225305 100644 --- a/conf/ezbookkeeping.ini +++ b/conf/ezbookkeeping.ini @@ -354,6 +354,7 @@ custom_map_tile_server_default_zoom_level = 14 # "national_bank_of_romania": https://www.bnr.ro/Exchange-rates-1224.aspx # "bank_of_russia": https://www.cbr.ru/eng/currency_base/daily/ # "swiss_national_bank": https://www.snb.ch/en/the-snb/mandates-goals/statistics/statistics-pub/current_interest_exchange_rates +# "central_bank_of_uzbekistan": https://cbu.uz/en/arkhiv-kursov-valyut/ # "international_monetary_fund": https://www.imf.org/external/np/fin/data/param_rms_mth.aspx data_source = euro_central_bank diff --git a/pkg/api/exchange_rates_test.go b/pkg/api/exchange_rates_test.go index f747dd7f..00c7b380 100644 --- a/pkg/api/exchange_rates_test.go +++ b/pkg/api/exchange_rates_test.go @@ -256,6 +256,26 @@ func TestExchangeRatesApiLatestExchangeRateHandler_SwissNationalBankDataSource(t checkExchangeRatesHaveSpecifiedCurrencies(t, exchangeRateResponse.BaseCurrency, supportedCurrencyCodes, exchangeRateResponse.ExchangeRates) } +func TestExchangeRatesApiLatestExchangeRateHandler_CentralBankOfUzbekistanDataSource(t *testing.T) { + exchangeRateResponse := executeLatestExchangeRateHandler(t, settings.CentralBankOfUzbekistanDataSource) + + if exchangeRateResponse == nil { + return + } + + assert.Equal(t, "UZS", exchangeRateResponse.BaseCurrency) + + supportedCurrencyCodes := []string{"AED", "AFN", "AMD", "ARS", "AUD", "AZN", + "BDT", "BGN", "BHD", "BND", "BRL", "BYN", "CAD", "CHF", "CNY", "CUP", "CZK", + "DKK", "DZD", "EGP", "EUR", "GBP", "GEL", "HKD", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", + "JOD", "JPY", "KGS", "KHR", "KRW", "KWD", "KZT", "LAK", "LBP", "LYD", + "MAD", "MDL", "MMK", "MNT", "MXN", "MYR", "NOK", "NZD", "OMR", "PHP", "PKR", "PLN", + "QAR", "RON", "RSD", "RUB", "SAR", "SDG", "SEK", "SGD", "SYP", + "THB", "TJS", "TMT", "TND", "TRY", "UAH", "USD", "UYU", "VES", "VND", "YER", "ZAR"} + + checkExchangeRatesHaveSpecifiedCurrencies(t, exchangeRateResponse.BaseCurrency, supportedCurrencyCodes, exchangeRateResponse.ExchangeRates) +} + func TestExchangeRatesApiLatestExchangeRateHandler_InternationalMonetaryFundDataSource(t *testing.T) { exchangeRateResponse := executeLatestExchangeRateHandler(t, settings.InternationalMonetaryFundDataSource) diff --git a/pkg/exchangerates/central_bank_of_uzbekistan_datasource.go b/pkg/exchangerates/central_bank_of_uzbekistan_datasource.go new file mode 100644 index 00000000..2c552cd7 --- /dev/null +++ b/pkg/exchangerates/central_bank_of_uzbekistan_datasource.go @@ -0,0 +1,163 @@ +package exchangerates + +import ( + "encoding/json" + "math" + "net/http" + "time" + + "github.com/mayswind/ezbookkeeping/pkg/core" + "github.com/mayswind/ezbookkeeping/pkg/errs" + "github.com/mayswind/ezbookkeeping/pkg/log" + "github.com/mayswind/ezbookkeeping/pkg/models" + "github.com/mayswind/ezbookkeeping/pkg/utils" + "github.com/mayswind/ezbookkeeping/pkg/validators" +) + +const centralBankOfUzbekistanExchangeRateUrl = "https://cbu.uz/ru/arkhiv-kursov-valyut/json/" +const centralBankOfUzbekistanExchangeRateReferenceUrl = "https://cbu.uz/en/arkhiv-kursov-valyut/" +const centralBankOfUzbekistanDataSource = "O‘zbekiston Respublikasi Markaziy banki" +const centralBankOfUzbekistanBaseCurrency = "UZS" + +const centralBankOfUzbekistanUpdateDateFormat = "02.01.2006" +const centralBankOfUzbekistanUpdateDateTimezone = "Asia/Samarkand" + +// CentralBankOfUzbekistanDataSource defines the structure of exchange rates data source of the central bank of the Republic of Uzbekistan +type CentralBankOfUzbekistanDataSource struct { + ExchangeRatesDataSource +} + +// CentralBankOfUzbekistanExchangeRates represents the exchange rates data from the central bank of the Republic of Uzbekistan +type CentralBankOfUzbekistanExchangeRates []*CentralBankOfUzbekistanExchangeRate + +// CentralBankOfUzbekistanExchangeRate represents the exchange rate data from the central bank of the Republic of Uzbekistan +type CentralBankOfUzbekistanExchangeRate struct { + Currency string `json:"Ccy"` + Unit string `json:"Nominal"` + Rate string `json:"Rate"` + Date string `json:"Date"` +} + +// ToLatestExchangeRateResponse returns a view-object according to original data from the central bank of the Republic of Uzbekistan +func (e CentralBankOfUzbekistanExchangeRates) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse { + if len(e) < 1 { + log.Errorf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRateResponse] exchange rates is empty") + return nil + } + + timezone, err := time.LoadLocation(centralBankOfUzbekistanUpdateDateTimezone) + + if err != nil { + log.Errorf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", danmarksNationalbankDataUpdateDateTimezone) + return nil + } + + exchangeRates := make(models.LatestExchangeRateSlice, 0, len(e)) + latestUpdateTime := int64(0) + + for i := 0; i < len(e); i++ { + exchangeRate := e[i] + + if _, exists := validators.AllCurrencyNames[exchangeRate.Currency]; !exists { + continue + } + + updateTime, err := time.ParseInLocation(centralBankOfUzbekistanUpdateDateFormat, exchangeRate.Date, timezone) + + if err != nil { + log.Errorf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", exchangeRate.Date) + return nil + } + + if updateTime.Unix() > latestUpdateTime { + latestUpdateTime = updateTime.Unix() + } + + finalExchangeRate := exchangeRate.ToLatestExchangeRate(c) + + if finalExchangeRate == nil { + continue + } + + exchangeRates = append(exchangeRates, finalExchangeRate) + } + + latestExchangeRateResp := &models.LatestExchangeRateResponse{ + DataSource: centralBankOfUzbekistanDataSource, + ReferenceUrl: centralBankOfUzbekistanExchangeRateReferenceUrl, + UpdateTime: latestUpdateTime, + BaseCurrency: centralBankOfUzbekistanBaseCurrency, + ExchangeRates: exchangeRates, + } + + return latestExchangeRateResp +} + +// ToLatestExchangeRate returns a data pair according to original data from the central bank of the Republic of Uzbekistan +func (e *CentralBankOfUzbekistanExchangeRate) ToLatestExchangeRate(c core.Context) *models.LatestExchangeRate { + rate, err := utils.StringToFloat64(e.Rate) + + if err != nil { + log.Warnf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRate] failed to parse rate, currency is %s, rate is %s", e.Currency, e.Rate) + return nil + } + + if rate <= 0 { + log.Warnf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %s", e.Currency, e.Rate) + return nil + } + + unit, err := utils.StringToFloat64(e.Unit) + + if err != nil { + log.Warnf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRate] failed to parse unit, currency is %s, unit is %s", e.Currency, e.Unit) + return nil + } + + if unit <= 0 { + log.Warnf(c, "[central_bank_of_uzbekistan_datasource.ToLatestExchangeRate] unit is less or equal zero, currency is %s, unit is %s", e.Currency, e.Unit) + return nil + } + + finalRate := 1000 * unit / rate + + if math.IsInf(finalRate, 0) { + return nil + } + + return &models.LatestExchangeRate{ + Currency: e.Currency, + Rate: utils.Float64ToString(finalRate), + } +} + +// BuildRequests returns the the central bank of the Republic of Uzbekistan exchange rates http requests +func (e *CentralBankOfUzbekistanDataSource) BuildRequests() ([]*http.Request, error) { + req, err := http.NewRequest("GET", centralBankOfUzbekistanExchangeRateUrl, nil) + + if err != nil { + return nil, err + } + + return []*http.Request{req}, nil +} + +// Parse returns the common response entity according to the the central bank of the Republic of Uzbekistan data source raw response +func (e *CentralBankOfUzbekistanDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) { + centralBankOfUzbekistanData := &CentralBankOfUzbekistanExchangeRates{} + err := json.Unmarshal(content, centralBankOfUzbekistanData) + + if err != nil { + log.Errorf(c, "[central_bank_of_uzbekistan_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error()) + return nil, errs.ErrFailedToRequestRemoteApi + } + + latestExchangeRateResponse := centralBankOfUzbekistanData.ToLatestExchangeRateResponse(c) + + if latestExchangeRateResponse == nil { + log.Errorf(c, "[central_bank_of_uzbekistan_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content)) + return nil, errs.ErrFailedToRequestRemoteApi + } + + return latestExchangeRateResponse, nil +} diff --git a/pkg/exchangerates/central_bank_of_uzbekistan_datasource_test.go b/pkg/exchangerates/central_bank_of_uzbekistan_datasource_test.go new file mode 100644 index 00000000..42d0223a --- /dev/null +++ b/pkg/exchangerates/central_bank_of_uzbekistan_datasource_test.go @@ -0,0 +1,145 @@ +package exchangerates + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/mayswind/ezbookkeeping/pkg/core" + "github.com/mayswind/ezbookkeeping/pkg/models" +) + +const centralBankOfUzbekistanMinimumRequiredContent = "[\n" + + " {\n" + + " \"Ccy\": \"USD\",\n" + + " \"Nominal\": \"1\",\n" + + " \"Rate\": \"12800.13\",\n" + + " \"Date\": \"15.11.2024\"\n" + + " },\n" + + " {\n" + + " \"Ccy\": \"VND\",\n" + + " \"Nominal\": \"10\",\n" + + " \"Rate\": \"5.04\",\n" + + " \"Date\": \"15.11.2024\"\n" + + " }\n" + + "]" + +func TestCentralBankOfUzbekistanDataSource_StandardDataExtractBaseCurrency(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(centralBankOfUzbekistanMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Equal(t, "UZS", actualLatestExchangeRateResponse.BaseCurrency) +} + +func TestCentralBankOfUzbekistanDataSource_StandardDataExtractUpdateTime(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(centralBankOfUzbekistanMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Equal(t, int64(1731610800), actualLatestExchangeRateResponse.UpdateTime) +} + +func TestCentralBankOfUzbekistanDataSource_StandardDataExtractExchangeRates(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(centralBankOfUzbekistanMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{ + Currency: "USD", + Rate: "0.07812420655102723", + }) + assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{ + Currency: "VND", + Rate: "1984.126984126984", + }) +} + +func TestCentralBankOfUzbekistanDataSource_BlankContent(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte("")) + assert.NotEqual(t, nil, err) +} + +func TestCentralBankOfUzbekistanDataSource_EmptyData(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte("[]")) + assert.NotEqual(t, nil, err) +} + +func TestCentralBankOfUzbekistanDataSource_InvalidCurrency(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+ + " {\n"+ + " \"Ccy\": \"XXX\",\n"+ + " \"Nominal\": \"1\",\n"+ + " \"Rate\": \"1\",\n"+ + " \"Date\": \"15.11.2024\"\n"+ + " }\n"+ + "]")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} + +func TestCentralBankOfUzbekistanDataSource_InvalidNominal(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+ + " {\n"+ + " \"Ccy\": \"USD\",\n"+ + " \"Nominal\": null,\n"+ + " \"Rate\": \"12800.13\",\n"+ + " \"Date\": \"15.11.2024\"\n"+ + " }\n"+ + "]")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("[\n"+ + " {\n"+ + " \"Ccy\": \"USD\",\n"+ + " \"Nominal\": \"0\",\n"+ + " \"Rate\": \"12800.13\",\n"+ + " \"Date\": \"15.11.2024\"\n"+ + " }\n"+ + "]")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} + +func TestCentralBankOfUzbekistanDataSource_InvalidRate(t *testing.T) { + dataSource := &CentralBankOfUzbekistanDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+ + " {\n"+ + " \"Ccy\": \"USD\",\n"+ + " \"Nominal\": \"1\",\n"+ + " \"Rate\": null,\n"+ + " \"Date\": \"15.11.2024\"\n"+ + " }\n"+ + "]")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("[\n"+ + " {\n"+ + " \"Ccy\": \"USD\",\n"+ + " \"Nominal\": \"1\",\n"+ + " \"Rate\": \"0\",\n"+ + " \"Date\": \"15.11.2024\"\n"+ + " }\n"+ + "]")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} diff --git a/pkg/exchangerates/exchange_rates_datasource_container.go b/pkg/exchangerates/exchange_rates_datasource_container.go index 037db730..fa868fcd 100644 --- a/pkg/exchangerates/exchange_rates_datasource_container.go +++ b/pkg/exchangerates/exchange_rates_datasource_container.go @@ -59,6 +59,9 @@ func InitializeExchangeRatesDataSource(config *settings.Config) error { } else if config.ExchangeRatesDataSource == settings.SwissNationalBankDataSource { Container.Current = &SwissNationalBankDataSource{} return nil + } else if config.ExchangeRatesDataSource == settings.CentralBankOfUzbekistanDataSource { + Container.Current = &CentralBankOfUzbekistanDataSource{} + return nil } else if config.ExchangeRatesDataSource == settings.InternationalMonetaryFundDataSource { Container.Current = &InternationalMonetaryFundDataSource{} return nil diff --git a/pkg/settings/setting.go b/pkg/settings/setting.go index d4923fdc..8d583584 100644 --- a/pkg/settings/setting.go +++ b/pkg/settings/setting.go @@ -114,6 +114,7 @@ const ( NationalBankOfRomaniaDataSource string = "national_bank_of_romania" BankOfRussiaDataSource string = "bank_of_russia" SwissNationalBankDataSource string = "swiss_national_bank" + CentralBankOfUzbekistanDataSource string = "central_bank_of_uzbekistan" InternationalMonetaryFundDataSource string = "international_monetary_fund" ) @@ -901,6 +902,7 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio dataSource == NationalBankOfRomaniaDataSource || dataSource == BankOfRussiaDataSource || dataSource == SwissNationalBankDataSource || + dataSource == CentralBankOfUzbekistanDataSource || dataSource == InternationalMonetaryFundDataSource { config.ExchangeRatesDataSource = dataSource } else {