Browse Source

apply feedback

pull/5917/head
Sven Günther 1 month ago
committed by Thomas Kaul
parent
commit
ab68e52efe
  1. 13
      apps/api/src/app/import/import.service.ts
  2. 17
      apps/api/src/app/order/order.service.ts
  3. 9
      apps/api/src/events/asset-profile-changed.event.ts
  4. 44
      apps/api/src/events/asset-profile-changed.listener.ts
  5. 4
      apps/api/src/events/events.module.ts

13
apps/api/src/app/import/import.service.ts

@ -609,15 +609,16 @@ export class ImportService {
priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH
}); });
uniqueActivities.forEach(({ SymbolProfile }) => { for (const { SymbolProfile } of uniqueActivities) {
this.eventEmitter.emit( this.eventEmitter.emit(
AssetProfileChangedEvent.getName(), AssetProfileChangedEvent.getName(),
new AssetProfileChangedEvent( new AssetProfileChangedEvent({
SymbolProfile.currency, currency: SymbolProfile.currency,
SymbolProfile.symbol dataSource: SymbolProfile.dataSource,
) symbol: SymbolProfile.symbol
})
); );
}); }
} }
return activities; return activities;

17
apps/api/src/app/order/order.service.ts

@ -227,18 +227,19 @@ export class OrderService {
} }
this.eventEmitter.emit( this.eventEmitter.emit(
PortfolioChangedEvent.getName(), AssetProfileChangedEvent.getName(),
new PortfolioChangedEvent({ new AssetProfileChangedEvent({
userId: order.userId currency: order.SymbolProfile.currency,
dataSource: order.SymbolProfile.dataSource,
symbol: order.SymbolProfile.symbol
}) })
); );
this.eventEmitter.emit( this.eventEmitter.emit(
AssetProfileChangedEvent.getName(), PortfolioChangedEvent.getName(),
new AssetProfileChangedEvent( new PortfolioChangedEvent({
order.SymbolProfile.currency, userId: order.userId
order.SymbolProfile.symbol })
)
); );
return order; return order;

9
apps/api/src/events/asset-profile-changed.event.ts

@ -1,12 +1,11 @@
export class AssetProfileChangedEvent { import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces';
private static readonly eventName = 'asset-profile.changed';
export class AssetProfileChangedEvent {
public constructor( public constructor(
public readonly currency: string, public readonly data: AssetProfileIdentifier & { currency: string }
public readonly symbol: string
) {} ) {}
public static getName(): string { public static getName(): string {
return AssetProfileChangedEvent.eventName; return 'assetProfile.changed';
} }
} }

44
apps/api/src/events/asset-profile-changed.listener.ts

@ -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
}); });
} }

4
apps/api/src/events/events.module.ts

@ -1,8 +1,8 @@
import { OrderModule } from '@ghostfolio/api/app/order/order.module';
import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module';
import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.module'; import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.module';
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';
import { DataGatheringModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module'; import { DataGatheringModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
@ -16,7 +16,7 @@ import { PortfolioChangedListener } from './portfolio-changed.listener';
DataGatheringModule, DataGatheringModule,
DataProviderModule, DataProviderModule,
ExchangeRateDataModule, ExchangeRateDataModule,
PrismaModule, OrderModule,
RedisCacheModule RedisCacheModule
], ],
providers: [AssetProfileChangedListener, PortfolioChangedListener] providers: [AssetProfileChangedListener, PortfolioChangedListener]

Loading…
Cancel
Save