|
| 1 | +/** |
| 2 | + * GET /api/finance/sparklines?symbols=NVDA,AAPL,… |
| 3 | + * |
| 4 | + * Returns last-week close samples per symbol for the tiny watchlist sparklines. |
| 5 | + * Paid-gated; uses the per-profile market-data provider (connected Alpaca → |
| 6 | + * Yahoo fallback) and read-through caches each symbol's samples. |
| 7 | + */ |
| 8 | + |
| 9 | +import { NextRequest, NextResponse } from 'next/server'; |
| 10 | +import { requireActiveSubscription } from '@/lib/subscription/guard'; |
| 11 | +import { getActiveProfileId } from '@/lib/profiles/profile-utils'; |
| 12 | +import { getFallbackMarketDataProvider, type MarketDataProvider } from '@/lib/finance/market-data'; |
| 13 | +import { getMarketDataProviderForProfile } from '@/lib/finance/market-data/for-profile'; |
| 14 | +import { readThrough, CANDLES_TTL_SECONDS } from '@/lib/finance/market-data/cache'; |
| 15 | +import { parseSymbolList } from '@/lib/finance/watchlist'; |
| 16 | + |
| 17 | +export const dynamic = 'force-dynamic'; |
| 18 | + |
| 19 | +const MAX_SYMBOLS = 60; |
| 20 | +const MAX_POINTS = 24; |
| 21 | + |
| 22 | +/** Downsample an array to at most `max` evenly-spaced points (keeps last). */ |
| 23 | +function downsample(values: number[], max: number): number[] { |
| 24 | + if (values.length <= max) return values; |
| 25 | + const step = values.length / max; |
| 26 | + const out: number[] = []; |
| 27 | + for (let i = 0; i < max; i++) out.push(values[Math.min(values.length - 1, Math.floor(i * step))]); |
| 28 | + out[out.length - 1] = values[values.length - 1]; |
| 29 | + return out; |
| 30 | +} |
| 31 | + |
| 32 | +async function samplesFor(provider: MarketDataProvider, symbol: string): Promise<number[]> { |
| 33 | + return readThrough<number[]>(symbol, `sparkline:${provider.id}`, CANDLES_TTL_SECONDS, async () => { |
| 34 | + const candles = await provider.getCandles(symbol, '5D'); |
| 35 | + return downsample(candles.map((c) => c.close), MAX_POINTS); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +export async function GET(request: NextRequest): Promise<NextResponse> { |
| 40 | + const gate = await requireActiveSubscription(request); |
| 41 | + if (gate) return gate; |
| 42 | + |
| 43 | + const { valid } = parseSymbolList(request.nextUrl.searchParams.get('symbols') ?? ''); |
| 44 | + if (valid.length === 0) return NextResponse.json({ samples: {} }); |
| 45 | + |
| 46 | + const profileId = await getActiveProfileId(); |
| 47 | + const provider = await getMarketDataProviderForProfile(profileId); |
| 48 | + const fallback = getFallbackMarketDataProvider(); |
| 49 | + |
| 50 | + const symbols = valid.slice(0, MAX_SYMBOLS); |
| 51 | + const entries = await Promise.all( |
| 52 | + symbols.map(async (symbol): Promise<[string, number[]]> => { |
| 53 | + try { |
| 54 | + return [symbol, await samplesFor(provider, symbol)]; |
| 55 | + } catch { |
| 56 | + try { |
| 57 | + return [symbol, await samplesFor(fallback, symbol)]; |
| 58 | + } catch { |
| 59 | + return [symbol, []]; |
| 60 | + } |
| 61 | + } |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + return NextResponse.json({ samples: Object.fromEntries(entries) }); |
| 66 | +} |
0 commit comments