|
|
@ -64,11 +64,12 @@ export class ExchangeRateDataService { |
|
|
|
} = {}; |
|
|
|
|
|
|
|
for (let currency of currencies) { |
|
|
|
exchangeRatesByCurrency[currency] = await this.getExchangeRates({ |
|
|
|
startDate, |
|
|
|
currencyFrom: currency, |
|
|
|
currencyTo: targetCurrency |
|
|
|
}); |
|
|
|
exchangeRatesByCurrency[`${currency}${targetCurrency}`] = |
|
|
|
await this.getExchangeRates({ |
|
|
|
startDate, |
|
|
|
currencyFrom: currency, |
|
|
|
currencyTo: targetCurrency |
|
|
|
}); |
|
|
|
|
|
|
|
let previousExchangeRate = 1; |
|
|
|
|
|
|
@ -82,17 +83,25 @@ export class ExchangeRateDataService { |
|
|
|
let dateString = format(date, DATE_FORMAT); |
|
|
|
|
|
|
|
// Check if the exchange rate for the current date is missing
|
|
|
|
if (isNaN(exchangeRatesByCurrency[currency][dateString])) { |
|
|
|
if ( |
|
|
|
isNaN( |
|
|
|
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString] |
|
|
|
) |
|
|
|
) { |
|
|
|
// If missing, fill with the previous exchange rate
|
|
|
|
exchangeRatesByCurrency[currency][dateString] = previousExchangeRate; |
|
|
|
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString] = |
|
|
|
previousExchangeRate; |
|
|
|
|
|
|
|
Logger.error( |
|
|
|
`No exchange rate has been found for ${DEFAULT_CURRENCY}${currency} at ${dateString}`, |
|
|
|
'ExchangeRateDataService' |
|
|
|
); |
|
|
|
if (currency === DEFAULT_CURRENCY) { |
|
|
|
Logger.error( |
|
|
|
`No exchange rate has been found for ${currency}${targetCurrency} at ${dateString}`, |
|
|
|
'ExchangeRateDataService' |
|
|
|
); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// If available, update the previous exchange rate
|
|
|
|
previousExchangeRate = exchangeRatesByCurrency[currency][dateString]; |
|
|
|
previousExchangeRate = |
|
|
|
exchangeRatesByCurrency[`${currency}${targetCurrency}`][dateString]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -136,24 +145,20 @@ export class ExchangeRateDataService { |
|
|
|
getYesterday() |
|
|
|
); |
|
|
|
|
|
|
|
if (Object.keys(result).length !== this.currencyPairs.length) { |
|
|
|
// Load currencies directly from data provider as a fallback
|
|
|
|
// if historical data is not fully available
|
|
|
|
const quotes = await this.dataProviderService.getQuotes({ |
|
|
|
items: this.currencyPairs.map(({ dataSource, symbol }) => { |
|
|
|
return { dataSource, symbol }; |
|
|
|
}), |
|
|
|
requestTimeout: ms('30 seconds') |
|
|
|
}); |
|
|
|
const quotes = await this.dataProviderService.getQuotes({ |
|
|
|
items: this.currencyPairs.map(({ dataSource, symbol }) => { |
|
|
|
return { dataSource, symbol }; |
|
|
|
}), |
|
|
|
requestTimeout: ms('30 seconds') |
|
|
|
}); |
|
|
|
|
|
|
|
for (const symbol of Object.keys(quotes)) { |
|
|
|
if (isNumber(quotes[symbol].marketPrice)) { |
|
|
|
result[symbol] = { |
|
|
|
[format(getYesterday(), DATE_FORMAT)]: { |
|
|
|
marketPrice: quotes[symbol].marketPrice |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
for (const symbol of Object.keys(quotes)) { |
|
|
|
if (isNumber(quotes[symbol].marketPrice)) { |
|
|
|
result[symbol] = { |
|
|
|
[format(getYesterday(), DATE_FORMAT)]: { |
|
|
|
marketPrice: quotes[symbol].marketPrice |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -253,6 +258,7 @@ export class ExchangeRateDataService { |
|
|
|
`No exchange rate has been found for ${aFromCurrency}${aToCurrency}`, |
|
|
|
'ExchangeRateDataService' |
|
|
|
); |
|
|
|
|
|
|
|
return aValue; |
|
|
|
} |
|
|
|
|
|
|
|