diff --git a/src/modules/investments/instruments/application/candle.service.ts b/src/modules/investments/instruments/application/candle.service.ts index 812e843..489c4e4 100644 --- a/src/modules/investments/instruments/application/candle.service.ts +++ b/src/modules/investments/instruments/application/candle.service.ts @@ -29,6 +29,7 @@ import { timestamp } from './instrument-market'; const REPLACED_WINDOW_COUNT = 1; const NEXT_WINDOW_OFFSET = 1; +const NO_CANDLE_GAPS = 0; type CandleInput = { instrumentType: 'bond' | 'share'; @@ -114,7 +115,10 @@ export class CandleService { ? rangeFor(input, detail.instrument, now) : null; const saved = range ? await this.repository.read(input.uid, range) : null; - if (!observe && !saved) { + const hasCompleteRange = Boolean( + range && saved && candleGaps(range, saved.coverage, now).length === NO_CANDLE_GAPS, + ); + if (!observe && (!saved || !hasCompleteRange)) { await this.request(userId, revision, input, now); } const work = await this.repository.work(userId, revision, scope(input)); diff --git a/test/tbank-candles-persistence.e2e-spec.ts b/test/tbank-candles-persistence.e2e-spec.ts index c4fc5c7..eb041e8 100644 --- a/test/tbank-candles-persistence.e2e-spec.ts +++ b/test/tbank-candles-persistence.e2e-spec.ts @@ -240,7 +240,8 @@ describe('atomic shared candle reconciliation (SQLite)', () => { data: { fetchedAt: new Date(0) }, }); const stale = await reopened.read('owner', input, true); - expect(stale.meta.availability).toBe('complete'); + expect(stale.meta.availability).toBe('available'); + expect(stale.meta.completeness).toBe('complete'); expect(stale.meta.freshness).toBe('stale'); expect(stale.meta.coverage?.missingRanges).toEqual([]); expect(stale.meta.coverage?.completeRanges.length).toBeGreaterThan(0); @@ -302,7 +303,7 @@ describe('atomic shared candle reconciliation (SQLite)', () => { expect(await prisma.tbankCandleCoverage.findMany()).toEqual(before); expect(await prisma.tbankCandle.count()).toBe(0); const read = await service.read('owner', input, true); - expect(read.meta.sync.state).toBe('failed'); + expect(read.meta.lastAttempt.state).toBe('failed'); expect(read.data?.candles).toEqual([]); }); it.each([