@ -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,7 +249,9 @@ export class ImportService {
const activities : Activity [ ] = [ ] ;
const activities : Activity [ ] = [ ] ;
for ( let {
for ( let [
index ,
{
accountId ,
accountId ,
comment ,
comment ,
date ,
date ,
@ -258,7 +261,8 @@ export class ImportService {
SymbolProfile ,
SymbolProfile ,
type ,
type ,
unitPrice
unitPrice
} of activitiesExtendedWithErrors ) {
}
] 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 c onvert the unit price and fee to the asset currency if the transaction is in different currency.
// C onvert 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 } " `
) ;
) ;
}
}