Browse Source

Finished feature to convert bought currency with exchange rate

pull/2215/head
Hugo Persson 2 years ago
parent
commit
1236f7587c
  1. 35
      apps/api/src/app/import/import.service.ts
  2. 10
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

35
apps/api/src/app/import/import.service.ts

@ -248,7 +248,7 @@ export class ImportService {
const activities: Activity[] = []; const activities: Activity[] = [];
for (const { for (let {
accountId, accountId,
comment, comment,
date, date,
@ -296,6 +296,28 @@ export class ImportService {
Account?: { id: string; name: string }; 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) { if (isDryRun) {
order = { order = {
comment, 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( 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}"`
); );
} }

10
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -33,6 +33,16 @@ export class ExchangeRateDataService {
return this.currencyPairs; return this.currencyPairs;
} }
public isCurrencyPairSupported(
firstCurrency: string,
secondCurrency: string
) {
return (
this.getCurrencies().includes(firstCurrency) &&
this.getCurrencies().includes(secondCurrency)
);
}
public async initialize() { public async initialize() {
this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
this.currencies = await this.prepareCurrencies(); this.currencies = await this.prepareCurrencies();

Loading…
Cancel
Save