import { NextResponse } from 'next/server'; import { getCachedCatalog, hasPlatformCredentials } from '@/lib/firm-catalog'; import { getCredentials } from '@/lib/db'; import type { FirmConfig } from '@/types'; /** * The firms this machine knows about — the master's catalog, annotated with * whether credentials for each are stored here. * * Firms are created and edited on the master dashboard; there is no POST. The * only writable thing per firm is its credentials, at * PUT /api/firms/[id]/credentials. * * The stored password is deliberately not returned. It used to be, and nothing * ever read it. */ export async function GET() { try { const result: FirmConfig[] = getCachedCatalog().map((firm) => { const creds = getCredentials(firm.id); const hasCredentials = hasPlatformCredentials(firm.platform, creds); return { id: firm.id, firm: firm.name, platform: firm.platform, username: creds?.username ?? '', hasCredentials, bannedSymbols: firm.bannedSymbols, accounts: firm.accountTypes.map((a) => ({ id: a.id, label: a.label, prefix: a.prefix, profitTarget: a.profitTarget, consistency: a.consistency, minDayPnL: a.minDayPnL, minTradingDays: a.minTradingDays, accountSize: a.accountSize, maxLoss: a.maxLoss, maxPositionSize: a.maxPositionSize, targetSameEquity: a.targetSameEquity, splitEnabled: a.splitEnabled, minLegs: a.minLegs, firstDayStop: a.firstDayStop, minDayStop: a.minDayStop, combineCmeHolidays: a.combineCmeHolidays, withdrawalStages: a.withdrawalStages, dailyLossLimit: a.dailyLossLimit ?? 0, tradesEveryDay: a.tradesEveryDay === true, })), catalogUpdatedAt: firm.updatedAt, }; }); return NextResponse.json(result); } catch (err) { console.error('[GET /api/firms]', err); return NextResponse.json({ error: 'Failed to fetch firms' }, { status: 500 }); } }