From 68316bfb951a683f4ca0fd22446ea7694eb133e3 Mon Sep 17 00:00:00 2001 From: Arjun Garg Date: Tue, 10 Nov 2026 19:36:30 -0500 Subject: [PATCH] fix(rest/nodejs): reject completion for deleted products Completion skipped the stock reservation for a line item whose product no longer exists (`if (product && !reserveStock(...))`) and still created the order, so a checkout could complete for a product that was removed from the catalog, with nothing reserved. Throw InvalidRequestError ("Product X not found", 400) inside the completion transaction instead, matching the Python reference (#248) and the create/update paths. Earlier reservations roll back and no order is written. Add lifecycle tests for a single deleted product and for a deleted product on a later line. --- rest/nodejs/src/api/checkout.ts | 7 ++- rest/nodejs/test/lifecycle.test.ts | 90 ++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/rest/nodejs/src/api/checkout.ts b/rest/nodejs/src/api/checkout.ts index f8346e4..b53fa2d 100644 --- a/rest/nodejs/src/api/checkout.ts +++ b/rest/nodejs/src/api/checkout.ts @@ -1174,7 +1174,12 @@ export class CheckoutService { // Throw rather than return: `db.transaction(fn)()` commits whenever // the callback returns normally, so an early return here would // commit the reservations made for the lines before this one. - if (product && !reserveStock(line.item.id, line.quantity)) { + if (!product) { + // The product left the catalog after the checkout was created. + // Fail closed rather than skip the reservation and order it. + throw new InvalidRequestError(`Product ${line.item.id} not found`); + } + if (!reserveStock(line.item.id, line.quantity)) { throw new OutOfStockError( `Item ${line.item.id} is out of stock`, 409 diff --git a/rest/nodejs/test/lifecycle.test.ts b/rest/nodejs/test/lifecycle.test.ts index 7885b9b..893e1fa 100644 --- a/rest/nodejs/test/lifecycle.test.ts +++ b/rest/nodejs/test/lifecycle.test.ts @@ -503,6 +503,96 @@ test("an out-of-stock second line rolls back the first reservation", async () => assert.equal(checkout.status, "ready_for_complete"); }); +// A product can be removed from the catalog between checkout creation and +// completion. Completion must fail closed: skipping the reservation and +// ordering a product that no longer exists would sell stock nobody tracks. +function addProduct(id: string, stock: number): void { + getProductsDb() + .prepare( + "INSERT INTO products (id, title, price, image_url) VALUES (?, ?, ?, ?)" + ) + .run(id, "Temporary Item", 2000, ""); + getTransactionsDb() + .prepare("INSERT INTO inventory (product_id, quantity) VALUES (?, ?)") + .run(id, stock); +} + +function removeProduct(id: string): void { + getProductsDb().prepare("DELETE FROM products WHERE id = ?").run(id); +} + +async function assertProductNotFound(res: Response, productId: string) { + assert.equal(res.status, 400); + const body = (await res.json()) as { + ucp: { status: string }; + messages: { code: string; content: string }[]; + }; + assert.equal(body.ucp.status, "error"); + assert.equal(body.messages[0].code, "INVALID_REQUEST"); + assert.equal(body.messages[0].content, `Product ${productId} not found`); +} + +test("completion fails when a line item's product was deleted", async () => { + const app = buildApp(); + addProduct("gone_single", 5); + const id = await createReadyToComplete(app, [ + { id: "gone_single", quantity: 2 }, + ]); + removeProduct("gone_single"); + const ordersBefore = countRows("orders"); + + const res = await complete(app, id); + await assertProductNotFound(res, "gone_single"); + + assert.equal( + getInventory("gone_single"), + 5, + "a failed completion must not consume stock" + ); + assert.equal(countRows("orders"), ordersBefore, "no order may be persisted"); + const checkout = (await ( + await app.request(`/checkout-sessions/${id}`) + ).json()) as { status: string; order?: unknown }; + assert.equal(checkout.status, "ready_for_complete"); + assert.equal(checkout.order, undefined, "no order reference may survive"); + + // The checkout is still completable once the product exists again. + getProductsDb() + .prepare( + "INSERT INTO products (id, title, price, image_url) VALUES (?, ?, ?, ?)" + ) + .run("gone_single", "Temporary Item", 2000, ""); + assert.equal((await complete(app, id)).status, 200); + assert.equal(getInventory("gone_single"), 3); +}); + +test("a deleted product on a later line rolls back the earlier reservation", async () => { + const app = buildApp(); + addProduct("gone_second", 5); + const id = await createReadyToComplete(app, [ + { id: "bouquet_roses", quantity: 1 }, + { id: "gone_second", quantity: 1 }, + ]); + removeProduct("gone_second"); + const rosesBefore = getInventory("bouquet_roses"); + const ordersBefore = countRows("orders"); + + const res = await complete(app, id); + await assertProductNotFound(res, "gone_second"); + + assert.equal( + getInventory("bouquet_roses"), + rosesBefore, + "the first line's reservation must roll back with the failure" + ); + assert.equal(getInventory("gone_second"), 5); + assert.equal(countRows("orders"), ordersBefore, "no order may be persisted"); + const checkout = (await ( + await app.request(`/checkout-sessions/${id}`) + ).json()) as { status: string }; + assert.equal(checkout.status, "ready_for_complete"); +}); + test("an idempotent completion replay reserves stock once", async () => { const app = buildApp(); const id = await createReadyToComplete(app);