feat(backtests): separate saving from retry-safe run submission
This commit is contained in:
parent
5eb0985057
commit
9b9403e36b
18 changed files with 1200 additions and 226 deletions
|
|
@ -1,8 +1,9 @@
|
|||
import { Hono } from 'hono';
|
||||
import { getVerifiedHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { isBacktestRequestError } from '@stock-bot/stock-config/backtest';
|
||||
import { BacktestServiceV2 } from '../services/backtest-v2.service';
|
||||
import { BacktestServiceV2, RunSubmissionConflictError } from '../services/backtest-v2.service';
|
||||
|
||||
const logger = getLogger('backtests-v2-routes');
|
||||
|
||||
|
|
@ -40,7 +41,11 @@ export function createBacktestV2Routes(container: IServiceContainer) {
|
|||
app.post('/api/v2/backtests', async c => {
|
||||
try {
|
||||
const body = await c.req.json();
|
||||
const backtest = await backtestService.createBacktest(body);
|
||||
if (body.autoStart !== undefined && typeof body.autoStart !== 'boolean') {
|
||||
return c.json({ error: 'autoStart must be a boolean' }, 400);
|
||||
}
|
||||
// Omitted autoStart preserves legacy clients. New web clients explicitly save only.
|
||||
const backtest = await backtestService.createBacktest(body, { autoStart: body.autoStart });
|
||||
return c.json(backtest, 201);
|
||||
} catch (error) {
|
||||
logger.error('Failed to create backtest', error);
|
||||
|
|
@ -91,15 +96,29 @@ export function createBacktestV2Routes(container: IServiceContainer) {
|
|||
|
||||
app.post('/api/v2/backtests/:backtestId/runs', async c => {
|
||||
try {
|
||||
const key = c.req.header('Idempotency-Key');
|
||||
const principal = getVerifiedHttpPrincipal(c.req.raw);
|
||||
if (key !== undefined && !principal) {
|
||||
return c.json({ error: 'Verified submission principal required' }, 401);
|
||||
}
|
||||
if (key !== undefined && !/^[\x21-\x7e]{1,200}$/.test(key)) {
|
||||
return c.json(
|
||||
{ error: 'Idempotency-Key must contain 1–200 visible ASCII characters' },
|
||||
400
|
||||
);
|
||||
}
|
||||
const body = await c.req.json();
|
||||
const run = await backtestService.createRun({
|
||||
backtestId: c.req.param('backtestId'),
|
||||
speedMultiplier: body.speedMultiplier,
|
||||
});
|
||||
const run = await backtestService.createRun(
|
||||
{ backtestId: c.req.param('backtestId'), speedMultiplier: body.speedMultiplier },
|
||||
key !== undefined && principal ? { principal, key } : undefined
|
||||
);
|
||||
c.header('Location', `/api/v2/runs/${run.id}`);
|
||||
return c.json(run, 202);
|
||||
} catch (error) {
|
||||
logger.error('Failed to create run', error);
|
||||
if (error instanceof RunSubmissionConflictError) {
|
||||
return c.json({ error: error.message }, 409);
|
||||
}
|
||||
if (isBacktestRequestError(error)) {
|
||||
return c.json({ error: error.message }, 400);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ export function createRoutes(
|
|||
cors({
|
||||
origin: security.allowedOrigins,
|
||||
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'Authorization'],
|
||||
allowHeaders: ['Content-Type', 'Authorization', 'Idempotency-Key'],
|
||||
credentials: true,
|
||||
})
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue