Review this generated order-placement function before treating it as an architecture boundary.
Place one order with authenticated customer data, reserve stock safely, charge at most once across retries, and expose useful operational evidence.
JavaScript
async function placeOrder(request, db, payment, metrics) {
const customer = await db.query(
`SELECT * FROM customers WHERE id = '${request.customerId}'`,
);
if (!customer) throw new Error('customer not found');
const lines = [];
for (const item of request.items) {
const product = await db.query('SELECT * FROM products WHERE id = ?', [item.sku]);
if (product.stock < item.quantity) throw new Error('out of stock');
lines.push({ ...item, price: product.price });
}
const amount = lines.reduce((sum, line) => sum + line.price * line.quantity, 0);
const receipt = await payment.charge({ amount, orderId: request.orderId });
await db.query('INSERT INTO orders VALUES (?, ?, ?)', [
request.orderId, request.customerId, JSON.stringify(lines),
]);
metrics.push({ name: 'order.created', customer, amount });
return { orderId: request.orderId, receipt };
}
generated code is illustrative, not from any one model