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 { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { Currency, MarketData } from '@prisma/client';
@ -37,16 +38,26 @@ jest.mock('../../services/exchange-rate-data.service', () => {
describe('CurrentRateService', () => {
let currentRateService: CurrentRateService;
let dataProviderService: DataProviderService;
let exchangeRateDataService: ExchangeRateDataService;
let marketDataService: MarketDataService;
beforeAll(async () => {
dataProviderService = new DataProviderService(
null,
null,
null,
null,
null,
null
);
exchangeRateDataService = new ExchangeRateDataService(null);
marketDataService = new MarketDataService(null);
await exchangeRateDataService.initialize();
currentRateService = new CurrentRateService(
dataProviderService,
exchangeRateDataService,
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 { Injectable } from '@nestjs/common';
import { Currency } from '@prisma/client';
import { isToday } from 'date-fns';
import { MarketDataService } from './market-data.service';
@Injectable()
export class CurrentRateService {
public constructor(
private readonly dataProviderService: DataProviderService,
private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly marketDataService: MarketDataService
) {}
@ -17,6 +20,11 @@ export class CurrentRateService {
symbol,
userCurrency
}: GetValueParams): Promise<number> {
if (isToday(date)) {
const dataProviderResult = await this.dataProviderService.get([symbol]);
return dataProviderResult?.[symbol]?.marketPrice ?? 0;
}
const marketData = await this.marketDataService.get({
date,
symbol

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

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

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

Loading…
Cancel
Save