From a033bde416965abe4183cf20c89ad38d49c9df43 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 7 Nov 2026 15:43:59 -0400 Subject: [PATCH] fix(api): include end date in FMP getHistorical when from equals to getHistorical filtered results with isBefore(date, currentTo), so a single-day request (from == to) always returned {}. Extend to by one day in that case, as getDividends already does. Co-Authored-By: Claude Sonnet 5.5 --- .../financial-modeling-prep.service.spec.ts | 62 +++++++++++++++++++ .../financial-modeling-prep.service.ts | 4 ++ 2 files changed, 66 insertions(+) create mode 100644 apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.spec.ts diff --git a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.spec.ts b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.spec.ts new file mode 100644 index 0000000000..2d63f8383f --- /dev/null +++ b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.spec.ts @@ -0,0 +1,62 @@ +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; +import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; + +import { FinancialModelingPrepService } from './financial-modeling-prep.service'; + +jest.mock('@ghostfolio/api/services/fetch/fetch.service', () => { + return { + FetchService: jest.fn() + }; +}); + +describe('FinancialModelingPrepService', () => { + let fetchService: { fetch: jest.Mock }; + let financialModelingPrepService: FinancialModelingPrepService; + + beforeEach(() => { + fetchService = { + fetch: jest.fn().mockResolvedValue({ + json: () => + Promise.resolve([ + { close: 100, date: '2024-01-02' }, + { close: 101, date: '2024-01-03' } + ]) + }) + }; + + financialModelingPrepService = new FinancialModelingPrepService( + { + get: jest.fn().mockReturnValue(5000) + } as unknown as ConfigurationService, + null as unknown as CryptocurrencyService, + fetchService as unknown as FetchService, + null as unknown as PrismaService + ); + }); + + describe('getHistorical', () => { + it('returns the price of a single day when from equals to', async () => { + const date = new Date('2024-01-02T00:00:00.000Z'); + + const result = await financialModelingPrepService.getHistorical({ + from: date, + symbol: 'AAPL', + to: date + }); + + expect(result).toEqual({ '2024-01-02': { marketPrice: 100 } }); + }); + + it('excludes the end date of a multi-day range', async () => { + const result = await financialModelingPrepService.getHistorical({ + from: new Date('2024-01-02T00:00:00.000Z'), + symbol: 'AAPL', + to: new Date('2024-01-03T00:00:00.000Z') + }); + + expect(result).toEqual({ '2024-01-02': { marketPrice: 100 } }); + }); + }); +}); diff --git a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts index 3b38345efa..042c788acf 100644 --- a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts +++ b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts @@ -361,6 +361,10 @@ export class FinancialModelingPrepService let currentFrom = from; + if (isSameDay(from, to)) { + to = addDays(to, 1); + } + try { while (isBefore(currentFrom, to) || isSameDay(currentFrom, to)) { const currentTo = isBefore(