--- issue: 100 title: "Backend: updatePurchaseRequest does findById then findByIdAndUpdate — non-atomic race" severity: medium domain: Marketplace labels: [bug, backend, concurrency] status: open created: 2026-05-30 source: Full Codebase Audit 2026-05-30 --- # Backend: updatePurchaseRequest does findById then findByIdAndUpdate — non-atomic race **Severity:** medium **Domain:** Marketplace **Labels:** bug, backend, concurrency ## Description `PurchaseRequestService.ts:413` reads the document first (`findById`) to check allowed status transitions, then writes it (`findByIdAndUpdate`). Between the read and the write, another request can change the status, defeating the transition guard. ## Options 1. Use `findOneAndUpdate` with `status:{$in:allowedCurrentStatuses}` condition — atomic. 2. Keep two queries but wrap in a transaction. 3. Leave as-is. ## Recommendation Use a single conditional `findOneAndUpdate` to make the transition atomic and halve round-trips. ## Affected Files - `backend/src/services/marketplace/PurchaseRequestService.ts:413` ## References - [Full Codebase Audit 2026-05-30](../09%20-%20Audits/Full%20Codebase%20Audit%20-%202026-05-30.md) — DEC-46