mirror of https://github.com/ghostfolio/ghostfolio
				
				
			
				 6 changed files with 125 additions and 63 deletions
			
			
		@ -0,0 +1,30 @@ | 
				
			|||||
 | 
					import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; | 
				
			||||
 | 
					import { DataProviderService } from '@ghostfolio/api/services/data-provider.service'; | 
				
			||||
 | 
					import { AlphaVantageService } from '@ghostfolio/api/services/data-provider/alpha-vantage/alpha-vantage.service'; | 
				
			||||
 | 
					import { GhostfolioScraperApiService } from '@ghostfolio/api/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service'; | 
				
			||||
 | 
					import { RakutenRapidApiService } from '@ghostfolio/api/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service'; | 
				
			||||
 | 
					import { YahooFinanceService } from '@ghostfolio/api/services/data-provider/yahoo-finance/yahoo-finance.service'; | 
				
			||||
 | 
					import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; | 
				
			||||
 | 
					import { PrismaService } from '@ghostfolio/api/services/prisma.service'; | 
				
			||||
 | 
					import { Module } from '@nestjs/common'; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import { CurrentRateService } from './current-rate.service'; | 
				
			||||
 | 
					import { MarketDataService } from './market-data.service'; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					@Module({ | 
				
			||||
 | 
					  imports: [], | 
				
			||||
 | 
					  controllers: [], | 
				
			||||
 | 
					  providers: [ | 
				
			||||
 | 
					    AlphaVantageService, | 
				
			||||
 | 
					    ConfigurationService, | 
				
			||||
 | 
					    CurrentRateService, | 
				
			||||
 | 
					    DataProviderService, | 
				
			||||
 | 
					    ExchangeRateDataService, | 
				
			||||
 | 
					    GhostfolioScraperApiService, | 
				
			||||
 | 
					    MarketDataService, | 
				
			||||
 | 
					    PrismaService, | 
				
			||||
 | 
					    RakutenRapidApiService, | 
				
			||||
 | 
					    YahooFinanceService | 
				
			||||
 | 
					  ] | 
				
			||||
 | 
					}) | 
				
			||||
 | 
					export class CoreModule {} | 
				
			||||
@ -1,60 +1,65 @@ | 
				
			|||||
import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service'; | 
					import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service'; | 
				
			||||
import { Currency } from '@prisma/client'; | 
					 | 
				
			||||
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; | 
					import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; | 
				
			||||
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; | 
					import { Currency, MarketData } from '@prisma/client'; | 
				
			||||
 | 
					
 | 
				
			||||
jest.mock('../../services/exchange-rate-data.service', () => { | 
					import { MarketDataService } from './market-data.service'; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					jest.mock('./market-data.service', () => { | 
				
			||||
  return { | 
					  return { | 
				
			||||
    // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
					    MarketDataService: jest.fn().mockImplementation(() => { | 
				
			||||
    exchangeRateDataService: jest.fn().mockImplementation(() => { | 
					 | 
				
			||||
      return { | 
					      return { | 
				
			||||
        toCurrency: (aValue: number, | 
					        get: (date: Date, symbol: string) => { | 
				
			||||
                     aFromCurrency: Currency, | 
					          return Promise.resolve<MarketData>({ | 
				
			||||
                     aToCurrency: Currency) => { | 
					            date, | 
				
			||||
          return 1 * aValue; | 
					            symbol, | 
				
			||||
        } | 
					            createdAt: date, | 
				
			||||
 | 
					            id: 'aefcbe3a-ee10-4c4f-9f2d-8ffad7b05584', | 
				
			||||
 | 
					            marketPrice: 1847.839966 | 
				
			||||
 | 
					          }); | 
				
			||||
        } | 
					        } | 
				
			||||
 | 
					      }; | 
				
			||||
    }) | 
					    }) | 
				
			||||
  }; | 
					  }; | 
				
			||||
}); | 
					}); | 
				
			||||
 | 
					
 | 
				
			||||
// https://jestjs.io/docs/manual-mocks#mocking-node-modules
 | 
					jest.mock('../../services/exchange-rate-data.service', () => { | 
				
			||||
// jest.mock('?', () => {
 | 
					  return { | 
				
			||||
//   return {
 | 
					    ExchangeRateDataService: jest.fn().mockImplementation(() => { | 
				
			||||
//     // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
					      return { | 
				
			||||
//     prismaService: jest.fn().mockImplementation(() => {
 | 
					        initialize: () => Promise.resolve(), | 
				
			||||
//       return {
 | 
					        toCurrency: (value: number) => { | 
				
			||||
//         marketData: {
 | 
					          return 1 * value; | 
				
			||||
//           findFirst: (data: any) => {
 | 
					        } | 
				
			||||
//             return {
 | 
					      }; | 
				
			||||
//               marketPrice: 100
 | 
					    }) | 
				
			||||
//             };
 | 
					  }; | 
				
			||||
//           }
 | 
					}); | 
				
			||||
//         }
 | 
					 | 
				
			||||
//       };
 | 
					 | 
				
			||||
//     })
 | 
					 | 
				
			||||
//   };
 | 
					 | 
				
			||||
// });
 | 
					 | 
				
			||||
 | 
					 | 
				
			||||
xdescribe('CurrentRateService', () => { | 
					 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					describe('CurrentRateService', () => { | 
				
			||||
 | 
					  let currentRateService: CurrentRateService; | 
				
			||||
  let exchangeRateDataService: ExchangeRateDataService; | 
					  let exchangeRateDataService: ExchangeRateDataService; | 
				
			||||
  let prismaService: PrismaService; | 
					  let marketDataService: MarketDataService; | 
				
			||||
 | 
					
 | 
				
			||||
  beforeEach(() => { | 
					  beforeAll(async () => { | 
				
			||||
    exchangeRateDataService = new ExchangeRateDataService(undefined); | 
					    exchangeRateDataService = new ExchangeRateDataService(null); | 
				
			||||
    prismaService = new PrismaService(); | 
					    marketDataService = new MarketDataService(null); | 
				
			||||
  }); | 
					 | 
				
			||||
 | 
					
 | 
				
			||||
  it('getValue', () => { | 
					    await exchangeRateDataService.initialize(); | 
				
			||||
    const currentRateService = new CurrentRateService(exchangeRateDataService, prismaService); | 
					 | 
				
			||||
 | 
					
 | 
				
			||||
    expect(currentRateService.getValue({ | 
					    currentRateService = new CurrentRateService( | 
				
			||||
      date: new Date(), | 
					      exchangeRateDataService, | 
				
			||||
      symbol: 'AIA', | 
					      marketDataService | 
				
			||||
 | 
					    ); | 
				
			||||
 | 
					  }); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					  it('getValue', async () => { | 
				
			||||
 | 
					    expect( | 
				
			||||
 | 
					      await currentRateService.getValue({ | 
				
			||||
        currency: Currency.USD, | 
					        currency: Currency.USD, | 
				
			||||
 | 
					        date: new Date(Date.UTC(2020, 0, 1, 0, 0, 0)), | 
				
			||||
 | 
					        symbol: 'AMZN', | 
				
			||||
        userCurrency: Currency.CHF | 
					        userCurrency: Currency.CHF | 
				
			||||
    })).toEqual(0); | 
					      }) | 
				
			||||
 | 
					    ).toEqual(1847.839966); | 
				
			||||
  }); | 
					  }); | 
				
			||||
 | 
					 | 
				
			||||
}); | 
					}); | 
				
			||||
 | 
				
			|||||
@ -0,0 +1,20 @@ | 
				
			|||||
 | 
					import { PrismaService } from '@ghostfolio/api/services/prisma.service'; | 
				
			||||
 | 
					import { Injectable } from '@nestjs/common'; | 
				
			||||
 | 
					import { MarketData } from '@prisma/client'; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					@Injectable() | 
				
			||||
 | 
					export class MarketDataService { | 
				
			||||
 | 
					  public constructor(private prisma: PrismaService) {} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					  public async get({ | 
				
			||||
 | 
					    date, | 
				
			||||
 | 
					    symbol | 
				
			||||
 | 
					  }: { | 
				
			||||
 | 
					    date: Date; | 
				
			||||
 | 
					    symbol: string; | 
				
			||||
 | 
					  }): Promise<MarketData> { | 
				
			||||
 | 
					    return await this.prisma.marketData.findFirst({ | 
				
			||||
 | 
					      where: { date, symbol } | 
				
			||||
 | 
					    }); | 
				
			||||
 | 
					  } | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue