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

84 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-07-08 22:42:19 +02:00
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { resetHours } from '@ghostfolio/common/helper';
2021-07-03 16:37:33 +02:00
import { Injectable } from '@nestjs/common';
2021-07-08 22:42:19 +02:00
import { isToday } from 'date-fns';
2021-07-27 21:07:28 +02:00
import { MarketDataService } from './market-data.service';
import { GetValueObject } from '@ghostfolio/api/app/core/get-value.object';
import { GetValuesParams } from '@ghostfolio/api/app/core/get-values.params';
import { GetValueParams } from '@ghostfolio/api/app/core/get-value.params';
2021-07-03 16:37:33 +02:00
@Injectable()
export class CurrentRateService {
public constructor(
2021-07-08 22:42:19 +02:00
private readonly dataProviderService: DataProviderService,
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<GetValueObject> {
2021-07-08 22:42:19 +02:00
if (isToday(date)) {
const dataProviderResult = await this.dataProviderService.get([symbol]);
return {
date: resetHours(date),
2021-07-20 21:07:17 +02:00
marketPrice: dataProviderResult?.[symbol]?.marketPrice ?? 0,
symbol: symbol
};
2021-07-08 22:42:19 +02:00
}
2021-07-03 16:37:33 +02:00
const marketData = await this.marketDataService.get({
date,
symbol
});
2021-07-03 16:37:33 +02:00
if (marketData) {
return {
date: marketData.date,
2021-07-20 21:07:17 +02:00
symbol: marketData.symbol,
marketPrice: this.exchangeRateDataService.toCurrency(
marketData.marketPrice,
currency,
userCurrency
)
};
2021-07-03 16:37:33 +02:00
}
throw new Error(`Value not found for ${symbol} at ${resetHours(date)}`);
}
public async getValues({
2021-07-20 20:42:56 +02:00
currencies,
dateQuery,
2021-07-20 20:42:56 +02:00
symbols,
userCurrency
}: GetValuesParams): Promise<GetValueObject[]> {
const marketData = await this.marketDataService.getRange({
dateQuery,
2021-07-20 20:42:56 +02:00
symbols
});
if (marketData) {
return marketData.map((marketDataItem) => {
return {
date: marketDataItem.date,
2021-07-20 21:07:17 +02:00
symbol: marketDataItem.symbol,
marketPrice: this.exchangeRateDataService.toCurrency(
marketDataItem.marketPrice,
2021-07-20 20:42:56 +02:00
currencies[marketDataItem.symbol],
userCurrency
)
};
});
}
throw new Error(`Values not found for symbols ${symbols.join(', ')}`);
}
}