Files
Flight-Ops/app/api/planes/[planeId]/dispatch/route.ts
T

36 lines
1.5 KiB
TypeScript

import { dispatchPlane, performAction } from '@/lib/game/actions';
// POST /api/planes/:planeId/dispatch (v0.8.0) — body { contractId: string }.
// Binds an ACCEPTED contract to a grounded plane at its origin; the v0.7.0
// finalize path then flies it and pays out on arrival. 400 missing/non-string
// contractId or unknown fields; 404 unknown plane/contract id; 409 wrong contract
// status, plane not available, plane not at origin, insufficient fuel. On success
// returns the full state snapshot (plane already 'flying' with departure set).
export async function POST(req: Request, ctx: { params: Promise<{ planeId: string }> }) {
const { planeId } = await ctx.params;
let body: unknown;
try {
const text = await req.text();
body = text ? JSON.parse(text) : {};
} catch {
return Response.json({ error: 'invalid JSON body' }, { status: 400 });
}
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
return Response.json({ error: 'body must be a JSON object' }, { status: 400 });
}
const input = body as Record<string, unknown>;
for (const key of Object.keys(input)) {
if (key !== 'contractId') {
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
}
}
if (typeof input.contractId !== 'string' || input.contractId.length === 0) {
return Response.json({ error: 'contractId must be a non-empty string' }, { status: 400 });
}
const contractId = input.contractId;
return performAction((inst) => dispatchPlane(inst, planeId, contractId));
}