import { NextResponse } from 'next/server'; import { getClients } from '@/lib/clients'; import { getAccountConfig } from '@/lib/account-type-resolver'; 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) => { // Forgotten, the accounts read unloaded and stay out of trading // until the refetch below (or, if it fails, a later one) lands. // Taken before the wipe: the resolver may un-assign an account while // the rebuild runs, and it still counts. Accounts with no type aren't // traded, so they aren't failures. const ours = client.accountList.filter((account) => getAccountConfig(account.id).accountType).map((a) => a.id); for (const account of client.accountList) { // History first: the client reads the stored rows it keeps as the // rebuild's yardstick before they go. client.forgetHistory(account.id, account.name); wipeAccountData(account.id); wiped++; } await client.fetchDaysTraded(); // Counted from the result, not the fetch's own tally: a client with // no session fetches nothing and reports no failures. failedAccounts += ours.filter((id) => !client.historyReady(id)).length; })); return NextResponse.json({ ok: true, wiped, failedAccounts }); }