ghostfolio/apps/api/src/app/core/current-rate.service.ts

43 lines
1013 B
TypeScript
Raw Normal View History

import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
2021-07-03 16:37:33 +02:00
import { Injectable } from '@nestjs/common';
import { Currency } from '@prisma/client';
2021-07-03 16:37:33 +02:00
import { MarketDataService } from './market-data.service';
2021-07-03 16:37:33 +02:00
@Injectable()
export class CurrentRateService {
public constructor(
private readonly exchangeRateDataService: ExchangeRateDataService,
2021-07-03 16:37:33 +02:00
private readonly marketDataService: MarketDataService
) {}
2021-07-03 16:37:33 +02:00
public async getValue({
currency,
date,
symbol,
userCurrency
}: GetValueParams): Promise<number> {
const marketData = await this.marketDataService.get({
date,
symbol
});
2021-07-03 16:37:33 +02:00
if (marketData) {
return this.exchangeRateDataService.toCurrency(
marketData.marketPrice,
currency,
userCurrency
);
}
2021-07-03 16:37:33 +02:00
throw new Error(`Value not found for ${symbol} at ${date}`);
}
}
export interface GetValueParams {
date: Date;
symbol: string;
currency: Currency;
userCurrency: Currency;
}