Spot the bug in the order replacement

from RESTful API design
Node 24 intermediate 4 min 1 issue to find

Find the lost-update flaw in this generated replacement handler.

JavaScript
async function replaceOrder(id, input, ifMatch, db) {
  const current = await db.orders.find(id);
  if (!current) return { status: 404 };
  if (ifMatch !== `"order-${current.version}"`) {
    return { status: 412 };
  }
  const next = { id, status: input.status, version: current.version + 1 };
  await db.orders.update(id, next);
  return { status: 200, body: next };
}
Open in playground
Report an error