Browse Source

Refactoring

pull/5917/head
Thomas Kaul 1 month ago
parent
commit
db40f3747f
  1. 14
      apps/api/src/app/import/import.service.ts
  2. 45
      apps/api/src/events/asset-profile-changed.listener.ts

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

@ -4,7 +4,6 @@ import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { OrderService } from '@ghostfolio/api/app/order/order.service'; import { OrderService } from '@ghostfolio/api/app/order/order.service';
import { PlatformService } from '@ghostfolio/api/app/platform/platform.service'; 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 { AssetProfileChangedEvent } from '@ghostfolio/api/events/asset-profile-changed.event';
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 { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
@ -29,7 +28,6 @@ import {
} from '@ghostfolio/common/types'; } from '@ghostfolio/common/types';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
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, isAfter, isSameSecond, parseISO } from 'date-fns'; import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns';
@ -46,7 +44,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 eventEmitter: EventEmitter2,
private readonly marketDataService: MarketDataService, private readonly marketDataService: MarketDataService,
private readonly orderService: OrderService, private readonly orderService: OrderService,
private readonly platformService: PlatformService, private readonly platformService: PlatformService,
@ -608,17 +605,6 @@ export class ImportService {
}), }),
priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH
}); });
for (const { SymbolProfile } of uniqueActivities) {
this.eventEmitter.emit(
AssetProfileChangedEvent.getName(),
new AssetProfileChangedEvent({
currency: SymbolProfile.currency,
dataSource: SymbolProfile.dataSource,
symbol: SymbolProfile.symbol
})
);
}
} }
return activities; return activities;

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

@ -22,53 +22,40 @@ export class AssetProfileChangedListener {
@OnEvent(AssetProfileChangedEvent.getName()) @OnEvent(AssetProfileChangedEvent.getName())
public async handleAssetProfileChanged(event: AssetProfileChangedEvent) { public async handleAssetProfileChanged(event: AssetProfileChangedEvent) {
const isEnabled = this.configurationService.get(
'ENABLE_FEATURE_GATHER_NEW_EXCHANGE_RATES'
);
if (isEnabled === false) {
return;
}
if (event.data.currency === DEFAULT_CURRENCY) {
return;
}
Logger.log( Logger.log(
`Asset profile changed: ${event.data.symbol} (${event.data.currency})`, `Asset profile of ${event.data.symbol} (${event.data.dataSource}) has changed`,
'AssetProfileChangedListener' 'AssetProfileChangedListener'
); );
const existingCurrencies = this.exchangeRateDataService.getCurrencies(); if (
const currencyAlreadyExists = existingCurrencies.includes( this.configurationService.get(
event.data.currency 'ENABLE_FEATURE_GATHER_NEW_EXCHANGE_RATES'
); ) === false ||
event.data.currency === DEFAULT_CURRENCY
if (currencyAlreadyExists) { ) {
return; return;
} }
const existingCurrencies = this.exchangeRateDataService.getCurrencies();
if (!existingCurrencies.includes(event.data.currency)) {
Logger.log( Logger.log(
`New currency detected: ${event.data.currency}`, `New currency ${event.data.currency} has been detected`,
'AssetProfileChangedListener' 'AssetProfileChangedListener'
); );
await this.exchangeRateDataService.initialize(); await this.exchangeRateDataService.initialize();
}
const { dateOfFirstActivity } = const { dateOfFirstActivity } =
await this.orderService.getStatisticsByCurrency(event.data.currency); await this.orderService.getStatisticsByCurrency(event.data.currency);
const startDate = dateOfFirstActivity ?? new Date(); if (dateOfFirstActivity) {
Logger.log(
`Triggering exchange rate data gathering for ${DEFAULT_CURRENCY}${event.data.currency} from ${startDate.toISOString()}.`,
'AssetProfileChangedListener'
);
await this.dataGatheringService.gatherSymbol({ await this.dataGatheringService.gatherSymbol({
dataSource: this.dataProviderService.getDataSourceForExchangeRates(), dataSource: this.dataProviderService.getDataSourceForExchangeRates(),
symbol: `${DEFAULT_CURRENCY}${event.data.currency}`, date: dateOfFirstActivity,
date: startDate symbol: `${DEFAULT_CURRENCY}${event.data.currency}`
}); });
} }
} }
}

Loading…
Cancel
Save