# Review of design.md (order-service checkout, revision 3)

**The core is sound, but four gaps need closing before you write the code they touch.** The parts that usually go wrong are done right: stock is reserved atomically with a guarded `UPDATE`, the payment call happens outside any transaction, the order id is reused as the provider's idempotency key, and there is a reconciliation job. Most of what's missing is undecided behaviour around that job and around order status changes. Each gap below has a concrete case. All of them are fixes to the doc, not a redesign.

## Must fix before building

**1. Prices: the doc doesn't say where `unit_price` and `total` come from.** The client sends "the cart", and there is no product or price table. If the server takes prices from the request, a client can send `{sku: "X", quantity: 1, unit_price: 0.01}` and get a `PAID` order for one cent. Expected: the server looks up prices from a source it controls and calculates `total` itself. Name that source and which module owns it.

**2. Reconciliation has no rule for "the provider has no charge for this order".** Example: an instance crashes after step 2 commits but before step 3 sends anything. Or all four attempts time out before reaching the provider. The order stays `PENDING` with stock reserved. Reconciliation asks the provider, the provider says "not found", and the doc only covers `PAID` and `FAILED`. Today the order sits there until the 30-minute alert fires, and then someone handles it by hand. You need to decide: either retry the charge (safe, because the order id is the idempotency key), or mark it `FAILED` and release the stock. Also say what happens when the provider answers "still processing".

**3. Status changes are not guarded, and both instances will run the reconciliation job.** Both instances run the 5-minute job, so they can both pick up the same `PENDING` order that the provider declined. Each one marks it `FAILED` and restores stock, so a quantity of 2 comes back as 4. On the `PAID` side, the confirmation email goes out twice. Expected: every change is a compare-and-set, e.g. `UPDATE orders SET status = 'FAILED' WHERE id = :id AND status = 'PENDING'`, and stock is restored or the email queued only if that update changed one row. A Postgres advisory lock that lets only one instance run the job is good to have, but it doesn't replace the guard. Also say explicitly that reconciliation's `FAILED` path restores stock and its `PAID` path queues the email, through the same code as steps 4 and 5. As written, step 6 says neither.

**4. The idempotency key is unique across all customers, and nothing checks that a repeat request matches the original.** Customer B happens to send the same key as customer A and gets A's order back, which leaks one customer's data to another. Expected: the constraint is `UNIQUE (customer_id, idempotency_key)` and the lookup uses both columns. Separately, if the same key arrives with a different cart, return `422` rather than quietly returning the old order. Storing a hash of the request makes that check possible.

This is the order lifecycle with the missing pieces marked:

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved
    PENDING --> PAID: charge succeeded (step 4 or reconciliation)
    PENDING --> FAILED: declined (step 5 or reconciliation), restore stock
    PENDING --> PENDING: result unknown, return 202
    PENDING --> UNDEFINED: provider has no charge (gap 2)
    note right of PAID: every transition must be WHERE status = 'PENDING' (gap 3)
```

## Should fix (small, cheap now)

- **The client can't find out how a `202` order ends.** There is no `GET /orders/{id}` or webhook. Add the endpoint.
- **Deadlocks on multi-item carts.** Two carts with `[A, B]` and `[B, A]` lock the stock rows in opposite order. Sort SKUs before the `UPDATE`s. It costs one line.
- **The module rule contradicts step 2.** Step 2 writes to the stock table and the orders table in one transaction, but the doc also says no module reads another module's tables. Say how that works: for example, `inventory.reserve(tx, items)` takes the caller's transaction, so inventory still owns its SQL. Otherwise someone will put stock SQL inside `OrderCheckout`.
- **Missing constraints.** Add primary keys on `stock.sku` and `(order_items.order_id, sku)`, `CHECK (available >= 0)`, and `CHECK (quantity > 0)`. A negative quantity would *add* stock through the reserve query.

## Tests to add to the plan

The current list covers the main request path well. It has no tests for reconciliation, which is where the gaps above are. Add:

- reconciliation settles `PAID`, settles `FAILED` (and restores stock), and handles "charge not found"
- two reconciliation runs at the same time on one declined order restore stock exactly once
- a crash between reservation and the charge call (order committed, provider never called)
- two simultaneous `POST /orders` requests with the same key: one order, stock taken once
- the same key from a different customer, and the same key with a different cart
- a client-supplied price is ignored

**Scale isn't a concern.** 2,000 orders a day is about one every 40 seconds, and this design handles that easily.

## What to do tomorrow

Spend the first hour updating the doc for gaps 1–4. Then you can start with the parts they don't affect: the schema with the added constraints, the reservation transaction, and the payment client with its retries. Write the reconciliation job only after gaps 2 and 3 are decided.

VERDICT: READY WITH CHANGES
