- POST /api/instruments/contracts now accepts optional { symbols[] } body
to resolve a subset rather than all enabled instruments
- Settings toggle() fires a targeted resolve when enabling a symbol,
merging the result into contracts state without a full page refresh
- Active Contract cell is hidden (null) when the instrument is disabled
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getClients } from '@/lib/clients';
|
|
import { getInstruments } from '@/lib/db';
|
|
import { resolveContracts, getAllCachedContracts } from '@/lib/contract-resolver';
|
|
|
|
/**
|
|
* GET /api/instruments/contracts
|
|
* Returns cached resolved contracts (fast, no external calls).
|
|
*/
|
|
export async function GET() {
|
|
return NextResponse.json(getAllCachedContracts());
|
|
}
|
|
|
|
/**
|
|
* POST /api/instruments/contracts
|
|
* Triggers a fresh resolve via Tradovate + Yahoo Finance.
|
|
* Body (optional): { symbols: string[] } — when provided, resolves only those symbols.
|
|
* Otherwise resolves all enabled instruments.
|
|
*/
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const clients = getClients();
|
|
// Find first client with a valid token
|
|
let accessToken: string | null = null;
|
|
for (const [, c] of clients) {
|
|
const token = (c as any).accessInfo?.accessToken;
|
|
if (token) { accessToken = token; break; }
|
|
}
|
|
if (!accessToken) {
|
|
return NextResponse.json({ error: 'No authenticated client available' }, { status: 503 });
|
|
}
|
|
|
|
const body = await req.json().catch(() => ({}));
|
|
const requestedSymbols: string[] | undefined = Array.isArray(body?.symbols) ? body.symbols : undefined;
|
|
|
|
const symbols = requestedSymbols?.length
|
|
? requestedSymbols
|
|
: getInstruments().filter((i) => i.enabled).map((i) => i.symbol);
|
|
|
|
const resolved = await resolveContracts(symbols, accessToken);
|
|
return NextResponse.json(resolved);
|
|
} catch (err: any) {
|
|
console.error('[POST /api/instruments/contracts]', err);
|
|
return NextResponse.json({ error: err?.message ?? 'Resolve failed' }, { status: 500 });
|
|
}
|
|
}
|