|
@ -10,12 +10,10 @@ import { PlatformService } from '@ghostfolio/api/app/platform/platform.service'; |
|
|
import { PortfolioService } from '@ghostfolio/api/app/portfolio/portfolio.service'; |
|
|
import { PortfolioService } from '@ghostfolio/api/app/portfolio/portfolio.service'; |
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
|
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; |
|
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; |
|
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|
|
|
|
|
import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service'; |
|
|
import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service'; |
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|
|
import { DATA_GATHERING_QUEUE_PRIORITY_HIGH } from '@ghostfolio/common/config'; |
|
|
import { DATA_GATHERING_QUEUE_PRIORITY_HIGH } from '@ghostfolio/common/config'; |
|
|
import { |
|
|
import { |
|
|
DATE_FORMAT, |
|
|
|
|
|
getAssetProfileIdentifier, |
|
|
getAssetProfileIdentifier, |
|
|
parseDate |
|
|
parseDate |
|
|
} from '@ghostfolio/common/helper'; |
|
|
} from '@ghostfolio/common/helper'; |
|
@ -29,8 +27,8 @@ 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, format, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
import { isNumber, uniqBy } from 'lodash'; |
|
|
import { uniqBy } from 'lodash'; |
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
|
@Injectable() |
|
|
@Injectable() |
|
@ -40,7 +38,6 @@ export class ImportService { |
|
|
private readonly configurationService: ConfigurationService, |
|
|
private readonly configurationService: ConfigurationService, |
|
|
private readonly dataGatheringService: DataGatheringService, |
|
|
private readonly dataGatheringService: DataGatheringService, |
|
|
private readonly dataProviderService: DataProviderService, |
|
|
private readonly dataProviderService: DataProviderService, |
|
|
private readonly exchangeRateDataService: ExchangeRateDataService, |
|
|
|
|
|
private readonly orderService: OrderService, |
|
|
private readonly orderService: OrderService, |
|
|
private readonly platformService: PlatformService, |
|
|
private readonly platformService: PlatformService, |
|
|
private readonly portfolioService: PortfolioService, |
|
|
private readonly portfolioService: PortfolioService, |
|
@ -49,9 +46,8 @@ export class ImportService { |
|
|
|
|
|
|
|
|
public async getDividends({ |
|
|
public async getDividends({ |
|
|
dataSource, |
|
|
dataSource, |
|
|
symbol, |
|
|
symbol |
|
|
userCurrency |
|
|
}: AssetProfileIdentifier): Promise<Activity[]> { |
|
|
}: AssetProfileIdentifier & { userCurrency: string }): Promise<Activity[]> { |
|
|
|
|
|
try { |
|
|
try { |
|
|
const { firstBuyDate, historicalData, orders } = |
|
|
const { firstBuyDate, historicalData, orders } = |
|
|
await this.portfolioService.getPosition(dataSource, undefined, symbol); |
|
|
await this.portfolioService.getPosition(dataSource, undefined, symbol); |
|
@ -121,22 +117,16 @@ export class ImportService { |
|
|
currency: undefined, |
|
|
currency: undefined, |
|
|
createdAt: undefined, |
|
|
createdAt: undefined, |
|
|
fee: 0, |
|
|
fee: 0, |
|
|
feeInBaseCurrency: 0, |
|
|
feeInAssetProfileCurrency: 0, |
|
|
id: assetProfile.id, |
|
|
id: assetProfile.id, |
|
|
isDraft: false, |
|
|
isDraft: false, |
|
|
SymbolProfile: assetProfile, |
|
|
SymbolProfile: assetProfile, |
|
|
symbolProfileId: assetProfile.id, |
|
|
symbolProfileId: assetProfile.id, |
|
|
type: 'DIVIDEND', |
|
|
type: 'DIVIDEND', |
|
|
unitPrice: marketPrice, |
|
|
unitPrice: marketPrice, |
|
|
|
|
|
unitPriceInAssetProfileCurrency: marketPrice, |
|
|
updatedAt: undefined, |
|
|
updatedAt: undefined, |
|
|
userId: Account?.userId, |
|
|
userId: Account?.userId |
|
|
valueInBaseCurrency: |
|
|
|
|
|
await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
|
|
value, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
userCurrency, |
|
|
|
|
|
date |
|
|
|
|
|
) |
|
|
|
|
|
}; |
|
|
}; |
|
|
}) |
|
|
}) |
|
|
); |
|
|
); |
|
@ -266,17 +256,17 @@ export class ImportService { |
|
|
|
|
|
|
|
|
const activities: Activity[] = []; |
|
|
const activities: Activity[] = []; |
|
|
|
|
|
|
|
|
for (const [index, activity] of activitiesExtendedWithErrors.entries()) { |
|
|
for (const activity of activitiesExtendedWithErrors) { |
|
|
const accountId = activity.accountId; |
|
|
const accountId = activity.accountId; |
|
|
const comment = activity.comment; |
|
|
const comment = activity.comment; |
|
|
const currency = activity.currency; |
|
|
const currency = activity.currency; |
|
|
const date = activity.date; |
|
|
const date = activity.date; |
|
|
const error = activity.error; |
|
|
const error = activity.error; |
|
|
let fee = activity.fee; |
|
|
const fee = activity.fee; |
|
|
const quantity = activity.quantity; |
|
|
const quantity = activity.quantity; |
|
|
const SymbolProfile = activity.SymbolProfile; |
|
|
const SymbolProfile = activity.SymbolProfile; |
|
|
const type = activity.type; |
|
|
const type = activity.type; |
|
|
let unitPrice = activity.unitPrice; |
|
|
const unitPrice = activity.unitPrice; |
|
|
|
|
|
|
|
|
const assetProfile = assetProfiles[ |
|
|
const assetProfile = assetProfiles[ |
|
|
getAssetProfileIdentifier({ |
|
|
getAssetProfileIdentifier({ |
|
@ -284,7 +274,6 @@ export class ImportService { |
|
|
symbol: SymbolProfile.symbol |
|
|
symbol: SymbolProfile.symbol |
|
|
}) |
|
|
}) |
|
|
] ?? { |
|
|
] ?? { |
|
|
currency: SymbolProfile.currency, |
|
|
|
|
|
dataSource: SymbolProfile.dataSource, |
|
|
dataSource: SymbolProfile.dataSource, |
|
|
symbol: SymbolProfile.symbol |
|
|
symbol: SymbolProfile.symbol |
|
|
}; |
|
|
}; |
|
@ -320,35 +309,6 @@ export class ImportService { |
|
|
Account?: { id: string; name: string }; |
|
|
Account?: { id: string; name: string }; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (SymbolProfile.currency !== assetProfile.currency) { |
|
|
|
|
|
// Convert the unit price and fee to the asset currency if the imported
|
|
|
|
|
|
// activity is in a different currency
|
|
|
|
|
|
unitPrice = await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
|
|
unitPrice, |
|
|
|
|
|
SymbolProfile.currency, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
date |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (!isNumber(unitPrice)) { |
|
|
|
|
|
throw new Error( |
|
|
|
|
|
`activities.${index} historical exchange rate at ${format( |
|
|
|
|
|
date, |
|
|
|
|
|
DATE_FORMAT |
|
|
|
|
|
)} is not available from "${SymbolProfile.currency}" to "${ |
|
|
|
|
|
assetProfile.currency |
|
|
|
|
|
}"` |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fee = await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
|
|
fee, |
|
|
|
|
|
SymbolProfile.currency, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
date |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isDryRun) { |
|
|
if (isDryRun) { |
|
|
order = { |
|
|
order = { |
|
|
comment, |
|
|
comment, |
|
@ -400,6 +360,7 @@ export class ImportService { |
|
|
|
|
|
|
|
|
order = await this.orderService.createOrder({ |
|
|
order = await this.orderService.createOrder({ |
|
|
comment, |
|
|
comment, |
|
|
|
|
|
currency, |
|
|
date, |
|
|
date, |
|
|
fee, |
|
|
fee, |
|
|
quantity, |
|
|
quantity, |
|
@ -439,21 +400,8 @@ export class ImportService { |
|
|
...order, |
|
|
...order, |
|
|
error, |
|
|
error, |
|
|
value, |
|
|
value, |
|
|
feeInBaseCurrency: await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
|
|
fee, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
userCurrency, |
|
|
|
|
|
date |
|
|
|
|
|
), |
|
|
|
|
|
// @ts-ignore
|
|
|
// @ts-ignore
|
|
|
SymbolProfile: assetProfile, |
|
|
SymbolProfile: assetProfile |
|
|
valueInBaseCurrency: |
|
|
|
|
|
await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
|
|
value, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
userCurrency, |
|
|
|
|
|
date |
|
|
|
|
|
) |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -520,7 +468,8 @@ export class ImportService { |
|
|
return ( |
|
|
return ( |
|
|
activity.accountId === accountId && |
|
|
activity.accountId === accountId && |
|
|
activity.comment === comment && |
|
|
activity.comment === comment && |
|
|
activity.SymbolProfile.currency === currency && |
|
|
(activity.currency === currency || |
|
|
|
|
|
activity.SymbolProfile.currency === currency) && |
|
|
activity.SymbolProfile.dataSource === dataSource && |
|
|
activity.SymbolProfile.dataSource === dataSource && |
|
|
isSameSecond(activity.date, date) && |
|
|
isSameSecond(activity.date, date) && |
|
|
activity.fee === fee && |
|
|
activity.fee === fee && |
|
@ -538,6 +487,7 @@ export class ImportService { |
|
|
return { |
|
|
return { |
|
|
accountId, |
|
|
accountId, |
|
|
comment, |
|
|
comment, |
|
|
|
|
|
currency, |
|
|
date, |
|
|
date, |
|
|
error, |
|
|
error, |
|
|
fee, |
|
|
fee, |
|
@ -545,7 +495,6 @@ export class ImportService { |
|
|
type, |
|
|
type, |
|
|
unitPrice, |
|
|
unitPrice, |
|
|
SymbolProfile: { |
|
|
SymbolProfile: { |
|
|
currency, |
|
|
|
|
|
dataSource, |
|
|
dataSource, |
|
|
symbol, |
|
|
symbol, |
|
|
activitiesCount: undefined, |
|
|
activitiesCount: undefined, |
|
@ -553,6 +502,7 @@ export class ImportService { |
|
|
assetSubClass: undefined, |
|
|
assetSubClass: undefined, |
|
|
countries: undefined, |
|
|
countries: undefined, |
|
|
createdAt: undefined, |
|
|
createdAt: undefined, |
|
|
|
|
|
currency: undefined, |
|
|
holdings: undefined, |
|
|
holdings: undefined, |
|
|
id: undefined, |
|
|
id: undefined, |
|
|
isActive: true, |
|
|
isActive: true, |
|
@ -633,12 +583,6 @@ export class ImportService { |
|
|
`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}")` |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (assetProfile.currency !== currency) { |
|
|
|
|
|
throw new Error( |
|
|
|
|
|
`activities.${index}.currency ("${currency}") does not match with currency of ${assetProfile.symbol} ("${assetProfile.currency}")` |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assetProfiles[getAssetProfileIdentifier({ dataSource, symbol })] = |
|
|
assetProfiles[getAssetProfileIdentifier({ dataSource, symbol })] = |
|
|