Browse Source

Refactoring

pull/2215/head
Thomas 2 years ago
parent
commit
dec04386cf
  1. 47
      apps/api/src/app/import/import.service.ts
  2. 15
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

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

@ -13,6 +13,7 @@ import { DataProviderService } from '@ghostfolio/api/services/data-provider/data
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';
import { import {
DATE_FORMAT,
getAssetProfileIdentifier, getAssetProfileIdentifier,
parseDate parseDate
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
@ -24,7 +25,7 @@ import {
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource, Prisma, SymbolProfile } from '@prisma/client'; import { DataSource, Prisma, SymbolProfile } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { endOfToday, isAfter, isSameDay, parseISO } from 'date-fns'; import { endOfToday, format, isAfter, isSameDay, parseISO } from 'date-fns';
import { uniqBy } from 'lodash'; import { uniqBy } from 'lodash';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
@ -248,17 +249,20 @@ export class ImportService {
const activities: Activity[] = []; const activities: Activity[] = [];
for (let { for (let [
accountId, index,
comment, {
date, accountId,
error, comment,
fee, date,
quantity, error,
SymbolProfile, fee,
type, quantity,
unitPrice SymbolProfile,
} of activitiesExtendedWithErrors) { type,
unitPrice
}
] of activitiesExtendedWithErrors.entries()) {
const assetProfile = assetProfiles[ const assetProfile = assetProfiles[
getAssetProfileIdentifier({ getAssetProfileIdentifier({
dataSource: SymbolProfile.dataSource, dataSource: SymbolProfile.dataSource,
@ -297,16 +301,22 @@ export class ImportService {
}); });
if (SymbolProfile.currency !== assetProfile.currency) { if (SymbolProfile.currency !== assetProfile.currency) {
// We convert the unit price and fee to the asset currency if the transaction is in different currency. // Convert the unit price and fee to the asset currency if the imported transaction is in a different currency
unitPrice = await this.exchangeRateDataService.toCurrencyAtDate( unitPrice = await this.exchangeRateDataService.toCurrencyAtDate(
unitPrice, unitPrice,
SymbolProfile.currency, SymbolProfile.currency,
assetProfile.currency, assetProfile.currency,
date date
); );
if (unitPrice === undefined) {
if (!unitPrice) {
throw new Error( throw new Error(
`No currency conversion from ${SymbolProfile.currency} to ${assetProfile.currency} for ${SymbolProfile.symbol} at ${date}` `activities.${index} historical exchange rate at ${format(
date,
DATE_FORMAT
)} is not available from "${SymbolProfile.currency}" to "${
assetProfile.currency
}"`
); );
} }
@ -555,7 +565,7 @@ export class ImportService {
]) ])
)?.[symbol]; )?.[symbol];
if (assetProfile === undefined) { if (!assetProfile) {
throw new Error( throw new Error(
`activities.${index}.symbol ("${symbol}") is not valid for the specified data source ("${dataSource}")` `activities.${index}.symbol ("${symbol}") is not valid for the specified data source ("${dataSource}")`
); );
@ -563,14 +573,13 @@ export class ImportService {
if ( if (
assetProfile.currency !== currency && assetProfile.currency !== currency &&
!this.exchangeRateDataService.isCurrencyPairSupported( !this.exchangeRateDataService.hasCurrencyPair(
// We check if we can convert the currency bought in to currency of asset
currency, currency,
assetProfile.currency assetProfile.currency
) )
) { ) {
throw new Error( throw new Error(
`activities.${index}.currency ("${currency}") does not match with "${assetProfile.currency} and no exchange rate is available from "${currency}" to "${assetProfile.currency}"` `activities.${index}.currency ("${currency}") does not match with "${assetProfile.currency}" and no exchange rate is available from "${currency}" to "${assetProfile.currency}"`
); );
} }

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

@ -33,14 +33,13 @@ export class ExchangeRateDataService {
return this.currencyPairs; return this.currencyPairs;
} }
public isCurrencyPairSupported( public hasCurrencyPair(currency1: string, currency2: string) {
firstCurrency: string, return this.currencyPairs.some(({ symbol }) => {
secondCurrency: string return (
) { symbol === `${currency1}${currency2}` ||
return ( symbol === `${currency2}${currency1}`
this.getCurrencies().includes(firstCurrency) && );
this.getCurrencies().includes(secondCurrency) });
);
} }
public async initialize() { public async initialize() {

Loading…
Cancel
Save