- DB migration: ALTER TABLE account_configs ADD COLUMN max_position_size INTEGER NOT NULL DEFAULT 0 - Added max_position_size to AccountConfigRow, createAccountConfig, updateAccountConfig in lib/db.ts - Added maxPositionSize to AccountConfig type in types.ts (0 = no limit) - GET /api/firms/[id] now returns maxPositionSize per account - POST /api/firms/[id]/accounts and PUT /api/account-configs/[id] accept maxPositionSize - Firm settings page: new Max Contracts column (blank = no limit) - auto-trade: contracts capped at maxPositionSize when > 0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getFirmById, createAccountConfig } from '@/lib/db';
|
|
|
|
export async function POST(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id: idStr } = await params;
|
|
const firmId = parseInt(idStr, 10);
|
|
|
|
if (isNaN(firmId)) {
|
|
return NextResponse.json({ error: 'Invalid firm id' }, { status: 400 });
|
|
}
|
|
|
|
if (!getFirmById(firmId)) {
|
|
return NextResponse.json({ error: 'Firm not found' }, { status: 404 });
|
|
}
|
|
|
|
const body = await req.json() as {
|
|
prefix?: string;
|
|
profitTarget?: number;
|
|
consistency?: number;
|
|
minDayPnL?: number;
|
|
minTradingDays?: number;
|
|
accountSize?: number;
|
|
maxLoss?: number;
|
|
maxPositionSize?: number;
|
|
};
|
|
|
|
const { prefix, profitTarget, consistency, minDayPnL, minTradingDays, accountSize, maxLoss, maxPositionSize } = body;
|
|
|
|
if (
|
|
typeof prefix !== 'string' || !prefix.trim() ||
|
|
typeof profitTarget !== 'number' ||
|
|
typeof consistency !== 'number' ||
|
|
typeof minDayPnL !== 'number' ||
|
|
typeof minTradingDays !== 'number' ||
|
|
typeof accountSize !== 'number'
|
|
) {
|
|
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const row = createAccountConfig(firmId, {
|
|
prefix: prefix.trim(),
|
|
profitTarget,
|
|
consistency,
|
|
minDayPnL,
|
|
minTradingDays,
|
|
accountSize,
|
|
maxLoss: maxLoss ?? 0,
|
|
maxPositionSize: maxPositionSize ?? 0,
|
|
});
|
|
|
|
return NextResponse.json({
|
|
id: row.id,
|
|
prefix: row.prefix,
|
|
profitTarget: row.profit_target,
|
|
consistency: row.consistency,
|
|
minDayPnL: row.min_day_pnl,
|
|
minTradingDays: row.min_trading_days,
|
|
accountSize: row.account_size,
|
|
maxLoss: row.max_loss,
|
|
maxPositionSize: row.max_position_size,
|
|
}, { status: 201 });
|
|
} catch (err) {
|
|
console.error('[POST /api/firms/:id/accounts]', err);
|
|
return NextResponse.json({ error: 'Failed to create account type' }, { status: 500 });
|
|
}
|
|
}
|