Browse Source

Move currency list to new currency module

pull/4290/head
enen92 3 months ago
parent
commit
92477c9137
  1. 2
      apps/api/src/app/admin/admin.module.ts
  2. 5
      apps/api/src/app/admin/admin.service.ts
  3. 3
      apps/api/src/app/app.controller.ts
  4. 2
      apps/api/src/app/app.module.ts
  5. 1
      apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts
  6. 4
      apps/api/src/app/info/info.module.ts
  7. 6
      apps/api/src/app/info/info.service.ts
  8. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts
  9. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts
  10. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts
  11. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts
  12. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts
  13. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts
  14. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts
  15. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-liability.spec.ts
  16. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-msft-buy-with-dividend.spec.ts
  17. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts
  18. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts
  19. 7
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts
  20. 13
      apps/api/src/services/currency/currency.module.ts
  21. 77
      apps/api/src/services/currency/currency.service.ts
  22. 2
      apps/api/src/services/exchange-rate-data/exchange-rate-data.module.ts
  23. 73
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

2
apps/api/src/app/admin/admin.module.ts

@ -4,6 +4,7 @@ import { SubscriptionModule } from '@ghostfolio/api/app/subscription/subscriptio
import { TransformDataSourceInRequestModule } from '@ghostfolio/api/interceptors/transform-data-source-in-request/transform-data-source-in-request.module'; import { TransformDataSourceInRequestModule } from '@ghostfolio/api/interceptors/transform-data-source-in-request/transform-data-source-in-request.module';
import { ApiModule } from '@ghostfolio/api/services/api/api.module'; import { ApiModule } from '@ghostfolio/api/services/api/api.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { CurrencyModule } from '@ghostfolio/api/services/currency/currency.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 { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module'; import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module';
@ -23,6 +24,7 @@ import { QueueModule } from './queue/queue.module';
ApiModule, ApiModule,
BenchmarkModule, BenchmarkModule,
ConfigurationModule, ConfigurationModule,
CurrencyModule,
DataGatheringModule, DataGatheringModule,
DataProviderModule, DataProviderModule,
ExchangeRateDataModule, ExchangeRateDataModule,

5
apps/api/src/app/admin/admin.service.ts

@ -3,6 +3,7 @@ import { OrderService } from '@ghostfolio/api/app/order/order.service';
import { SubscriptionService } from '@ghostfolio/api/app/subscription/subscription.service'; import { SubscriptionService } from '@ghostfolio/api/app/subscription/subscription.service';
import { environment } from '@ghostfolio/api/environments/environment'; import { environment } from '@ghostfolio/api/environments/environment';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { CurrencyService } from '@ghostfolio/api/services/currency/currency.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 { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
@ -49,6 +50,7 @@ import { groupBy } from 'lodash';
export class AdminService { export class AdminService {
public constructor( public constructor(
private readonly benchmarkService: BenchmarkService, private readonly benchmarkService: BenchmarkService,
private readonly currencyService: CurrencyService,
private readonly configurationService: ConfigurationService, private readonly configurationService: ConfigurationService,
private readonly dataProviderService: DataProviderService, private readonly dataProviderService: DataProviderService,
private readonly exchangeRateDataService: ExchangeRateDataService, private readonly exchangeRateDataService: ExchangeRateDataService,
@ -112,7 +114,7 @@ export class AdminService {
} }
public async get(): Promise<AdminData> { public async get(): Promise<AdminData> {
const exchangeRates = this.exchangeRateDataService const exchangeRates = this.currencyService
.getCurrencies() .getCurrencies()
.filter((currency) => { .filter((currency) => {
return currency !== DEFAULT_CURRENCY; return currency !== DEFAULT_CURRENCY;
@ -513,6 +515,7 @@ export class AdminService {
if (key === PROPERTY_IS_READ_ONLY_MODE && value === 'true') { if (key === PROPERTY_IS_READ_ONLY_MODE && value === 'true') {
await this.putSetting(PROPERTY_IS_USER_SIGNUP_ENABLED, 'false'); await this.putSetting(PROPERTY_IS_USER_SIGNUP_ENABLED, 'false');
} else if (key === PROPERTY_CURRENCIES) { } else if (key === PROPERTY_CURRENCIES) {
await this.currencyService.initialize();
await this.exchangeRateDataService.initialize(); await this.exchangeRateDataService.initialize();
} }

3
apps/api/src/app/app.controller.ts

@ -1,3 +1,4 @@
import { CurrencyService } from '@ghostfolio/api/services/currency/currency.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 { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
@ -5,6 +6,7 @@ import { Controller } from '@nestjs/common';
@Controller() @Controller()
export class AppController { export class AppController {
public constructor( public constructor(
private readonly currencyService: CurrencyService,
private readonly exchangeRateDataService: ExchangeRateDataService private readonly exchangeRateDataService: ExchangeRateDataService
) { ) {
this.initialize(); this.initialize();
@ -12,6 +14,7 @@ export class AppController {
private async initialize() { private async initialize() {
try { try {
await this.currencyService.initialize();
await this.exchangeRateDataService.initialize(); await this.exchangeRateDataService.initialize();
} catch {} } catch {}
} }

2
apps/api/src/app/app.module.ts

@ -22,6 +22,7 @@ import { ServeStaticModule } from '@nestjs/serve-static';
import { StatusCodes } from 'http-status-codes'; import { StatusCodes } from 'http-status-codes';
import { join } from 'path'; import { join } from 'path';
import { CurrencyModule } from '../services/currency/currency.module';
import { AccessModule } from './access/access.module'; import { AccessModule } from './access/access.module';
import { AccountModule } from './account/account.module'; import { AccountModule } from './account/account.module';
import { AdminModule } from './admin/admin.module'; import { AdminModule } from './admin/admin.module';
@ -75,6 +76,7 @@ import { UserModule } from './user/user.module';
CacheModule, CacheModule,
ConfigModule.forRoot(), ConfigModule.forRoot(),
ConfigurationModule, ConfigurationModule,
CurrencyModule,
DataGatheringModule, DataGatheringModule,
DataProviderModule, DataProviderModule,
EventEmitterModule.forRoot(), EventEmitterModule.forRoot(),

1
apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts

@ -1,6 +1,7 @@
import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { CryptocurrencyModule } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.module'; import { CryptocurrencyModule } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.module';
import { CurrencyModule } from '@ghostfolio/api/services/currency/currency.module';
import { AlphaVantageService } from '@ghostfolio/api/services/data-provider/alpha-vantage/alpha-vantage.service'; import { AlphaVantageService } from '@ghostfolio/api/services/data-provider/alpha-vantage/alpha-vantage.service';
import { CoinGeckoService } from '@ghostfolio/api/services/data-provider/coingecko/coingecko.service'; import { CoinGeckoService } from '@ghostfolio/api/services/data-provider/coingecko/coingecko.service';
import { YahooFinanceDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service'; import { YahooFinanceDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service';

4
apps/api/src/app/info/info.module.ts

@ -4,8 +4,8 @@ import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.mo
import { UserModule } from '@ghostfolio/api/app/user/user.module'; import { UserModule } from '@ghostfolio/api/app/user/user.module';
import { TransformDataSourceInResponseModule } from '@ghostfolio/api/interceptors/transform-data-source-in-response/transform-data-source-in-response.module'; import { TransformDataSourceInResponseModule } from '@ghostfolio/api/interceptors/transform-data-source-in-response/transform-data-source-in-response.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { CurrencyModule } from '@ghostfolio/api/services/currency/currency.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 { PropertyModule } from '@ghostfolio/api/services/property/property.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.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 { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module'; import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module';
@ -21,9 +21,9 @@ import { InfoService } from './info.service';
imports: [ imports: [
BenchmarkModule, BenchmarkModule,
ConfigurationModule, ConfigurationModule,
CurrencyModule,
DataGatheringModule, DataGatheringModule,
DataProviderModule, DataProviderModule,
ExchangeRateDataModule,
JwtModule.register({ JwtModule.register({
secret: process.env.JWT_SECRET_KEY, secret: process.env.JWT_SECRET_KEY,
signOptions: { expiresIn: '30 days' } signOptions: { expiresIn: '30 days' }

6
apps/api/src/app/info/info.service.ts

@ -3,7 +3,7 @@ import { PlatformService } from '@ghostfolio/api/app/platform/platform.service';
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
import { UserService } from '@ghostfolio/api/app/user/user.service'; import { UserService } from '@ghostfolio/api/app/user/user.service';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { CurrencyService } from '@ghostfolio/api/services/currency/currency.service';
import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import { import {
DEFAULT_CURRENCY, DEFAULT_CURRENCY,
@ -41,7 +41,7 @@ export class InfoService {
public constructor( public constructor(
private readonly benchmarkService: BenchmarkService, private readonly benchmarkService: BenchmarkService,
private readonly configurationService: ConfigurationService, private readonly configurationService: ConfigurationService,
private readonly exchangeRateDataService: ExchangeRateDataService, private readonly currencyService: CurrencyService,
private readonly jwtService: JwtService, private readonly jwtService: JwtService,
private readonly platformService: PlatformService, private readonly platformService: PlatformService,
private readonly propertyService: PropertyService, private readonly propertyService: PropertyService,
@ -127,7 +127,7 @@ export class InfoService {
statistics, statistics,
subscriptionOffers, subscriptionOffers,
baseCurrency: DEFAULT_CURRENCY, baseCurrency: DEFAULT_CURRENCY,
currencies: this.exchangeRateDataService.getCurrencies() currencies: this.currencyService.getCurrencies()
}; };
} }

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts

@ -76,12 +76,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts

@ -76,12 +76,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-liability.spec.ts

@ -63,12 +63,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-msft-buy-with-dividend.spec.ts

@ -76,12 +76,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-no-orders.spec.ts

@ -58,12 +58,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts

@ -77,12 +77,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

7
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts

@ -77,12 +77,7 @@ describe('PortfolioCalculator', () => {
currentRateService = new CurrentRateService(null, null, null, null); currentRateService = new CurrentRateService(null, null, null, null);
exchangeRateDataService = new ExchangeRateDataService( exchangeRateDataService = new ExchangeRateDataService(null, null, null);
null,
null,
null,
null
);
portfolioSnapshotService = new PortfolioSnapshotService(null); portfolioSnapshotService = new PortfolioSnapshotService(null);

13
apps/api/src/services/currency/currency.module.ts

@ -0,0 +1,13 @@
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { CurrencyService } from '@ghostfolio/api/services/currency/currency.service';
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';
import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { Module } from '@nestjs/common';
@Module({
exports: [CurrencyService],
imports: [ConfigurationModule, PrismaModule, PropertyModule],
providers: [CurrencyService]
})
export class CurrencyModule {}

77
apps/api/src/services/currency/currency.service.ts

@ -0,0 +1,77 @@
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import {
DEFAULT_CURRENCY,
DERIVED_CURRENCIES,
PROPERTY_CURRENCIES
} from '@ghostfolio/common/config';
import { Injectable } from '@nestjs/common';
import { uniq } from 'lodash';
@Injectable()
export class CurrencyService {
private currencies: string[] = [];
public constructor(
private readonly prismaService: PrismaService,
private readonly propertyService: PropertyService
) {}
public getCurrencies() {
return this.currencies?.length > 0 ? this.currencies : [DEFAULT_CURRENCY];
}
public async initialize() {
this.currencies = await this.prepareCurrencies();
}
private async prepareCurrencies(): Promise<string[]> {
let currencies: string[] = [DEFAULT_CURRENCY];
(
await this.prismaService.account.findMany({
distinct: ['currency'],
orderBy: [{ currency: 'asc' }],
select: { currency: true },
where: {
currency: {
not: null
}
}
})
).forEach(({ currency }) => {
currencies.push(currency);
});
(
await this.prismaService.symbolProfile.findMany({
distinct: ['currency'],
orderBy: [{ currency: 'asc' }],
select: { currency: true }
})
).forEach(({ currency }) => {
currencies.push(currency);
});
const customCurrencies = (await this.propertyService.getByKey(
PROPERTY_CURRENCIES
)) as string[];
if (customCurrencies?.length > 0) {
currencies = currencies.concat(customCurrencies);
}
// Add derived currencies
currencies.push('USX');
for (const { currency, rootCurrency } of DERIVED_CURRENCIES) {
if (currencies.includes(currency) || currencies.includes(rootCurrency)) {
currencies.push(currency);
currencies.push(rootCurrency);
}
}
return uniq(currencies).filter(Boolean).sort();
}
}

2
apps/api/src/services/exchange-rate-data/exchange-rate-data.module.ts

@ -1,4 +1,5 @@
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { CurrencyModule } from '@ghostfolio/api/services/currency/currency.module';
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module';
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 { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module'; import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module';
@ -11,6 +12,7 @@ import { Module } from '@nestjs/common';
exports: [ExchangeRateDataService], exports: [ExchangeRateDataService],
imports: [ imports: [
ConfigurationModule, ConfigurationModule,
CurrencyModule,
DataProviderModule, DataProviderModule,
MarketDataModule, MarketDataModule,
PrismaModule, PrismaModule,

73
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -1,14 +1,9 @@
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor'; import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { CurrencyService } from '@ghostfolio/api/services/currency/currency.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { DEFAULT_CURRENCY } from '@ghostfolio/common/config';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import {
DEFAULT_CURRENCY,
DERIVED_CURRENCIES,
PROPERTY_CURRENCIES
} from '@ghostfolio/common/config';
import { import {
DATE_FORMAT, DATE_FORMAT,
getYesterday, getYesterday,
@ -23,26 +18,20 @@ import {
isToday, isToday,
subDays subDays
} from 'date-fns'; } from 'date-fns';
import { isNumber, uniq } from 'lodash'; import { isNumber } from 'lodash';
import ms from 'ms'; import ms from 'ms';
@Injectable() @Injectable()
export class ExchangeRateDataService { export class ExchangeRateDataService {
private currencies: string[] = [];
private currencyPairs: IDataGatheringItem[] = []; private currencyPairs: IDataGatheringItem[] = [];
private exchangeRates: { [currencyPair: string]: number } = {}; private exchangeRates: { [currencyPair: string]: number } = {};
public constructor( public constructor(
private readonly currencyService: CurrencyService,
private readonly dataProviderService: DataProviderService, private readonly dataProviderService: DataProviderService,
private readonly marketDataService: MarketDataService, private readonly marketDataService: MarketDataService
private readonly prismaService: PrismaService,
private readonly propertyService: PropertyService
) {} ) {}
public getCurrencies() {
return this.currencies?.length > 0 ? this.currencies : [DEFAULT_CURRENCY];
}
public getCurrencyPairs() { public getCurrencyPairs() {
return this.currencyPairs; return this.currencyPairs;
} }
@ -133,7 +122,6 @@ export class ExchangeRateDataService {
} }
public async initialize() { public async initialize() {
this.currencies = await this.prepareCurrencies();
this.currencyPairs = []; this.currencyPairs = [];
this.exchangeRates = {}; this.exchangeRates = {};
@ -141,7 +129,7 @@ export class ExchangeRateDataService {
currency1, currency1,
currency2, currency2,
dataSource dataSource
} of this.prepareCurrencyPairs(this.currencies)) { } of this.prepareCurrencyPairs(this.currencyService.getCurrencies())) {
this.currencyPairs.push({ this.currencyPairs.push({
dataSource, dataSource,
symbol: `${currency1}${currency2}` symbol: `${currency1}${currency2}`
@ -469,55 +457,6 @@ export class ExchangeRateDataService {
return factors; return factors;
} }
private async prepareCurrencies(): Promise<string[]> {
let currencies: string[] = [DEFAULT_CURRENCY];
(
await this.prismaService.account.findMany({
distinct: ['currency'],
orderBy: [{ currency: 'asc' }],
select: { currency: true },
where: {
currency: {
not: null
}
}
})
).forEach(({ currency }) => {
currencies.push(currency);
});
(
await this.prismaService.symbolProfile.findMany({
distinct: ['currency'],
orderBy: [{ currency: 'asc' }],
select: { currency: true }
})
).forEach(({ currency }) => {
currencies.push(currency);
});
const customCurrencies = (await this.propertyService.getByKey(
PROPERTY_CURRENCIES
)) as string[];
if (customCurrencies?.length > 0) {
currencies = currencies.concat(customCurrencies);
}
// Add derived currencies
currencies.push('USX');
for (const { currency, rootCurrency } of DERIVED_CURRENCIES) {
if (currencies.includes(currency) || currencies.includes(rootCurrency)) {
currencies.push(currency);
currencies.push(rootCurrency);
}
}
return uniq(currencies).filter(Boolean).sort();
}
private prepareCurrencyPairs(aCurrencies: string[]) { private prepareCurrencyPairs(aCurrencies: string[]) {
return aCurrencies return aCurrencies
.filter((currency) => { .filter((currency) => {

Loading…
Cancel
Save