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

36 lines
1.5 KiB
TypeScript

import { flyPlane, performAction } from '@/lib/game/actions';
// POST /api/planes/:planeId/fly (v0.12.0) — body { destIcao: string }. Flies a
// GROUNDED plane to any known airport (its loaded packages' destinations or pure
// repositioning); the engine delivers bound cargo on arrival. 400 missing/
// non-string destIcao, unknown fields, or an ICAO not in the airport dataset;
// 404 unknown plane id; 409 plane flying or insufficient fuel for the leg. On
// success returns the full state snapshot (plane "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 !== 'destIcao') {
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
}
}
if (typeof input.destIcao !== 'string' || input.destIcao.length === 0) {
return Response.json({ error: 'destIcao must be a non-empty string' }, { status: 400 });
}
const destIcao = input.destIcao;
return performAction((inst) => flyPlane(inst, planeId, destIcao));
}