|
@ -8,7 +8,10 @@ import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate- |
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile.service'; |
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile.service'; |
|
|
import { parseDate } from '@ghostfolio/common/helper'; |
|
|
import { parseDate } from '@ghostfolio/common/helper'; |
|
|
import { ImportResponse, UniqueAsset } from '@ghostfolio/common/interfaces'; |
|
|
import { ImportResponse, UniqueAsset } from '@ghostfolio/common/interfaces'; |
|
|
import { OrderWithAccount } from '@ghostfolio/common/types'; |
|
|
import { |
|
|
|
|
|
AccountWithPlatform, |
|
|
|
|
|
OrderWithAccount |
|
|
|
|
|
} from '@ghostfolio/common/types'; |
|
|
import { Injectable } from '@nestjs/common'; |
|
|
import { Injectable } from '@nestjs/common'; |
|
|
import { SymbolProfile } from '@prisma/client'; |
|
|
import { SymbolProfile } from '@prisma/client'; |
|
|
import Big from 'big.js'; |
|
|
import Big from 'big.js'; |
|
@ -28,14 +31,18 @@ export class ImportService { |
|
|
|
|
|
|
|
|
public async getDividends({ |
|
|
public async getDividends({ |
|
|
dataSource, |
|
|
dataSource, |
|
|
symbol |
|
|
symbol, |
|
|
}: UniqueAsset): Promise<ImportResponse> { |
|
|
userCurrency |
|
|
|
|
|
}: UniqueAsset & { userCurrency: string }): Promise<ImportResponse> { |
|
|
try { |
|
|
try { |
|
|
const { firstBuyDate } = await this.portfolioService.getPosition( |
|
|
const { firstBuyDate, historicalData, orders } = |
|
|
dataSource, |
|
|
await this.portfolioService.getPosition(dataSource, undefined, symbol); |
|
|
undefined, |
|
|
|
|
|
symbol |
|
|
const accounts = orders.map(({ Account }) => { |
|
|
); |
|
|
return Account; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const mostFrequentAccount = this.getMostFrequentAccount(accounts); |
|
|
|
|
|
|
|
|
const [[assetProfile], dividends] = await Promise.all([ |
|
|
const [[assetProfile], dividends] = await Promise.all([ |
|
|
this.symbolProfileService.getSymbolProfiles([ |
|
|
this.symbolProfileService.getSymbolProfiles([ |
|
@ -55,10 +62,20 @@ export class ImportService { |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
activities: Object.entries(dividends).map( |
|
|
activities: Object.entries(dividends).map( |
|
|
([dateString, historicalDataItem]) => { |
|
|
([dateString, { marketPrice }]) => { |
|
|
|
|
|
const quantity = |
|
|
|
|
|
historicalData.find((historicalDataItem) => { |
|
|
|
|
|
return historicalDataItem.date === dateString; |
|
|
|
|
|
})?.quantity ?? 0; |
|
|
|
|
|
|
|
|
|
|
|
const value = new Big(quantity).mul(marketPrice).toNumber(); |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
accountId: undefined, |
|
|
quantity, |
|
|
accountUserId: undefined, |
|
|
value, |
|
|
|
|
|
Account: mostFrequentAccount, |
|
|
|
|
|
accountId: mostFrequentAccount.id, |
|
|
|
|
|
accountUserId: mostFrequentAccount.userId, |
|
|
comment: undefined, |
|
|
comment: undefined, |
|
|
createdAt: undefined, |
|
|
createdAt: undefined, |
|
|
date: parseDate(dateString), |
|
|
date: parseDate(dateString), |
|
@ -66,15 +83,17 @@ export class ImportService { |
|
|
feeInBaseCurrency: 0, |
|
|
feeInBaseCurrency: 0, |
|
|
id: assetProfile.id, |
|
|
id: assetProfile.id, |
|
|
isDraft: false, |
|
|
isDraft: false, |
|
|
quantity: 0, |
|
|
|
|
|
SymbolProfile: <SymbolProfile>(<unknown>assetProfile), |
|
|
SymbolProfile: <SymbolProfile>(<unknown>assetProfile), |
|
|
symbolProfileId: undefined, |
|
|
symbolProfileId: assetProfile.id, |
|
|
type: 'DIVIDEND', |
|
|
type: 'DIVIDEND', |
|
|
unitPrice: historicalDataItem.marketPrice, |
|
|
unitPrice: marketPrice, |
|
|
updatedAt: undefined, |
|
|
updatedAt: undefined, |
|
|
userId: undefined, |
|
|
userId: mostFrequentAccount.userId, |
|
|
value: 0, |
|
|
valueInBaseCurrency: this.exchangeRateDataService.toCurrency( |
|
|
valueInBaseCurrency: 0 |
|
|
value, |
|
|
|
|
|
assetProfile.currency, |
|
|
|
|
|
userCurrency |
|
|
|
|
|
) |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
) |
|
|
) |
|
@ -225,6 +244,31 @@ export class ImportService { |
|
|
return activities; |
|
|
return activities; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private getMostFrequentAccount(accounts: AccountWithPlatform[]) { |
|
|
|
|
|
const accountFrequencyCountMap: { [accountId: string]: number } = {}; |
|
|
|
|
|
|
|
|
|
|
|
// Iterate through the array of accounts and increment the frequency for each account
|
|
|
|
|
|
for (const account of accounts) { |
|
|
|
|
|
accountFrequencyCountMap[account.id] = |
|
|
|
|
|
(accountFrequencyCountMap[account.id] || 0) + 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Find the account with the highest frequency
|
|
|
|
|
|
let maxFrequencyCount = 0; |
|
|
|
|
|
let mostFrequentAccount: AccountWithPlatform; |
|
|
|
|
|
|
|
|
|
|
|
for (const accountId in accountFrequencyCountMap) { |
|
|
|
|
|
if (accountFrequencyCountMap[accountId] > maxFrequencyCount) { |
|
|
|
|
|
mostFrequentAccount = accounts.find( |
|
|
|
|
|
(account) => account.id === accountId |
|
|
|
|
|
); |
|
|
|
|
|
maxFrequencyCount = accountFrequencyCountMap[accountId]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return mostFrequentAccount; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private async validateActivities({ |
|
|
private async validateActivities({ |
|
|
activitiesDto, |
|
|
activitiesDto, |
|
|
maxActivitiesToImport, |
|
|
maxActivitiesToImport, |
|
|