|
|
@ -1,7 +1,7 @@ |
|
|
|
|
|
import { OrderService } from '@ghostfolio/api/app/order/order.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 { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.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 { DEFAULT_CURRENCY } from '@ghostfolio/common/config'; |
|
|
import { DEFAULT_CURRENCY } from '@ghostfolio/common/config'; |
|
|
|
|
|
|
|
|
@ -17,7 +17,7 @@ export class AssetProfileChangedListener { |
|
|
private readonly dataGatheringService: DataGatheringService, |
|
|
private readonly dataGatheringService: DataGatheringService, |
|
|
private readonly dataProviderService: DataProviderService, |
|
|
private readonly dataProviderService: DataProviderService, |
|
|
private readonly exchangeRateDataService: ExchangeRateDataService, |
|
|
private readonly exchangeRateDataService: ExchangeRateDataService, |
|
|
private readonly prismaService: PrismaService |
|
|
private readonly orderService: OrderService |
|
|
) {} |
|
|
) {} |
|
|
|
|
|
|
|
|
@OnEvent(AssetProfileChangedEvent.getName()) |
|
|
@OnEvent(AssetProfileChangedEvent.getName()) |
|
|
@ -30,62 +30,44 @@ export class AssetProfileChangedListener { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (event.currency === DEFAULT_CURRENCY) { |
|
|
if (event.data.currency === DEFAULT_CURRENCY) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Logger.log( |
|
|
Logger.log( |
|
|
`Asset profile changed: ${event.symbol} (${event.currency}). Checking if exchange rate gathering is needed.`, |
|
|
`Asset profile changed: ${event.data.symbol} (${event.data.currency})`, |
|
|
'AssetProfileChangedListener' |
|
|
'AssetProfileChangedListener' |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const existingCurrencies = this.exchangeRateDataService.getCurrencies(); |
|
|
const existingCurrencies = this.exchangeRateDataService.getCurrencies(); |
|
|
const currencyAlreadyExists = existingCurrencies.includes(event.currency); |
|
|
const currencyAlreadyExists = existingCurrencies.includes( |
|
|
|
|
|
event.data.currency |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
if (currencyAlreadyExists) { |
|
|
if (currencyAlreadyExists) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Logger.log( |
|
|
Logger.log( |
|
|
`New currency detected: ${event.currency}. Initializing exchange rate data service.`, |
|
|
`New currency detected: ${event.data.currency}`, |
|
|
'AssetProfileChangedListener' |
|
|
'AssetProfileChangedListener' |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
await this.exchangeRateDataService.initialize(); |
|
|
await this.exchangeRateDataService.initialize(); |
|
|
|
|
|
|
|
|
if ( |
|
|
const { dateOfFirstActivity } = |
|
|
!this.exchangeRateDataService.hasCurrencyPair( |
|
|
await this.orderService.getStatisticsByCurrency(event.data.currency); |
|
|
DEFAULT_CURRENCY, |
|
|
|
|
|
event.currency |
|
|
|
|
|
) |
|
|
|
|
|
) { |
|
|
|
|
|
Logger.warn( |
|
|
|
|
|
`Currency pair ${DEFAULT_CURRENCY}${event.currency} was not added after initialization.`, |
|
|
|
|
|
'AssetProfileChangedListener' |
|
|
|
|
|
); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const firstOrderWithCurrency = await this.prismaService.order.findFirst({ |
|
|
|
|
|
orderBy: [{ date: 'asc' }], |
|
|
|
|
|
select: { date: true }, |
|
|
|
|
|
where: { |
|
|
|
|
|
SymbolProfile: { |
|
|
|
|
|
currency: event.currency |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const startDate = firstOrderWithCurrency.date ?? new Date(); |
|
|
const startDate = dateOfFirstActivity ?? new Date(); |
|
|
|
|
|
|
|
|
Logger.log( |
|
|
Logger.log( |
|
|
`Triggering exchange rate data gathering for ${DEFAULT_CURRENCY}${event.currency} from ${startDate.toISOString()}.`, |
|
|
`Triggering exchange rate data gathering for ${DEFAULT_CURRENCY}${event.data.currency} from ${startDate.toISOString()}.`, |
|
|
'AssetProfileChangedListener' |
|
|
'AssetProfileChangedListener' |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
await this.dataGatheringService.gatherSymbol({ |
|
|
await this.dataGatheringService.gatherSymbol({ |
|
|
dataSource: this.dataProviderService.getDataSourceForExchangeRates(), |
|
|
dataSource: this.dataProviderService.getDataSourceForExchangeRates(), |
|
|
symbol: `${DEFAULT_CURRENCY}${event.currency}`, |
|
|
symbol: `${DEFAULT_CURRENCY}${event.data.currency}`, |
|
|
date: startDate |
|
|
date: startDate |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|