|
|
@ -15,7 +15,6 @@ import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathe |
|
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|
|
|
import { DATA_GATHERING_QUEUE_PRIORITY_HIGH } from '@ghostfolio/common/config'; |
|
|
|
import { |
|
|
|
DATE_FORMAT, |
|
|
|
getAssetProfileIdentifier, |
|
|
|
parseDate |
|
|
|
} from '@ghostfolio/common/helper'; |
|
|
@ -29,8 +28,8 @@ import { |
|
|
|
import { Injectable } from '@nestjs/common'; |
|
|
|
import { DataSource, Prisma, SymbolProfile } from '@prisma/client'; |
|
|
|
import { Big } from 'big.js'; |
|
|
|
import { endOfToday, format, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
|
import { isNumber, uniqBy } from 'lodash'; |
|
|
|
import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
|
import { uniqBy } from 'lodash'; |
|
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
@ -121,14 +120,14 @@ export class ImportService { |
|
|
|
currency: undefined, |
|
|
|
createdAt: undefined, |
|
|
|
fee: 0, |
|
|
|
feeInSymbolCurrency: 0, |
|
|
|
feeInAssetProfileCurrency: 0, |
|
|
|
id: assetProfile.id, |
|
|
|
isDraft: false, |
|
|
|
SymbolProfile: assetProfile, |
|
|
|
symbolProfileId: assetProfile.id, |
|
|
|
type: 'DIVIDEND', |
|
|
|
unitPrice: marketPrice, |
|
|
|
unitPriceInSymbolCurrency: marketPrice, |
|
|
|
unitPriceInAssetProfileCurrency: marketPrice, |
|
|
|
updatedAt: undefined, |
|
|
|
userId: Account?.userId, |
|
|
|
valueInBaseCurrency: |
|
|
@ -267,17 +266,17 @@ export class ImportService { |
|
|
|
|
|
|
|
const activities: Activity[] = []; |
|
|
|
|
|
|
|
for (const [index, activity] of activitiesExtendedWithErrors.entries()) { |
|
|
|
for (const activity of activitiesExtendedWithErrors) { |
|
|
|
const accountId = activity.accountId; |
|
|
|
const comment = activity.comment; |
|
|
|
const currency = activity.currency; |
|
|
|
const date = activity.date; |
|
|
|
const error = activity.error; |
|
|
|
let fee = activity.fee; |
|
|
|
const fee = activity.fee; |
|
|
|
const quantity = activity.quantity; |
|
|
|
const SymbolProfile = activity.SymbolProfile; |
|
|
|
const type = activity.type; |
|
|
|
let unitPrice = activity.unitPrice; |
|
|
|
const unitPrice = activity.unitPrice; |
|
|
|
|
|
|
|
const assetProfile = assetProfiles[ |
|
|
|
getAssetProfileIdentifier({ |
|
|
@ -285,7 +284,6 @@ export class ImportService { |
|
|
|
symbol: SymbolProfile.symbol |
|
|
|
}) |
|
|
|
] ?? { |
|
|
|
currency: SymbolProfile.currency, |
|
|
|
dataSource: SymbolProfile.dataSource, |
|
|
|
symbol: SymbolProfile.symbol |
|
|
|
}; |
|
|
@ -321,35 +319,6 @@ export class ImportService { |
|
|
|
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) { |
|
|
|
order = { |
|
|
|
comment, |
|
|
@ -401,6 +370,7 @@ export class ImportService { |
|
|
|
|
|
|
|
order = await this.orderService.createOrder({ |
|
|
|
comment, |
|
|
|
currency, |
|
|
|
date, |
|
|
|
fee, |
|
|
|
quantity, |
|
|
@ -445,7 +415,7 @@ export class ImportService { |
|
|
|
valueInBaseCurrency: |
|
|
|
await this.exchangeRateDataService.toCurrencyAtDate( |
|
|
|
value, |
|
|
|
assetProfile.currency, |
|
|
|
currency ?? assetProfile.currency, |
|
|
|
userCurrency, |
|
|
|
date |
|
|
|
) |
|
|
@ -515,7 +485,8 @@ export class ImportService { |
|
|
|
return ( |
|
|
|
activity.accountId === accountId && |
|
|
|
activity.comment === comment && |
|
|
|
activity.SymbolProfile.currency === currency && |
|
|
|
(activity.currency === currency || |
|
|
|
activity.SymbolProfile.currency === currency) && |
|
|
|
activity.SymbolProfile.dataSource === dataSource && |
|
|
|
isSameSecond(activity.date, date) && |
|
|
|
activity.fee === fee && |
|
|
@ -533,6 +504,7 @@ export class ImportService { |
|
|
|
return { |
|
|
|
accountId, |
|
|
|
comment, |
|
|
|
currency, |
|
|
|
date, |
|
|
|
error, |
|
|
|
fee, |
|
|
@ -540,7 +512,7 @@ export class ImportService { |
|
|
|
type, |
|
|
|
unitPrice, |
|
|
|
SymbolProfile: { |
|
|
|
currency, |
|
|
|
currency: undefined, |
|
|
|
dataSource, |
|
|
|
symbol, |
|
|
|
activitiesCount: undefined, |
|
|
|