import { NextResponse } from 'next/server'; import { getClients } from '@/lib/clients'; import { wipeAccountData } from '@/lib/db'; /** * POST /api/accounts/wipe-all — the bulk form of /api/accounts/[id]/wipe: * drop every account's cached rows on every firm and rebuild them all from * fresh Tradovate reports. One report per account, so this takes a while on * big rosters; firms run in parallel, accounts within a firm sequentially * (that's fetchDaysTraded's own pacing against the flaky report server). * * Same destructive caveat as the single wipe: cached rows older than the * report's 28-day window cannot be refetched. The UI confirms first. */ export async function POST() { const clients = [...getClients().values()]; let wiped = 0; let failedAccounts = 0; await Promise.all(clients.map(async (client) => { for (const account of client.accountList) { wipeAccountData(account.id); delete client.dailyPnL[account.id]; delete client.fullDailyPnL[account.id]; delete client.daysTraded[account.id]; delete client.priorProfit[account.id]; delete client.lastFundDates[account.id]; delete client.fundTransactions[account.id]; delete client.lastFetchErrors[account.name]; delete client.lastFetchRaw[account.name]; wiped++; } const result = await client.fetchDaysTraded(); failedAccounts += result.failedAccounts; })); return NextResponse.json({ ok: true, wiped, failedAccounts }); }