stock-portfolio_byQwen3.6/app/api/admin/rebuild-snapshots/route.ts

49 lines
1.3 KiB
TypeScript

import { NextResponse } from 'next/server';
import { reconstructPortfolioHistory } from '@/actions/snapshots';
export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';
export const maxDuration = 3600;
export async function POST(req: Request) {
const rebuildSecret = process.env.REBUILD_SECRET || process.env.CRON_SECRET;
const authHeader = req.headers.get('Authorization');
if (!rebuildSecret) {
return NextResponse.json(
{ error: 'REBUILD_SECRET or CRON_SECRET not configured' },
{ status: 500 }
);
}
if (authHeader !== `Bearer ${rebuildSecret}`) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
try {
console.log('[Rebuild Snapshots] Starting full rebuild...');
const result = await reconstructPortfolioHistory();
console.log('[Rebuild Snapshots] Rebuild complete:', result);
return NextResponse.json({
success: true,
message: '历史快照全量重建完成',
daysReconstructed: result.daysReconstructed,
});
} catch (error) {
console.error('[Rebuild Snapshots] Rebuild failed:', error);
return NextResponse.json(
{
error: '重建失败',
details: String(error),
},
{ status: 500 }
);
}
}