Skip to content

fix: add stock validation and decrement on order placementAdd stock validation and decrement on orders#13

Open
AaravKashyap12 wants to merge 1 commit into
Dhanika-Bhat:mainfrom
AaravKashyap12:fix/order-stock-handling
Open

fix: add stock validation and decrement on order placementAdd stock validation and decrement on orders#13
AaravKashyap12 wants to merge 1 commit into
Dhanika-Bhat:mainfrom
AaravKashyap12:fix/order-stock-handling

Conversation

@AaravKashyap12
Copy link
Copy Markdown

Closes #12

What this PR does

Fixes two related inventory bugs in server/routes/orders.js:

1. Stock validation before order is accepted:

if (product.stock < item.quantity) {
    return res.status(400).json({
        message: `Insufficient stock for "${product.name}". Available: ${product.stock}`
    });
}

2. Atomic stock decrement after order is saved:

for (const item of items) {
    await Product.findByIdAndUpdate(item.product, {
        $inc: { stock: -item.quantity }
    });
}

Why this matters

Without this fix, customers can order any quantity regardless of available stock, and inventory is never updated — making the stock field meaningless.

Changes

File Change
server/routes/orders.js Stock check + decrement logic
server/test/orders.test.js 2 regression tests (insufficient stock, valid order)
server/package.json Added test script

Verification

npm test → 2 passed, 0 failing

@vercel
Copy link
Copy Markdown

vercel Bot commented May 18, 2026

@AaravKashyap12 is attempting to deploy a commit to the dhanika2006-7071's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: no stock validation on order — overselling and no stock decrement

1 participant