Review this generated transfer endpoint before it handles real accounts.
Transfer an exact positive amount between authorized accounts, commit atomically, publish one event, and let the caller retry an unknown result safely.
JavaScript
async function createTransfer(request, db) {
const body = await request.json();
const amount = Number(body.amount);
const requestId = crypto.randomUUID();
const source = await db.accounts.find(body.sourceId);
const target = await db.accounts.find(body.targetId);
if (!source) return json(404, { error: "not found" });
if (!target) return json(404, { error: "not found" });
source.balance -= amount;
target.balance += amount;
await db.accounts.save(source);
await db.accounts.save(target);
await db.events.publish("transfer.created", { requestId, amount });
return json(
201,
{ id: requestId, sourceId: source.id, targetId: target.id, amount },
);
}
generated code is illustrative, not from any one model