diff --git a/apps/api/src/app/import/import.service.ts b/apps/api/src/app/import/import.service.ts index 3d9c2999d..a4bcc880b 100644 --- a/apps/api/src/app/import/import.service.ts +++ b/apps/api/src/app/import/import.service.ts @@ -248,7 +248,7 @@ export class ImportService { const activities: Activity[] = []; - for (const { + for (let { accountId, comment, date, @@ -296,6 +296,28 @@ export class ImportService { Account?: { id: string; name: string }; }); + if (SymbolProfile.currency !== assetProfile.currency) { + // We convert the unit price and fee to the asset currency if the transaction is in different currency. + unitPrice = await this.exchangeRateDataService.toCurrencyAtDate( + unitPrice, + SymbolProfile.currency, + assetProfile.currency, + date + ); + if (unitPrice === undefined) { + throw new Error( + `No currency conversion from ${SymbolProfile.currency} to ${assetProfile.currency} for ${SymbolProfile.symbol} at ${date}` + ); + } + + fee = await this.exchangeRateDataService.toCurrencyAtDate( + fee, + SymbolProfile.currency, + assetProfile.currency, + date + ); + } + if (isDryRun) { order = { comment, @@ -539,9 +561,16 @@ export class ImportService { ); } - if (assetProfile.currency !== currency) { + if ( + assetProfile.currency !== currency && + !this.exchangeRateDataService.isCurrencyPairSupported( + // We check if we can convert the currency bought in to currency of asset + currency, + assetProfile.currency + ) + ) { throw new Error( - `activities.${index}.currency ("${currency}") does not match with "${assetProfile.currency}"` + `activities.${index}.currency ("${currency}") does not match with "${assetProfile.currency} and no exchange rate is available from "${currency}" to "${assetProfile.currency}"` ); } diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts index d94037530..f2f3ef4ec 100644 --- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts @@ -33,6 +33,16 @@ export class ExchangeRateDataService { return this.currencyPairs; } + public isCurrencyPairSupported( + firstCurrency: string, + secondCurrency: string + ) { + return ( + this.getCurrencies().includes(firstCurrency) && + this.getCurrencies().includes(secondCurrency) + ); + } + public async initialize() { this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); this.currencies = await this.prepareCurrencies();