Browse Source

Support benchmarks whose market data starts later than portfolio

pull/8106/head
Thomas Kaul 5 days ago
parent
commit
9aa2a49b53
  1. 23
      apps/api/src/app/endpoints/benchmarks/benchmarks.service.spec.ts
  2. 16
      apps/api/src/app/endpoints/benchmarks/benchmarks.service.ts

23
apps/api/src/app/endpoints/benchmarks/benchmarks.service.spec.ts

@ -14,7 +14,7 @@ describe('BenchmarksService', () => {
let get: jest.Mock;
let getExchangeRatesByCurrency: jest.Mock;
let getPerformance: jest.Mock;
let getRangeCount: jest.Mock;
let getRange: jest.Mock;
let marketDataItems: jest.Mock;
beforeEach(() => {
@ -28,13 +28,13 @@ describe('BenchmarksService', () => {
{ date: '2010-07-19' }
]
});
getRangeCount = jest.fn().mockResolvedValue(0);
getRange = jest.fn().mockResolvedValue([]);
marketDataItems = jest.fn();
benchmarksService = new BenchmarksService(
new BenchmarkService(null, null, null, null, null, null),
{ getExchangeRatesByCurrency } as unknown as ExchangeRateDataService,
{ getRangeCount, marketDataItems } as unknown as MarketDataService,
{ getRange, marketDataItems } as unknown as MarketDataService,
{ getPerformance } as unknown as PortfolioService,
{ get } as unknown as SymbolService
);
@ -72,7 +72,7 @@ describe('BenchmarksService', () => {
]
});
expect(getRangeCount).not.toHaveBeenCalled();
expect(getRange).not.toHaveBeenCalled();
});
it('returns no values before the market data of the benchmark starts', async () => {
@ -88,12 +88,17 @@ describe('BenchmarksService', () => {
]
});
expect(getRangeCount).toHaveBeenCalledWith({
expect(getRange).toHaveBeenCalledWith({
assetProfileIdentifiers: [
{ dataSource: DataSource.COINGECKO, symbol: 'bitcoin' }
],
dateQuery: { lt: new Date('2009-01-01') }
dateQuery: { lt: new Date('2009-01-01') },
take: 1
});
expect(marketDataItems).toHaveBeenCalledWith(
expect.objectContaining({ orderBy: { date: 'asc' } })
);
});
it('uses the exchange rate at the start of the market data', async () => {
@ -146,7 +151,9 @@ describe('BenchmarksService', () => {
});
it('returns no market data if the market data at the baseline date is missing', async () => {
getRangeCount.mockResolvedValue(1);
getRange.mockResolvedValue([
{ date: new Date('2008-12-31'), marketPrice: 40 }
]);
marketDataItems.mockResolvedValue([
{ date: new Date('2010-07-18'), marketPrice: 50 },
@ -156,6 +163,8 @@ describe('BenchmarksService', () => {
await expect(getMarketDataForUser()).resolves.toEqual({
marketData: []
});
expect(getExchangeRatesByCurrency).not.toHaveBeenCalled();
});
it('returns no market data if the benchmark has none', async () => {

16
apps/api/src/app/endpoints/benchmarks/benchmarks.service.ts

@ -114,13 +114,14 @@ export class BenchmarksService {
if (!isSameDay(firstMarketDataItem.date, baselineDate)) {
// If market data exists before the baseline date, the market data at
// the baseline date is missing and the benchmark does not start later
const marketDataCountBeforeBaselineDate =
await this.marketDataService.getRangeCount({
const [marketDataItemBeforeBaselineDate] =
await this.marketDataService.getRange({
assetProfileIdentifiers: [{ dataSource, symbol }],
dateQuery: { lt: baselineDate }
dateQuery: { lt: baselineDate },
take: 1
});
if (marketDataCountBeforeBaselineDate > 0) {
if (marketDataItemBeforeBaselineDate) {
this.logger.error(
`No historical market data has been found for ${symbol} (${dataSource}) at ${format(
baselineDate,
@ -130,6 +131,13 @@ export class BenchmarksService {
return { marketData };
}
this.logger.warn(
`The market data of ${symbol} (${dataSource}) starts at ${format(
firstMarketDataItem.date,
DATE_FORMAT
)}, after the baseline date ${format(baselineDate, DATE_FORMAT)}`
);
}
const exchangeRates =

Loading…
Cancel
Save