Review this generated order-creation endpoint before release.
Create an order for an authorized account, charge an exact positive amount once, expose only the public representation, and let the caller retry an unknown result safely.
JavaScript
async function createOrder(request, db, payments) {
const body = await request.json();
const key = request.headers.get("Idempotency-Key");
const accountId = body.accountId;
const order = {
id: crypto.randomUUID(),
accountId,
total: Number(body.total),
status: "paid",
internalNote: body.internalNote,
};
await db.orders.insert(order);
await payments.charge(accountId, order.total);
return json(
201,
order,
{ Location: `/orders/${order.id}`, "Idempotency-Key": key },
);
}
generated code is illustrative, not from any one model