Cached daily P&L rows outlive what the Cash History report still produces (stale pre-roll dates being the recent example), and nothing deletes them any more. POST /api/accounts/[id]/wipe drops the account's cached P&L, fund transactions and meta, clears the in-memory view, and rebuilds from a fresh report — fetchDaysTraded now takes an optional account filter so one account doesn't cost a report round-trip on every other. The button confirms inline first: rows older than the report's 28-day window cannot be refetched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getClients } from '@/lib/clients';
|
|
import { wipeAccountData } from '@/lib/db';
|
|
|
|
/**
|
|
* POST /api/accounts/[id]/wipe — drop every cached row for the account and
|
|
* rebuild it from a fresh Tradovate report.
|
|
*
|
|
* Destructive on purpose: cached rows older than the report's 28-day window
|
|
* cannot be refetched. The UI warns and confirms before calling this.
|
|
*/
|
|
export async function POST(_req: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { id } = await params;
|
|
const accountId = Number(id);
|
|
if (!Number.isInteger(accountId)) {
|
|
return NextResponse.json({ error: 'Bad account id' }, { status: 400 });
|
|
}
|
|
|
|
for (const client of getClients().values()) {
|
|
const account = client.accountList.find((a) => a.id === accountId);
|
|
if (!account) continue;
|
|
|
|
wipeAccountData(accountId);
|
|
|
|
// Clear the in-memory view too — a failed refetch must leave the
|
|
// account visibly empty, not showing the data that was just wiped.
|
|
delete client.dailyPnL[accountId];
|
|
delete client.fullDailyPnL[accountId];
|
|
delete client.daysTraded[accountId];
|
|
delete client.priorProfit[accountId];
|
|
delete client.lastFundDates[accountId];
|
|
delete client.fundTransactions[accountId];
|
|
delete client.lastFetchErrors[account.name];
|
|
delete client.lastFetchRaw[account.name];
|
|
|
|
await client.fetchDaysTraded([accountId]);
|
|
|
|
return NextResponse.json({
|
|
ok: true,
|
|
entries: (client.dailyPnL[accountId] ?? []).length,
|
|
daysTraded: client.daysTraded[accountId] ?? 0,
|
|
fetchError: client.lastFetchErrors[account.name] ?? null,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ error: 'Account not found' }, { status: 404 });
|
|
}
|