Spot the bug in the order handler

from Unfamiliar code reading
Node 24 intermediate 7 min 3 issues to find

Find the trust, data-lineage, and effect-order bugs a generated explanation could miss.

JavaScript
let lastTenant = "";

export function handleOrder(request, dependencies) {
  const input = JSON.parse(request.body);
  lastTenant = request.headers["x-tenant"];
  const totalCents = input.items.reduce(
    (sum, item) => sum + item.priceCents * item.quantity,
    0,
  );
  dependencies.payments.charge(lastTenant, totalCents);
  dependencies.events.publish({ type: "OrderPaid", id: input.id });
  dependencies.orders.save({ ...input, tenant: lastTenant, totalCents });
  return { status: 201, id: input.id };
}
Open in playground
Report an error