Support today in current rate service

pull/239/head
Thomas 3 years ago
parent ddf24163b4
commit 5037393866

@ -1,4 +1,5 @@
import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service'; import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { Currency, MarketData } from '@prisma/client'; import { Currency, MarketData } from '@prisma/client';
@ -37,16 +38,26 @@ jest.mock('../../services/exchange-rate-data.service', () => {
describe('CurrentRateService', () => { describe('CurrentRateService', () => {
let currentRateService: CurrentRateService; let currentRateService: CurrentRateService;
let dataProviderService: DataProviderService;
let exchangeRateDataService: ExchangeRateDataService; let exchangeRateDataService: ExchangeRateDataService;
let marketDataService: MarketDataService; let marketDataService: MarketDataService;
beforeAll(async () => { beforeAll(async () => {
dataProviderService = new DataProviderService(
null,
null,
null,
null,
null,
null
);
exchangeRateDataService = new ExchangeRateDataService(null); exchangeRateDataService = new ExchangeRateDataService(null);
marketDataService = new MarketDataService(null); marketDataService = new MarketDataService(null);
await exchangeRateDataService.initialize(); await exchangeRateDataService.initialize();
currentRateService = new CurrentRateService( currentRateService = new CurrentRateService(
dataProviderService,
exchangeRateDataService, exchangeRateDataService,
marketDataService marketDataService
); );

@ -1,12 +1,15 @@
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Currency } from '@prisma/client'; import { Currency } from '@prisma/client';
import { isToday } from 'date-fns';
import { MarketDataService } from './market-data.service'; import { MarketDataService } from './market-data.service';
@Injectable() @Injectable()
export class CurrentRateService { export class CurrentRateService {
public constructor( public constructor(
private readonly dataProviderService: DataProviderService,
private readonly exchangeRateDataService: ExchangeRateDataService, private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly marketDataService: MarketDataService private readonly marketDataService: MarketDataService
) {} ) {}
@ -17,6 +20,11 @@ export class CurrentRateService {
symbol, symbol,
userCurrency userCurrency
}: GetValueParams): Promise<number> { }: GetValueParams): Promise<number> {
if (isToday(date)) {
const dataProviderResult = await this.dataProviderService.get([symbol]);
return dataProviderResult?.[symbol]?.marketPrice ?? 0;
}
const marketData = await this.marketDataService.get({ const marketData = await this.marketDataService.get({
date, date,
symbol symbol

@ -1,7 +1,7 @@
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { MarketData } from '@prisma/client'; import { MarketData } from '@prisma/client';
import { subDays } from 'date-fns'; import { addDays } from 'date-fns';
@Injectable() @Injectable()
export class MarketDataService { export class MarketDataService {
@ -16,9 +16,10 @@ export class MarketDataService {
}): Promise<MarketData> { }): Promise<MarketData> {
return await this.prisma.marketData.findFirst({ return await this.prisma.marketData.findFirst({
where: { where: {
// Filter for given date
date: { date: {
gte: subDays(date, 1), gte: date,
lt: date lt: addDays(date, 1)
}, },
symbol symbol
} }

@ -69,8 +69,9 @@ jest.mock('./current-rate.service.ts', () => {
describe('PortfolioCalculator', () => { describe('PortfolioCalculator', () => {
let currentRateService: CurrentRateService; let currentRateService: CurrentRateService;
beforeEach(() => { beforeEach(() => {
currentRateService = new CurrentRateService(null, null); currentRateService = new CurrentRateService(null, null, null);
}); });
describe('calculate transaction points', () => { describe('calculate transaction points', () => {

@ -29,7 +29,7 @@ export class DataProviderService {
private readonly rakutenRapidApiService: RakutenRapidApiService, private readonly rakutenRapidApiService: RakutenRapidApiService,
private readonly yahooFinanceService: YahooFinanceService private readonly yahooFinanceService: YahooFinanceService
) { ) {
this.rakutenRapidApiService.setPrisma(this.prisma); this.rakutenRapidApiService?.setPrisma(this.prisma);
} }
public async get( public async get(

Loading…
Cancel
Save